-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
247 lines (215 loc) · 7.26 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from os import makedirs, remove
from subprocess import call
from platform import system
import socket
from threading import Thread
from time import sleep, time
secret_key = b''
FILES_CURRENTLY_RECEIVING = FILES_RECEIVED = SIZE_RECEIVED = SIZE_SENT = 0
secret_key_accepted = False
reachable_port = None
BUFFER_SIZE = 1024*1024
FINISHED = False
USER_DEFINED_LIMIT = 500
MAX_SIMULTANEOUS_FILES = 0
i = 0
while True:
i+=1
try:
globals()[f"_{i}"] = open('x','ab')
except:
MAX_SIMULTANEOUS_FILES = True
finally:
if MAX_SIMULTANEOUS_FILES or i == USER_DEFINED_LIMIT * 2:
MAX_SIMULTANEOUS_FILES = int(i // 2)
for _ in range(i+1):
try:
globals()[f"_{_}"].close()
del globals()[f"_{_}"]
except:
pass
break
remove('x')
def size_calculator(file_size_raw: int):
file_size=''
file_size_tree=[]
size_units=['B','KB','MB','GB','TB','PB', 'EB', 'ZB', 'YB']
while file_size_raw > 1024:
file_size_tree.append(file_size_raw%1024)
file_size_raw=file_size_raw//1024
else:
file_size_tree.append(file_size_raw)
for i in range(len(file_size_tree)):
file_size_tree[i]=str(file_size_tree[i])+size_units[i]
file_size_tree.reverse()
for size_value in file_size_tree:
if size_value[0]!='0':
file_size+=str(size_value)+' '
return file_size
def duration_calculator(start: float, end: float):
final_duration = ''
duration_tree = []
units = ['sec', 'minute', 'hour', 'day']
def host_ports_for_reachability(port):
global reachable_port
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', port))
sock.listen()
while True:
while reachable_port:
sleep(1)
connection, address = sock.accept()
while reachable_port:
sleep(1)
reachable_port = True
current_port = True
if __receive_from_connection(connection) == b'reach':
while reachable_port and not current_port:
sleep(1)
__send_to_connection(connection, b'pass')
reachable_port = port
sock.close()
else:
reachable_port = None
except:
pass
def __initial_authenticator():
global secret_key, secret_key_accepted
local_ip = socket.gethostbyname_ex(socket.gethostname())[-1]
if local_ip[0:3] == '127':
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('10.255.255.255', 1))
local_ip = s.getsockname()[0]
print(f'waiting at... {local_ip}')
for port in range(65534, 1, -500):
Thread(target=host_ports_for_reachability, args=(port,)).start()
while type(reachable_port) != int:
sleep(1)
print(f"Connected")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', reachable_port))
sock.listen()
while True:
if secret_key:
if FILES_CURRENTLY_RECEIVING < MAX_SIMULTANEOUS_FILES:
connection, address = sock.accept()
if not secret_key_accepted:
authenticate_with_sender(connection)
else:
Thread(target=authenticate_with_sender, args=(connection,)).start()
else:
secret_key = input("Enter Secret Key as shown in sender: ").encode()
def authenticate_with_sender(connection):
global secret_key, secret_key_accepted
try:
__send_to_connection(connection, secret_key)
if __receive_from_connection(connection) == b'0':
secret_key_accepted = True
Thread(target=__file_receive, args=(connection,)).start()
else:
__send_to_connection(connection, b'-1')
connection.close()
secret_key = b''
secret_key = input("Wrong Secret Key entered try again: ").encode()
except:
try:
connection.close()
except:
pass
def __file_receive(connection):
global FILES_CURRENTLY_RECEIVING, FILES_RECEIVED
try:
FILES_CURRENTLY_RECEIVING += 1
path = __receive_from_connection(connection).decode()
try:
open(path, 'wb').close()
except FileNotFoundError:
temp = path.split('/')
del temp[-1]
folder_name = ''
for _ in temp:
folder_name += _ + '/'
makedirs(folder_name, exist_ok=True)
open(path, 'wb').close()
with open(path, 'ab') as file:
while True:
data = __receive_file_data(connection)
if data:
file.write(data)
else:
file.close()
break
sleep(1)
FILES_CURRENTLY_RECEIVING -= 1
__send_to_connection(connection, b'{-0}')
FILES_RECEIVED += 1
connection.close()
except:
try:
connection.close()
except:
pass
def __receive_file_data(connection):
global SIZE_RECEIVED
length = b''
while len(length) != 15:
length += connection.recv(15- len(length))
length = int(length)
SIZE_RECEIVED += length
if length:
data = b''
while len(data) != length:
data += connection.recv(length - len(data))
connection.send(b'-')
return data
else:
connection.send(b'-')
return False
def __send_to_connection(connection, data_bytes: bytes):
global SIZE_SENT
data_byte_length = len(data_bytes)
connection.send(str(data_byte_length).encode())
if connection.recv(1) == b'-':
connection.send(data_bytes)
if connection.recv(1) == b'-':
SIZE_SENT += data_byte_length
def __receive_from_connection(connection):
global SIZE_RECEIVED
length = int(connection.recv(BUFFER_SIZE))
connection.send(b'-')
data_bytes = b''
while len(data_bytes) != length:
data_bytes += connection.recv(BUFFER_SIZE)
connection.send(b'-')
SIZE_RECEIVED += length
return data_bytes
def update_screen():
global FILES_CURRENTLY_RECEIVING, FINISHED
while FILES_CURRENTLY_RECEIVING == 0:
sleep(0.1)
s_time = time()
while not FINISHED:
if FILES_CURRENTLY_RECEIVING != 0:
sleep(0.5)
else:
FINISHED = True
if not FINISHED:
if system() == 'Windows':
call('cls', shell=True)
else:
call('clear', shell=True)
print('-' * 17, flush=True)
print('RECEIVING REPORT', flush=True)
print('-' * 17, flush=True)
print(f"Currently: {FILES_CURRENTLY_RECEIVING} Files", flush=True)
print(f"Completed: {FILES_RECEIVED} Files", flush=True)
print(f"Sent Data: {size_calculator(SIZE_SENT)}", flush=True)
print(f"Received Data: {size_calculator(SIZE_RECEIVED)}", flush=True)
print(f"Time Elapsed: {int(time()-s_time)} seconds", flush=True)
print('-' * 9, flush=True)
print('FINISHED')
print('-' * 9, flush=True)
input()
Thread(target=__initial_authenticator).start()
Thread(target=update_screen).start()