-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchat_server.py
More file actions
241 lines (201 loc) · 8.95 KB
/
chat_server.py
File metadata and controls
241 lines (201 loc) · 8.95 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os
import socket
import time
import tkinter as tk
from datetime import datetime
from queue import Queue
from threading import Thread
import pygubu
from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.PublicKey import RSA
import PySecChat
PROJECT_PATH = os.path.dirname(__file__)
PROJECT_UI = PROJECT_PATH + "/server.ui"
NAME = 'Server'
ENCODING = 'utf-8'
BUFFERSIZE = 64
KEYSIZE = 1024
# 0.0.0.0 will bind the server to all network interfaces
addr = ('0.0.0.0', 1252)
class PySecChatServer(PySecChat.PySecChat):
def __init__(self, root):
self.tk = root
self.builder = builder = pygubu.Builder()
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
self.mainwindow = builder.get_object('frame_1')
builder.connect_callbacks(self)
self.clilist = builder.get_object('lstboxClients')
self.cliscroll = builder.get_object('scrollClients')
self.clilist.configure(yscrollcommand=self.cliscroll.set)
self.cliscroll.configure(command=self.clilist.yview)
self.popup_menu = tk.Menu()
self.popup_menu.add_command(label="Kick", command=self.kick_client)
self.clilist.bind('<Button-3>', self.clilist_context_popup)
self.loglist = builder.get_object('lstboxLog')
self.logscroll = builder.get_object('scrollLog')
self.loglist.configure(yscrollcommand=self.logscroll.set)
self.logscroll.configure(command=self.loglist.yview)
def kick_client(self):
for i in self.clilist.curselection():
sel = self.clients[i]
sel.disconnect()
self.loglist.insert(tk.END, f'Client: {sel.address} was kicked')
def clilist_context_popup(self, event):
try:
self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popup_menu.grab_release()
class message:
def __init__(self, msg, msgtype, targets):
self.msg = msg
self.targets = targets
self.msgtype = msgtype
if self.msgtype == 'PK':
self.data = PySecChatServer.setupPubKey(PySecChatServer, msg)
else:
self.data = None
class Client:
def __init__(self, socket, address):
self.connection = socket
self.address = address
self.pubkey = None # Used to encrypt messages to send to the client
self.rsaestablished = False
def disconnect(self):
self.connection.close()
def __str__(self):
return f'{self.address}'
def updateCliList(self):
self.clilist.delete(0, tk.END)
for client in self.clients:
self.clilist.insert(tk.END, client)
def acceptConnections(self):
while True:
try:
clicon, cliaddr = self.sock.accept()
except:
break
newclient = self.Client(clicon, cliaddr[0])
self.clients.append(newclient)
clientThread = Thread(target=self.handleclient, args=(newclient,))
self.threads.append(clientThread)
clientThread.start()
def handleSendQueue(self):
self.sendqueue = Queue()
while True:
if self.sendqueue.not_empty:
msg = self.sendqueue.get()
if msg != None:
if msg.targets is not None:
for target in msg.targets:
if msg.msgtype == 'PK':
target.connection.sendall(msg.data)
elif msg.msgtype == 'MSG':
msg.data = self.setupMsg(target.pubkey.encrypt(msg.msg.encode('utf-8')))
target.connection.sendall(msg.data)
def handleclient(self, client):
self.loglist.insert(tk.END, f'Client connected from {client.address}')
self.updateCliList()
self.loglist.insert(tk.END, f'Sending server pubkey to {client.address}')
self.sendqueue.put(self.message(self.pubkeybytes, 'PK', [client]))
full_msg = b''
new_msg = True
while 1:
try:
data = client.connection.recv(BUFFERSIZE)
except:
app.loglist.insert(tk.END, f'Client {client.address} disconnected.')
self.clients.remove(client)
self.sendqueue.put(self.message(f'Client {client.address} disconnected.', 'MSG', self.clients))
self.updateCliList()
break
if data:
if new_msg:
try:
msgtype = data[:3]
msglen = int(data[4:PySecChat.HEADERLEN])
new_msg = False
except:
app.loglist.insert(tk.END, f'Invalid protocol from {client.address}, Disconnecting')
client.disconnect()
self.clients.remove(client)
self.updateCliList()
break
full_msg += data
if len(full_msg) - PySecChat.HEADERLEN == msglen:
if msgtype.decode('utf-8').strip(' ') == 'PK':
app.loglist.insert(tk.END, f'PubKey Recieved from {client.address}')
srvpubkey = RSA.importKey(full_msg[PySecChat.HEADERLEN:])
encryptor = PKCS1_OAEP.new(srvpubkey)
client.pubkey = encryptor
app.loglist.insert(tk.END, 'Sending ENCTEST message to client')
self.sendqueue.put(self.message('ENCTEST', 'MSG', [client]))
time.sleep(0.5)
self.sendqueue.put(self.message(f'Welcome to the server', 'MSG', [client]))
self.sendqueue.put(self.message(f'The time is: {datetime.now()}', 'MSG', [client]))
elif msgtype.decode('utf-8').strip(' ') == 'MSG':
decrypted = self.srvdecryptor.decrypt(full_msg[PySecChat.HEADERLEN:]).decode('utf-8')
if client.rsaestablished is False:
if decrypted == 'ENCTEST':
client.rsaestablished = True
app.loglist.insert(tk.END, f'{client.address} ENCTEST recieved')
allbutself = self.clients.copy()
allbutself.remove(client)
self.sendqueue.put(
self.message(f'Client connected from {client.address}.', 'MSG', allbutself))
self.updateCliList()
else:
app.loglist.insert(tk.END,
f'Client {client.address} encryption test failed, Disconnecting')
client.disconnect()
else:
app.loglist.insert(tk.END, f'{client.address}: {decrypted}')
allbutself = self.clients.copy()
allbutself.remove(client)
self.sendqueue.put(self.message(f'{client.address}: {decrypted}', 'MSG', allbutself))
# playsound.playsound('bing.wav')
new_msg = True
full_msg = b''
def getInput(self):
while True:
msg = input()
if msg == 'exit':
for client in self.clients:
client.disconnect()
self.sock.close()
exit()
def exitApp(self):
for client in self.clients:
if client is not None:
client.connection.close()
self.sock.close()
exit()
def run(self):
self.rsakey = self.createKeys(KEYSIZE)
self.rsapubkey = self.rsakey.publickey()
self.pubkeybytes = self.rsapubkey.exportKey(format='PEM')
self.srvdecryptor = PKCS1_OAEP.new(self.rsakey)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind(addr)
self.sock.listen(64)
# Array of clients connected to the server (clientsocket, clientaddress)
self.clients = []
# List of active threads
self.threads = list()
self.acceptConnThread = Thread(target=self.acceptConnections)
self.acceptConnThread.setDaemon(True)
self.threads.append(self.acceptConnThread)
self.acceptConnThread.start()
self.getInputThread = Thread(target=self.getInput)
self.getInputThread.setDaemon(True)
self.threads.append(self.getInputThread)
self.getInputThread.start()
self.sendthread = Thread(target=self.handleSendQueue)
self.sendthread.setDaemon(True)
self.threads.append(self.sendthread)
self.sendthread.start()
self.mainwindow.mainloop()
if __name__ == '__main__':
root = tk.Tk()
app = PySecChatServer(root)
app.run()