Skip to content

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

fix(windows): UDP flow misrouting after hours and per-packet lookup cost (Discord voice, slowdown)

AnoF-Cyber365d62247519
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 5be6540..31bb3ce 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -106,6 +106,24 @@ static BOOL is_proxy_server_endpoint(UINT32 dst_ip, UINT16 dst_port)
return FALSE;
}
+// UDP counterpart: the datagrams between the relay and the proxy's UDP relay (SOCKS5 UDP
+// ASSOCIATE) in either direction. The relay's send socket draws its local port from the same
+// pool as the apps, so without this a port collision with a tracked app flow redirects the
+// relay's own packets back into the relay and breaks all proxied UDP until restart.
+static BOOL is_proxy_udp_endpoint(UINT32 ip, UINT16 port_host)
+{
+ int n = g_proxy_config_count;
+ if (n > MAX_PROXY_CONFIGS) n = MAX_PROXY_CONFIGS;
+ for (int i = 0; i < n; i++)
+ {
+ const PROXY_CONFIG *c = &g_proxy_configs[i];
+ if (c->config_id != 0 && c->udp_connected &&
+ c->udp_relay_addr.sin_addr.s_addr == ip && ntohs(c->udp_relay_addr.sin_port) == port_host)
+ return TRUE;
+ }
+ return FALSE;
+}
+
static void reset_stale_tcp_state(UINT16 sp, BOOL is_ipv6)
{
if (sp == g_local_relay_port) return; // never our own relay's port
@@ -185,7 +203,7 @@ DWORD WINAPI packet_processor(LPVOID arg)
goto ipv6u_send;
}
- if (is_connection_tracked(sp, TRUE, TRUE))
+ if (udp_flow_validate(sp, TRUE, 0, (const UINT8*)ipv6_header->DstAddr, dp))
{
udp_header->DstPort = htons(LOCAL_UDP_RELAY_PORT);
static const UINT8 _lb6u2[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
@@ -213,10 +231,18 @@ DWORD WINAPI packet_processor(LPVOID arg)
continue;
}
+ if (udp_direct_cached(TRUE, ipv6_header->DstAddr, sp, dp))
+ {
+ WinDivertSend(windivert_handle, packet, packet_len, NULL, &addr);
+ continue;
+ }
+
RuleAction action6u;
DWORD pid6u = 0;
UINT32 pcid6u = 0;
action6u = check_process_rule_v6((const UINT8*)ipv6_header->SrcAddr, sp, (const UINT8*)ipv6_header->DstAddr, dp, TRUE, &pid6u, &pcid6u);
+ if (action6u == RULE_ACTION_DIRECT && pid6u > 0)
+ udp_direct_remember(TRUE, ipv6_header->DstAddr, sp, dp);
if (action6u == RULE_ACTION_PROXY && !g_localhost_via_proxy)
{
@@ -541,7 +567,14 @@ DWORD WINAPI packet_processor(LPVOID arg)
continue;
}
}
- else if (is_connection_tracked(ntohs(udp_header->SrcPort), TRUE, FALSE))
+ else if (is_proxy_udp_endpoint(ip_header->DstAddr, ntohs(udp_header->DstPort)) ||
+ is_proxy_udp_endpoint(ip_header->SrcAddr, ntohs(udp_header->SrcPort)))
+ {
+ // relay <-> proxy UDP relay traffic: never redirect or evaluate
+ WinDivertSend(windivert_handle, packet, packet_len, NULL, &addr);
+ continue;
+ }
+ else if (udp_flow_validate(ntohs(udp_header->SrcPort), FALSE, ip_header->DstAddr, NULL, ntohs(udp_header->DstPort)))
{
UINT16 src_port = ntohs(udp_header->SrcPort);
udp_header->DstPort = htons(LOCAL_UDP_RELAY_PORT);
@@ -575,11 +608,20 @@ DWORD WINAPI packet_processor(LPVOID arg)
continue;
}
+ // Known DIRECT flow (decided within the last few seconds): skip the lookup.
+ if (udp_direct_cached(FALSE, &dest_ip, src_port, dest_port))
+ {
+ WinDivertSend(windivert_handle, packet, packet_len, NULL, &addr);
+ continue;
+ }
+
RuleAction action;
DWORD pid = 0;
UINT32 proxy_config_id = 0;
action = check_process_rule(src_ip, src_port, dest_ip, dest_port, TRUE, &pid, &proxy_config_id);
+ if (action == RULE_ACTION_DIRECT && pid > 0)
+ udp_direct_remember(FALSE, &dest_ip, src_port, dest_port);
// override PROXY to DIRECT if localhost proxy is disabled and destination is localhost
BYTE dest_first_octet = (dest_ip >> 0) & 0xFF;
@@ -1001,6 +1043,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_Start(void)
InitializeSRWLock(&lock);
dns_cache_init();
+ udp_direct_reset();
// If domain rules were configured before start, flush the OS DNS cache so the very
// first connections re-resolve on the wire and populate our IP->hostname snoop cache.
diff --git a/Windows/src/pb_conntrack.c b/Windows/src/pb_conntrack.c
index 2b7f7f6..8327a1c 100644
--- a/Windows/src/pb_conntrack.c
+++ b/Windows/src/pb_conntrack.c
@@ -175,6 +175,85 @@ BOOL find_v6_udp_sender(const UINT8 orig_dest_ip6[16], UINT16 orig_dest_port, UI
return found;
}
+// ---- UDP flow validation + direct-decision cache (see pb_internal.h) ----
+BOOL udp_flow_validate(UINT16 src_port, BOOL is_ipv6, UINT32 dest_ip, const UINT8 dest_ip6[16], UINT16 dest_port)
+{
+ ULONGLONG now = GetTickCount64();
+ int hash = src_port % CONNECTION_HASH_SIZE;
+ BOOL need_write = FALSE, valid = FALSE;
+
+ AcquireSRWLockShared(&lock);
+ CONNECTION_INFO *c = connection_hash_table[hash];
+ while (c != NULL && !(c->src_port == src_port && c->is_udp && c->is_ipv6 == is_ipv6)) c = c->next;
+ if (c == NULL) { ReleaseSRWLockShared(&lock); return FALSE; }
+ BOOL same = (c->orig_dest_port == dest_port) &&
+ (is_ipv6 ? memcmp(c->orig_dest_ip6, dest_ip6, 16) == 0 : c->orig_dest_ip == dest_ip);
+ if (c->last_activity < now && now - c->last_activity > UDP_REEVAL_IDLE_MS) need_write = TRUE; // evict (guard: another thread may have stamped a later tick)
+ else if (same) { InterlockedExchange64((LONGLONG volatile*)&c->last_activity, (LONGLONG)now); valid = TRUE; }
+ else need_write = TRUE; // retarget
+ ReleaseSRWLockShared(&lock);
+ if (!need_write) return valid;
+
+ AcquireSRWLockExclusive(&lock);
+ CONNECTION_INFO **pp = &connection_hash_table[hash];
+ while (*pp != NULL && !((*pp)->src_port == src_port && (*pp)->is_udp && (*pp)->is_ipv6 == is_ipv6)) pp = &(*pp)->next;
+ c = *pp;
+ if (c == NULL) { ReleaseSRWLockExclusive(&lock); return FALSE; }
+ if (c->last_activity < now && now - c->last_activity > UDP_REEVAL_IDLE_MS)
+ {
+ *pp = c->next;
+ rev_unlink(c);
+ free(c);
+ valid = FALSE;
+ }
+ else
+ {
+ rev_unlink(c); // dest changes: re-key the reverse index
+ c->orig_dest_port = dest_port;
+ if (is_ipv6) memcpy(c->orig_dest_ip6, dest_ip6, 16); else c->orig_dest_ip = dest_ip;
+ c->last_activity = now;
+ rev_insert(c);
+ valid = TRUE;
+ }
+ ReleaseSRWLockExclusive(&lock);
+ return valid;
+}
+
+#define UDP_DIRECT_SLOTS 4096
+typedef struct { UINT8 dst[16]; UINT16 sp, dp; BOOL v6; DWORD until; } UDP_DIRECT_ENTRY;
+static UDP_DIRECT_ENTRY g_udp_direct[UDP_DIRECT_SLOTS];
+
+static UDP_DIRECT_ENTRY *udp_direct_slot(BOOL v6, const void *dst, UINT16 sp, UINT16 dp, UINT8 key[16])
+{
+ memset(key, 0, 16);
+ memcpy(key, dst, v6 ? 16 : 4);
+ UINT32 h = ((UINT32)sp << 16) ^ dp ^ (v6 ? 0x9E3779B9u : 0);
+ for (int i = 0; i < 16; i++) h = h * 31u + key[i];
+ return &g_udp_direct[h % UDP_DIRECT_SLOTS];
+}
+
+BOOL udp_direct_cached(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port)
+{
+ UINT8 key[16];
+ UDP_DIRECT_ENTRY *e = udp_direct_slot(is_ipv6, dst, src_port, dst_port, key);
+ return e->until != 0 && e->sp == src_port && e->dp == dst_port && e->v6 == is_ipv6 &&
+ memcmp(e->dst, key, 16) == 0 && (int)(e->until - GetTickCount()) > 0;
+}
+
+void udp_direct_remember(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port)
+{
+ UINT8 key[16];
+ UDP_DIRECT_ENTRY *e = udp_direct_slot(is_ipv6, dst, src_port, dst_port, key);
+ memcpy(e->dst, key, 16); e->sp = src_port; e->dp = dst_port; e->v6 = is_ipv6;
+ DWORD u = GetTickCount() + UDP_DIRECT_TTL_MS;
+ e->until = u ? u : 1;
+}
+
+void udp_direct_reset(void)
+{
+ memset(g_udp_direct, 0, sizeof(g_udp_direct));
+}
+
BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp, BOOL is_ipv6)
{
BOOL tracked = FALSE;
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index 06806a7..d312179 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -150,7 +150,7 @@ typedef struct PID_CACHE_ENTRY {
UINT32 src_ip;
UINT16 src_port;
DWORD pid;
- DWORD timestamp;
+ ULONGLONG timestamp; // GetTickCount64() - a 32-bit field breaks the TTL math after ~49.7 days of uptime
BOOL is_udp;
struct PID_CACHE_ENTRY *next;
} PID_CACHE_ENTRY;
@@ -331,6 +331,24 @@ void add_connection(UINT16 src_port, BOOL is_udp, UINT32 src_ip, UINT32 dest_ip,
void add_connection_v6(UINT16 src_port, BOOL is_udp, const UINT8 src_ip6[16], const UINT8 dest_ip6[16], UINT16 dest_port, UINT32 proxy_config_id);
BOOL get_connection_full_v6(UINT16 src_port, BOOL is_udp, UINT8 dest_ip6[16], UINT16 *dest_port, UINT32 *proxy_config_id);
BOOL find_v6_udp_sender(const UINT8 orig_dest_ip6[16], UINT16 orig_dest_port, UINT8 src_ip6[16], UINT16 *src_port);
+// UDP flow validation for the app->relay redirect. A UDP "connection" is keyed by source port
+// only, so it must be re-checked on every datagram: (a) if the destination changed (a socket
+// that moves to another server, e.g. a voice call migrating) the entry is retargeted, and
+// (b) if the flow has been idle longer than UDP_REEVAL_IDLE_MS the source port may belong to a
+// different socket now, so the entry is dropped and the caller re-evaluates the rules.
+// Returns TRUE if a valid entry exists (and was refreshed), FALSE if not tracked / evicted.
+#define UDP_REEVAL_IDLE_MS 20000
+BOOL udp_flow_validate(UINT16 src_port, BOOL is_ipv6, UINT32 dest_ip, const UINT8 dest_ip6[16], UINT16 dest_port);
+
+// Short-lived cache of DIRECT decisions for UDP, keyed by (src port, destination). UDP has no
+// SYN to decide once per connection, so every datagram of a direct flow used to repeat the
+// full process/rule lookup (OpenProcess per packet) on the single capture thread.
+// Capture-thread only (NUM_PACKET_THREADS == 1), so no locking.
+#define UDP_DIRECT_TTL_MS 5000
+BOOL udp_direct_cached(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port);
+void udp_direct_remember(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port);
+void udp_direct_reset(void);
+
BOOL is_connection_tracked(UINT16 src_port, BOOL is_udp, BOOL is_ipv6);
BOOL get_connection(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_port);
BOOL get_connection_full(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_port, UINT32 *proxy_config_id);