-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_uploader.py
238 lines (178 loc) · 7.26 KB
/
random_uploader.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
from sockets_lib.tcp_connection import TCPClientConnection, TCPServerConnection, TCPServer
from sockets_lib.udp_connection import UDPPublisher
from sockets_lib.connection import ConnectionPoller, ConnectionPollerThread, ConnectionIsClosedError
from threading import Thread
from functools import partial
from itertools import zip_longest
import time
import signal
import sys
import threading
import logging
import random
import json
if len(sys.argv) != 7:
help = """Usage: python3 {} listener-ip listener-port subnet local-ip port max-ip
listener-ip IP address of the connection state listener
listener-port Port used by the connection state listener
subnet First 3 numbers of IP address (e.g. 10.0.0)
local-ip Last number of IP address (e.g. 4)
port Port to listen on for incoming connections and to connect to
for 'uploads'
max-ip Highest value that any address on the subnet uses
For example, if this device had IP address 192.168.1.3, and
was connected to devices with address 192.168.1.1 - 192.168.1.10,
the usage would be
python3 {} ip port 192.168.1 3 port 10
"""
print(help.format(sys.argv[0], sys.argv[0]))
sys.exit(1)
listener_ip = sys.argv[1]
listener_port = int(sys.argv[2])
subnet = sys.argv[3]
local_ip = "{}.{}".format(subnet, sys.argv[4])
server_port = int(sys.argv[5])
max_ip = int(sys.argv[6])
root_log = logging.getLogger("app")
logging.basicConfig(level=logging.INFO, format="%(message)s")
log = logging.getLogger("app.random-uploads")
log.info("Starting random upload bot on {}:{} talking to {}.1:{} - {}.{}:{}".format(local_ip, server_port, subnet, server_port, subnet, max_ip, server_port))
running = True
clients = []
def cleanup():
server.stop()
for client in clients:
client.stop()
client.join()
global running
running = False
def signal_handler(sig, frame):
cleanup()
time.sleep(1)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
class ConnectionNotifier:
"""
Class to notify pox listener when connections go up/down
Assumed port 6634
"""
def __init__(self):
self.__poller = ConnectionPoller()
self.__poll_thread = ConnectionPollerThread(self.__poller)
self.__poll_thread.start()
self.__connection = UDPPublisher(listener_ip, listener_port, self.__closed)
self.__poller.add_connection(self.__connection)
def __closed(self, socket):
self.__connection = None
self.__poll_thread.stop()
def stop(self):
self.__poller.close_all_connections()
self.__poll_thread.join()
def send_start_connection(self, target_ip):
msg = {"src":local_ip, "dest":target_ip, "state":"open"}
self.__connection.send(json.dumps(msg).encode())
def send_stop_connection(self, target_ip):
msg = {"src":local_ip, "dest":target_ip, "state":"close"}
self.__connection.send(json.dumps(msg).encode())
class Server:
def __init__(self, addr="localhost", port=9000):
log.info("Starting server...")
self.__server = TCPServer(addr, port, self.__got_connection)
self.__connections = []
log.debug("Starting server poller...")
self.__poller = ConnectionPoller()
self.__poll_thread = ConnectionPollerThread(self.__poller)
self.__poll_thread.start()
def __got_connection(self, server_socket, address):
log.info("New connection from" + str(address))
conn_if = TCPServerConnection(
server_socket, address, None, self.__connection_closed
)
self.__poller.add_connection(conn_if)
def __connection_closed(self, connection):
log.info("Server connection closed:" + str(connection.address()))
self.__poller.remove_connection(connection)
def start(self):
self.__server.start()
def stop(self):
log.info("Stopping server")
self.__server.stop()
self.__server.join()
log.debug("Closing connections")
self.__poller.close_all_connections()
time.sleep(1)
log.debug("Stopping poller")
self.__poll_thread.stop()
self.__poll_thread.join()
log.debug("Stopped")
class Client(Thread):
def __init__(self, addr="localhost", port=9000, notifier=None):
super().__init__()
self.__random = random.Random()
self.__addr = addr
self.__port = port
self.__done = False
self.__notifier = connection_notifier
global clients
clients.append(self)
def __closed(self, connection):
self.__poll_thread.stop()
self.__done = True
log.info("Client side closed; {} clients still open".format(len(clients)))
def run(self):
self.__shutdown = False
log.info("Starting upload to {}:{}...".format(self.__addr, self.__port))
try:
self.__poller = ConnectionPoller()
self.__poll_thread = ConnectionPollerThread(self.__poller)
self.__connection = TCPClientConnection(self.__addr, self.__port, None, self.__closed)
self.__poller.add_connection(self.__connection)
self.__poll_thread.start()
if self.__notifier:
self.__notifier.send_start_connection(self.__addr)
time.sleep(1)
total_bytes = 0;
num_writes = self.__random.randint(50, 200)
for i in range(num_writes):
if self.__shutdown:
break
msg_len = self.__random.randint(500, 1000)
total_bytes = total_bytes + msg_len
data = "".join([str(self.__random.random()) for _ in range(msg_len)])
log.debug("Queuing message to send")
try:
self.__connection.send(data.encode())
except ConnectionIsClosedError:
log.warning("Server closed before client finished")
break
time.sleep(self.__random.uniform(0, 0.25))
if self.__notifier:
self.__notifier.send_stop_connection(self.__addr)
time.sleep(1)
log.info("Uploaded {} bytes to {}:{}".format(total_bytes, self.__addr, self.__port))
log.debug("Closing client")
self.__poller.close_all_connections()
except ConnectionRefusedError:
log.warning("Unable to connect to {}:{}".format(self.__addr, self.__port))
self.__shutdown = True
self.__done = True
def stop(self):
log.info("Stopping client")
self.__shutdown = True
self.__poll_thread.join()
def done(self):
return self.__done
server = Server(local_ip, server_port)
server.start()
connection_notifier = ConnectionNotifier()
while running:
time.sleep(random.randint(5, 10))
for client in clients:
if client.done():
client.stop()
client.join()
clients.remove(client)
if len(clients) < 10:
c = Client("{}.{}".format(subnet, random.randint(1, max_ip)), server_port, connection_notifier)
c.start()
cleanup()