-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.py
152 lines (132 loc) · 4.12 KB
/
join.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
#!/usr/bin/python3
# client script
import socket
import threading
import sys
import time
import getpass
from cryptography.fernet import Fernet
def encrypt_data(fileData):
with open('/etc/join/key-chatroom.txt', 'rb') as keyFile:
key = keyFile.read()
keyFile.close()
fernet = Fernet(key)
encryptedData = fernet.encrypt(fileData)
return encryptedData
def decrypt_data(fileData):
with open('/etc/join/key-chatroom.txt', 'rb') as keyFile:
key = keyFile.read()
keyFile.close()
fernet = Fernet(key)
decryptedData = fernet.decrypt(fileData)
return decryptedData
def main():
address = '192.0.0.1'
port = 1001
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
#print("Connecting to the server...")
client.connect((address, port))
except ConnectionRefusedError:
print("Failed to connect to the server!")
sys.exit()
print("connected successfully.")
with open('/etc/join/user.txt', 'r') as f:
userFile = f.read()
f.close()
comma = ','
userName = ''
commaPos = []
for pos, char in enumerate(userFile):
if (char == comma):
commaPos.append(pos)
userCount = len(commaPos) - 1
for count in range(userCount):
userName = userFile[commaPos[count]+1:commaPos[count+1]]
rtk = sys.argv[1]
#print("Sending roomtoken")
rtk = rtk.encode()
rtk = encrypt_data(rtk)
client.send(rtk)
time.sleep(0.2)
answ = client.recv(1024).decode()
if answ == 'ack':
userName_ = userName.encode()
userName_ = encrypt_data(userName_)
client.send(userName_)
time.sleep(0.2)
answ = client.recv(1024).decode()
#print(answ)
if answ == '200':
pass
elif answ == '404':
msg = client.recv(1024).decode()
print(msg)
sys.exit()
else:
print('an error occurred')
sys.exit()
elif answ == 'rst':
print('invalid chatroom')
sys.exit()
def receive():
while True:
try:
message = client.recv(1024)
if not message:
break
message = decrypt_data(message)
try:
message = decrypt_data(message)
except Exception as e:
pass
message = message.decode()
print(message)
except Exception as e:
client.close()
print (e)
except KeyboardInterrupt:
client.shutdown(socket.SHUT_RDWR)
client.close()
sys.exit()
except:
print('ERROR 204: connection lost!')
#client.shutdown(socket.SHUT_RDWR)
client.close()
sys.exit()
def write():
try:
while True:
#message = getpass.getpass()
message = input()
print('''\033[A \033[A''')
if message == 'exit':
print('exiting.')
client.shutdown(socket.SHUT_RDWR)
client.close()
sys.exit()
else:
pass
message = f'{userName}: {message}'
message = message.encode()
message = encrypt_data(message)
client.send(message)
except Exception as e:
print('ERROR:', e)
except KeyboardInterrupt:
client.shutdown(socket.SHUT_RDWR)
client.close()
sys.exit()
receive_thread = threading.Thread(target=receive)
#print('recieve-thread started')
receive_thread.start()
write_thread = threading.Thread(target=write)
#print('write-thread started')
write_thread.start()
try:
main()
except KeyboardInterrupt:
sys.exit()
except Exception as e:
sys.exit(e)