-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver.py
128 lines (105 loc) · 4.1 KB
/
Receiver.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
import socket
import time
import argparse
import os
import Packet
import Connection
def decrypt_receiver(filename, key):
enc_file = 'enc_{}'.format(filename)
print('[Receiver]: Decrypting "{}"'.format(enc_file))
f = open(enc_file, "rb").read()
f = key.decrypt(f)
open(filename, "wb").write(f)
os.remove(enc_file)
class Receiver:
def __init__(self, listenport=33122, timeout_t=30):
self.timeout = timeout_t
self.last_cleanup = time.time()
self.port = listenport
self.host = ''
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.settimeout(timeout_t)
self.s.bind((self.host, self.port))
self.connections = {}
self.MESSAGE_HANDLER = {
'start': self.handle_start,
'data': self.handle_data,
'end': self.handle_end,
'ack': self._handle_ack
}
def start(self):
print("Started")
while True:
try:
message, address = self.receive()
msg_type, seqno, data, checksum = Packet.split_packet(message)
if Packet.validate_checksum(message):
self.MESSAGE_HANDLER.get(msg_type, self._handle_other)(seqno, data, address)
if time.time() - self.last_cleanup > self.timeout:
self.cleanup()
if len(self.connections) == 0:
self.s.close()
break
except socket.timeout:
self.cleanup()
raise TimeoutError
except KeyboardInterrupt:
break
except ValueError:
pass
def receive(self):
return self.s.recvfrom(4096)
def send(self, message, address):
self.s.sendto(message, address)
def send_ack(self, seqno, address):
message = Packet.make_packet('ack', seqno, str(self.connections[address].wc).encode())
self.send(message, address)
def handle_start(self, seqno, data, address):
if address not in self.connections:
self.connections[address] = Connection.Connection(address[0], address[1], seqno, data.decode())
conn = self.connections[address]
acks = conn.ack(seqno, data)
for values in acks:
ackno, res_data = values
self.send_ack(ackno, address)
def handle_data(self, seqno, data, address):
if address in self.connections:
conn = self.connections[address]
acks = conn.ack(seqno, data)
for values in acks:
ackno, res_data = values
for l in res_data:
conn.record(l)
self.send_ack(ackno, address)
def handle_end(self, seqno, data, address):
if address in self.connections:
conn = self.connections[address]
acks = conn.ack(seqno, data)
for values in acks:
ackno, res_data = values
for l in res_data:
conn.record(l)
self.send_ack(ackno, address)
conn.end()
del self.connections[address]
def _handle_ack(self, seqno, data, address):
pass
def _handle_other(self, seqno, data, address):
pass
def cleanup(self):
now = time.time()
for address in list(self.connections):
conn = self.connections[address]
if now - conn.updated > self.timeout:
conn.end()
del self.connections[address]
self.last_cleanup = time.time()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Receive files via a fast and secure UDP channel.",
epilog="Unicorns powered this")
parser.add_argument("-p", "--port", help="UDP port, defaults to 33122", type=int, default=33122)
parser.add_argument("-t", "--timeout", help="Timeout for each socket, defaults to 3s", type=int, default=10)
args = parser.parse_args()
r = Receiver(args.port, args.timeout)
r.start()