Skip to content

Instantly share code, notes, and snippets.

@Tugzrida
Created August 4, 2024 02:20
Show Gist options
  • Save Tugzrida/944d7fd3768e84bf68e5fdbb67101d0b to your computer and use it in GitHub Desktop.
Save Tugzrida/944d7fd3768e84bf68e5fdbb67101d0b to your computer and use it in GitHub Desktop.
Basic UDP NAT rebinding simulator to test QUIC
#!/usr/bin/env python3
import socket, threading
# Basic UDP NAT rebinding simulator to test QUIC
# Won't work with more than one client
local_port = 443 # Local UDP port to listen on
local_src_port = 65123 # Initial source port
# Destination host to forward traffic to
remote_host = "192.0.2.1"
remote_port = 443
def forwardData():
ds = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ds.setblocking(False)
ds.bind(("0.0.0.0", local_port))
print(f"Listening on UDP port {local_port}...")
us = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
us.setblocking(False)
us.bind(("0.0.0.0", local_src_port))
while True:
if local_src_port != us.getsockname()[1]:
print("rebinding")
us = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
us.setblocking(False)
us.bind(("0.0.0.0", local_src_port))
try:
dsdata, dsaddr = ds.recvfrom(1500)
print(f"Uploading {len(dsdata)} bytes from {dsaddr}")
us.sendto(dsdata, (remote_host, remote_port))
except BlockingIOError:
pass
try:
usdata, usaddr = us.recvfrom(1500)
print(f"Dnloading {len(usdata)} bytes from {usaddr}")
ds.sendto(usdata, dsaddr)
except BlockingIOError:
pass
forwarder = threading.Thread(target=forwardData, daemon=True)
forwarder.start()
while True:
print(f"Sourcing from port {local_src_port}, Enter to incrememt")
input()
local_src_port += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment