-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
216 lines (175 loc) · 6.31 KB
/
server.py
File metadata and controls
216 lines (175 loc) · 6.31 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
import socket
import threading
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from hybrid_rsa_aes import HybridCipher
localPrivateKey = rsa.generate_private_key(
public_exponent=65537, key_size=2048, backend=default_backend()
)
localPublicKey = localPrivateKey.public_key()
print(localPublicKey)
header = 64
port = 30012
build = "2.2 Secure"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("", port))
print(f"Bound to port {port}")
server.listen()
accounts = [
{"username": "sleepy", "password": "pass1"},
{"username": "test", "password": "pass2"},
{"username": "test1", "password": "pass3"},
]
accounts_with_client_data = []
for account in accounts:
accounts_with_client_data.append(
{
"username": account["username"],
"password": account["password"],
"client": None,
"public_key": None,
}
)
def encrypt(message, clientPublicKey):
encrypted_message = (
HybridCipher()
.encrypt(data=message, rsa_public_key=clientPublicKey)
.encode("utf-8")
)
return encrypted_message
def decrypt(encrypted_message):
decrypted_message = HybridCipher().decrypt(
cipher_text=encrypted_message.decode("utf-8"), rsa_private_key=localPrivateKey
)
return decrypted_message
def recieve_string_from_client(client):
try:
message_length = client.recv(header).decode("utf-8")
recieved_message = decrypt(client.recv(int(message_length)))
print(recieved_message)
return recieved_message
except Exception as e:
print(e)
return "error"
def send_string_to_client(client, message, clientPublicKey):
try:
client.send(
str(len(encrypt(message, clientPublicKey))).zfill(header).encode("utf-8")
)
client.send(encrypt(message, clientPublicKey))
except Exception as e:
print(e)
return "error"
def send_message_to_client(client, message, clientPublicKey):
try:
message = "MESSAGE|" + message
client.send(
str(len(encrypt(message, clientPublicKey))).zfill(header).encode("utf-8")
)
client.send(encrypt(message, clientPublicKey))
except Exception as e:
print(e)
return "error"
def broadcast(message):
for client_data in accounts_with_client_data:
if client_data["client"] != None:
send_message_to_client(
client_data["client"], message, client_data["public_key"]
)
def handle(account_login):
while True:
message = recieve_string_from_client(account_login["client"])
account_id = accounts_with_client_data.index(account_login)
if message == "error":
account_login["client"].close()
accounts_with_client_data[account_id]["client"] = None
accounts_with_client_data[account_id]["public_key"] = None
broadcast(f"LOG | {account_login['username']} left the chat.")
break
# Global Prefix List
if message.startswith("MESSAGE|"):
broadcast(account_login["username"] + " > " + message[8:])
def recieve():
def client():
client, address = server.accept()
print(f"Connection from {address} has been established!")
proper_login = False
client.send(
str(
len(
localPublicKey.public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.PKCS1,
)
)
)
.zfill(header)
.encode("utf-8")
)
client.send(
localPublicKey.public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.PKCS1,
)
)
message_length = client.recv(header).decode("utf-8")
try:
clientPublicKey = client.recv(int(message_length))
clientPublicKey = serialization.load_der_public_key(
clientPublicKey, backend=default_backend()
)
except Exception as e:
client.close()
print(e)
return
print(clientPublicKey)
send_string_to_client(client, build, clientPublicKey)
send_string_to_client(client, "LOGIN|REQUEST", clientPublicKey)
recieved_string = recieve_string_from_client(client)
if recieved_string == "error":
client.close()
return
if recieved_string.startswith("USERNAME|"):
username = recieved_string[9:]
recieved_string = recieve_string_from_client(client)
if recieved_string == "error":
client.close()
return
if recieved_string.startswith("PASSWORD|"):
password = recieved_string[9:]
account_id = 0
proper_login = False
account_login = {"username": username, "password": password}
print(account_login)
print(accounts)
if account_login in accounts:
account_id = accounts.index(account_login)
if accounts_with_client_data[account_id]["client"] != None:
send_string_to_client(
client, "LOGIN|ALREADY_LOGGED_IN", clientPublicKey
)
client.close()
return
else:
proper_login = True
if proper_login:
send_string_to_client(client, "LOGIN|SUCCESS", clientPublicKey)
accounts_with_client_data[account_id]["client"] = client
accounts_with_client_data[account_id]["public_key"] = clientPublicKey
broadcast(f"LOG | {username} joined the chat.")
threading.Thread(target=handle, args=(client, username)).start()
else:
send_string_to_client(client, "LOGIN|REJECTED", clientPublicKey)
client.close()
return
if proper_login:
account_login = accounts_with_client_data[account_id]
try:
thread = threading.Thread(target=handle, args=[account_login])
thread.start()
except:
pass
while True:
client()
recieve()