-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
104 lines (82 loc) · 3.35 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
#Multithread Socket Code goes to Digamber
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from encryption import Decode_Encryption_Method
import socket
import time
ClientMultiSocket = socket.socket()
#host = '127.0.0.1'
host = '172.17.4.17'
port = 2004
print('Waiting for connection response')
def client_RSA_handshake(ClientSocket):
public_key_bytes = ClientSocket.recv(2048)
print(public_key_bytes)
public_key = RSA.import_key(public_key_bytes)
session_key = get_random_bytes(16)
cipher_rsa = PKCS1_OAEP.new(public_key)
enc_session_key = cipher_rsa.encrypt(session_key)
print('SENDING ENC SESSION KEY')
print(enc_session_key)
ClientSocket.send(enc_session_key)
print(ClientSocket.recv(1024).decode('utf-8'))
return session_key
def recv_experiment_details(ClientSocket):
print('Starting to recieve experiment details')
config = dict()
config['enc_method'] = int(ClientSocket.recv(1024).decode('utf-8'))
ClientMultiSocket.send(str.encode('Received'))
config['packet_bytes'] = int(ClientSocket.recv(1024).decode('utf-8'))
ClientMultiSocket.send(str.encode('Received'))
config['n_packets_bundled'] = int(ClientSocket.recv(1024).decode('utf-8'))
ClientMultiSocket.send(str.encode('Received'))
config['packets_per_sec'] = int(ClientSocket.recv(1024).decode('utf-8'))
ClientMultiSocket.send(str.encode('Received'))
config['tot_packets'] = int(ClientSocket.recv(1024).decode('utf-8'))
ClientMultiSocket.send(str.encode('Received'))
time.sleep(0.5)
print('recieved details')
ClientSocket.send(str.encode('Good to go!'))
print('and sent confirmation back.')
return config
def await_syncronization(ClientSocket):
ClientSocket.recv(1024)
def run_experiment(ClientSocket, config, session_key):
encryption_method, decryption_method = Decode_Encryption_Method(config['enc_method'])
prev_time = time.time()
bundle, bundle_size = bytes(), 0
for packet_num in range(config['tot_packets']):
#print(f'Adding packet {packet_num}')
data = str.encode(str(packet_num))
data += str.encode('0') * (config['packet_bytes'] - len(data))
bundle += data
bundle_size += 1
while time.time() - prev_time < 1 / config['packets_per_sec']:
continue
if bundle_size == config['n_packets_bundled']:
ciphertext = encryption_method(bundle, session_key)
ClientSocket.send(ciphertext)
ClientSocket.recv(1024)
packet_delay = time.time() - prev_time
packet_delay = round(packet_delay, 6)
ClientSocket.send(str.encode(str(packet_delay)))
ClientSocket.recv(1024)
bundle, bundle_size = bytes(), 0
prev_time = time.time()
ClientSocket.send(encryption_method(str.encode('exit'), session_key))
print('Done with experiment!')
def stop_experiment(ClientMutliSocket):
return False
try:
ClientMultiSocket.connect((host, port))
except socket.error as e:
print(str(e))
while True:
config = recv_experiment_details(ClientMultiSocket)
session_key = client_RSA_handshake(ClientMultiSocket)
await_syncronization(ClientMultiSocket)
run_experiment(ClientMultiSocket, config, session_key)
print('howdy')
if stop_experiment(ClientMultiSocket):
break