-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
193 lines (171 loc) · 5.63 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
import Crypto
from netinterface import network_interface
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Random import get_random_bytes
from Crypto.Hash import SHA512
from protocolyzer import Protocolyzer
from protocolyzer import Message
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES, PKCS1_OAEP
from getpass import getpass
import time
NET_PATH = './'
OWN_ADDR = 'A'
netint = network_interface(NET_PATH, OWN_ADDR)
timeouttimestamp=time.time()-60
state = 0
def current_time():
return str(time.strftime("%H:%M:%S", time.localtime()))+str(" : ")
def init_connection():
recipient_key = RSA.import_key(open("public.pem").read())
global session_key
session_key = get_random_bytes(16)
print(current_time()+"Session key: "+str(session_key))
global proto
proto = Protocolyzer(session_key)
cipher_rsa = PKCS1_OAEP.new(recipient_key)
enc_session_key = cipher_rsa.encrypt(session_key)
netint.send_msg('B', enc_session_key)
status, result = wait_for_msg()
if status:
if proto.deprotocolyze(result).type == 2:
return True
return False
def get_login_data():
print("Login process")
print("Id: ", end='')
id = input()
passwd = getpass() # Hides characters during input
return id, passwd
def login():
id, passwd = get_login_data()
message_id = Message(data=bytes(id, 'utf-8'), type=3)
message_pw = Message(data=bytes(passwd, 'utf-8'), type=4)
netint.send_msg('B', proto.protocolyze(message_id))
status,msg = wait_for_msg()
if status:
if proto.deprotocolyze(msg).type == 5:
netint.send_msg('B', proto.protocolyze(message_pw))
return status
def wait_for_msg():
status = False
i = 0
while not status and i < 15:
status, msg = netint.receive_msg(blocking=False)
i += 1
time.sleep(0.2)
return status, msg
def upload(filename):
global state
global timeouttimestamp
with open(filename, 'r') as f:
lines = f.readlines()
lines_str = ""
for l in lines:
lines_str += l
message_data = Message(data=bytes(lines_str,'utf-8'), type=7)
netint.send_msg('B', proto.protocolyze(message_data))
status, result = wait_for_msg()
if status:
msg = proto.deprotocolyze(result)
if msg.type == 5:
return True
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
return False
def download(filename):
global state
global timeouttimestamp
status, result = wait_for_msg()
if not status:
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
return False
msg = proto.deprotocolyze(result)
if(msg.type == 8):
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
return
with open(filename, 'w+') as f:
for line in list(msg.data.decode('utf-8')):
f.write(line)
f.close()
return True
def command():
global state
global timeouttimestamp
print("Type your command: ",end='')
cmd = input()
split = cmd.split()
if split[0] == "upload":
message_command = Message(data=bytes(cmd, 'utf-8'), type=6)
netint.send_msg('B', proto.protocolyze(message_command))
status,msg = wait_for_msg()
result = proto.deprotocolyze(msg)
if status:
if result.type != 5:
if(result.type == 8):
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
return
print(current_time()+result.data.decode('utf-8'))
else:
if upload(split[1]):
print(current_time()+"Upload successful")
else:
print(current_time()+"Upload failed")
else:
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
elif split[0] == "download":
message_command = Message(data=bytes(cmd, 'utf-8'), type=6)
netint.send_msg('B', proto.protocolyze(message_command))
if download(split[1]):
print(current_time()+"Download successful")
else:
print(current_time()+"Download failed")
else:
message_command = Message(data=bytes(cmd, 'utf-8'), type=6)
netint.send_msg('B', proto.protocolyze(message_command))
status, result = wait_for_msg()
msg=proto.deprotocolyze(result)
if status:
if(msg.type == 8):
print(current_time()+"Timeout error")
state=0
return
print(msg.data.decode("utf-8"))
else:
print(current_time()+"Timeout error")
timeouttimestamp=time.time()
state=0
while True:
if state == 0:
waittime=time.time()-timeouttimestamp
if(waittime >= 60):
if init_connection():
print(current_time()+"Connected successfully!")
state = 1
else:
print(current_time()+"Connection failed!")
else:
print(str(round(60-waittime)) +"seconds till you can attempt to login")
time.sleep(10)
elif state == 1:
if login():
print(current_time()+"Successful login!")
state = 2
else:
print(current_time()+"Login failed, wrong id or password!")
elif state == 2:
try:
command()
except:
print("Error while communicating with the server, disconnecting")
timeouttimestamp=time.time()
state = 0