Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

fix(linux): UDP session eviction freed a session a worker was still setting up (heap corruption under load); raise the open file limit; UDP sender check no longer drops legitimate datagrams; full end-to-end suite

anof-cyberf3efa8584a57
diff --git a/Linux/README.md b/Linux/README.md
index 58b7b83..3f35954 100644
--- a/Linux/README.md
+++ b/Linux/README.md
@@ -240,7 +240,7 @@ Add as many proxies as you need (GUI or profile); each rule chooses its own. Pro
## Security model
- **Redirected TCP connections** are bound to the client's source address and port. A process that connects to the relay from a different loopback address (for example `127.0.0.2`) cannot claim another process's pending flow, and an entry is consumed on first use.
-- **Redirected UDP flows** are addressed by a loopback flow address that the relay maps back to the original destination and to the process that opened it. These addresses are not secret (they are visible in `/proc/net/udp`), so the relay checks, for every new client port, that the sender really is that process (it owns a UDP socket on that port and is still the same executable); datagrams from anyone else are dropped. A process that opens UDP sockets at a high rate displaces only its own oldest sessions, never other programs'.
+- **Redirected UDP flows** are addressed by a loopback flow address that the relay maps back to the original destination and to the process that opened it. These addresses are not secret (they are visible in `/proc/net/udp`), so the relay checks, for every new client port, that the sender really is that process (it owns a UDP socket on that port and is still the same executable); datagrams from anyone else are dropped. A datagram from a process that has already exited cannot be verified any more and is delivered (a program that sends one datagram and quits must still get it through). One process can hold at most 2048 of the 4096 UDP sessions; past that it displaces only its own oldest session, never another program's.
- **Decisions made in user space** (long host lists, domain rules) use the executable the kernel recorded when the connection was opened, not `/proc/<pid>/exe` looked up later, so a process that exits and has its PID reused cannot change a pending decision.
- **The DNS tap** only learns from answer records that belong to the question (the asked name, CNAMEs chained from it, and A records owned by those names); unrelated records in a response are ignored. Note that any local process whose DNS passes through the relay can still teach the engine a name→address mapping for *its own* lookups, and domain rules are matched by address, so on a shared host another user's resolver answers can influence which addresses a domain rule matches. Run ProxyBridge on machines you trust locally.
- **A local proxy is exempted** (its own connections go direct) when a process listens on the configured proxy address and port. While that port is free, for example because the proxy is not running yet, any local process that binds it is treated as the proxy. Point rules at proxies that are running, or at a proxy on another host, when other users share the machine.
@@ -275,6 +275,8 @@ Add as many proxies as you need (GUI or profile); each rule chooses its own. Pro
## Things to Note
+- **UDP sessions outlive the socket.** A proxied UDP session (and the TCP control connection it holds to the proxy) stays open until it has been idle for 60 seconds, because the engine cannot see an application close its socket. A program that opens a new socket for every lookup (most resolvers) therefore keeps one proxy connection per lookup for a minute; a proxy that limits connections per client (Nexus Proxy: 500) refuses the rest, and ProxyBridge then drops that flow's datagrams for 5 seconds before it tries again.
+
- **Stalled proxies do not hold everything up.** Connections that need a handshake with their proxy are served by a pool of workers that starts with 8 and grows to 64 while handshakes queue up (extra workers leave after 20 idle seconds). No single proxy may occupy more than 48 workers at once, so a proxy that accepts connections and then never answers (up to the 10 s handshake timeout each) cannot delay connections that use another proxy.
- **DNS Traffic Handling**: by default DNS (port 53) follows your rules like any other traffic; a profile with `"DnsViaProxy": false` makes DNS always use a direct connection regardless of rules.
@@ -438,6 +440,8 @@ gcc -o ProxyBridge main.o \
-lproxybridge -lpthread
```
+**Full end-to-end suite (`tests/e2e_full.sh`).** Runs the installed or built CLI with real `.pbprofile` files in front of two Nexus instances and compares **engine off with engine on**: the same Python client (TCP, UDP, real DNS packets over UDP and TCP, QUIC Version Negotiation datagrams, load) must get identical results either way, and the proxy logs must show the traffic went through the proxy, or did not for DIRECT and BLOCK rules. Phases: baseline, all proxied, TCP-only, UDP-only, all direct, UDP blocked, DNS via proxy on vs off (host name vs address sent to the proxy), load (500 TCP connections, 1000 UDP sockets, 700 one-shot resolver sockets, datagram rate ramp), **real HTTP/3** with a curl that supports it (QUIC over SOCKS5 UDP ASSOCIATE, skipped when offline), and failure handling (wrong password, dead proxy, restarted proxy). It prints throughput, latency and loss for both modes. `sudo NODE=... NEXUS_JS=... PB_BIN=/usr/local/bin/ProxyBridge tests/e2e_full.sh`; `PHASES="2 8"`, `CLIENT_SCENARIOS`, `KEEP_PROFILES`, `KEEP_LOGS` select and keep things (see the script header). It needs root, node, python3 and the Nexus script; it adds one line to `/etc/hosts` for the DNS-via-proxy phase and removes it on exit.
+
**Testing against Nexus Proxy.** [Nexus Proxy](https://github.com/InterceptSuite/nexus-proxy) is a Node.js SOCKS5 server that really forwards TCP and UDP. `tests/e2e_nexus.sh` runs ProxyBridge in front of two Nexus instances (one with a username/password) with real origin servers and a real DNS server, and checks rules by IP, port, port range, process, CIDR and domain, TCP and UDP, 200 MB / 100 MB checksummed transfers, 100 parallel and 1,500 sequential connections, 300 concurrent UDP sockets, 59 KB datagrams, a sustained UDP stream, and recovery after the proxy restarts. Stock Nexus cannot do UDP ASSOCIATE on Linux (its IPv6 relay socket binds `0.0.0.0`, IPv4 destinations are not sent as `::ffff:` addresses, and mapped replies are not matched); `tests/nexus-linux-udp.patch` fixes that, and without it the UDP checks are skipped. Nexus limits each client IP to 500 connections, so it cannot be used for the 10,000-connection scale test.
**Config location:** the GUI stores profiles, proxies and rules in `/etc/proxybridge/config.ini` (mode 0600). Set `PROXYBRIDGE_CONFIG_DIR` to use another directory (used by the tests).
diff --git a/Linux/gui/gui_boot.c b/Linux/gui/gui_boot.c
index eab4157..77fa917 100644
--- a/Linux/gui/gui_boot.c
+++ b/Linux/gui/gui_boot.c
@@ -110,7 +110,7 @@ char *boot_unit_text(const char *cli, const char *profile, char **error) {
quote_word(s, profile);
g_string_append(s, "\nRestart=on-failure\nRestartSec=5\n"
// the engine needs root for eBPF and /proc, nothing else: no new privileges, no writable system or home directories
- "NoNewPrivileges=yes\nProtectSystem=strict\nProtectHome=read-only\nPrivateTmp=yes\nProtectKernelModules=yes\n"
+ "LimitNOFILE=1048576\nNoNewPrivileges=yes\nProtectSystem=strict\nProtectHome=read-only\nPrivateTmp=yes\nProtectKernelModules=yes\n"
"ProtectKernelLogs=yes\nProtectClock=yes\nRestrictSUIDSGID=yes\nLockPersonality=yes\nRestrictRealtime=yes\n"
"\n[Install]\nWantedBy=multi-user.target\n");
return g_string_free(s, FALSE);
diff --git a/Linux/gui/tests/boot_test.c b/Linux/gui/tests/boot_test.c
index 45e6d82..5c27af6 100644
--- a/Linux/gui/tests/boot_test.c
+++ b/Linux/gui/tests/boot_test.c
@@ -37,7 +37,7 @@ int main(void)
CHECK(t == NULL && err && strstr(err, "absolute"));
g_free(err); err = NULL;
t = boot_unit_text("/usr/local/bin/ProxyBridge", "/etc/proxybridge/profiles/Work.pbprofile", &err);
- CHECK(t && strstr(t, "NoNewPrivileges=yes") && strstr(t, "ProtectSystem=strict") && strstr(t, "ProtectHome=read-only"));
+ CHECK(t && strstr(t, "LimitNOFILE=1048576") && strstr(t, "NoNewPrivileges=yes") && strstr(t, "ProtectSystem=strict") && strstr(t, "ProtectHome=read-only"));
g_free(t);
// only root-owned, root-writable-only paths may be started at boot
diff --git a/Linux/src/ProxyBridge.c b/Linux/src/ProxyBridge.c
index a6c5222..25f31a1 100644
--- a/Linux/src/ProxyBridge.c
+++ b/Linux/src/ProxyBridge.c
@@ -446,14 +446,25 @@ static pthread_mutex_t loop_mu = PTHREAD_MUTEX_INITIALIZER;
int pb_core_udp_sender_ok(const struct pb_orig *o, uint16_t client_port)
{
- if (!pb_proc_pid_owns_udp(o->pid, client_port)) return 0;
- if (!o->exe_ino) return 1; // kernel-side identity is only recorded for DECIDE flows
- char link[64];
- struct stat st;
- snprintf(link, sizeof link, "/proc/%u/exe", o->pid);
- if (stat(link, &st) != 0) return 0;
- uint32_t dev = (major(st.st_dev) << 20) | minor(st.st_dev); // same encoding as the BPF program's exe2class key
- return dev == o->exe_dev && st.st_ino == o->exe_ino;
+ /* The owner's fd table is read through /proc while the owner may be opening and closing sockets in other threads: a directory
+ * listing taken during such changes can miss an entry. A miss is therefore repeated before a datagram is refused. */
+ for (int attempt = 0; attempt < 4; attempt++) {
+ if (pb_proc_pid_owns_udp(o->pid, client_port)) {
+ if (!o->exe_ino) return 1; /* kernel-side identity is only recorded for DECIDE flows */
+ char link[64];
+ struct stat st;
+ snprintf(link, sizeof link, "/proc/%u/exe", o->pid);
+ if (stat(link, &st) != 0) return 1; /* exited in the meantime: it did own the socket a moment ago */
+ uint32_t dev = (major(st.st_dev) << 20) | minor(st.st_dev); /* same encoding as the BPF program's exe2class key */
+ return dev == o->exe_dev && st.st_ino == o->exe_ino;
+ }
+ char dir[32];
+ snprintf(dir, sizeof dir, "/proc/%u", o->pid);
+ if (access(dir, F_OK) != 0) return 1; /* the owner has exited: a program that sends one datagram and
+ quits cannot be verified afterwards, and its datagram must
+ still arrive */
+ }
+ return 0;
}
int pb_core_is_proxy_process(uint32_t pid, uint32_t proxy_id)
diff --git a/Linux/src/pb_relay.c b/Linux/src/pb_relay.c
index 649a050..6dddecc 100644
--- a/Linux/src/pb_relay.c
+++ b/Linux/src/pb_relay.c
@@ -12,6 +12,7 @@
#include <poll.h>
#include <pthread.h>
#include <signal.h>
+#include <sys/resource.h>
#include <stdarg.h>
#include <stdatomic.h>
#include <stdio.h>
@@ -35,7 +36,7 @@
#define MAX_DATA 8
#define PIPE_SZ (1 << 20)
#define JOBQ_MAX 4096 // handshake queue; excess accepts are shed with a log
-#define MAX_UDP_SESS 1024
+#define MAX_UDP_SESS 4096
#define MAX_FLOWS 64
#define UDP_QMAX 32
#define UDP_QBYTES (256 * 1024)
@@ -1302,15 +1303,23 @@ static void sess_free_grave(void)
// One process gets at most half of the session table: past that, its own least recently used session makes room, so a
// program that opens UDP sockets at a high rate only ever displaces itself and cannot starve every other one of UDP.
+// A session whose setup is still running on a worker thread is never the one to go (the worker still writes into it): when
+// every session of the process is in that state, the new one is refused instead.
#define MAX_UDP_SESS_PER_PID (MAX_UDP_SESS / 2)
static struct sess *sess_new(const struct sockaddr_in *cli, uint32_t id, uint32_t flow, int kind, uint32_t pid)
{
int mine = 0;
struct sess *oldest = NULL;
- for (struct sess *t = R.sess_head; t; t = t->next)
- if (t->pid == pid) { mine++; if (!oldest || t->last < oldest->last) oldest = t; }
- if (mine >= MAX_UDP_SESS_PER_PID && oldest) sess_destroy(oldest);
+ for (struct sess *t = R.sess_head; t; t = t->next) {
+ if (t->pid != pid) continue;
+ mine++;
+ if (t->state != ST_SETUP && (!oldest || t->last < oldest->last)) oldest = t;
+ }
+ if (mine >= MAX_UDP_SESS_PER_PID) {
+ if (!oldest) return NULL;
+ sess_destroy(oldest);
+ }
if (R.nsess >= MAX_UDP_SESS) return NULL;
struct sess *s = calloc(1, sizeof *s);
if (!s) return NULL;
@@ -1752,6 +1761,19 @@ static int add_ep(int ep, int fd, void *tag, uint32_t ev)
return epoll_ctl(ep, EPOLL_CTL_ADD, fd, &e);
}
+// Every relayed TCP connection holds two descriptors and every UDP session two more, while the usual soft limit is 1024 (it is
+// also the default of a systemd service): without this the relay would run out at about 500 connections. The hard limit is the
+// ceiling the administrator chose; the soft limit may always be raised up to it.
+static void raise_fd_limit(void)
+{
+ struct rlimit rl;
+ if (getrlimit(RLIMIT_NOFILE, &rl) != 0) return;
+ rlim_t want = rl.rlim_max == RLIM_INFINITY || rl.rlim_max > 1048576 ? 1048576 : rl.rlim_max;
+ if (rl.rlim_cur >= want) return;
+ rl.rlim_cur = want;
+ if (setrlimit(RLIMIT_NOFILE, &rl) != 0) pb_core_log("relay: could not raise the open file limit: %s", strerror(errno));
+}
+
int pb_relay_start(const struct pb_relay_cfg *cfg)
{
pthread_mutex_lock(&R.life);
@@ -1764,6 +1786,7 @@ int pb_relay_start(const struct pb_relay_cfg *cfg)
if (c.tcp_idle_timeout_s <= 0) c.tcp_idle_timeout_s = 300;
if (c.udp_idle_timeout_s <= 0) c.udp_idle_timeout_s = 60;
if (c.handshake_timeout_s <= 0) c.handshake_timeout_s = 10;
+ raise_fd_limit();
R.cfg = c;
atomic_store(&R.stopping, false);
atomic_store(&R.nconn, 0);
diff --git a/Linux/src/tests/proc_test.c b/Linux/src/tests/proc_test.c
index 11e75fe..2434238 100644
--- a/Linux/src/tests/proc_test.c
+++ b/Linux/src/tests/proc_test.c
@@ -2,6 +2,7 @@
#include "../pb_proc.h"
#include <arpa/inet.h>
#include <ifaddrs.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -90,6 +91,55 @@ int main(void)
}
CHECK(found > 0 && ok == found); // every configured address is local
+ // which process owns a UDP socket bound to a port (the relay uses it to tell a flow's own datagrams from an impostor's)
+ {
+ int u = socket(AF_INET, SOCK_DGRAM, 0);
+ struct sockaddr_in ua = { .sin_family = AF_INET };
+ inet_pton(AF_INET, "127.0.0.1", &ua.sin_addr);
+ bind(u, (struct sockaddr *)&ua, sizeof ua);
+ socklen_t ul = sizeof ua;
+ getsockname(u, (struct sockaddr *)&ua, &ul);
+ uint16_t up = ntohs(ua.sin_port);
+ CHECK(pb_proc_pid_owns_udp((uint32_t)getpid(), up)); // our own socket
+ CHECK(!pb_proc_pid_owns_udp((uint32_t)getppid(), up)); // not the parent's
+ CHECK(!pb_proc_pid_owns_udp((uint32_t)getpid(), (uint16_t)(up == 65535 ? 1 : up + 1))); // a port nobody here owns
+ CHECK(!pb_proc_pid_owns_udp(4194000u, up)); // a process that does not exist
+ int u6 = socket(AF_INET6, SOCK_DGRAM, 0);
+ if (u6 >= 0) {
+ struct sockaddr_in6 a6 = { .sin6_family = AF_INET6, .sin6_addr = IN6ADDR_LOOPBACK_INIT };
+ bind(u6, (struct sockaddr *)&a6, sizeof a6);
+ socklen_t l6 = sizeof a6;
+ getsockname(u6, (struct sockaddr *)&a6, &l6);
+ CHECK(pb_proc_pid_owns_udp((uint32_t)getpid(), ntohs(a6.sin6_port))); // IPv6 sockets are found too
+ close(u6);
+ }
+ int pp[2];
+ pipe(pp);
+ pid_t kid2 = fork();
+ if (kid2 == 0) { // the child owns a socket, we do not
+ int k = socket(AF_INET, SOCK_DGRAM, 0);
+ struct sockaddr_in ka = { .sin_family = AF_INET };
+ inet_pton(AF_INET, "127.0.0.1", &ka.sin_addr);
+ bind(k, (struct sockaddr *)&ka, sizeof ka);
+ socklen_t kl = sizeof ka;
+ getsockname(k, (struct sockaddr *)&ka, &kl);
+ uint16_t kp = ntohs(ka.sin_port);
+ write(pp[1], &kp, sizeof kp);
+ pause();
+ _exit(0);
+ }
+ uint16_t kport = 0;
+ CHECK(read(pp[0], &kport, sizeof kport) == (ssize_t)sizeof kport && kport != 0);
+ CHECK(pb_proc_pid_owns_udp((uint32_t)kid2, kport)); // the child owns it ...
+ CHECK(!pb_proc_pid_owns_udp((uint32_t)getpid(), kport)); // ... and we do not
+ kill(kid2, SIGKILL);
+ waitpid(kid2, NULL, 0);
+ CHECK(!pb_proc_pid_owns_udp((uint32_t)kid2, kport)); // gone with the process
+ close(pp[0]);
+ close(pp[1]);
+ close(u);
+ }
+
printf("proc_test: %d checks, %d failed\n", checks, fails);
return fails != 0;
}
diff --git a/Linux/src/tests/relay_test.c b/Linux/src/tests/relay_test.c
index c6f4435..82f6064 100644
--- a/Linux/src/tests/relay_test.c
+++ b/Linux/src/tests/relay_test.c
@@ -1307,6 +1307,35 @@ static void test_handshake_timeout(void)
close(fd);
}
+// More concurrent UDP sockets of one process than its share of the session table. Sessions are replaced oldest first, but never
+// one that a worker thread is still setting up (that was a use-after-free found by the full end-to-end suite). Under ASan this
+// test fails on the old code; in every build the relay must survive and keep serving.
+static void test_udp_session_cap(void)
+{
+ set_flow("127.1.2.3", "127.0.0.1", g_uecho1, PB_ACT_PROXY, 1);
+ enum { N = 2400 }; // more than the per-process share (half of 4096)
+ int fds[N];
+ for (int i = 0; i < N; i++) {
+ fds[i] = udp_client();
+ char m[16];
+ snprintf(m, sizeof m, "cap%d", i);
+ udp_send_flow(fds[i], "127.1.2.3", m);
+ }
+ msleep(2500); // let the associations finish and the answers arrive
+ int answered = 0;
+ for (int i = 0; i < N; i++) {
+ char b[64], ip[32];
+ uint16_t sp;
+ if (udp_recv_to(fds[i], b, sizeof b, ip, &sp, 20) > 0) answered++; // sockets whose session was replaced get nothing
+ close(fds[i]);
+ }
+ CHECK(answered >= 1000, "UDP: %d sockets past the per-process session share, the relay survives and answers (%d answered)", (int)N, answered);
+ msleep(500);
+ int c = udp_client();
+ CHECK(udp_echo_ok(c, "127.1.2.3", "after-cap"), "UDP still works after the session table was full");
+ close(c);
+}
+
static void test_udp(void)
{
char b[256], ip[32];
@@ -1968,6 +1997,7 @@ int main(void)
RUN(test_tcp_halfclose);
RUN(test_sigpipe);
RUN(test_udp);
+ RUN(test_udp_session_cap);
RUN(test_flows);
RUN(test_proxy_process_loop);
RUN(test_proxy_helper);
diff --git a/Linux/tests/e2e_full.sh b/Linux/tests/e2e_full.sh
new file mode 100755
index 0000000..90ea51c
--- /dev/null
+++ b/Linux/tests/e2e_full.sh
@@ -0,0 +1,302 @@
+#!/bin/bash
+# Full end-to-end suite: the installed (or built) ProxyBridge CLI, driven by real .pbprofile files, against REAL forwarding SOCKS5
+# servers (Nexus Proxy, one open and one with username/password) and real origin servers written in Python.
+#
+# The same Python client (tests/full_client.py) runs with the engine OFF (the baseline), with every kind of profile ON, and the
+# suite compares the results: the application must not be able to tell the difference, and the proxy logs must show that the traffic
+# really went through it (or really did not, for DIRECT/BLOCK rules).
+#
+# phase 1 engine OFF baseline for TCP, UDP, DNS (real packets), QUIC-shaped UDP; the proxies must see nothing
+# phase 2 all traffic proxied same client, same results, proxies must carry every flow; throughput/loss compared with the baseline
+# phase 3 TCP proxied, UDP direct UDP never reaches the proxies
+# phase 4 UDP proxied, TCP direct TCP never reaches the proxies
+# phase 5 everything DIRECT engine on, nothing proxied
+# phase 6 UDP blocked, TCP proxied no UDP answer may arrive
+# phase 7 DNS via proxy on vs off the proxy is asked for the host name or for the address
+# phase 8 load 500 TCP connections, 1000 UDP sockets, 20000-datagram bursts
+# phase 9 HTTP/3 (real QUIC) with curl direct first, then through the proxy over UDP ASSOCIATE (needs internet + curl --http3)
+# phase 10 failure handling wrong password and a dead proxy fail closed; a restarted proxy is used again
+#
+# sudo NODE=/path/to/node NEXUS_JS=/path/to/nexus/script.js tests/e2e_full.sh
+# PB_BIN=/usr/local/bin/ProxyBridge test the installed program (default: $PB_DIR/ProxyBridge, PB_DIR defaults to Linux/output)
+# H3_CURL=/snap/bin/curl curl with HTTP/3 support (default: any curl whose --version lists HTTP3)
+# KEEP_LOGS=dir copy the ProxyBridge, proxy and client output there when something fails
+# DL_MB=100 UL_MB=50 STREAM_N=6000 sizes PHASES="1 2" only some phases CLIENT_SCENARIOS="dns" phases 1-5 run only these
+# Stock Nexus cannot do UDP ASSOCIATE on Linux and resets connections beyond 500 per source address: this script works on a
+# private copy with tests/nexus-linux-udp.patch applied and a higher limit (see the comment where the copy is made).
+set -u
+HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; ROOT="$(dirname "$HERE")"
+PB_DIR="${PB_DIR:-$ROOT/output}"; PB_BIN="${PB_BIN:-$PB_DIR/ProxyBridge}"; PBL="$HERE/pbl"
+NODE="${NODE:-node}"; NEXUS_JS="${NEXUS_JS:-}"
+[ -x "$PB_BIN" ] || { echo "ProxyBridge not found: $PB_BIN (build first, or set PB_BIN/PB_DIR)"; exit 1; }
+command -v "$NODE" >/dev/null 2>&1 && [ -f "$NEXUS_JS" ] || { echo "SKIP: set NODE and NEXUS_JS (see header)"; exit 0; }
+[ "$(id -u)" = 0 ] || { echo "run as root"; exit 1; }
+want() { [ -z "${PHASES:-}" ] || [[ " $PHASES " == *" $1 "* ]]; } # PHASES="1 2 8" runs only those phases
+SCEN="${CLIENT_SCENARIOS:-tcp udp dns quic}" # what phases 1-5 run
+export DL_MB="${DL_MB:-100}" UL_MB="${UL_MB:-50}" STREAM_N="${STREAM_N:-6000}"
+WORK="$(mktemp -d)"; PASS=0; FAIL=0; SKIP=0; PIDS=(); PB=""
+: > "$WORK/off.txt"; REF=0 # filled by phase 1 (only needed when phases are skipped)
+ok() { echo " PASS $1"; PASS=$((PASS+1)); }
+bad() { echo " FAIL $1"; FAIL=$((FAIL+1)); }
+skip() { echo " SKIP $1"; SKIP=$((SKIP+1)); }
+info() { echo " info $1"; }
+has() { [[ "$2" == *"$3"* ]] && ok "$1" || bad "$1 (got: ${2:0:200})"; }
+HOSTS_TAG="# pb-e2e-full"
+cleanup() { stop_pb; for p in "${PIDS[@]}"; do kill "$p" 2>/dev/null; done; wait 2>/dev/null
+ sed -i "/$HOSTS_TAG/d" /etc/hosts 2>/dev/null
+ for i in 1 2 3 4 5 6 53; do ip addr del 10.77.1.$i/32 dev lo 2>/dev/null; done; rm -rf "$WORK"; }
+trap cleanup EXIT
+for i in 1 2 3 4 5 6 53; do ip addr add 10.77.1.$i/32 dev lo 2>/dev/null; done
+# With "DNS via proxy" the proxy is given a host name and has to resolve it itself, with the resolver of this machine: the test
+# zone name needs an entry there (removed again on exit). Without it the proxy could not connect and the check would fail.
+echo "10.77.1.5 app.example.test $HOSTS_TAG" >> /etc/hosts
+
+# ---- the proxies -------------------------------------------------------------------------------------------------------------
+nexus_udp_ok() { # script port -> yes|no
+ "$NODE" "$1" -p "$2" >/dev/null 2>&1 & local p=$!; sleep 1.5
+ python3 - "$2" <<'PY'
+import socket, sys
+try:
+ c = socket.create_connection(("127.0.0.1", int(sys.argv[1])), timeout=3); c.sendall(b"\x05\x01\x00"); c.recv(2)
+ c.sendall(b"\x05\x03\x00\x01\x00\x00\x00\x00\x00\x00"); r = c.recv(64); print("yes" if r[1] == 0 else "no")
+except Exception: print("no")
+PY
+ kill $p 2>/dev/null; wait $p 2>/dev/null; }
+# A private copy of Nexus: the UDP patch (stock Nexus cannot do UDP ASSOCIATE on Linux) and a higher per-address connection limit.
+# Stock Nexus resets connections beyond 500 per source address, and every proxied UDP socket holds one until its session expires:
+# the load phases would then measure Nexus's limit, not ProxyBridge.
+mkdir -p "$WORK/nexus"; cp "$NEXUS_JS" "$WORK/nexus/script.js"
+NX_JS="$WORK/nexus/script.js"
+if [ "$(nexus_udp_ok "$NX_JS" 1099)" != yes ]; then
+ (cd "$WORK/nexus" && patch -s -p1 < "$HERE/nexus-linux-udp.patch") || { echo "cannot patch Nexus for UDP"; exit 1; }
+ [ "$(nexus_udp_ok "$NX_JS" 1099)" = yes ] || { echo "the patched Nexus still cannot do UDP ASSOCIATE"; exit 1; }
+ info "stock Nexus cannot do UDP ASSOCIATE on Linux: using a patched private copy"
+fi
+sed -i 's/ipCount >= 500/ipCount >= 20000/; s/maxConnections = 10000/maxConnections = 50000/' "$NX_JS"
+start_nx() { # port log [args]
+ local port=$1 log=$2; shift 2
+ "$NODE" "$NX_JS" -v -p "$port" "$@" > "$log" 2>&1 & NXPID=$!; PIDS+=($NXPID)
+ for _ in $(seq 40); do ss -ltn 2>/dev/null | grep -q ":$port " && return 0; sleep 0.25; done; echo "nexus on $port did not start"; exit 1; }
+NX1LOG="$WORK/nx1.log"; NX2LOG="$WORK/nx2.log"
+start_nx 1080 "$NX1LOG"; NX1=$NXPID
+start_nx 1081 "$NX2LOG" -u nx -P secret; NX2=$NXPID
+
+# ---- the origins -------------------------------------------------------------------------------------------------------------
+python3 "$HERE/nexus_origins.py" > "$WORK/origins.log" 2>&1 & PIDS+=($!)
+PB_DNS_ZONE="app.example.test=10.77.1.5;svc.corp.test=10.77.1.6" python3 "$HERE/fake_dns.py" 10.77.1.53 "$WORK/dns.log" >/dev/null 2>&1 & PIDS+=($!)
+python3 "$HERE/full_quic_server.py" 10.77.1.1 10.77.1.2 > "$WORK/quic.log" 2>&1 & PIDS+=($!)
+sleep 1.5
+
+# ---- profiles (real .pbprofile files, written with the test helper) -------------------------------------------------------------
+PX=(--proxy n1=socks5://127.0.0.1:1080 --proxy n2=socks5://nx:[email protected]:1081)
+mk() { local n=$1; shift; "$PBL" --write "$WORK/$n.pbprofile" "$@" || exit 1; }
+# The origin servers are python3 programs too, and the client's source address is one of the 10.77.1.x aliases: a rule for the
+# whole /24 on every port would also catch the servers' replies (sendto to the client's ephemeral port) and proxy them back into
+# the proxy. The rules therefore name the service ports, like a real set-up would.
+UDPPORTS='18710;18711;53;4433'; TCPPORTS='18700-18705;53'
+TCP_PROXY=(--rule "python3*:10.77.1.2:$TCPPORTS:TCP:PROXY=n2" --rule "python3*:10.77.1.0/24:$TCPPORTS:TCP:PROXY=n1")
+UDP_PROXY=(--rule "python3*:10.77.1.2:$UDPPORTS:UDP:PROXY=n2" --rule "python3*:10.77.1.0/24:$UDPPORTS:UDP:PROXY=n1")
+mk all "${PX[@]}" "${TCP_PROXY[@]}" "${UDP_PROXY[@]}"
+mk tcp_only "${PX[@]}" --rule "python3*:10.77.1.0/24:$UDPPORTS:UDP:DIRECT" "${TCP_PROXY[@]}"
+mk udp_only "${PX[@]}" --rule "python3*:10.77.1.0/24:$TCPPORTS:TCP:DIRECT" "${UDP_PROXY[@]}"
+mk direct "${PX[@]}" --rule "python3*:10.77.1.0/24:$UDPPORTS:UDP:DIRECT" --rule "python3*:10.77.1.0/24:$TCPPORTS:TCP:DIRECT"
+mk udp_block "${PX[@]}" --rule "python3*:10.77.1.0/24:$UDPPORTS:UDP:BLOCK" "${TCP_PROXY[@]}"
+mk names_on --proxy n1=socks5://127.0.0.1:1080 --rule 'python3*:10.77.1.53:53:UDP:PROXY=n1' --rule 'python3*:*.example.test:18700:TCP:PROXY=n1'
+mk names_off --proxy 'n1=socks5://127.0.0.1:1080?dns=local' --rule 'python3*:10.77.1.53:53:UDP:PROXY=n1' --rule 'python3*:*.example.test:18700:TCP:PROXY=n1'
+mk auth_bad --proxy bad=socks5://nx:[email protected]:1081 --rule 'python3*:10.77.1.1:18700:TCP:PROXY=bad' --rule 'python3*:10.77.1.1:18710:UDP:PROXY=bad'
+mk h3 "${PX[@]}" --rule 'curl*:*:443:UDP:PROXY=n1'
+info "profiles written to $WORK: $(ls "$WORK"/*.pbprofile | xargs -n1 basename | tr '\n' ' ')"
+[ -n "${KEEP_PROFILES:-}" ] && { mkdir -p "$KEEP_PROFILES" && cp "$WORK"/*.pbprofile "$KEEP_PROFILES/" && info "profiles copied to $KEEP_PROFILES"; }
+
+# ---- helpers -----------------------------------------------------------------------------------------------------------------
+start_pb() { # profile [verbose]
+ stop_pb
+ stdbuf -oL "$PB_BIN" --profile "$WORK/$1.pbprofile" --verbose "${PB_VERBOSE:-3}" > "$WORK/pb_$1.out" 2>&1 & PB=$!
+ for _ in $(seq 60); do grep -q "ProxyBridge started" "$WORK/pb_$1.out" && return 0; kill -0 $PB 2>/dev/null || break; sleep 0.25; done
+ echo "ProxyBridge did not start with $1:"; cat "$WORK/pb_$1.out"; exit 1; }
+stop_pb() { [ -n "$PB" ] && { kill -INT $PB 2>/dev/null; wait $PB 2>/dev/null; }; PB=""; }
+nx_count() { grep -cF -- "$2" "$1" 2>/dev/null || true; } # log, text
+declare -A NXB # counters before a run
+nx_mark() { NXB[tcp1]=$(nx_count $NX1LOG "[CONNECT]"); NXB[tcp2]=$(nx_count $NX2LOG "[CONNECT]")
+ NXB[udp1]=$(nx_count $NX1LOG "[UDP]"); NXB[udp2]=$(nx_count $NX2LOG "[UDP]")
+ NXB[dst1]=$(nx_count $NX1LOG "└─>"); NXB[dst2]=$(nx_count $NX2LOG "└─>"); }
+nx_delta() { D_TCP=$(( $(nx_count $NX1LOG "[CONNECT]") - NXB[tcp1] + $(nx_count $NX2LOG "[CONNECT]") - NXB[tcp2] ))
+ D_UDP=$(( $(nx_count $NX1LOG "[UDP]") - NXB[udp1] + $(nx_count $NX2LOG "[UDP]") - NXB[udp2] ))
+ D_DST=$(( $(nx_count $NX1LOG "└─>") - NXB[dst1] + $(nx_count $NX2LOG "└─>") - NXB[dst2] )); }
+declare -A MET # METRIC <phase>/<name> -> value
+run_client() { # phase scenario... (output kept in $WORK/<phase>.txt)
+ local ph=$1; shift
+ nx_mark
+ python3 "$HERE/full_client.py" "$@" > "$WORK/$ph.txt" 2>&1
+ nx_delta
+ while read -r _ n v; do MET["$ph/$n"]=$v; done < <(grep "^METRIC " "$WORK/$ph.txt")
+}
+results() { grep "^RESULT ok" "$WORK/$1.txt" | sed 's/^RESULT ok //; s/ |.*//' | sort; }
+nbad() { local c; c=$(grep -c "^RESULT BAD" "$WORK/$1.txt"); echo "${c:-0}"; }
+nok() { local c; c=$(grep -c "^RESULT ok" "$WORK/$1.txt"); echo "${c:-0}"; }
+check_phase() { # phase label : every client check passed
+ local b; b=$(nbad "$1")
+ if [ "$b" = 0 ] && [ "$(nok "$1")" -gt 0 ]; then ok "$2: $(nok "$1") client checks passed"
+ else bad "$2: $b failed checks"; grep -E "^RESULT BAD|crashed" "$WORK/$1.txt" | head -8 | sed 's/^/ /'; fi; }
+metric() { echo "${MET[$1]:-n/a}"; }
+ratio() { python3 -c "
+a, b = '$1', '$2'
+print('n/a' if 'n/a' in (a, b) or float(b) == 0 else round(float(a) / float(b), 2))"; }
+
+# =================================================================================================================================
+if want 1; then
+echo "== phase 1: engine OFF (baseline). The proxies must not see anything"
+run_client off $SCEN
+check_phase off "baseline"
+[ "$D_TCP$D_UDP$D_DST" = "000" ] && ok "baseline: the proxies saw nothing (the engine is off)" || bad "baseline: proxies saw tcp=$D_TCP udp=$D_UDP dst=$D_DST"
+REF=$(curl -s -m 120 --socks5 127.0.0.1:1080 -o /dev/null -w "%{speed_download}" "http://10.77.1.1:18700/big?mb=$DL_MB"); REF=$(python3 -c "print(round($REF/1048576))")
+info "reference: curl speaking SOCKS5 to nexus itself downloads at ${REF} MB/s (the Python client is slower than curl, so this is only for orientation)"
+fi
+
+if want 2; then
+echo "== phase 2: all traffic proxied (TCP + UDP), the same client"
+start_pb all
+run_client all $SCEN
+check_phase all "proxied"
+[ "$(results off | md5sum)" = "$(results all | md5sum)" ] && ok "same checks pass with the engine on as with it off ($(nok all))" || { bad "result sets differ between engine off and on"; diff <(results off) <(results all) | head -6 | sed 's/^/ /'; }
+[ "$D_TCP" -ge 600 ] && ok "the proxies carried the TCP connections ($D_TCP CONNECTs)" || bad "too few CONNECTs reached the proxies ($D_TCP)"
+[ "$D_UDP" -ge 100 ] && ok "the proxies opened UDP associations ($D_UDP)" || bad "too few UDP associations ($D_UDP)"
+for port in 18710 18711 53 4433; do
+ grep -qE "└─> 10\.77\.1\.[0-9]+:$port" "$NX1LOG" "$NX2LOG" && ok " a proxy relayed UDP to port $port" || bad " no UDP relayed to port $port"
+done
+for m in tcp_download_MBps tcp_upload_MBps tcp_request_p50_ms tcp_cycles_per_s udp_rtt_p50_us udp_stream_loss_pct dns_udp_qps; do
+ info "$(printf '%-22s off=%-8s on=%-8s on/off=%s' "$m" "$(metric off/$m)" "$(metric all/$m)" "$(ratio "$(metric all/$m)" "$(metric off/$m)")")"
+done
+python3 -c "import sys; sys.exit(0 if float('${MET[all/tcp_download_MBps]:-0}') >= 0.5 * float('${MET[off/tcp_download_MBps]:-1}') else 1)" \
+ && ok "download through ProxyBridge + nexus: ${MET[all/tcp_download_MBps]:-?} MB/s, at least half of the engine-off rate (${MET[off/tcp_download_MBps]:-?} MB/s)" \
+ || bad "download ${MET[all/tcp_download_MBps]:-?} MB/s is below half of the engine-off rate ${MET[off/tcp_download_MBps]:-?} MB/s"
+python3 -c "import sys; sys.exit(0 if float('${MET[all/udp_stream_loss_pct]:-100}') <= 1.0 else 1)" && ok "UDP stream through the proxy: ${MET[all/udp_stream_loss_pct]}% lost" || bad "UDP stream loss above 1% (${MET[all/udp_stream_loss_pct]:-?})"
+fi
+
+if want 3; then
+echo "== phase 3: TCP proxied, UDP direct"
+start_pb tcp_only
+run_client tcp_only $SCEN
+check_phase tcp_only "TCP proxied / UDP direct"
+[ "$D_TCP" -ge 600 ] && ok "TCP reached the proxies ($D_TCP)" || bad "TCP did not reach the proxies ($D_TCP)"
+# DNS over TCP is proxied here, the UDP flows (echo, DNS, QUIC) must never appear
+[ "$D_UDP" = 0 ] && [ "$D_DST" = 0 ] && ok "no UDP reached the proxies" || bad "UDP leaked into the proxies (assoc=$D_UDP dst=$D_DST)"
+fi
+
+if want 4; then
+echo "== phase 4: UDP proxied, TCP direct"
+start_pb udp_only
+run_client udp_only $SCEN
+check_phase udp_only "UDP proxied / TCP direct"
+[ "$D_UDP" -ge 100 ] && ok "UDP reached the proxies ($D_UDP associations)" || bad "UDP did not reach the proxies ($D_UDP)"
+[ "$D_TCP" = 0 ] && ok "no TCP connection reached the proxies" || bad "TCP leaked into the proxies ($D_TCP)"
+fi
+
+if want 5; then
+echo "== phase 5: everything DIRECT (engine on, nothing proxied)"
+start_pb direct
+run_client direct $SCEN
+check_phase direct "all DIRECT"
+[ "$D_TCP$D_UDP$D_DST" = "000" ] && ok "the proxies saw nothing" || bad "DIRECT rules leaked into the proxies (tcp=$D_TCP udp=$D_UDP)"
+[ "$(results off | md5sum)" = "$(results direct | md5sum)" ] && ok "same checks as the baseline" || bad "result sets differ from the baseline"
+fi
+
+if want 6; then
+echo "== phase 6: UDP blocked, TCP proxied"
+start_pb udp_block
+run_client udp_block udpblock tcp
+check_phase udp_block "UDP BLOCK / TCP PROXY"
+[ "$D_UDP" = 0 ] && ok "blocked UDP never reached a proxy" || bad "blocked UDP reached a proxy ($D_UDP)"
+grep -q "via BLOCK" "$WORK/pb_udp_block.out" && ok "the connection log shows the block" || bad "no BLOCK in the connection log"
+fi
+
+if want 7; then
+echo "== phase 7: DNS via proxy ON vs OFF (per proxy)"
+NAMECLIENT='import sys; sys.path.insert(0, "'"$HERE"'"); import full_client as c; r = c.dns_udp("app.example.test"); print("answer", r); print(c.http_get("10.77.1.5", 18700))'
+for mode in on off; do
+ start_pb names_$mode; nx_mark
+ out=$(python3 -c "$NAMECLIENT" 2>&1)
+ has "names_$mode: the client resolves through the proxy and reaches the origin" "$out" "origin-5"
+ if [ "$mode" = on ]; then
+ grep -qF "[CONNECT] app.example.test:18700" "$NX1LOG" && ok "DNS via proxy ON: the proxy was asked for the host name app.example.test" || bad "ON: the proxy never received the host name"
+ else
+ tail -n 30 "$NX1LOG" | grep -qF "[CONNECT] 10.77.1.5:18700" && ok "DNS via proxy OFF: the proxy was given the address 10.77.1.5" || bad "OFF: the proxy did not receive the plain address"
+ n_before=$(grep -cF "[CONNECT] app.example.test:18700" "$NX1LOG")
+ [ "$n_before" = 1 ] && ok " and no second request with the name was made" || bad " host name was sent although DNS via proxy is off ($n_before)"
+ fi
+done
+fi
+
+if want 8; then
+echo "== phase 8: load"
+start_pb all
+run_client load_on load churn
+check_phase load_on "load through ProxyBridge + the proxies"
+[ "$D_TCP" -ge 450 ] && [ "$D_UDP" -ge 1500 ] && ok " the proxies carried the load ($D_TCP CONNECTs, $D_UDP UDP associations)" || bad " load did not go through the proxies (tcp=$D_TCP udp=$D_UDP)"
+stop_pb
+run_client load_off load churn
+check_phase load_off "load without the engine (baseline)"
+for r in 3000 6000 12000; do info "UDP at $r datagrams/s, loss: engine off=$(metric load_off/udp_burst_${r}_pps_loss_pct)% through ProxyBridge+nexus=$(metric load_on/udp_burst_${r}_pps_loss_pct)%"; done
+fi
+
+if want 9; then
+echo "== phase 9: HTTP/3 (real QUIC) with curl"
+H3_CURL="${H3_CURL:-}"
+if [ -z "$H3_CURL" ]; then for c in curl /snap/bin/curl; do command -v "$c" >/dev/null 2>&1 && "$c" --version 2>/dev/null | grep -qi "http3" && { H3_CURL=$(command -v "$c"); break; }; done; fi
+H3URL="https://www.cloudflare.com/"
+if [ -z "$H3_CURL" ]; then skip "no curl with HTTP/3 support (set H3_CURL)"
+else
+ h3() { timeout 40 "$H3_CURL" --http3-only -s -m 30 -o /dev/null -w "%{http_code} v%{http_version}" "$H3URL" 2>/dev/null | tail -n 1; } # snap curl prints a banner on stderr
+ base=$(h3); info "direct (engine off): $base [$H3_CURL]"
+ if [[ "$base" != "200 v3" ]]; then skip "HTTP/3 does not work even without ProxyBridge (offline or UDP 443 filtered): $base"
+ else
+ start_pb h3; nx_mark
+ via=$(h3)
+ has "curl --http3-only through ProxyBridge + nexus (QUIC over SOCKS5 UDP ASSOCIATE)" "$via" "200 v3"
+ nx_delta; [ "$D_UDP" -ge 1 ] && grep -qE "└─> .*:443" "$NX1LOG" && ok " nexus relayed UDP to port 443 ($D_UDP association)" || bad " nexus saw no UDP to port 443"
+ grep -qE "curl.*:443 via SOCKS5 127.0.0.1:1080 \(UDP\)" "$WORK/pb_h3.out" && ok " the connection log shows the QUIC flow as UDP via the proxy" || bad " no 'via SOCKS5 ... (UDP)' line for curl in the log"
+ n=0; for i in 1 2 3 4 5; do [ "$(h3)" = "200 v3" ] && n=$((n+1)); done; [ "$n" = 5 ] && ok " 5 more HTTP/3 requests in a row" || bad " only $n/5 further HTTP/3 requests succeeded"
+ big=$(timeout 60 "$H3_CURL" --http3-only -s -m 50 -o /dev/null -w "%{http_code} %{size_download}" "https://www.cloudflare.com/cdn-cgi/trace" 2>/dev/null | tail -n 1); has " small QUIC body transfer" "$big" "200"
+ fi
+fi
+fi
+
+if want 10; then
+echo "== phase 10: failure handling"
+start_pb auth_bad
+out=$(python3 -c "
+import sys; sys.path.insert(0, '$HERE'); import full_client as c
+try: print('GOT', c.http_get('10.77.1.1', 18700, timeout=8))
+except OSError as e: print('FAILED', type(e).__name__)" 2>&1)
+[[ "$out" != *origin-1* ]] && ok "wrong proxy password: the connection fails closed (the origin is not reached directly): $out" || bad "wrong password leaked the connection direct: $out"
+start_pb all
+kill $NX1; wait $NX1 2>/dev/null; sleep 1
+out=$(python3 -c "
+import sys; sys.path.insert(0, '$HERE'); import full_client as c
+try: print('GOT', c.http_get('10.77.1.1', 18700, timeout=6))
+except OSError as e: print('FAILED', type(e).__name__)" 2>&1)
+[[ "$out" != *origin-1* ]] && ok "proxy down: TCP fails closed, no leak around it ($out)" || bad "TCP leaked around the dead proxy: $out"
+python3 "$HERE/full_client.py" udpblock | grep -q "RESULT ok" && ok "proxy down: UDP gets no answer either" || bad "UDP answered although its proxy is down"
+start_nx 1080 "$NX1LOG.2"; NX1=$NXPID; sleep 1
+out=$(python3 -c "
+import sys; sys.path.insert(0, '$HERE'); import full_client as c
+print(c.http_get('10.77.1.1', 18700))" 2>&1); has "proxy restarted: TCP works again" "$out" "origin-1"
+rec=$(python3 - <<'PY'
+import socket, time
+s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.settimeout(1); t0 = time.time()
+while time.time() - t0 < 15:
+ try:
+ s.sendto(b"again", ("10.77.1.1", 18710)); d, a = s.recvfrom(100); print("recovered in %.1fs: %s" % (time.time() - t0, d.decode())); break
+ except socket.timeout: pass
+else: print("NOT RECOVERED")
+PY
+); has "proxy restarted: UDP re-associates" "$rec" "recovered in"; info "$rec"
+stop_pb
+fi
+
+echo; echo "passed=$PASS failed=$FAIL skipped=$SKIP"
+if [ "$FAIL" != 0 ]; then
+ [ -n "${KEEP_LOGS:-}" ] && { mkdir -p "$KEEP_LOGS" && cp "$WORK"/*.out "$WORK"/*.log "$WORK"/*.txt "$KEEP_LOGS/" 2>/dev/null; echo "logs kept in $KEEP_LOGS"; }
+ echo "---- last ProxyBridge output"; tail -15 "$WORK"/pb_*.out 2>/dev/null | tail -30
+ echo "---- nexus 1"; tail -8 "$NX1LOG"; echo "---- nexus 2"; tail -8 "$NX2LOG"; exit 1
+fi
diff --git a/Linux/tests/fake_dns.py b/Linux/tests/fake_dns.py
index d0ef5b2..a53fdf5 100644
--- a/Linux/tests/fake_dns.py
+++ b/Linux/tests/fake_dns.py
@@ -103,11 +103,18 @@ class TCP(socketserver.BaseRequestHandler):
class TS(socketserver.ThreadingMixIn, socketserver.TCPServer):
allow_reuse_address = True
daemon_threads = True
+ request_queue_size = 256
class US(socketserver.ThreadingMixIn, socketserver.UDPServer):
allow_reuse_address = True
daemon_threads = True
+ request_queue_size = 1024
+
+ def server_bind(self):
+ # a burst of a few hundred queries (parallel resolvers) must fit in the receive buffer while the handler threads start
+ self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4 << 20)
+ super().server_bind()
ip = sys.argv[1]
diff --git a/Linux/tests/full_client.py b/Linux/tests/full_client.py
new file mode 100755
index 0000000..01b1d48
--- /dev/null
+++ b/Linux/tests/full_client.py
@@ -0,0 +1,519 @@
+#!/usr/bin/env python3
+"""Client side of the full end-to-end suite (tests/e2e_full.sh). The client never knows whether ProxyBridge is running: the very
+same scenarios run with the engine off and on, and the suite compares the results.
+
+usage: full_client.py <scenario> [<scenario> ...]
+ tcp http get from two origins, 200 parallel and 400 sequential connections, request latency, a big download and a big upload
+ with checksums
+ udp one socket to several destinations, connected socket, datagram sizes 1..59904 bytes, bound source port, 300 concurrent
+ sockets, a 3000 datagrams/s stream (loss), round-trip time
+ dns real DNS packets (A query, NXDOMAIN, 200 parallel queries, queries over TCP, queries per second)
+ quic QUIC long-header packets (RFC 9000 Version Negotiation): the datagram shape of HTTP/3, 1200 bytes, connection ids mirrored
+ load 500 parallel TCP connections, 1000 concurrent UDP sockets (LOAD_UDP), 20000 small UDP datagrams back to back
+ churn what a resolver does: a new UDP socket per lookup, CHURN_N (700) times in a row
+ newflow latency of the first datagram of brand-new UDP flows (echo and DNS port)
+ udpblock one datagram that a BLOCK rule must stop: no reply may arrive
+
+Output: "RESULT ok|BAD <name> | <detail>", "METRIC <name> <value>", "INFO <text>". Environment: DL_MB (default 100), UL_MB (50),
+STREAM_N (6000), the origin addresses are fixed (see tests/nexus_origins.py and tests/full_quic_server.py).
+"""
+import concurrent.futures as cf
+import hashlib, os, random, socket, struct, sys, threading, time
+
+O1, O2, DNSIP = "10.77.1.1", "10.77.1.2", "10.77.1.53"
+BLOCK = bytes((i * 7 + 13) & 255 for i in range(1 << 20))
+DL_MB = int(os.environ.get("DL_MB", "100"))
+UL_MB = int(os.environ.get("UL_MB", "50"))
+STREAM_N = int(os.environ.get("STREAM_N", "6000"))
+# Every proxied UDP socket holds one TCP control connection to the proxy until its session expires. Stock Nexus Proxy resets
+# connections beyond 500 per source address (tests/e2e_full.sh raises that in its private copy); ProxyBridge itself allows 2048
+# UDP sessions per process.
+LOAD_UDP = int(os.environ.get("LOAD_UDP", "1000"))
+
+
+def res(name, ok, detail=""):
+ print("RESULT", "ok" if ok else "BAD", name, "|", detail, flush=True) # the suite compares names, not details
+
+
+def metric(name, value):
+ print("METRIC", name, value, flush=True)
+
+
+def info(text):
+ print("INFO", text, flush=True)
+
+
+def pattern_sha(mb):
+ h = hashlib.sha256()
+ for _ in range(mb):
+ h.update(BLOCK)
+ return h.hexdigest()
+
+
+def pct(values, p):
+ values = sorted(values)
+ return values[min(len(values) - 1, int(len(values) * p))]
+
+
+# ------------------------------------------------------------------ TCP
+def http_get(ip, port, path="/", timeout=15):
+ s = socket.create_connection((ip, port), timeout=timeout)
+ try:
+ s.sendall(("GET %s HTTP/1.0\r\nHost: x\r\n\r\n" % path).encode())
+ data = b""
+ while True:
+ d = s.recv(65536)
+ if not d:
+ break
+ data += d
+ return data.split(b"\r\n\r\n", 1)[-1]
+ finally:
+ s.close()
+
+
+def tcp():
+ res("tcp: origin 1 answers", http_get(O1, 18700) == b"origin-1\n")
+ res("tcp: origin 2 answers", http_get(O2, 18700) == b"origin-2\n")
+
+ def one(_):
+ try:
+ return http_get(O1, 18700) == b"origin-1\n"
+ except OSError:
+ return False
+ with cf.ThreadPoolExecutor(64) as ex:
+ okc = sum(ex.map(one, range(200)))
+ res("tcp: 200 parallel connections", okc == 200, "%d/200" % okc)
+ t0 = time.perf_counter()
+ okc = sum(one(i) for i in range(400))
+ res("tcp: 400 sequential connect/request/close cycles", okc == 400, "%d/400" % okc)
+ metric("tcp_cycles_per_s", round(400 / (time.perf_counter() - t0)))
+ lat = []
+ for _ in range(200):
+ t = time.perf_counter()
+ http_get(O1, 18700)
+ lat.append((time.perf_counter() - t) * 1000)
+ metric("tcp_request_p50_ms", round(pct(lat, .5), 2))
+ metric("tcp_request_p99_ms", round(pct(lat, .99), 2))
+
+ # big download with a checksum of the received stream
+ s = socket.create_connection((O1, 18700), timeout=60)
+ s.sendall(("GET /big?mb=%d HTTP/1.0\r\n\r\n" % DL_MB).encode())
+ t0 = time.perf_counter()
+ buf = b""
+ while b"\r\n\r\n" not in buf:
+ buf += s.recv(65536)
+ head, _, rest = buf.partition(b"\r\n\r\n")
+ h = hashlib.sha256(rest)
+ got = len(rest)
+ while True:
+ d = s.recv(1 << 20)
+ if not d:
+ break
+ h.update(d)
+ got += len(d)
+ dt = time.perf_counter() - t0
+ s.close()
+ res("tcp: %d MB download, checksum identical" % DL_MB, got == DL_MB << 20 and h.hexdigest() == pattern_sha(DL_MB), "%d bytes" % got)
+ metric("tcp_download_MBps", round(DL_MB / dt))
+
+ # big upload; the origin answers with the sha256 of what it received
+ s = socket.create_connection((O2, 18700), timeout=60)
+ s.sendall(("POST /upload HTTP/1.0\r\nContent-Length: %d\r\n\r\n" % (UL_MB << 20)).encode())
+ t0 = time.perf_counter()
+ for _ in range(UL_MB):
+ s.sendall(BLOCK)
+ dt = time.perf_counter() - t0
+ ans = b""
+ while True:
+ d = s.recv(4096)
+ if not d:
+ break
+ ans += d
+ s.close()
+ res("tcp: %d MB upload, origin received identical bytes" % UL_MB, ans.split(b"\r\n\r\n", 1)[-1].decode() == pattern_sha(UL_MB))
+ metric("tcp_upload_MBps", round(UL_MB / dt))
+
+
+# ------------------------------------------------------------------ UDP
+def usock(t=4.0):
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.settimeout(t)
+ return s
+
+
+def udp():
+ s = usock()
+ for dst, tag in (((O1, 18710), b"U1:"), ((O2, 18710), b"U2:"), ((O1, 18710), b"U1:")):
+ try:
+ s.sendto(b"hello", dst)
+ d, a = s.recvfrom(100)
+ res("udp: one socket, destination %s:%d" % dst, d == tag + b"hello" and a == dst, "from %s:%d" % a)
+ except OSError as e:
+ res("udp: one socket, destination %s:%d" % dst, False, str(e))
+ c = usock()
+ c.connect((O1, 18711))
+ try:
+ c.send(b"conn")
+ d = c.recv(100)
+ res("udp: connected socket, peer address restored", d == b"U3:conn" and c.getpeername() == (O1, 18711), "peer %s:%d" % c.getpeername())
+ except OSError as e:
+ res("udp: connected socket, peer address restored", False, str(e))
+ for n in (1, 100, 1200, 1472, 8000, 30000, 59904):
+ b = usock(8)
+ payload = bytes((i * 31 + n) & 255 for i in range(n))
+ try:
+ b.sendto(payload, (O1, 18710))
+ d, a = b.recvfrom(70000)
+ res("udp: datagram of %d bytes" % n, d == b"U1:" + payload)
+ except OSError as e:
+ res("udp: datagram of %d bytes" % n, False, str(e))
+ port = random.randint(41000, 49000)
+ b = usock()
+ b.bind(("0.0.0.0", port))
+ try:
+ b.sendto(b"bound", (O1, 18710))
+ d, a = b.recvfrom(100)
+ res("udp: socket bound to a chosen source port gets the reply", d == b"U1:bound" and b.getsockname()[1] == port, "port %d" % port)
+ except OSError as e:
+ res("udp: socket bound to a chosen source port", False, str(e))
+
+ def one(i):
+ k = usock(8)
+ dst = (("10.77.1.%d" % (1 + i % 2)), 18710)
+ try:
+ k.sendto(b"n%d" % i, dst)
+ d, a = k.recvfrom(100)
+ return d.endswith(b"n%d" % i) and a == dst
+ except OSError:
+ return False
+ finally:
+ k.close()
+ with cf.ThreadPoolExecutor(32) as ex:
+ okc = sum(ex.map(one, range(300)))
+ res("udp: 300 concurrent sockets across two destinations", okc == 300, "%d/300" % okc)
+
+ # sustained stream, replies counted while still sending
+ q = usock(1)
+ q.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4 << 20)
+ got, stop = set(), threading.Event()
+
+ def rx():
+ q.settimeout(0.2)
+ while not stop.is_set():
+ try:
+ d, _ = q.recvfrom(2000)
+ got.add(int.from_bytes(d[3:7], "big"))
+ except socket.timeout:
+ pass
+ th = threading.Thread(target=rx, daemon=True)
+ th.start()
+ t0 = time.perf_counter()
+ for i in range(STREAM_N):
+ q.sendto(i.to_bytes(4, "big") + b"x" * 996, (O1, 18710))
+ while time.perf_counter() < t0 + (i + 1) / 3000.0:
+ time.sleep(0)
+ sent_for = time.perf_counter() - t0
+ time.sleep(2)
+ stop.set()
+ th.join()
+ loss = 100.0 * (STREAM_N - len(got)) / STREAM_N
+ res("udp: stream of %d datagrams at 3000/s, loss <= 1%%" % STREAM_N, loss <= 1.0, "%.2f%% lost" % loss)
+ metric("udp_stream_loss_pct", round(loss, 2))
+ metric("udp_stream_send_pps", round(STREAM_N / sent_for))
+
+ rtt = []
+ r = usock(2)
+ for _ in range(1000):
+ t = time.perf_counter()
+ r.sendto(b"p", (O1, 18710))
+ r.recvfrom(100)
+ rtt.append((time.perf_counter() - t) * 1e6)
+ metric("udp_rtt_p50_us", round(pct(rtt, .5)))
+ metric("udp_rtt_p99_us", round(pct(rtt, .99)))
+ res("udp: 1000 round trips", True)
+
+
+# ------------------------------------------------------------------ DNS
+def dns_query(name, qtype=1, txid=None):
+ txid = random.randint(0, 65535) if txid is None else txid
+ q = struct.pack("!HHHHHH", txid, 0x0100, 1, 0, 0, 0)
+ for label in name.split("."):
+ q += bytes([len(label)]) + label.encode()
+ return txid, q + b"\0" + struct.pack("!HH", qtype, 1)
+
+
+def skip_name(m, off):
+ while True:
+ l = m[off]
+ if l == 0:
+ return off + 1
+ if l & 0xC0 == 0xC0:
+ return off + 2
+ off += 1 + l
+
+
+def dns_answer(m, txid):
+ """(rcode, [ipv4 strings]) of a response, or None when it is not the answer to txid."""
+ if len(m) < 12 or struct.unpack("!H", m[:2])[0] != txid:
+ return None
+ flags, qd, an = struct.unpack("!HHH", m[2:8])
+ off = 12
+ for _ in range(qd):
+ off = skip_name(m, off) + 4
+ ips = []
+ for _ in range(an):
+ off = skip_name(m, off)
+ t, _, _, rl = struct.unpack("!HHIH", m[off:off + 10])
+ off += 10
+ if t == 1 and rl == 4:
+ ips.append(socket.inet_ntoa(m[off:off + 4]))
+ off += rl
+ return flags & 0xF, ips
+
+
+def dns_udp(name, timeout=4):
+ txid, q = dns_query(name)
+ s = usock(timeout)
+ try:
+ s.sendto(q, (DNSIP, 53))
+ m, _ = s.recvfrom(4096)
+ return dns_answer(m, txid)
+ except OSError:
+ return None
+ finally:
+ s.close()
+
+
+def dns_tcp(name):
+ txid, q = dns_query(name)
+ s = socket.create_connection((DNSIP, 53), timeout=5)
+ try:
+ s.sendall(struct.pack("!H", len(q)) + q)
+ n = struct.unpack("!H", s.recv(2))[0]
+ m = b""
+ while len(m) < n:
+ m += s.recv(n - len(m))
+ return dns_answer(m, txid)
+ except OSError:
+ return None
+ finally:
+ s.close()
+
+
+def dns():
+ r = dns_udp("app.example.test")
+ res("dns: UDP A query app.example.test", r == (0, ["10.77.1.5"]), str(r))
+ r = dns_udp("svc.corp.test")
+ res("dns: UDP A query svc.corp.test", r == (0, ["10.77.1.6"]), str(r))
+ r = dns_udp("does-not-exist.test")
+ res("dns: UDP NXDOMAIN", r is not None and r[0] == 3, str(r))
+ with cf.ThreadPoolExecutor(32) as ex:
+ okc = sum(1 for x in ex.map(lambda i: dns_udp("app.example.test"), range(200)) if x == (0, ["10.77.1.5"]))
+ res("dns: 200 parallel UDP queries", okc == 200, "%d/200" % okc)
+ okc = sum(1 for _ in range(20) if dns_tcp("svc.corp.test") == (0, ["10.77.1.6"]))
+ res("dns: 20 queries over TCP", okc == 20, "%d/20" % okc)
+ lat, lost = [], 0
+ for _ in range(500):
+ t = time.perf_counter()
+ if dns_udp("app.example.test", timeout=2) != (0, ["10.77.1.5"]):
+ lost += 1
+ lat.append((time.perf_counter() - t) * 1000)
+ res("dns: 500 sequential UDP queries, none lost", lost == 0, "%d lost" % lost)
+ metric("dns_udp_qps", round(500 / (sum(lat) / 1000)))
+ metric("dns_udp_p99_ms", round(pct(lat, .99), 2))
+
+
+# ------------------------------------------------------------------ QUIC-shaped UDP
+def quic_initial(version=0x1a2a3a4a, size=1200):
+ dcid, scid = os.urandom(8), os.urandom(8)
+ pkt = bytes([0xC3]) + struct.pack("!I", version) + bytes([len(dcid)]) + dcid + bytes([len(scid)]) + scid + b"\x00"
+ body = os.urandom(size - len(pkt) - 2)
+ pkt += struct.pack("!H", 0x4000 | len(body)) + body
+ return dcid, scid, pkt
+
+
+def vn_ok(reply, dcid, scid):
+ if len(reply) < 7 or not reply[0] & 0x80 or struct.unpack("!I", reply[1:5])[0] != 0:
+ return False
+ dl = reply[5]
+ if reply[6:6 + dl] != scid: # the reply's DCID is the client's SCID
+ return False
+ sl = reply[6 + dl]
+ if reply[7 + dl:7 + dl + sl] != dcid: # ... and its SCID is the client's DCID
+ return False
+ tail = reply[7 + dl + sl:]
+ return len(tail) >= 4 and len(tail) % 4 == 0 and struct.pack("!I", 1) in [tail[i:i + 4] for i in range(0, len(tail), 4)]
+
+
+def quic_once(dst, timeout=5):
+ dcid, scid, pkt = quic_initial()
+ s = usock(timeout)
+ try:
+ s.sendto(pkt, dst)
+ d, a = s.recvfrom(2000)
+ return a == dst and vn_ok(d, dcid, scid)
+ except OSError:
+ return False
+ finally:
+ s.close()
+
+
+def quic():
+ res("quic: 1200-byte Initial with an unknown version -> Version Negotiation", quic_once((O1, 4433)))
+ res("quic: same to a second server", quic_once((O2, 4433)))
+ with cf.ThreadPoolExecutor(32) as ex:
+ okc = sum(ex.map(lambda i: quic_once((("10.77.1.%d" % (1 + i % 2)), 4433)), range(100)))
+ res("quic: 100 parallel handshakes (Version Negotiation each)", okc == 100, "%d/100" % okc)
+ s = usock()
+ okc = 0
+ for i in range(20): # one socket, many packets, both servers: 5-tuple stays stable
+ dcid, scid, pkt = quic_initial()
+ s.sendto(pkt, (("10.77.1.%d" % (1 + i % 2)), 4433))
+ try:
+ d, _ = s.recvfrom(2000)
+ okc += vn_ok(d, dcid, scid)
+ except OSError:
+ pass
+ res("quic: one socket, 20 packets to two servers", okc == 20, "%d/20" % okc)
+ s.sendto(b"not quic at all", (O1, 4433))
+ try:
+ d, _ = s.recvfrom(100)
+ res("quic: a non-QUIC datagram reaches the server unchanged", d == b"NOTQUIC" + b"not quic", d[:20].decode("latin1"))
+ except OSError as e:
+ res("quic: a non-QUIC datagram reaches the server unchanged", False, str(e))
+ dcid, scid, pkt = quic_initial(version=1)
+ s.sendto(pkt, (O2, 4433))
+ try:
+ d, _ = s.recvfrom(100)
+ res("quic: a version 1 Initial reaches the server unchanged", d.startswith(b"NOTQUIC") and d[7:] == pkt[:8])
+ except OSError as e:
+ res("quic: a version 1 Initial reaches the server unchanged", False, str(e))
+
+
+# ------------------------------------------------------------------ load
+def load():
+ def one(_):
+ try:
+ return http_get(O1, 18700, timeout=30) == b"origin-1\n"
+ except OSError:
+ return False
+ t0 = time.perf_counter()
+ with cf.ThreadPoolExecutor(128) as ex:
+ okc = sum(ex.map(one, range(500)))
+ res("load: 500 TCP connections, 128 at a time", okc == 500, "%d/500 in %.1fs" % (okc, time.perf_counter() - t0))
+ socks = []
+ got = 0
+ try:
+ for i in range(LOAD_UDP):
+ k = usock(10)
+ socks.append(k)
+ k.sendto(b"u%d" % i, ((("10.77.1.%d" % (1 + i % 2)), 18710)))
+ for i, k in enumerate(socks):
+ try:
+ d, _ = k.recvfrom(100)
+ got += d.endswith(b"u%d" % i)
+ except OSError:
+ pass
+ finally:
+ for k in socks:
+ k.close()
+ res("load: %d UDP sockets open at once, each answered" % LOAD_UDP, got >= 0.99 * LOAD_UDP, "%d/%d" % (got, LOAD_UDP))
+ # one socket sends small datagrams at a fixed rate; the answers are counted while it is still sending
+ def burst(n, pps):
+ q = usock(1)
+ q.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8 << 20)
+ seen, stop = set(), threading.Event()
+
+ def rx():
+ q.settimeout(0.2)
+ while not stop.is_set():
+ try:
+ d, _ = q.recvfrom(2000)
+ seen.add(int.from_bytes(d[3:7], "big"))
+ except socket.timeout:
+ pass
+ th = threading.Thread(target=rx, daemon=True)
+ th.start()
+ t0 = time.perf_counter()
+ for i in range(n):
+ q.sendto(i.to_bytes(4, "big") + b"y" * 60, (O1, 18710))
+ while time.perf_counter() < t0 + (i + 1) / pps:
+ time.sleep(0)
+ time.sleep(2)
+ stop.set()
+ th.join()
+ q.close()
+ return 100.0 * (n - len(seen)) / n
+ for pps in (3000, 6000, 12000):
+ loss = burst(10000, pps)
+ metric("udp_burst_%d_pps_loss_pct" % pps, round(loss, 2))
+ if pps == 3000:
+ res("load: 10000 small UDP datagrams at 3000/s, loss <= 1%", loss <= 1.0, "%.2f%% lost" % loss)
+ else:
+ info("%d datagrams/s: %.2f%% lost (reported, not a pass/fail line: a single-threaded Node proxy saturates)" % (pps, loss))
+
+
+def newflow():
+ """Latency of the FIRST datagram of a brand-new UDP flow (new socket each time): for a proxied flow this includes the SOCKS5
+ UDP ASSOCIATE. Echo port and DNS port are measured separately because port 53 takes its own path in the engine."""
+ for label, dst, payload in (("echo", (O1, 18710), b"x"), ("dns", (DNSIP, 53), None)):
+ lat, fails = [], 0
+ for _ in range(60):
+ s = usock(3)
+ try:
+ if payload is None:
+ txid, q = dns_query("app.example.test")
+ msg = q
+ else:
+ msg = payload
+ t = time.perf_counter()
+ s.sendto(msg, dst)
+ s.recvfrom(2000)
+ lat.append((time.perf_counter() - t) * 1000)
+ except OSError:
+ fails += 1
+ finally:
+ s.close()
+ if lat:
+ metric("newflow_%s_p50_ms" % label, round(pct(lat, .5), 2))
+ metric("newflow_%s_p99_ms" % label, round(pct(lat, .99), 2))
+ res("newflow: first datagram of 60 new %s flows answered" % label, fails == 0, "%d lost" % fails)
+
+
+def churn():
+ """What a resolver does: a new UDP socket per lookup, one query, close. Every proxied UDP socket keeps a session (and a TCP
+ control connection to the proxy) until it has been idle for the UDP idle timeout (60 s by default), so this piles up sessions."""
+ n = int(os.environ.get("CHURN_N", "700"))
+ lost, first_lost, t0 = 0, None, time.perf_counter()
+ for i in range(n):
+ if dns_udp("app.example.test", timeout=2) != (0, ["10.77.1.5"]):
+ lost += 1
+ first_lost = i if first_lost is None else first_lost
+ dt = time.perf_counter() - t0
+ res("churn: %d lookups, each on a new UDP socket" % n, lost == 0, "%d lost%s" % (lost, "" if first_lost is None else ", first at #%d" % first_lost))
+ metric("churn_lookups_per_s", round(n / dt))
+
+
+def udpblock():
+ s = usock(2.5)
+ try:
+ s.sendto(b"should-not-arrive", (O1, 18710))
+ s.recvfrom(100)
+ res("udp blocked: nothing may come back", False, "a reply arrived")
+ except (socket.timeout, OSError):
+ res("udp blocked: nothing may come back", True)
+
+
+SCENARIOS = {"tcp": tcp, "udp": udp, "dns": dns, "quic": quic, "load": load, "udpblock": udpblock, "newflow": newflow, "churn": churn}
+
+if __name__ == "__main__":
+ names = sys.argv[1:] or ["tcp", "udp", "dns", "quic"]
+ for n in names:
+ if n not in SCENARIOS:
+ print("unknown scenario", n, file=sys.stderr)
+ sys.exit(2)
+ for n in names:
+ try:
+ SCENARIOS[n]()
+ except Exception as e: # a crashed scenario is a failure, not a silent skip
+ res("%s: scenario crashed" % n, False, "%s: %s" % (type(e).__name__, e))
diff --git a/Linux/tests/full_quic_server.py b/Linux/tests/full_quic_server.py
new file mode 100755
index 0000000..d853a2a
--- /dev/null
+++ b/Linux/tests/full_quic_server.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+"""A UDP server that speaks the first step of QUIC (RFC 9000 section 17.2.1, Version Negotiation), for the end-to-end tests.
+
+A real QUIC stack answers a long-header packet that carries a version it does not know with a Version Negotiation packet. That is
+a genuine QUIC exchange (first datagram >= 1200 bytes, long header, connection ids, a reply that mirrors them), so it exercises
+UDP the way HTTP/3 does without needing a TLS stack. Packets that are not QUIC long headers are answered with "NOTQUIC" + the
+first 8 bytes so the client can tell the server saw them.
+
+usage: full_quic_server.py <ip> [<ip> ...] (listens on UDP port 4433 of each address)
+"""
+import os, socket, struct, sys, threading, time
+
+SUPPORTED = (1,)
+
+
+def version_negotiation(pkt):
+ # long header: 1 byte flags, 4 bytes version, dcid len + dcid, scid len + scid
+ if len(pkt) < 7 or not pkt[0] & 0x80:
+ return None
+ version = struct.unpack("!I", pkt[1:5])[0]
+ if version == 0 or version in SUPPORTED:
+ return None
+ dl = pkt[5]
+ if len(pkt) < 6 + dl + 1:
+ return None
+ dcid = pkt[6:6 + dl]
+ sl = pkt[6 + dl]
+ scid = pkt[7 + dl:7 + dl + sl]
+ if len(scid) != sl:
+ return None
+ # the reply mirrors the connection ids: its DCID is the client's SCID and its SCID is the client's DCID
+ out = bytes([0x80 | (os.urandom(1)[0] & 0x7F)]) + struct.pack("!I", 0)
+ out += bytes([len(scid)]) + scid + bytes([len(dcid)]) + dcid
+ for v in SUPPORTED:
+ out += struct.pack("!I", v)
+ return out
+
+
+def serve(ip):
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 4 << 20)
+ s.bind((ip, 4433))
+
+ def loop():
+ while True:
+ d, a = s.recvfrom(70000)
+ r = version_negotiation(d) or (b"NOTQUIC" + d[:8])
+ try:
+ s.sendto(r, a)
+ except OSError:
+ pass
+ threading.Thread(target=loop, daemon=True).start()
+
+
+for ip in sys.argv[1:]:
+ serve(ip)
+print("quic servers up", flush=True)
+while True:
+ time.sleep(3600)
diff --git a/Linux/tests/pbl.py b/Linux/tests/pbl.py
index 1cf6a33..daf9390 100755
--- a/Linux/tests/pbl.py
+++ b/Linux/tests/pbl.py
@@ -9,6 +9,7 @@ set-ups with compact arguments; this script turns them into a .pbprofile (held i
--rule process:hosts:ports:protocol:action[=proxy][+localhost] (proxy = name or 1-based index)
--dns-via-proxy true|false --log-every-connection true|false --verbose N
--profile FILE-OR-NAME a profile to start from; the other arguments are added to it
+ --write FILE write the profile to FILE (mode 0600) and exit instead of running the program
PB_DIR selects the build (default: Linux/output).
"""
import json, os, re, sys, tempfile
@@ -75,17 +76,19 @@ def main():
prof = {"Version": "1.0", "ProxyConfigs": [], "ProxyRules": []}
verbose = None
base = None
+ write_to = None
proxies, rules = [], []
i = 0
while i < len(args):
a = args[i]
- if a in ("--proxy", "--rule", "--profile", "--verbose", "--dns-via-proxy", "--log-every-connection") and i + 1 < len(args):
+ if a in ("--proxy", "--rule", "--profile", "--verbose", "--dns-via-proxy", "--log-every-connection", "--write") and i + 1 < len(args):
v = args[i + 1]
i += 2
if a == "--proxy": proxies.append(parse_proxy(v))
elif a == "--rule": rules.append(parse_rule(v))
elif a == "--profile": base = v
elif a == "--verbose": verbose = v
+ elif a == "--write": write_to = v
elif a == "--dns-via-proxy": prof["DnsViaProxy"] = boolean(v)
else: prof["LogEveryConnection"] = boolean(v)
else:
@@ -123,6 +126,11 @@ def main():
r["ProxyConfigId"] = hit[0]
prof["ProxyRules"].append(r)
+ if write_to:
+ fdw = os.open(write_to, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
+ os.write(fdw, json.dumps(prof, indent=2).encode())
+ os.close(fdw)
+ return
fd, tmp = tempfile.mkstemp(prefix="pbl-", suffix=".pbprofile")
os.write(fd, json.dumps(prof).encode())
os.unlink(tmp) # the exec'd program reads it through /proc/self/fd/<fd>