-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
184 lines (144 loc) · 5.39 KB
/
proxy.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
import string
import socket
import random
import copy
import time
from Header import RDTHeader
import threading
import concurrent.futures
lock = threading.Lock()
buffer_lock = threading.Lock()
connection_pool = {}
test_buffer_pool = {}
case_test_status_pool = {}
num_test = 16
def randSleep(min, max):
delay = random.randint(min, max) * 0.001
time.sleep(delay)
def case_test(pkt, outSock: socket.socket):
global connection_pool
global test_buffer_pool
global case_test_status_pool
global num_test
try:
header = RDTHeader().from_bytes(pkt)
addr = header.tgt
test_case = header.test_case
lock.acquire()
if f"{header.src}-{header.tgt}" not in connection_pool.keys() and f"{header.tgt}-{header.src}" not in connection_pool.keys():
connection_pool[f"{header.src}-{header.tgt}"] = []
test_buffer_pool[f"{header.src}-{header.tgt}"] = []
case_test_status_pool[f"{header.src}-{header.tgt}"] = []
for i in range(0, num_test):
case_test_status_pool[f"{header.src}-{header.tgt}"].append(False)
if header.test_case != 20:
connection_key = f"{header.src}-{header.tgt}" if f"{header.src}-{header.tgt}" in connection_pool.keys() else f"{header.tgt}-{header.src}"
connection_pool[connection_key].append(header)
else:
outSock.sendto(pkt, addr)
lock.release()
if test_case == 0:
if len(connection_pool[connection_key]) == 1:
outSock.sendto(pkt, addr)
else:
outSock.sendto(pkt, addr)
lock.acquire()
case_test_status_pool[connection_key][test_case] = True
lock.release()
if test_case == 5:
bottle_neck = 5
if len(connection_pool[connection_key]) == 1:
lock.acquire()
case_test_status_pool[connection_key][test_case] = True
lock.release()
if len(test_buffer_pool[connection_key]) > bottle_neck:
pass
else:
buffer_lock.acquire()
test_buffer_pool[connection_key].append(header)
buffer_lock.release()
outSock.sendto(pkt, addr)
randSleep(min=5, max=10)
buffer_lock.acquire()
test_buffer_pool[connection_key].remove(header)
buffer_lock.release()
except Exception as e:
print(e)
lock.release()
buffer_lock.release()
finally:
lock.release()
buffer_lock.release()
def listener(ReceiveSock: socket.socket, outSock: socket.socket):
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
while True:
try:
print("Try to receive data...")
pkt = ReceiveSock.recv(1024)
executor.submit(case_test, pkt, outSock)
except socket.error as e:
print(e)
lock.release()
buffer_lock.release()
def clean_connection():
server_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_sock.bind(("127.0.0.1", 12234))
while True:
try:
data = server_sock.recv(1024)
data = data.decode()
lock.acquire()
try:
if data in connection_pool.keys():
del connection_pool[data]
except Exception as e:
print(e)
try:
if data in test_buffer_pool.keys():
del test_buffer_pool[data]
except Exception as e:
print(e)
try:
if data in case_test_status_pool.keys():
del case_test_status_pool[data]
except Exception as e:
print(e)
lock.release()
print(f"[PROXY] Delete connection :{data}")
except Exception as e:
lock.release()
print(e)
finally:
pass
def result():
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_sock.bind(("127.0.0.1", 12230))
server_sock.listen(5)
while True:
client_sock, client_addr = server_sock.accept()
try:
data = client_sock.recv(1024)
key = data.decode().split(":")
response = case_test_status_pool[key[0]][int(key[1])]
client_sock.sendall(str(response).encode())
except Exception as e:
print(e)
finally:
client_sock.close()
if __name__ == '__main__':
fromSenderAddr = ('127.0.0.1', 12345)
toReceiverAddr = ('127.0.0.1', 12346)
fromReceiverAddr = ('127.0.0.1', 12347)
toSenderAddr = ('127.0.0.1', 12348)
fromSenderSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fromSenderSock.bind(fromSenderAddr)
fromReceiverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
fromReceiverSock.bind(fromReceiverAddr)
outSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("Listening...")
threading.Thread(target=listener, daemon=True, args=(fromSenderSock, outSock)).start()
threading.Thread(target=listener, daemon=True, args=(fromReceiverSock, outSock)).start()
threading.Thread(target=clean_connection, daemon=True).start()
threading.Thread(target=result, daemon=True).start()
while True:
pass