-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
65 lines (51 loc) · 1.81 KB
/
server.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
import socket
import base64
import random
import string
# Constants
MAX_CHUNK_SIZE = 32 # Maximum chunk size
DOMAIN_SUFFIX = ".example.com" # Suffix for the domain name
def process_dns_query(query_data):
parts = query_data.split(b'.')
data_b64 = parts[0]
index = int(parts[1])
data_chunk = base64.b64decode(data_b64)
return index, data_chunk
def save_to_file(data_chunks):
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
filename = f"received_{random_string}.txt"
with open(filename, 'ab') as file:
for index, data_chunk in sorted(data_chunks.items()):
file.write(data_chunk)
def main():
server_ip = '0.0.0.0' # Listen on all interfaces
server_port = 53
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((server_ip, server_port))
print("Server listening...")
data_chunks = {}
expected_index = 0
total_packets = 0
while True:
data, addr = sock.recvfrom(1024)
total_packets += 1
index, data_chunk = process_dns_query(data)
print(f"Received packet {index + 1}")
print("Data received:", data_chunk.decode()) # Print the received data chunk
if index == expected_index:
data_chunks[index] = data_chunk
expected_index += len(data_chunk)
else:
print(f"Missing chunk: {expected_index}")
continue
# Check if there are any missing chunks
while expected_index in data_chunks:
expected_index += len(data_chunks[expected_index])
if len(data_chunk) < MAX_CHUNK_SIZE:
break
save_to_file(data_chunks)
print("File received and saved.")
print(f"Total number of packets received: {total_packets}")
sock.close()
if __name__ == "__main__":
main()