-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
616 lines (475 loc) · 20.9 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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# =================================================
# EC500 A2 Hackathon - P2P Chat
# P2P Client
# =================================================
import os
import signal
import socket
import sys
import threading
import time
import requests
from databases.chathistory import ChatHistory
SERVER_HOST = "18.224.190.128" # IP of the server
PORT = 7070
active_chat = None # IP of the active chat
new_chat = None # Flag for communicating across threads if new chat has been started
created = 1 # Flag for communicating across threads:
# tells Listen() thread if a Chat() thread needs to be created
handshake = 0 # Flag for communicating across threads: indicates if handshake for chat has been made
once = 0 # Flag for communicating across threads: makes sure handshake message is only displayed once
disconnect = 0 # Flag for communicating across threads: lets Send() thread know that user has disconnected
my_ip = requests.get('https://api.ipify.org').text
my_username = None
def get_ip(username):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_HOST, PORT))
msg = sock.recv(1024)
if msg.decode() == '?':
request = "ip - " + username
sock.sendall(request.encode())
msg = sock.recv(1024)
if msg.decode() != "Not found":
return msg.decode()
else:
return None
def get_username(ip):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_HOST, PORT))
msg = sock.recv(1024)
if msg.decode() == '?':
request = "username - " + ip
sock.sendall(request.encode())
msg = sock.recv(1024)
if msg.decode() != "Not found":
return msg.decode()
else:
print("Invalid IP")
return None
# Talks to server to check if ip is connected
def is_online(username):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_HOST, PORT))
msg = sock.recv(1024)
if msg.decode() == '?':
request = "online - " + username
sock.sendall(request.encode())
msg = sock.recv(1024)
if msg.decode() == "0":
# print("User is not online")
return False
else:
# print("User is online")
return True
# Tells server that you are no longer online
def offline():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_HOST, PORT))
msg = sock.recv(1024)
if msg.decode() == '?':
request = "offline - " + my_ip
sock.sendall(request.encode())
def is_username(username):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((SERVER_HOST, PORT))
msg = sock.recv(1024)
if msg.decode() == '?':
request = "exists - " + username
sock.sendall(request.encode())
msg = sock.recv(1024)
if msg.decode() == "0":
print("This is not a valid username.")
return False
else:
# print("User is online")
return True
# Thread for listening for messages from a specific location
class Chat(threading.Thread):
def __init__(self, conn, addr, sock):
super().__init__()
self.conn = conn
self.addr = addr
self.sock = sock
self.username = get_username(self.addr[0])
def run(self):
global active_chat, created, handshake, new_chat, once, disconnect
created = 1
# Create db connection
history = ChatHistory(my_username, self.username)
# Receive messages
while True:
msg = self.conn.recv(4096)
# If this is no longer the active chat
if self.addr[0] != active_chat:
if msg.decode() and not once:
once = 1
# Check if user wants to connect again
print(self.username + " wants to chat! Would you like to chat with them? Answer with Y or N.")
# Wait for user response in other thread
new_chat = 1
while new_chat == 1:
time.sleep(0.1)
once = 0
# If user wants to chat, respond "Yes", and keep listening
if new_chat == 2:
new_chat = 0
msg = "Yes"
self.conn.sendall(msg.encode())
active_chat = self.addr[0]
handshake = 1
# If user doesn't want to chat, respond "No", and exit thread
else:
msg = "No"
self.conn.sendall(msg.encode())
break
# Decode and print message
elif msg.decode():
if msg.decode() == "Left":
print("The user has left the chat.")
print("What username would you like to connect to?\n>> ")
prev_chat = active_chat
active_chat = None
handshake = 0
break
else:
# ==============================================
# Storing messages that you receive
# ==============================================
message = msg.decode() # extract message
if message == "intro":
print("\nHere are all the messsages you missed:")
else:
history.add_message(self.username, my_username, message, 'read') # store in db
print(self.username + ": " + message) # show message
if message.find('2021') == -1:
print(">> ")
# Thread for listening for new connections
class Listen(threading.Thread):
# Run listening thread
def run(self):
global active_chat, new_chat, created, handshake, once
# Create socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('0.0.0.0', PORT)) # Use 0.0.0.0 if on ec2 instance
while True:
# Listen for a connection
self.sock.listen()
# Accept connection
conn, addr = self.sock.accept()
if (not active_chat or addr[0] != active_chat) and addr[0] != SERVER_HOST and not once:
once = 1
username = get_username(addr[0])
print(username + " wants to chat! Would you like to chat with them? Answer with Y or N.")
# Wait for user response in other thread
new_chat = 1
while new_chat == 1:
time.sleep(0.1)
once = 0
# If user wants to chat, respond "Yes", and pass off to thread
if new_chat == 2:
new_chat = 0
msg = "Yes"
conn.sendall(msg.encode())
chat = Chat(conn, addr, self.sock)
chat.daemon = True
chat.start()
active_chat = addr[0]
handshake = 1
# If user doesn't want to chat, respond "No", and keep listening
else:
msg = "No"
conn.sendall(msg.encode())
continue
# Start a chat thread for a chat initialized by the Send() thread
elif active_chat == addr[0] and not created:
chat = Chat(conn, addr, self.sock)
chat.daemon = True
chat.start()
active_chat = addr[0]
created = 1
# Don't create thread for server messages, just print them
elif addr[0] == SERVER_HOST:
msg = conn.recv(1024)
print(f"SERVER: {msg.decode()}")
# Thread for sending messages
# User first must specify what IP they want to send to
class Send(threading.Thread):
def __init__(self):
super().__init__()
self.username = None
self.ip = None
self.backlog = False
def send_backlog(self):
history = ChatHistory(my_username, self.username)
messages = history.get_all_unread(self.username)
if len(messages) > 0:
intro = "intro"
self.sock.sendall(intro.encode())
for msg in messages:
time.sleep(0.1)
full_msg = msg[2] + ": " + msg[1]
self.sock.sendall(full_msg.encode())
history.mark_read(msg[0])
# Run sending thread
def run(self):
global new_chat, active_chat, created, handshake, prev_chat
time.sleep(1)
while True:
time.sleep(0.5)
if self.backlog:
msg = input(">> ")
if msg == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
# Store message in database
if not new_chat:
if msg == "disconnect":
self.backlog = False
self.ip = None
self.username = None
else:
history = ChatHistory(my_username, self.username)
history.add_message(my_username, self.username, msg, 'unread')
continue
else:
# Responding to chat request
while response not in ['y', 'Y', 'n', 'N']:
response = input("Please respond with Y or N.\n>> ")
if response == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
if response in ['y', 'Y']:
new_chat = 2
self.backlog = False
else:
new_chat = 0
active_chat = self.ip
continue
# Check for new IP
if not active_chat:
self.ip = None
elif active_chat:
self.ip = active_chat
self.username = get_username(self.ip)
if not self.ip:
# Ask for a username
response = input("What username would you like to connect to?\n>> ")
if response == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
# If response is a username, save it
if not new_chat:
if response == my_username:
print("You cannot connect to yourself. Please try again.")
continue
# Check if valid username
if not is_username(response):
print("Please try again.")
continue
else:
self.ip = get_ip(response)
self.username = response
handshake = 0
else:
# Responding to chat request
while response not in ['y', 'Y', 'n', 'N']:
response = input("Please respond with Y or N.\n>> ")
if response == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
if response in ['y', 'Y']:
new_chat = 2
else:
new_chat = 0
active_chat = self.ip
continue
# Create socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Check if user is online
if is_online(self.username):
# If they are, get IP and connect
self.ip = get_ip(self.username)
print(f"\nConnecting to: {self.username}")
try:
self.sock.connect((self.ip, PORT))
created = 0
# Ask user if they want to chat
if not handshake:
# Wait to find out if user wants to chat or not
response = self.sock.recv(1024)
if response.decode() == "No":
print("Sorry, they do not want to chat with you.")
active_chat = None
# ==============================================
# Person is rejected, can prompt them to add messages
# that will be stored
# ==============================================
print("You can type messages now to be delivered the next time you connect.")
print("Enter 'disconnect' to stop storing messages for this user.")
self.backlog = True
continue
elif response.decode() == "Yes":
active_chat = self.ip
print(f"Connected to: {self.username}\n")
# Send any backlog messages
self.send_backlog()
# User has already agreed to chat
else:
active_chat = self.ip
print(f"Connected to: {self.username}\n")
# Send any backlog messages
self.send_backlog()
except:
print("Unable to connect. Please try again.")
active_chat = None
continue
# User is not online, backlog messages
else:
print("That user is offline.")
print("You can type messages now to be delivered the next time you connect.")
print("Enter 'disconnect' to stop storing messages for this user.")
self.backlog = True
continue
while True:
# Get user input
response = input(">> ")
if response == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
# Get new IP
if not active_chat and not new_chat:
if response == my_username:
print("You cannot connect to yourself. Please try again.")
continue
# Check if valid username
if not is_username(response):
print("Please try again.")
else:
self.ip = get_ip(response)
self.username = response
active_chat = self.ip
handshake = 0
break
# Get chat message
elif not new_chat:
msg = response
else:
# Responding to chat request
while response not in ['y', 'Y', 'n', 'N']:
response = input("Please respond with Y or N.\n>> ")
if response == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
if response in ['y', 'Y']:
new_chat = 2
else:
new_chat = 0
if handshake:
active_chat = self.ip
self.username = get_username(self.ip)
else:
active_chat = None
self.ip = None
break
# Disconnect
if msg == "disconnect":
prev_chat = active_chat
active_chat = None
# Let other user know you have disconnected
msg = "Left"
self.sock.sendall(msg.encode())
self.ip = None
self.username = None
self.backlog = None
break
# Connect to new chat if necessary
if active_chat and active_chat != self.ip:
if is_online(self.username):
self.ip = active_chat
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ip = get_ip(self.username)
self.sock.connect((self.ip, PORT))
if not handshake:
# Wait to find out if user wants to chat or not
response = self.sock.recv(1024)
if response.decode() == "No":
print("Sorry, they do not want to chat with you.")
active_chat = None
print("You can type messages now to be delivered the next time you connect.")
print("Enter 'disconnect' to stop storing messages for this user.")
self.backlog = True
break
elif response.decode() == "Yes":
print(f"Connected to: {self.username}\n")
# Send any backlog messages
self.send_backlog()
else:
print(f"Connected to: {self.username}\n")
# Send any backlog messages
self.send_backlog()
else:
print("You can type messages now to be delivered the next time you connect.")
print("Enter 'disconnect' to stop storing messages for this user.")
self.backlog = True
break
# Send message
try:
# Log message in chat history
history = ChatHistory(my_username, self.username)
self.sock.sendall(msg.encode())
history.add_message(my_username, self.username, msg, 'read')
except:
print("Something has gone wrong. The other user may have disconnected.")
active_chat = None
self.ip = None
self.username = None
break
# Communicates with the server to sign the user in
def SignIn():
global my_ip, my_username
# Create socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("\n----------------------------------------------------")
print("| Welcome to our P2P chat! |")
print("| To quit at any time, use ctrl+C or type 'exit' |")
print("----------------------------------------------------\n")
# Connect to server
print("Connecting to server...")
sock.connect((SERVER_HOST, PORT))
print("Connected to server\n")
while True:
# Get message from server
msg = sock.recv(1024)
if msg.decode() != "1":
if msg.decode() != '?':
print(msg.decode())
# Get user input
msg = input(">> ")
if msg == "exit":
os.kill(os.getpid(), signal.SIGINT)
time.sleep(1)
# Send message
sock.sendall(msg.encode())
else:
request = "login"
sock.sendall(request.encode())
# Sign-in was successful
else:
my_username = get_username(my_ip)
return 1
if __name__ == '__main__':
try:
if SignIn():
listen = Listen()
listen.daemon = True
listen.start()
send = Send()
send.daemon = True
send.start()
listen.join()
send.join()
except KeyboardInterrupt:
print("Signing out...")
offline()
sys.exit()