From 37eaa4f2a94325d86145b3ab00692c745e5941c4 Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Wed, 18 Nov 2026 20:11:26 +0300 Subject: [PATCH] socks: a UDP association is not relayed direct when its parent fails --- src/udpsockmap.c | 11 ++-- tests/cases/parent_failover.py | 97 ++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/src/udpsockmap.c b/src/udpsockmap.c index 8e60f62..b0b7170 100644 --- a/src/udpsockmap.c +++ b/src/udpsockmap.c @@ -162,8 +162,10 @@ static void udpreset(struct clientparam *param) /* * Authorize param->req and (re)build the server side of the association: * a parent proxy chain and/or an external address may be selected by the ACL. - * Returns the authorization result. param->remsock is INVALID_SOCKET if the - * socket can not be created, in this case the association can not continue. + * Returns the authorization result. param->remsock is INVALID_SOCKET when + * the authorization refused the association or the socket can not be + * created; the association can not continue then, so that a parent the + * rules demand is never quietly replaced by a direct relay. */ static int udpreconnect(struct clientparam *param) { @@ -189,9 +191,10 @@ static int udpreconnect(struct clientparam *param) param->remsock = INVALID_SOCKET; } udpreset(param); + return res; } - if (udpbind(param)) return res? res : 11; - return res; + if (udpbind(param)) return 11; + return 0; } int udpsockmap(struct clientparam *param, int timeo) diff --git a/tests/cases/parent_failover.py b/tests/cases/parent_failover.py index cabfce7..fa76884 100644 --- a/tests/cases/parent_failover.py +++ b/tests/cases/parent_failover.py @@ -9,9 +9,22 @@ The number of attempts is bounded by parentretries, two by default, so each case here needs at most one parent to fail before a working one is reached. """ +import socket +import struct import time +def recvall(sock, count): + """Bytes from a socket, fewer only when the peer stops sending.""" + data = b"" + while len(data) < count: + chunk = sock.recv(count - len(data)) + if not chunk: + break + data += chunk + return data + + def served(server, needle, since=0): """How many log lines carrying needle a proxy wrote past offset since.""" return sum(1 for line in server.output()[since:].splitlines() if needle in line) @@ -152,6 +165,90 @@ def run(t): t.eq(1, wait_served(goodp, needle, 1, gmark), "through the parent it names") + # --- UDP: a parent which is down is not replaced by a direct relay ------- + # The association itself is allowed without its destination, but the + # datagram carrying one has to build the chain its rule demands. With + # the only parent gone the association has to fail: relaying the next + # datagram from the proxy's own address would be a way around the rule. + sudp = t.free_port() + t.start("failover_udp", f""" + log + auth iponly + allow * + parent 1000 socks5 127.0.0.1 {dead} + socks -p{sudp} + """, ports=[sudp]) + + echo1 = t.udp_echo() + echo2 = t.udp_echo() + + ctrl = socket.create_connection(("127.0.0.1", sudp), t.timeout) + ctrl.settimeout(t.timeout) + udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + udp.settimeout(1.0) + try: + ctrl.sendall(b"\x05\x01\x00") + greeted = recvall(ctrl, 2) == b"\x05\x00" + ctrl.sendall(b"\x05\x03\x00\x01\x00\x00\x00\x00\x00\x00") + reply = recvall(ctrl, 4) + bound = None + if greeted and len(reply) >= 4 and reply[1] == 0 and reply[3] == 1: + addr = recvall(ctrl, 6) + if len(addr) == 6: + bound = struct.unpack("!H", addr[4:6])[0] + + def datagram(port, payload): + header = (b"\x00\x00\x00\x01" + socket.inet_aton("127.0.0.1") + + struct.pack("!H", port)) + udp.sendto(header + payload, ("127.0.0.1", bound)) + + def silence(port, payload): + """Whether nothing came back for the datagram sent.""" + datagram(port, payload) + try: + udp.recvfrom(65536) + return False + except OSError: + return True + + if bound is None: + t.fail("the association is granted", expected="a relay port", + actual=None) + else: + t.eq(True, silence(echo1, b"first"), + "the datagram to the dead parent is not delivered") + try: + closed = ctrl.recv(1) == b"" + except OSError: + closed = False + t.eq(True, closed, + "the association does not survive its failed parent") + t.eq(True, silence(echo2, b"second"), + "no datagram after it is relayed direct") + finally: + ctrl.close() + udp.close() + + # a parent which carries UDP is used for UDP: the failover above is the + # parent's failure, not the relay's refusal + suparent = t.free_port() + sup = t.free_port() + t.start("failover_udp_parent", f""" + log + auth iponly + allow * + socks -p{suparent} + """, ports=[suparent]) + t.start("failover_udp_up", f""" + log + auth iponly + allow * + parent 1000 socks5 127.0.0.1 {suparent} + socks -p{sup} + """, ports=[sup]) + reply, _ = t.socks_udp(f"127.0.0.1:{sup}", "127.0.0.1", echo1, b"udp") + t.eq(b"echo:udp", reply, "a datagram is carried by the working parent") + # --- what the parser still rejects -------------------------------------- out = t.run_config("failover_badweight", f""" auth iponly