-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
573 lines (447 loc) · 21.8 KB
/
client.py
File metadata and controls
573 lines (447 loc) · 21.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# -*- coding: utf8 -*-
# from socket import *
from tkinter import *
import tkinter as tk
import threading
import hashlib
import socket
import struct
import time
import sys
import os
from tkinter import filedialog
from tkinter import messagebox
from PIL import ImageTk, Image
import encryption
# ========================================== Default attribute configure ===============================================
login_window = None
main_window = None
client_socket = None
udp_socket = None
current_connect_session = ''
username = ''
users = {}
filename = ''
filename_short = ''
file_transfer_waiting = False
HOST = "127.111.0.1"
PORT = 12345
MCAST_GRP = '224.111.1.1'
MCAST_PORT = 5007
# ========================================= Tkinter Login and Main windows =============================================
class LoginWin:
def __init__(self):
self.window = tk.Tk()
self.username = tk.StringVar()
self.password = tk.StringVar()
# Adjust window size
self.window.title("Chat Login")
self.window.geometry('320x190')
self.window.resizable(width=False, height=False)
# Add image file
self.bg = ImageTk.PhotoImage(Image.open('picture/IMG_7753.jpg'))
# Create a Canvas
self.img_canvas = Canvas(self.window)
self.img_canvas.pack(fill='both', expand=True)
# Display image
self.img_canvas.create_image(0, 0, image=self.bg)
# self.img_canvas.create_image(0, 0, image=self.bg, anchor='nw')
# Display login context: Account
self.label1 = tk.Label(self.window)
self.label1.place(relx=0.12, rely=0.18, height=22, width=40)
self.label1.configure(text='帳號', font=("Arial", 12), bg='light slate gray')
# Input the account information
self.entry_username = tk.Entry(self.window)
self.entry_username.place(relx=0.28, rely=0.18, height=22, relwidth=0.6)
self.entry_username.configure(textvariable=self.username)
# Display login context: Password
self.label2 = tk.Label(self.window)
self.label2.place(relx=0.12, rely=0.35, height=22, width=40)
self.label2.configure(text='密碼', font=("Arial", 12), bg='light slate gray')
# Input the password information
self.entry_password = tk.Entry(self.window)
self.entry_password.place(relx=0.28, rely=0.35, height=22, relwidth=0.6)
self.entry_password.configure(show='*', textvariable=self.password)
# Display Login and register button
self.button_login = tk.Button(self.window)
self.button_login.place(relx=0.2, rely=0.65, height=28, width=70)
self.button_login.configure(text='Login', font=("Arial", 12), bg='light slate gray')
self.button_register = tk.Button(self.window)
self.button_register.place(relx=0.6, rely=0.65, height=28, width=70)
self.button_register.configure(text='Register', font=("Arial", 12), bg='light slate gray')
# show the login window
def show(self):
self.window.mainloop()
# not show the login window
def destroy(self):
self.window.destroy()
class MainWin:
turn_off = None
def __init__(self):
self.window = tk.Tk()
self.window.protocol('WM_DELETE_WINDOW', self.on_closing)
# Adjust window size
self.window.title("Chat Room")
self.window.geometry('500x350')
self.window.resizable(width=False, height=False)
# Add image file
self.bg = ImageTk.PhotoImage(Image.open('picture/IMG_6158.jpg'))
# Create a Canvas
self.img_canvas = Canvas(self.window)
self.img_canvas.pack(fill='both', expand=True)
# Display image
self.img_canvas.create_image(0, 0, image=self.bg)
# self.img_canvas.create_image(0, 0, image=self.bg, anchor='nw')
# username and message display format
self.message = tk.StringVar()
self.username = tk.StringVar()
# display this client username at the top and center
self.label1 = tk.Label(self.window)
self.label1.place(relx=0.3, rely=0.07, height=25, width=100)
self.label1.configure(textvariable=self.username, font=("Arial", 12), bg='light slate gray')
# display history of context
self.chat_history = tk.Text(self.window)
self.chat_history.place(relx=0.02, rely=0.17, relheight=0.7, relwidth=0.7)
self.chat_history.configure(state='disabled', bg='grey20', fg='white')
# display currently user listbox
self.username_list = tk.Listbox(self.window)
self.username_list.place(relx=0.75, rely=0.17, relheight=0.7, width=115)
self.username_list.configure(bg='grey20', fg='white')
# display currently user context
self.label2 = tk.Label(self.window)
self.label2.place(relx=0.79, rely=0.1, height=20, width=80)
self.label2.configure(text="Online List", font=("Arial", 12), bg='light slate gray')
# display entry message box
self.entry_message = tk.Entry(self.window)
self.entry_message.place(relx=0.02, rely=0.9, height=24, relwidth=0.6)
self.entry_message.configure(textvariable=self.message, bg='grey20', fg='white')
# display send message button
self.button_send_message = tk.Button(self.window)
self.button_send_message.place(relx=0.63, rely=0.9, height=24, width=45)
self.button_send_message.configure(text='發送', font=("Arial", 12), bg='light slate gray')
# display send file button
self.button_send_file = tk.Button(self.window)
self.button_send_file.place(relx=0.75, rely=0.9, height=24, width=115)
self.button_send_file.configure(text='傳送檔案', font=("Arial", 12), bg='light slate gray', state='disabled')
# show the login window
def show(self):
self.window.mainloop()
# not show the login window
def destroy(self):
try:
self.turn_off()
except:
pass
self.window.destroy()
# ask user for quit window event
def on_closing(self):
if messagebox.askokcancel("Quit", "Do you want to quit the chat?"):
self.window.destroy()
# ================================================== Button Event ======================================================
def on_button_login_clicked():
global client_socket, username, login_window, main_window, udp_socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # open TCP socket
client_socket.settimeout(5) # 設置連線逾時5秒
if login_window.username.get() != '' and login_window.password != '':
client_socket.connect((HOST, PORT))
encryption.send(client_socket, {'cmd': 'login', 'user': login_window.username.get(),
'password': hashlib.sha1(
login_window.password.get().encode('utf-8')).hexdigest()})
server_response = encryption.recv(client_socket)
# 若帳密正確,則初始化main_window狀態,並打開main_window畫面
if server_response['response'] == 'ok':
username = login_window.username.get()
login_window.destroy()
main_window = MainWin()
# initialize main_window state
main_window.username.set(username)
main_window.button_send_message.configure(command=on_button_send_message_clicked)
main_window.button_send_file.configure(command=on_button_send_file_clicked)
main_window.username_list.bind('<<ListboxSelect>>', online_session_select)
encryption.send(client_socket, {'cmd': 'get_users'})
encryption.send(client_socket, {'cmd': 'get_history', 'peer': ''})
# open UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# multicast setting
udp_socket.bind(("", MCAST_PORT)) # UDP bind
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# open recv_tcp_message_thread
recv_tcp_thread = threading.Thread(target=recv_tcp_async, args=())
recv_tcp_thread.setDaemon(True)
recv_tcp_thread.start()
# get client_socket information
print(client_socket)
# show the chat window
main_window.show()
# 若帳密不正確,則顯示failed
elif server_response['response'] == 'fail':
tk.messagebox.showerror("Warning! ", "Login failed: " + server_response['reason'])
else:
tk.messagebox.showerror("Warning! ", "Account and Password cannot be empty! ")
def on_button_register_clicked():
global client_socket, login_window
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.settimeout(5)
if login_window.username.get() != '' and login_window.password.get() != '':
client_socket.connect((HOST, PORT))
encryption.send(client_socket, {'cmd': 'register', 'user': login_window.username.get(),
'password': hashlib.sha1(
login_window.password.get().encode('utf-8')).hexdigest()})
server_response = encryption.recv(client_socket)
if server_response['response'] == 'ok':
tk.messagebox.showinfo("Notification! ", "Register Successfully! ")
elif server_response['response'] == 'fail':
tk.messagebox.showerror("Warning! ", "Register failed: " + server_response['reason'])
else:
tk.messagebox.showerror("Warning! ", "Account and Password cannot be empty! ")
close_socket()
def on_button_send_message_clicked():
global client_socket, username, current_connect_session, main_window
if main_window.message.get() != '':
encryption.send(client_socket, {'cmd': 'chat', 'peer': current_connect_session,
'message': main_window.message.get()})
append_message_to_history(username, time.strftime('%m/%d %Y - %H:%M:%S', time.localtime(time.time())),
main_window.message.get())
main_window.message.set('')
else:
tk.messagebox.showinfo("Warning! ", "Input cannot be empty! ")
def on_button_send_file_clicked():
global client_socket, main_window, filename, filename_short, file_transfer_waiting
try:
filename = tk.filedialog.askopenfilename()
if filename == '':
return
filename_short = ''
if len(filename.split('/')) < len(filename.split('\\')):
filename_short = filename.split('\\')[-1]
else:
filename_short = filename.split('/')[-1]
file_size = os.path.getsize(filename)
count = 0
while not 1 < file_size < 1024 and count < 6:
file_size /= 1024
count += 1
file_size = str(format(file_size, '.2f')) + ['B', 'KB', 'MB', 'GB', 'TB', 'PB'][count]
md5_checksum = get_file_md5(filename)
encryption.send(client_socket, {'cmd': 'file_request', 'peer': current_connect_session,
'filename': filename_short, 'size': file_size, 'md5': md5_checksum})
main_window.button_send_file.configure(text="Waiting...", state='disabled')
file_transfer_waiting = True
except:
sys.exit(1)
# =================================================== Mouse Event ======================================================
def online_session_select(event):
global current_connect_session, main_window, username, users, file_transfer_waiting
widget = event.widget
changed = False
if len(widget.curselection()) != 0:
index = int(widget.curselection()[0])
if index != 0:
# set the new message icon('( new)') at the end of the sender
if current_connect_session != widget.get(index).rstrip(' (*)'):
changed = True
current_connect_session = widget.get(index).rstrip(' (*)')
if not file_transfer_waiting:
main_window.button_send_file.configure(state='normal')
main_window.username.set('%s -> %s' % (username, current_connect_session))
users[current_connect_session] = False
refresh_username_list()
elif index == 0:
if current_connect_session != '':
changed = True
current_connect_session = ''
main_window.button_send_file.configure(state='disabled')
main_window.username.set('%s -> global' % username)
users[''] = False
refresh_username_list()
if changed:
encryption.send(client_socket, {'cmd': 'get_history', 'peer': current_connect_session})
# ================================================ Additional Function =================================================
def append_message_to_history(sender, send_time, message):
main_window.chat_history['state'] = 'normal'
main_window.chat_history.insert('end', '%s - %s\n' % (sender, send_time))
main_window.chat_history.insert('end', message + '\n\n', 'text')
main_window.chat_history.see('end')
main_window.chat_history['state'] = 'disabled'
def refresh_username_list():
main_window.username_list.delete(0, 'end')
for user in users.keys():
name = "公頻聊天室" if user == '' else user
if users[user]:
name += ' (*)'
main_window.username_list.insert('end', name)
def close_socket():
encryption.send(client_socket, {'cmd': 'close'})
client_socket.shutdown(2)
client_socket.close()
udp_socket.close()
# 將要傳送的檔案進行十六進位轉換,再進行MD5加密
def get_file_md5(file_path):
md5_object = hashlib.md5()
max_buffer = 8192
file = open(file_path, 'rb')
while True:
buffer = file.read(max_buffer)
if not buffer:
break
md5_object.update(buffer)
file.close()
hash = md5_object.hexdigest()
return str(hash).upper()
# ================================================ Recv Message Event ==================================================
def recv_tcp_async():
global udp_socket, client_socket, users, main_window, current_connect_session, file_transfer_waiting, filename_short, filename
while True:
data = encryption.recv(client_socket)
# 獲取user清單
if data['type'] == 'get_users':
users = {}
for user in [''] + data['data']:
users[user] = False
refresh_username_list()
# 獲取聊天紀錄
elif data['type'] == 'get_history':
if data['peer'] == current_connect_session:
# delete the old chat_history
main_window.chat_history['state'] = 'normal'
main_window.chat_history.delete('1.0', 'end')
main_window.chat_history['state'] = 'disabled'
# append new chat_history and display the new chat_history
for entry in data['data']:
append_message_to_history(entry[0], entry[1], entry[2])
# 新client加入聊天室
elif data['type'] == 'peer_joined':
users[data['peer']] = False
refresh_username_list()
# multicast
recv_message = udp_socket.recv(10240).decode()
print("user: %s, joined the chat" % recv_message)
# 有client離開聊天室
elif data['type'] == 'peer_left':
# multicast
recv_message = udp_socket.recv(10240).decode()
print("user: %s, left the chat" % recv_message)
if data['peer'] in users.keys():
del users[data['peer']]
if data['peer'] == current_connect_session:
current_connect_session = ''
main_window.button_send_file.configure(state='disabled')
main_window.username.set('%s -> global' % username)
users[''] = False
encryption.send(client_socket, {'cmd': 'get_history', 'peer': ''})
refresh_username_list()
# 新訊息
elif data['type'] == 'message':
if data['peer'] == current_connect_session:
append_message_to_history(data['peer'], time.strftime('%m/%d %Y - %H:%M:%S', time.localtime(time.time()))
, data['message'])
else:
users[data['peer']] = True
refresh_username_list()
# broadcast
elif data['type'] == 'broadcast':
if current_connect_session == '':
append_message_to_history(data['peer'],
time.strftime('%m/%d %Y - %H:%M:%S', time.localtime(time.time()))
, data['message'])
else:
users[''] = True
refresh_username_list()
# 接收文件
elif data['type'] == 'file_request':
# Accept the file
if tk.messagebox.askyesno("Notification! ", "%s want to send a file to you\n Filename: %s\ndata size: %s\n "
"Accept the file? " % (data['peer'], data['filename'], data['size'])):
encryption.send(client_socket, {'cmd': 'file_accept', 'peer': data['peer']})
try:
total_file_bytes = 0
addr = ('127.0.0.1', 8888)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(addr)
server.listen(5)
client_file_socket, addr = server.accept()
# 計算接收檔案的時間
start_recv_time = time.time()
with open(data['filename'], 'wb') as file:
while True:
fdata = client_file_socket.recv(1024)
total_file_bytes += len(fdata)
if not fdata:
break
file.write(fdata)
file.close()
client_file_socket.close()
server.close()
end_recv_time = time.time()
# check filename
received_md5 = get_file_md5(data['filename'])
if received_md5 == str(data['md5']):
tk.messagebox.showinfo("Notification !", "File take successfully !")
# append send file message to chat_history
main_window.chat_history['state'] = 'normal'
main_window.chat_history.insert('end', 'Received %s bytes from %s in %s seconds\n\n' %
(total_file_bytes, data['peer'], format(end_recv_time -
start_recv_time, '.2f'))
, 'hint')
main_window.chat_history.see('end')
main_window.chat_history['state'] = 'disabled'
except:
pass
# deny the file accept request
else:
encryption.send(client_socket, {'cmd': 'file_deny', 'peer': data['peer']})
# 拒絕接收文件
elif data['type'] == 'file_deny':
main_window.button_send_file.configure(text="傳送檔案")
if current_connect_session == '':
main_window.button_send_file.configure(state='disabled')
else:
main_window.button_send_file.configure(state='disabled')
tk.messagebox.showinfo("Notification !", "Receiver denied to accept the file ! ")
# 傳送文件
elif data['type'] == 'file_accept':
try:
total_file_bytes = 0
addr = (data['ip'], 8888)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(addr)
# 計算傳送檔案的時間
start_send_time = time.time()
with open(filename, 'rb') as file:
while True:
fdata = file.read(1024)
if not fdata:
break
total_file_bytes += len(fdata)
# client.send(fdata)
client.send(fdata)
file.close()
client.close()
end_send_time = time.time()
# append send file message to chat_history
main_window.chat_history['state'] = 'normal'
main_window.chat_history.insert('end', 'Send %s bytes in %s seconds\n\n' %
(total_file_bytes, format(end_send_time - start_send_time, '.2f')),
'hint')
main_window.chat_history.see('end')
main_window.chat_history['state'] = 'disabled'
finally:
filename = ''
filename_short = ''
file_transfer_waiting = False
main_window.button_send_file.configure(text='傳送檔案')
if current_connect_session == '':
main_window.button_send_file.configure(state='disabled')
else:
main_window.button_send_file.configure(state='normal')
tk.messagebox.showinfo("Notification ! ", "File send successfully ! ")
def main():
global login_window
login_window = LoginWin()
login_window.button_login.configure(command=on_button_login_clicked)
login_window.button_register.configure(command=on_button_register_clicked)
login_window.show()
if __name__ == '__main__':
main()