-
Notifications
You must be signed in to change notification settings - Fork 1
/
wojo.py
148 lines (108 loc) · 3.75 KB
/
wojo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import select
import socket
import sys
import math
import tkinter as tk
from tkinter import messagebox
import rsa
from choice import choice
bg = "#fff59d"
msg_box_color_yours = "#017ced"
msg_box_color_other = "#f50057"
msg_box_txt_color_yours = "#ffffff"
msg_box_txt_color_other = "#ffffff"
bufsize = 444
sendbufsize = 36
sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
destip = input("Dest ip: ")
destip = destip if destip else "127.0.0.1"
choice = bool(choice(["Start new connection", "Join open connection"]))
def listen(sock, port):
sock.bind(('', port))
sock.listen(1)
clientip, addr = sock.accept()
return clientip
def connect(socket, ip, port):
while True:
try:
socket.connect((ip, port))
except ConnectionRefusedError:
continue
break
if choice:
connect(sock1, destip, 1234)
recv_sock = listen(sock2, 5555)
send_sock = sock1
else:
recv_sock = listen(sock1, 1234)
connect(sock2, destip, 5555)
send_sock = sock2
n, e, d = rsa.generate()
if choice:
send_sock.send((str(n) + ',' + str(e)).encode())
recvd = recv_sock.recv(2048).decode().split(',')
else:
recvd = recv_sock.recv(2048).decode().split(',')
send_sock.send((str(n) + ',' + str(e)).encode())
other_n, other_e = [int(num) for num in recvd]
win = tk.Tk()
win.configure(background=bg)
win.iconbitmap("./res/favicon.ico")
win.geometry("400x525")
win.pack_propagate(0)
win.title("WoJo Messenger Extreme")
msg_frame = tk.Frame(win, bg=bg, padx=8, pady=4)
msg_frame.pack(fill=tk.BOTH, side=tk.TOP)
msg_send_frame = tk.Frame(win)
msg_send_frame.pack(fill=tk.X, side=tk.BOTTOM)
msg_input = tk.Entry(msg_send_frame)
msg_input.pack(fill=tk.X, side=tk.BOTTOM)
msg_send_btn_img = tk.PhotoImage(file="./res/send.png")
def clear(_=None):
for old_msg in msg_frame.winfo_children():
old_msg.destroy()
def close():
# win.destroy()
send_sock.close()
recv_sock.close()
sys.exit(0)
def send(_=None):
msg = msg_input.get()
if msg == "/clear":
clear()
return
msg_input.delete(0, tk.END)
msgInNumbers, msgLength = rsa.strtonum(msg)
encryptedMsg = rsa.encrypt(msgInNumbers, other_e, other_n)
recv(msg, True) # To show msg in your own window on the right side.
send_sock.send((str(encryptedMsg) + ',' + str(msgLength)).encode("utf-8"))
def recv(recv_msg, you):
# Shit to ensure that own messages wil be recieved in sendbufsize
for index in range(math.ceil(len(recv_msg)/sendbufsize)):
recv_part = recv_msg[index*sendbufsize:index*sendbufsize+sendbufsize]
msg_box = tk.Label(msg_frame, text=recv_part, bg=msg_box_color_yours if you else msg_box_color_other,
borderwidth=1, fg=msg_box_txt_color_yours if you else msg_box_txt_color_other, font=("Roboto", 11, "normal"), padx=4, relief="raised")
msg_box.pack(anchor=tk.E if you else tk.W)
msg_send_btn = tk.Button(msg_send_frame, image=msg_send_btn_img, relief="flat", cursor="hand2", command=send)
msg_send_btn.pack(side=tk.RIGHT, in_=msg_input)
msg_input.bind("<Return>", send)
msg_input.bind("<Escape>", clear)
msg_frame.bind("")
while True:
win.protocol("WM_DELETE_WINDOW", close)
win.update()
try: ready = select.select([recv_sock], [], [], 0)
except ValueError:
messagebox.showerror("Disconnection error", "An error occurred, the other probbably diconnected.")
close()
if ready[0]:
recvdData = recv_sock.recv(bufsize)
if not recvdData: close()
encryptedRecvdMsg, recvdMsgLength = [int(num) for num in recvdData.decode().split(',')]
decryptedRecvdMsgInNumbers = rsa.decrypt(encryptedRecvdMsg, d, n)
decryptedRecvdMsg = rsa.numtostr(decryptedRecvdMsgInNumbers, recvdMsgLength)
recv(decryptedRecvdMsg, False)
print(decryptedRecvdMsg[:6] +
('' if len(decryptedRecvdMsg) <= 6 else "..."), "\t<-- recvd")
print(encryptedRecvdMsg)