#!/usr/bin/env python3 import os.path import socket from signal import signal, SIGINT from sys import exit BUF_SIZE = 1024 DEST_SRV = ('45.141.57.154', 8303) # fokko DEST_SRV = ('149.202.127.134', 8303) DEST_SRV = ('127.0.0.1', 8899) # ddnet ++ # DEST_SRV = ('78.135.85.66', 8322) # ddnet turkey muli _ENV = {} def senf(key): global _ENV if not key in _ENV: return None return _ENV[key] if os.path.isfile('.env'): with open('.env') as f: for l in f: l = l.rstrip() cfg, val = l.split('=', 1) _ENV[cfg] = val if cfg == 'BIND_PORT': _ENV[cfg] = int(val) print(f"cfg: {cfg}") print(f"val: {val}") BIND_ADDR = (senf('BIND_IP') or '127.0.0.1', senf('BIND_PORT') or 8303) print(f"hello world listentin on {BIND_ADDR}") sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(BIND_ADDR) SRC_CL = None NET_CTRLMSG_KEEPALIVE = 0x00 NET_CTRLMSG_CLOSE = 0x04 tken_token = None def disconnect_pck(token): return b'\x10\x10\x00\x04' + token def handler(signal_received, frame): global sock global tken_token print('SIGINT or CTRL-C detected. Exiting gracefully') if tken_token: print(f"sending disconnect to {DEST_SRV}") sock.sendto(disconnect_pck(tken_token), DEST_SRV) exit(0) signal(SIGINT, handler) while True: data, addr = sock.recvfrom(BUF_SIZE) target = DEST_SRV if addr == DEST_SRV: target = SRC_CL elif SRC_CL == None: print(f"client connected to proxy {addr}") SRC_CL = addr print(f"got data from {addr} forward to {target}") print(data) if len(data) > 3: # ddnet TKEN # b'\x10\x00\x00\x02TKEN\x14S\x17C' if len(data) == 12: if data[0:8] == b'\x10\x00\x00\x02TKEN': tken_token = data[8:] print("yoinked token:") print(tken_token) # control message if data[2] == 0: # disconnect packet # b'\x10\x10\x00\x04\x9a\xcb9\xc9' if data[3] == NET_CTRLMSG_CLOSE: print("do not send client disconnect") SRC_CL = None continue # b'\x00\x10\x02C\x07\x0c"\x00AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\x00\x01\x00!\xa0\x04\xa3\x04(\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\xcf\x92\xfbh' if target: sock.sendto(data, target) else: print("wanring failed to send no target found")