-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
236 lines (167 loc) · 7.11 KB
/
client.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
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
import socket
import threading
import sys
import os
import tkinter as tk
import tkinter.scrolledtext as tkst
import tkinter.messagebox as tkmb
import tkinter.filedialog as filedialog
from datetime import datetime
import rsa
import base64
public_key , private_key = rsa.newkeys(1024)
public_key_partner = None
print(public_key.save_pkcs1("PEM"))
Host = 'localhost'
Port = 9999 # Any port between 0 and 65535
def add_message_to_message_box(message):
message_box.config(state=tk.NORMAL)
# Define colors for chat bubbles
bubble_bg_color = OCEAN_BLUE
bubble_text_color = WHITE
# Create a custom tag for the chat bubble style
message_box.tag_configure("bubble", background=bubble_bg_color, foreground=bubble_text_color, justify="left", wrap="word", spacing2=5)
# Get the current date and time
current_time = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
# Add the message to the message box with chat bubble styling
message_box.insert(tk.END, "\n", ("bubble",))
message_box.insert(tk.END, message + "\n", ("bubble",))
# Add the current time in a smaller font inside the bubble
message_box.insert(tk.END, f"{current_time}\n", "small")
# Configure small font style for the current time inside the bubble
message_box.tag_configure("small", font=("Helvetica", 10), foreground="gray")
message_box.config(state=tk.DISABLED)
message_box.see(tk.END)
def connect_to_server():
try:
client.connect((Host, Port))
global public_key_partner
public_key_partner = rsa.PublicKey.load_pkcs1(client.recv(1024))
client.send(public_key.save_pkcs1("PEM"))
#print("Connected to server on port: %s" % Port)
add_message_to_message_box("[SERVER] Connected to the server")
#communicate_to_server(client)
except:
message_box.showerror("unable to connect to server", f"Unable to connect to server {Host}:{Port}")
#print("Connection failed. Error : " + str(sys.exc_info()))
exit(0)
#print("Connecting to server...")
username = username_textbox.get()
if username != "":
client.sendall(username.encode('utf-8'))
else:
message_box.showerror("Username is empty", "Username is empty")
threading.Thread(target=listen_for_messages,
args=(client, )).start()
username_textbox.config(state=tk.DISABLED)
username_button.config(state=tk.DISABLED)
def send_message():
message = message_textbox.get()
if message != '':
client.sendall(rsa.encrypt(message.encode('utf-8'), public_key_partner))
message_textbox.delete(0, len(message))
else:
message_box.showerror("Message is empty", "Message is empty")
def choose_file():
file_path = filedialog.askopenfilename()
if file_path:
send_file_to_server(client, file_path)
DARK_GREY = '#121212'
MEDIUM_GREY = '#1F1824'
OCEAN_BLUE = '#464EB8'
WHITE = "white"
FONT=('Helvetica', 17)
BUTTON_FONT = ('Helvetica', 15)
SMALL_FONT = ('Helvetica', 13)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
root = tk.Tk()
root.geometry("600x520")
root.title("Messenger Client")
root.resizable(False,False)
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=4)
root.grid_rowconfigure(2, weight=1)
top_frame = tk.Frame(root, width=600, height=100, bg=DARK_GREY)
top_frame.grid(row=0, column=0, sticky=tk.NSEW)
middle_frame = tk.Frame(root, width=600, height=400, bg=MEDIUM_GREY)
middle_frame.grid(row=1, column=0, sticky=tk.NSEW)
bottom_frame = tk.Frame(root, width=600, height=100, bg=DARK_GREY)
bottom_frame.grid(row=2, column=0, sticky=tk.NSEW)
username_label = tk.Label(top_frame, text="Enter Username:", bg=DARK_GREY, fg=WHITE, font=FONT)
username_label.pack(side=tk.LEFT, padx=10)
username_textbox = tk.Entry(top_frame, font=FONT, bg=DARK_GREY, fg=WHITE, width=23)
username_textbox.pack(side=tk.LEFT)
username_button = tk.Button(top_frame, text="Join", font=BUTTON_FONT, bg=OCEAN_BLUE, fg=WHITE, command=connect_to_server)
username_button.pack(side=tk.LEFT, padx=15)
message_textbox = tk.Entry(bottom_frame, font=FONT, bg=MEDIUM_GREY, fg=WHITE, width=30)
message_textbox.pack(side=tk.LEFT, padx=10)
message_button = tk.Button(bottom_frame, text="Send", font=BUTTON_FONT, bg=OCEAN_BLUE, fg=WHITE, command=send_message)
message_button.pack(side=tk.LEFT, padx=10)
file_choose_button = tk.Button(bottom_frame, text="File @", font=BUTTON_FONT, bg=OCEAN_BLUE, fg=WHITE, command=choose_file)
file_choose_button.pack(side=tk.LEFT, padx=10)
message_box = tkst.ScrolledText(middle_frame, bg=MEDIUM_GREY, fg=WHITE, font=SMALL_FONT, width=67, height=23)
message_box.config(state=tk.DISABLED)
message_box.pack(side=tk.TOP)
def listen_for_messages(client):
while True:
try:
# Receive the data from the client
data = rsa.decrypt(client.recv(1024), private_key).decode('utf-8')
if data != "":
if data == "send_file":
receive_file(client)
else:
# Check if data has ":" in it
if " : " not in data:
add_message_to_message_box(data)
else:
message = data.split(" : ")
add_message_to_message_box(message[0] + " : " + message[1])
else:
message_box.showerror("Error", "Empty message received from server")
break
except Exception as e:
print(f"Error receiving data from server: {e}")
break
def receive_file(client):
try:
# Receive the file data in chunks
file_data = rsa.decrypt(client.recv(1024), private_key).decode('utf-8')
file = open("recived_file", "w")
file.write(file_data)
file.close()
add_message_to_message_box("New File received and saved as 'received_file'")
except Exception as e:
print(f"Error receiving file: {e}")
def send_file_to_server(client, file_path):
try:
client.sendall(rsa.encrypt("send_file".encode('utf-8'), public_key_partner))
with open(file_path, 'r') as file:
data = file.read(1048576)
encrypted_data = rsa.encrypt(str(data).encode('utf-8'), public_key_partner)
client.sendall(encrypted_data)
add_message_to_message_box(f"File '{os.path.basename(file_path)}' sent to server.")
except Exception as e:
add_message_to_message_box(f"Error sending file to server: {e}")
def send_message_to_server(client):
while True:
message = input("Message: ")
if message != "":
if message == "send_file":
client.sendall(message.encode('utf-8'))
send_file_to_server(client)
continue
client.sendall(message.encode('utf-8'))
else:
add_message_to_message_box("Message is empty")
break
#def communicate_to_server(client):
#while True:
#data = client.recv(2048).decode('utf-8')
#if data == "":
# break
#print(data)
def main():
root.mainloop()
if __name__ == '__main__':
main()