-
Notifications
You must be signed in to change notification settings - Fork 0
/
.null-ls_459787_async-client.py
91 lines (64 loc) · 2.33 KB
/
.null-ls_459787_async-client.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
# async-client.py
import asyncio
import signal
import socket
# This is the client's address, input the drain's IP below
RHOST = '127.0.0.1'
RPORT = 20001
actual_port = None
stop_event = asyncio.Event()
class UDPClientProtocol(asyncio.DatagramProtocol):
def __init__(self):
super().__init__()
def connection_made(self, transport):
self.transport = transport
print('Sending:')
self.transport.sendto(b'Hello Server')
def datagram_received(self, data, addr):
if data.decode() == "discovery":
print(f"Discovered server at {addr}")
else:
print(f"Received: {data.decode()} from {addr}")
def connection_lost(self, _):
print("Server closed, stop event loop")
loop = asyncio.get_event_loop()
loop.stop()
class GetPortProtocol(asyncio.DatagramProtocol):
def __init__(self, loop):
self.loop = loop
def connection_made(self, transport):
self.transport = transport
self.request_port()
def request_port(self):
print("Requesting port from server...")
self.transport.sendto(b'get_port')
def datagram_received(self, data: bytes, addr: tuple) -> None:
global actual_port
actual_port = int(data.decode())
print(f"Received actual port: {actual_port}")
self.loop.create_task(self.switch_to_actual_port(actual_port))
async def switch_to_actual_port(self, port):
self.loop.create
def test_port_number(host, port):
with socket(socket.AF_INET, socket.SOCK_DGRAM)as sock:
sock.timeout(3)
try:
sock.connect((host, port))
return True
except (socket.timeout, socket.error, OSError):
return False
async def handle_exit(sig):
print(f"Received exit signal {sig.name}...\nExiting...")
stop_event.set()
async def main():
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGTERM, lambda: asyncio.create_task(handle_exit(signal.SIGTERM)))
loop.add_signal_handler(signal.SIGINT, lambda: asyncio.create_task(handle_exit(signal.SIGINT)))
transport, protocol = await loop.create_datagram_endpoint(
UDPClientProtocol,
remote_addr=(RHOST, RPORT)
)
sentinel = asyncio.create_task(stop_event.wait())
await sentinel
if __name__ == "__main__":
asyncio.run(main())