Skip to content

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

updated UDP handling by removing timeouts and enabling TCP keepalives to maintain control connections, ensuring real-time packet delivery during UDP associations.

AnoF-Cyber74670839877a
diff --git a/Linux/src/ProxyBridge.c b/Linux/src/ProxyBridge.c
index aae37a9..b9bc9c4 100644
--- a/Linux/src/ProxyBridge.c
+++ b/Linux/src/ProxyBridge.c
@@ -1558,6 +1558,22 @@ static bool establish_udp_associate(void)
if (socks5_udp_relay_addr.sin_addr.s_addr == INADDR_ANY)
socks5_udp_relay_addr.sin_addr.s_addr = socks5_ip;
+ // Handshake done - remove the 3s timeout so the control socket stays open indefinitely.
+ struct timeval zero_tv = {0, 0};
+ setsockopt(tcp_sock, SOL_SOCKET, SO_RCVTIMEO, &zero_tv, sizeof(zero_tv));
+ setsockopt(tcp_sock, SOL_SOCKET, SO_SNDTIMEO, &zero_tv, sizeof(zero_tv));
+
+ // Enable TCP keepalives so the SOCKS5 proxy doesn't idle-close the control
+ // connection (many proxies terminate it after ~60s of silence, killing UDP ASSOCIATE).
+ int ka_on = 1;
+ int ka_idle = 10; // start keepalives after 10s idle
+ int ka_intvl = 2; // send keepalive every 2s
+ int ka_cnt = 5; // drop after 5 missed keepalives
+ setsockopt(tcp_sock, SOL_SOCKET, SO_KEEPALIVE, &ka_on, sizeof(ka_on));
+ setsockopt(tcp_sock, SOL_TCP, TCP_KEEPIDLE, &ka_idle, sizeof(ka_idle));
+ setsockopt(tcp_sock, SOL_TCP, TCP_KEEPINTVL, &ka_intvl, sizeof(ka_intvl));
+ setsockopt(tcp_sock, SOL_TCP, TCP_KEEPCNT, &ka_cnt, sizeof(ka_cnt));
+
socks5_udp_control_socket = tcp_sock;
socks5_udp_send_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
@@ -1645,7 +1661,12 @@ static void* udp_relay_server(void *arg)
int ready = poll(fds, nfds, 1000); // 1s timeout
if (ready <= 0)
+ {
+ // Proactively reconnect so the next client packet is not dropped.
+ if (!udp_associate_connected)
+ udp_associate_connected = establish_udp_associate();
continue;
+ }
// check if tcp control still alive
// if it dies the udp associate is dead too
@@ -1655,8 +1676,10 @@ static void* udp_relay_server(void *arg)
ssize_t peek_len = recv(socks5_udp_control_socket, peek_buf, 1, MSG_PEEK | MSG_DONTWAIT);
if (peek_len == 0 || (peek_len < 0 && errno != EAGAIN && errno != EWOULDBLOCK))
{
- // tcp died, proxy is gone
+ // tcp died — tear down and reconnect immediately so real-time streams
+ // lose as few packets as possible.
teardown_udp_associate();
+ udp_associate_connected = establish_udp_associate();
continue;
}
}
@@ -1701,10 +1724,26 @@ static void* udp_relay_server(void *arg)
ssize_t sent = sendto(socks5_udp_send_socket, send_buf, 10 + recv_len, 0,
(struct sockaddr *)&socks5_udp_relay_addr, sizeof(socks5_udp_relay_addr));
- // if send fails proxy died, teardown and retry later
if (sent < 0)
{
+ // sendto failed — proxy likely died. Tear down, reconnect immediately
+ // and retry the current packet so real-time streams lose at most one packet.
teardown_udp_associate();
+ if (establish_udp_associate())
+ {
+ udp_associate_connected = true;
+ sendto(socks5_udp_send_socket, send_buf, (size_t)(10 + recv_len), 0,
+ (struct sockaddr *)&socks5_udp_relay_addr, sizeof(socks5_udp_relay_addr));
+ }
+ }
+ else if (socks5_udp_send_socket >= 0)
+ {
+ // After sending (especially on a freshly established association), the proxy
+ // may respond within the same poll() cycle before the socket was in fds[].
+ // Do a non-blocking check now so we don't add an extra poll() round-trip.
+ struct pollfd quick = { socks5_udp_send_socket, POLLIN, 0 };
+ if (poll(&quick, 1, 0) > 0)
+ fds[1].revents |= POLLIN;
}
}
@@ -1759,6 +1798,8 @@ static void* udp_relay_server(void *arg)
client_addr.sin_family = AF_INET;
client_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
client_addr.sin_port = htons(conn->src_port);
+ // Keep session alive while proxy delivers data (benign relaxed write under read lock)
+ conn->last_activity = get_monotonic_ms();
found_client = true;
break;
}
@@ -2129,7 +2170,7 @@ static void cleanup_stale_connections(void)
while (*conn_ptr != NULL)
{
- if (now - (*conn_ptr)->last_activity > 60000) // 60 sec timeout
+ if (now - (*conn_ptr)->last_activity > 120000) // 120 sec timeout
{
CONNECTION_INFO *to_free = *conn_ptr;
*conn_ptr = (*conn_ptr)->next;
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 063efb8..b8184c1 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -439,15 +439,12 @@ static DWORD WINAPI packet_processor(LPVOID arg)
memcpy(ipv6_header->SrcAddr, orig_dst6, 16);
udp_header->SrcPort = htons(orig_dp);
}
- static const UINT8 _lb6u[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
- if (memcmp(ipv6_header->DstAddr, _lb6u, 16) != 0)
- {
- UINT32 tmp[4];
- memcpy(tmp, ipv6_header->DstAddr, 16);
- memcpy(ipv6_header->DstAddr, ipv6_header->SrcAddr, 16);
- memcpy(ipv6_header->SrcAddr, tmp, 16);
+ // ::1 loopback: keep OUTBOUND so the loopback adapter echo
+ // delivers reliably (same reasoning as IPv4 path below).
+ // Non-loopback: inject INBOUND.
+ static const UINT8 _lb6r[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
+ if (memcmp(ipv6_header->DstAddr, _lb6r, 16) != 0)
addr.Outbound = FALSE;
- }
goto ipv6u_send;
}
@@ -766,12 +763,15 @@ static DWORD WINAPI packet_processor(LPVOID arg)
ip_header->SrcAddr = orig_dest_ip;
udp_header->SrcPort = htons(orig_dest_port);
+ // loopback need outbound injection inbound dont work on windows loopback
+ // bcz fast path skip the recive layer we inject into
+ // outbound makes loopback echo it back as inbound which actualy reach socket
+ // impostor flag stops it getting recaptured again
+ // for real nic inbound injection works fine no extra hop
BYTE dst_first_octet = (ntohl(ip_header->DstAddr) >> 24) & 0xFF;
- BOOL dst_is_loopback = (dst_first_octet == 127);
- if (!dst_is_loopback)
- {
+ if (dst_first_octet != 127)
addr.Outbound = FALSE;
- }
+ // else: stay OUTBOUND — loopback echo delivers the packet
}
else
{
@@ -2817,6 +2817,20 @@ static BOOL establish_udp_associate_for_config(PROXY_CONFIG *cfg)
if (cfg->udp_relay_addr.sin_addr.s_addr == INADDR_ANY)
cfg->udp_relay_addr.sin_addr.s_addr = socks5_ip;
+ // haandshake completed remove the 3second timeout so the control socket stays open indefinitely
+ // keepalives below will detect actual disconnection.
+ DWORD zero_timeout = 0;
+ setsockopt(tcp_sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&zero_timeout, sizeof(zero_timeout));
+ setsockopt(tcp_sock, SOL_SOCKET, SO_SNDTIMEO, (const char*)&zero_timeout, sizeof(zero_timeout));
+
+ // we can enable TCP keepalives so the SOCKS5 proxy dont idleclose the control
+ // connection (few proxies terminate it after 60 second of silence, killing UDP ASSOCIATE).
+ BOOL ka_on = TRUE;
+ setsockopt(tcp_sock, SOL_SOCKET, SO_KEEPALIVE, (const char*)&ka_on, sizeof(ka_on));
+ struct tcp_keepalive ka = { 1, 10000, 2000 }; // idle 10s, retry every 2s
+ DWORD ka_bytes;
+ WSAIoctl(tcp_sock, SIO_KEEPALIVE_VALS, &ka, sizeof(ka), NULL, 0, &ka_bytes, NULL, NULL);
+
cfg->udp_tcp_ctrl = tcp_sock;
// create UDP socket for sending to SOCKS5 proxy
@@ -2926,7 +2940,19 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
struct timeval timeout = {1, 0};
if (select(0, &read_fds, NULL, NULL, &timeout) <= 0)
+ {
+ // Select timed out proactively reconnect any dropped UDP ASSOCIATEs so
+ // the connection is ready before the next client packet arrives.
+ // Real time communication need real time packet transfer, a single UDP Associate connction can take 1 to 2 seconds and it break the UDP steam for client app
+ // fuck you udp this cause slight increase in performance but needed for udp
+ for (int i = 0; i < g_proxy_config_count; i++)
+ {
+ PROXY_CONFIG *rc = &g_proxy_configs[i];
+ if (rc->type == PROXY_TYPE_SOCKS5 && !rc->udp_connected)
+ establish_udp_associate_for_config(rc);
+ }
continue;
+ }
// Check if any SOCKS5 proxy TCP control socket disconnected
for (int i = 0; i < g_proxy_config_count; i++)
@@ -2939,7 +2965,7 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
int result = recv(cfg->udp_tcp_ctrl, test_buf, sizeof(test_buf), MSG_PEEK);
if (result == 0 || (result == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK))
{
- log_message("[UDP RELAY] TCP control connection closed for proxy %s:%d", cfg->host, cfg->port);
+ log_message("[UDP RELAY] TCP control connection closed for proxy %s:%d — reconnecting", cfg->host, cfg->port);
closesocket(cfg->udp_tcp_ctrl);
cfg->udp_tcp_ctrl = INVALID_SOCKET;
if (cfg->udp_send_sock != INVALID_SOCKET)
@@ -2948,6 +2974,8 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
cfg->udp_send_sock = INVALID_SOCKET;
}
cfg->udp_connected = FALSE;
+ // Reconnect immediately so the next client packet is not dropped.
+ establish_udp_associate_for_config(cfg);
}
}
}
@@ -2978,7 +3006,6 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
if (get_connection(from_port, &dest_ip, &dest_port))
{
- // Get the proxy config for this connection
UINT32 proxy_config_id = get_connection_proxy_id(from_port);
PROXY_CONFIG *cfg = find_proxy_config(proxy_config_id);
@@ -2988,12 +3015,14 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
continue;
}
- // Ensure UDP ASSOCIATE is established (retry if needed)
+ // UDP ASSOCIATE is established (reconnect if dropped).
+ // If reconnect succeeds, fall through and send the current packet
+ // immediately so real-time streams lose at most one packet.
if (!cfg->udp_connected)
{
if (!establish_udp_associate_for_config(cfg))
{
- log_message("[UDP RELAY] Cannot send - UDP ASSOCIATE not established for %s:%d", cfg->host, cfg->port);
+ log_message("[UDP RELAY] UDP ASSOCIATE unavailable for %s:%d — dropping packet", cfg->host, cfg->port);
continue;
}
}
@@ -3015,10 +3044,17 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
if (sent == SOCKET_ERROR) {
int err = WSAGetLastError();
- log_message("[UDP RELAY ERROR] Failed to send to SOCKS5 proxy %s:%d: %d - closing", cfg->host, cfg->port, err);
+ log_message("[UDP RELAY ERROR] sendto proxy %s:%d failed: %d — reconnecting and retrying", cfg->host, cfg->port, err);
if (cfg->udp_tcp_ctrl != INVALID_SOCKET) { closesocket(cfg->udp_tcp_ctrl); cfg->udp_tcp_ctrl = INVALID_SOCKET; }
if (cfg->udp_send_sock != INVALID_SOCKET) { closesocket(cfg->udp_send_sock); cfg->udp_send_sock = INVALID_SOCKET; }
cfg->udp_connected = FALSE;
+ // Reconnect and retry the current packet so real-time streams
+ // lose at most one packet during a proxy reconnect event.
+ if (establish_udp_associate_for_config(cfg))
+ {
+ sendto(cfg->udp_send_sock, (char*)send_buf, 10 + recv_len, 0,
+ (struct sockaddr *)&cfg->udp_relay_addr, sizeof(cfg->udp_relay_addr));
+ }
}
}
}
@@ -3029,7 +3065,18 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
{
PROXY_CONFIG *cfg = &g_proxy_configs[i];
if (cfg->type != PROXY_TYPE_SOCKS5 || !cfg->udp_connected) continue;
- if (cfg->udp_send_sock == INVALID_SOCKET || !FD_ISSET(cfg->udp_send_sock, &read_fds)) continue;
+ if (cfg->udp_send_sock == INVALID_SOCKET) continue;
+ // If not signalled by the outer select, do a zero-timeout check for
+ // sockets that were created this iteration (e.g. just after reconnect).
+ if (!FD_ISSET(cfg->udp_send_sock, &read_fds))
+ {
+ fd_set quick;
+ FD_ZERO(&quick);
+ FD_SET(cfg->udp_send_sock, &quick);
+ struct timeval zero_tv = {0, 0};
+ if (select(0, &quick, NULL, NULL, &zero_tv) <= 0 || !FD_ISSET(cfg->udp_send_sock, &quick))
+ continue;
+ }
from_len = sizeof(from_addr);
recv_len = recvfrom(cfg->udp_send_sock, (char*)recv_buf, sizeof(recv_buf), 0,
@@ -3062,6 +3109,7 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
BOOL found = FALSE;
UINT32 target_ip = 0;
UINT16 target_port = 0;
+ CONNECTION_INFO *winner_conn = NULL;
AcquireSRWLockShared(&lock);
ULONGLONG best_activity = 0;
@@ -3074,15 +3122,24 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
{
if (!found || conn->last_activity > best_activity)
{
- target_ip = conn->src_ip;
- target_port = conn->src_port;
+ target_ip = conn->src_ip;
+ target_port = conn->src_port;
best_activity = conn->last_activity;
- found = TRUE;
+ found = TRUE;
+ winner_conn = conn;
+ // Do NOT update last_activity here; doing so mid-loop
+ // corrupts best_activity comparisons for later entries,
+ // causing split delivery when multiple clients share the
+ // same destination. Update after the loop completes.
}
}
conn = conn->next;
}
}
+ // Keep winner's session alive (update outside loop so comparisons above
+ // use the original, unmodified timestamps for all candidates).
+ if (winner_conn != NULL)
+ InterlockedExchange64((LONGLONG volatile*)&winner_conn->last_activity, (LONGLONG)GetTickCount64());
ReleaseSRWLockShared(&lock);
if (found)
@@ -3092,12 +3149,15 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)
target_addr.sin_family = AF_INET;
target_addr.sin_addr.s_addr = target_ip;
target_addr.sin_port = htons(target_port);
- sendto(udp_relay_socket, (char*)&recv_buf[10], recv_len-10, 0,
+ int fwd = sendto(udp_relay_socket, (char*)&recv_buf[10], recv_len-10, 0,
(struct sockaddr*)&target_addr, sizeof(target_addr));
+ if (fwd == SOCKET_ERROR)
+ log_message("[UDP RELAY] sendto client port %d failed: %d", target_port, WSAGetLastError());
}
else
{
- log_message("[UDP RELAY] No session found for proxy response from port %d — dropped", src_port);
+ log_message("[UDP RELAY] No session found for proxy response from %d.%d.%d.%d:%d — dropped",
+ recv_buf[4], recv_buf[5], recv_buf[6], recv_buf[7], src_port);
}
}
else if (recv_buf[3] == SOCKS5_ATYP_IPV6 && recv_len >= 22)
@@ -3598,6 +3658,7 @@ static void add_connection(UINT16 src_port, UINT32 src_ip, UINT32 dest_ip, UINT1
// check if already exists in this hash bucket
while (existing != NULL) {
if (existing->src_port == src_port) {
+ existing->is_ipv6 = FALSE;
existing->src_ip = src_ip;
existing->orig_dest_ip = dest_ip;
existing->orig_dest_port = dest_port;
@@ -3610,7 +3671,7 @@ static void add_connection(UINT16 src_port, UINT32 src_ip, UINT32 dest_ip, UINT1
existing = existing->next;
}
- CONNECTION_INFO *conn = (CONNECTION_INFO *)malloc(sizeof(CONNECTION_INFO));
+ CONNECTION_INFO *conn = (CONNECTION_INFO *)calloc(1, sizeof(CONNECTION_INFO));
if (conn == NULL) {
ReleaseSRWLockExclusive(&lock);
return;
@@ -3622,6 +3683,7 @@ static void add_connection(UINT16 src_port, UINT32 src_ip, UINT32 dest_ip, UINT1
conn->orig_dest_port = dest_port;
conn->proxy_config_id = proxy_config_id;
conn->is_tracked = TRUE;
+ conn->is_ipv6 = FALSE;
conn->last_activity = GetTickCount64();
conn->next = connection_hash_table[hash];
@@ -3845,7 +3907,7 @@ static void cleanup_stale_connections(void)
while (*conn_ptr != NULL)
{
- if (now - (*conn_ptr)->last_activity > 30000)
+ if (now - (*conn_ptr)->last_activity > 120000)
{
CONNECTION_INFO *to_free = *conn_ptr;
*conn_ptr = (*conn_ptr)->next;
@@ -4643,13 +4705,19 @@ PROXYBRIDGE_API BOOL ProxyBridge_Start(void)
Sleep(500);
+ // "not impostor" ensures WinDivert never re-captures packets it already injected.
+ // Without this, each WinDivertSend re-enters the capture queue, creating
+ // re-injection loops that delay delivery by seconds and cause DTLS handshake
+ // failures. With "not impostor", injected packets bypass the driver entirely
+ // and flow directly to the OS — zero extra hops, no loops.
snprintf(filter, sizeof(filter),
+ "not impostor and ("
"(tcp and (outbound or loopback or (tcp.DstPort == %d or tcp.SrcPort == %d))) or "
"(udp and (outbound or loopback or (udp.DstPort == %d or udp.SrcPort == %d))) or "
"(udp and not outbound and udp.SrcPort == 53) or "
"(ipv6 and udp and not outbound and udp.SrcPort == 53) or "
"(ipv6 and tcp and (outbound or loopback or (tcp.DstPort == %d or tcp.SrcPort == %d))) or "
- "(ipv6 and udp and (outbound or loopback or (udp.DstPort == %d or udp.SrcPort == %d)))",
+ "(ipv6 and udp and (outbound or loopback or (udp.DstPort == %d or udp.SrcPort == %d))))",
g_local_relay_port, g_local_relay_port, LOCAL_UDP_RELAY_PORT, LOCAL_UDP_RELAY_PORT,
g_local_relay_port, g_local_relay_port, LOCAL_UDP_RELAY_PORT, LOCAL_UDP_RELAY_PORT);