-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrv.py
67 lines (57 loc) · 2.63 KB
/
srv.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
""" _ _ _
___ _ __ _ _ _ __ | |_ ___ ___| |__ __ _| |_
/ __| '__| | | | '_ \| __/ _ \ / __| '_ \ / _` | __|
| (__| | | |_| | |_) | || (_) | (__| | | | (_| | |_
\___|_| \__, | .__/ \__\___/ \___|_| |_|\__,_|\__| Server
|___/|_|
by @CAprogs (https://github.com/CAprogs)
"""
import socket
from Server.server import Server, conversations, users
from Server.User import clear_console, ENC_DEC_MODE
from Server.Database import DatabaseHandler, DB_NAME
HOST = "127.0.0.1" # Hosting on localhost
PORT = 1234 # Port to open
LIMIT_CLIENTS = 1 # Number of clients that can connect to the server
LENGTH_OF_BYTES = 2048 # Length of the RSA keys in bytes
if __name__ == '__main__':
try:
clear_console()
print("")
SERVER_NAME = input("Choose a username (CTRL + C to exit) ▶︎ ")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(LIMIT_CLIENTS)
print(f"\nHosting the chat on {HOST}:{PORT}\n\nWaiting for connection ...")
server = Server(SERVER_NAME, HOST, PORT, s)
server.authenticate_client()
except KeyboardInterrupt:
database = DatabaseHandler(DB_NAME)
print("\n\nSaving datas ..\nExiting Session ..")
# register conversations
for id, conversation in conversations.items():
database.insert_conversation(conversation["sender"],
conversation["receiver"],
conversation["message"],
conversation["timestamp"])
# register the server as an user
database.insert_user(server.username,
server.public_key.decode(ENC_DEC_MODE),
server.host,
server.public_ip,
server.city,
server.region,
server.location,
server.timestamp)
# register all users
for id, user in users.items():
database.insert_user(user['username'],
user['public_key'].decode(ENC_DEC_MODE),
user['host'],
user['public_ip'],
user['city'],
user['region'],
user['location'],
user['timestamp'])
database.conn.close()
print("\nSession closed !\n")