-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_server.py
More file actions
112 lines (99 loc) · 3.44 KB
/
auth_server.py
File metadata and controls
112 lines (99 loc) · 3.44 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
import socket,random,csv,hashlib, secrets,re
def s_send(sock, data, service_data=''):
data = bytearray(f'{len(data)}$@$~{data}{service_data}'.encode())
sock.send(data)
def s_recv(sock):
data = sock.recv(1024).decode()
indx = data.find('$@$~')
atkn = re.search('\$token=(.{32,32})\$', data)
if atkn:
data = data[:atkn.start()]
atkn = atkn[1]
else:
atkn = ''
print('Recieved message length - {}'.format(data[:indx]))
return data[indx+4:], atkn
socket.socket.s_send = s_send
socket.socket.s_recv = s_recv
def register(conn, addr, filename):
conn.s_send('Create password!', '$$$~')
print('Waiting for new password...')
password = conn.s_recv()[0]
with open(filename, 'a', newline = '') as opened_f:
writer = csv.writer(opened_f, delimiter = ';')
row=(hashlib.md5(addr[0].encode()).hexdigest(), hashlib.md5(password.encode()).hexdigest())
writer.writerow(row)
conn.s_send('You have registered')
print(f'{addr[0]} have registered')
return row[1]
def authentification(conn, addr, password, attempts = 3):
if attempts == 0:
conn.s_send('You entered invalid password for 3 times')
return False
conn.s_send('Enter your password', '$$$~')
print('Waiting for password...')
recv_pswd = conn.s_recv()[0]
if password == hashlib.md5(recv_pswd.encode()).hexdigest():
access_token = secrets.token_hex(16)
conn.s_send('You are logged on!','$token='+access_token+"$")
print(f'{addr[0]} logged on')
return access_token
else:
conn.s_send('invalid password')
print(f'{addr[0]} entered invalid password')
authentification(conn, addr, password, attempts-1)
def listening(sock):
print("waiting for the client...")
while True:
try:
conn, addr = sock.accept()
print(f"connected {addr}")
break
except BlockingIOError:
pass
pass_file="clients.csv"
try:
with open(pass_file, 'r', newline = '') as clients2:
reader = csv.reader(clients2, delimiter = ';')
for row in reader:
if row[0] == hashlib.md5(addr[0].encode()).hexdigest():
password = row[1]
break
else:
password = register(conn, addr, pass_file)
except FileNotFoundError:
password = register(conn, addr, pass_file)
access_token = authentification(conn, addr, password)
if access_token:
conn.s_send('Let\'s talk!', '@$$~')
while True:
msg = s_recv(conn)
print(msg[0])
if msg[1] != access_token:
print('Warning! Wrong token!')
break
if msg[0] == "stop":
break
conn.s_send(input("Enter message for client: "), '@$$~')
else:
print('Authentification failed!')
conn.close()
print(f'Connection with {addr[0]} closed!')
sock = socket.socket()
sock.setblocking(True)
con_port=13131
while True:
try:
sock.bind(('', con_port))
break
except OSError as oserr:
print("{} (порт {} занят)".format(oserr,con_port))
con_port = random.randint(1024,65535)
sock.listen(0)
print('Server is running at port {}'.format(con_port))
for _ in range(5):
try:
listening(sock)
except (ConnectionAbortedError, ConnectionResetError) as err:
print(err)
sock.close()