#!/usr/bin/env python3 """Minimal BEP-15 UDP tracker for the Phase 4 integration test.""" import socket import struct import sys peer_port = int(sys.argv[1]) connection_id = 0x0102030405060708 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(("127.0.0.1", 0)) print("PORT %d" % sock.getsockname()[1], flush=True) while True: packet, addr = sock.recvfrom(2048) if len(packet) >= 16 and packet[8:12] == b"\x00\x00\x00\x00": sock.sendto(struct.pack("!IIQ", 0, struct.unpack("!I", packet[12:16])[0], connection_id), addr) continue if len(packet) >= 98 and packet[8:12] == b"\x00\x00\x00\x01": txid = struct.unpack("!I", packet[12:16])[0] response = ( struct.pack("!IIIII", 1, txid, 1800, 0, 1) + socket.inet_aton("127.0.0.1") + struct.pack("!H", peer_port) ) sock.sendto(response, addr) print("REQUEST announce", flush=True)