forked from YannChich/Http_Redirect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrudproxy.py
More file actions
174 lines (141 loc) · 7.01 KB
/
rudproxy.py
File metadata and controls
174 lines (141 loc) · 7.01 KB
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
import socket
import time
SizeOfPacket = 1024
clients = []
Server_IP = "127.0.0.10"
Scrap_IP = "127.0.0.5"
max_window = 64
last_received_id = {}
unacked_packets = {}
client_info = {}
# Get files trough TCP connection
def get_files():
# Create a TCP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.bind((Server_IP, 49153))
# Connect to the Scrap server
client_socket.connect((Scrap_IP, 49153)) # Utilisez Scrap_IP ici
print('Connected to Scrap server and asking for the list.')
# Sending a signal to the scrap server
message = "SIGNGET"
client_socket.send(message.encode())
# Receive data from the Scrap server
data = client_socket.recv(1024).decode()
print("Received the list of files.")
# Close the connection
client_socket.close()
return data
def hostname_to_numeric(hostname):
return sum(ord(c) for c in hostname)
def send_ack(server, addr, packet_id):
server.sendto("SIGNACK".encode(), addr)
client_info[addr]['congestion_window'] -= 1
if addr in unacked_packets and packet_id in unacked_packets[addr]:
del unacked_packets[addr][packet_id]
def send_lost_packet_signal(server, addr, expected_id):
print(f"Sending SIGNLOST:{expected_id} to {addr}")
server.sendto(f"SIGNLOST:{expected_id}".encode(), addr)
def send_signfull_signal(server, addr):
print(f"Sending SIGNFULL to {addr}")
server.sendto("SIGNFULL".encode(), addr)
def server_statistics(addr):
info = client_info.get(addr)
if not info:
return "No client information available."
elapsed_time = time.time() - info['connection_time']
stats = f"Packets sent: {info['packets_sent']}\nPackets lost: {info['packets_lost']}\nConnected time: {elapsed_time:.2f} seconds"
return stats
def remove_client(addr):
global clients
clients = [client for client in clients if client[1] != addr]
def receive(server):
global last_received_id
while True:
try:
data, addr = server.recvfrom(SizeOfPacket)
data = data.decode()
if data.startswith("SIGNEW:"):
name = data[data.index(":") + 1:]
numeric_value = hostname_to_numeric(name)
clients.append((name, addr, numeric_value))
last_received_id[addr] = numeric_value
print(f"Client : {data} ")
client_info[addr] = {'packets_sent': 0, 'packets_lost': 0, 'connection_time': time.time(),
'congestion_window': 1}
send_ack(server, addr, numeric_value)
elif addr in client_info:
congestion_window = client_info[addr]['congestion_window']
if congestion_window < max_window:
client_info[addr]['congestion_window'] += 1
else:
send_signfull_signal(server, addr)
if data.startswith("SIGNGET:"):
packet_id, payload = data.split(":", 1)[1].split(";", 1)
packet_id = int(packet_id)
client_info[addr]['packets_sent'] += 1
if packet_id == last_received_id[addr] + 1:
print(f"Received SIGNGET {packet_id} from {addr}: {payload}")
last_received_id[addr] = packet_id
message = get_files()
time.sleep(0.5)
if addr in unacked_packets and packet_id in unacked_packets[addr]:
del unacked_packets[addr][packet_id]
client_info[addr]['congestion_window'] -= 1
server.sendto(f"ACKGET:{packet_id};{message}".encode(), addr)
else:
print(f"Received out of order packet {packet_id} from {addr}: {payload}")
client_info[addr]['packets_lost'] += 1
send_lost_packet_signal(server, addr, last_received_id[addr] + 1)
elif data.startswith("SIGNECHO:"):
packet_id, timestamp = data.split(":", 1)[1].split(";", 1)
packet_id = int(packet_id)
client_info[addr]['packets_sent'] += 1
if packet_id == last_received_id[addr] + 1:
print(f"Received SIGNECHO {packet_id} from {addr}. Sending back the timestamp.")
last_received_id[addr] = packet_id
if addr in unacked_packets and packet_id in unacked_packets[addr]:
del unacked_packets[addr][packet_id]
client_info[addr]['congestion_window'] -= 1
server.sendto(f"ECHOREPLY:{packet_id};{timestamp}".encode(), addr)
else:
print(f"Received out of order packet {packet_id} from {addr}")
client_info[addr]['packets_lost'] += 1
send_lost_packet_signal(server, addr, last_received_id[addr] + 1)
elif data.startswith("SIGNSTAT:"):
packet_id = int(data.split(":", 1)[1])
client_info[addr]['packets_sent'] += 1
if packet_id == last_received_id[addr] + 1:
print(f"Received SIGNSTAT {packet_id} from {addr}. Sending server statistics.")
last_received_id[addr] = packet_id
stats = server_statistics(addr)
if addr in unacked_packets and packet_id in unacked_packets[addr]:
del unacked_packets[addr][packet_id]
client_info[addr]['congestion_window'] -= 1
server.sendto(f"STATREPLY:{packet_id};{stats}".encode(), addr)
else:
print(f"Received out of order packet {packet_id} from {addr}")
client_info[addr]['packets_lost'] += 1
send_lost_packet_signal(server, addr, last_received_id[addr] + 1)
elif data.startswith("SIGNEND:"):
packet_id, payload = data.split(":", 1)[1].split(";", 1)
packet_id = int(packet_id)
if packet_id == last_received_id[addr] + 1:
print(f"Received SIGNEND {packet_id} from {addr}. Closing connection.")
last_received_id[addr] = packet_id
send_ack(server, addr, packet_id)
remove_client(addr)
break
else:
print(f"Received out of order packet {packet_id} from {addr}")
client_info[addr]['packets_lost'] += 1
send_lost_packet_signal(server, addr, last_received_id[addr] + 1)
except:
pass
def main():
print("RUDP Server Running")
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind((Server_IP, 49152))
receive(server)
server.close()
if __name__ == "__main__":
main()