-
Notifications
You must be signed in to change notification settings - Fork 4
/
legacy_server.py
79 lines (58 loc) · 2.25 KB
/
legacy_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import asyncio, struct, os
from packet import DataPacket
from utils import PacketUtils
trustid = os.getenv("TRUST_ID")
async def handle_hello(reader, writer):
addr = writer.get_extra_info('peername')
flag = await reader.read(1)
flag = struct.unpack("B", flag)[0]
if (flag == 0x00):
client_trustid_len = await reader.read(1)
client_trustid_len = struct.unpack("B", client_trustid_len)[0]
client_trustid = await reader.read(client_trustid_len)
client_trustid = PacketUtils.decrypt(trustid, client_trustid)
if (client_trustid != None):
client_trustid = client_trustid.decode()
if (client_trustid == trustid):
response = DataPacket(trustid, b'200').pack()
writer.write(response)
await writer.drain()
print(f"Connection Established from {addr!r}")
while True:
if await handle_data(reader, writer) == -1: break
else:
print("Invalid TRUST_ID, Closing Connection...")
response = DataPacket(trustid, b'403').pack()
writer.write(response)
await writer.drain()
else:
print("Malformed Hello Packet, Closing Connection...")
print(f"Connection to {addr!r} Closed")
writer.close()
await writer.wait_closed()
async def handle_data(reader, writer):
flag = await reader.read(1)
if not flag:
return -1
flag = struct.unpack("B", flag)[0]
if (flag == 0x01):
data_length = await reader.read(4)
data_length = struct.unpack("I", data_length)[0]
data_encrypt = await reader.read(data_length)
data = PacketUtils.decrypt(trustid, data_encrypt)
print(f"Received Data: {data!r}")
return 0
async def main():
print("SocketCat Server, version v0.0.1")
print("All rights not reserved.")
assert trustid, "TRUST_ID must be set and cannot be empty"
server = await asyncio.start_server(
handle_hello, '0.0.0.0', 20049)
addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets)
print(f'Serving on {addrs}')
async with server:
await server.serve_forever()
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nExiting Gracefully...")