Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
Implement code changes to enhance functionality and improve performance
AnoF-Cyberc03f79d7e5d1
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.cindex 2635376..7e3e46b 100644--- a/Windows/src/ProxyBridge.c+++ b/Windows/src/ProxyBridge.c@@ -161,9 +161,16 @@ static UINT32 g_next_config_id = 1;static CONNECTION_INFO *connection_hash_table[CONNECTION_HASH_SIZE] = {NULL};static LOGGED_CONNECTION *logged_connections = NULL;+static int g_logged_count = 0; // running length of logged_connections (guarded by `lock`)static PROCESS_RULE *rules_list = NULL;static UINT32 g_next_rule_id = 1;static SRWLOCK lock;+// Fix added via Claude - The eror due to lack of gaurd case is causing leak in few cases, unwated different thread accessing the same data structure, so added a lock to gaurd the connection hash table and pid cache+// Guards rules_list and the rule nodes/strings it points to. Separate from `lock`+// (which guards the connection table + PID cache) so rule edits from the GUI thread+// never block the packet path's connection bookkeeping. A zero-initialised SRWLOCK is+// already in the valid unlocked state, so this is safe to use before ProxyBridge_Start.+static SRWLOCK g_rules_lock;static HANDLE windivert_handle = INVALID_HANDLE_VALUE;static HANDLE packet_thread[NUM_PACKET_THREADS] = {NULL};static HANDLE proxy_thread = NULL;@@ -281,12 +288,28 @@ static BOOL parse_token_list(const char *list, const char *delimiters, token_matif (list == NULL || list[0] == '\0' || strcmp(list, "*") == 0)return TRUE;- size_t len = strnlen_s(list, MAX_LIST_SIZE) + 1;- char *list_copy = (char *)malloc(len);- if (list_copy == NULL)- return FALSE;+ // strtok_s needs a writable copy. Use a stack buffer for the common (short) case and+ // only fall back to malloc for unusually long lists — avoids a heap alloc on the+ // packet thread for every rule that has a specific host/port filter.+ char stackbuf[256];+ size_t len = strnlen_s(list, MAX_LIST_SIZE) + 1;+ size_t dstsz = len;+ char *list_copy;+ BOOL on_heap = FALSE;+ if (len <= sizeof(stackbuf))+ {+ list_copy = stackbuf;+ dstsz = sizeof(stackbuf);+ }+ else+ {+ list_copy = (char *)malloc(len);+ if (list_copy == NULL)+ return FALSE;+ on_heap = TRUE;+ }- strncpy_s(list_copy, len, list, _TRUNCATE);+ strncpy_s(list_copy, dstsz, list, _TRUNCATE);BOOL matched = FALSE;char *context = NULL;char *token = strtok_s(list_copy, delimiters, &context);@@ -300,7 +323,8 @@ static BOOL parse_token_list(const char *list, const char *delimiters, token_mat}token = strtok_s(NULL, delimiters, &context);}- free(list_copy);+ if (on_heap)+ free(list_copy);return matched;}@@ -314,6 +338,50 @@ static void configure_tcp_socket(SOCKET sock, int bufsize, DWORD timeout)setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));}+// connect() with a bounded timeout. A blocking connect() to an unreachable host stalls+// for the OS SYN timeout (~21s on Windows), and the UDP relay runs on a single thread —+// so one dead/unreachable proxy config would freeze the whole relay (and delay real+// packets) while it waits. This does a non-blocking connect + select so a dead proxy+// fails in `timeout_ms` instead. Returns 0 on success, SOCKET_ERROR otherwise.+static int connect_with_timeout(SOCKET s, const struct sockaddr *addr, int addrlen, int timeout_ms)+{+ u_long nonblock = 1;+ ioctlsocket(s, FIONBIO, &nonblock);++ int result = 0;+ if (connect(s, addr, addrlen) == SOCKET_ERROR)+ {+ if (WSAGetLastError() != WSAEWOULDBLOCK)+ {+ result = SOCKET_ERROR;+ }+ else+ {+ fd_set wfds, efds;+ FD_ZERO(&wfds); FD_SET(s, &wfds);+ FD_ZERO(&efds); FD_SET(s, &efds);+ struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 };+ int sel = select(0, NULL, &wfds, &efds, &tv);+ if (sel <= 0 || FD_ISSET(s, &efds))+ {+ result = SOCKET_ERROR; // timed out or connect failed+ }+ else+ {+ int so_err = 0;+ int errlen = sizeof(so_err);+ getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&so_err, &errlen);+ if (so_err != 0)+ result = SOCKET_ERROR;+ }+ }+ }++ u_long blocking = 0;+ ioctlsocket(s, FIONBIO, &blocking); // restore blocking for the handshake reads+ return result;+}+static void configure_udp_socket(SOCKET sock, int bufsize, DWORD timeout){setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&bufsize, sizeof(bufsize));@@ -342,6 +410,21 @@ static int send_all(SOCKET sock, const char *buf, int len)return sent;}+// Read exactly n bytes, looping over partial TCP segments. SOCKS5/HTTP replies can be+// split across multiple segments (common on high-latency remote proxies); a single+// recv() may return fewer bytes than requested, so the fixed-length handshake reads+// must accumulate. Returns n on success, or SOCKET_ERROR on error / peer close.+static int recv_n(SOCKET s, char *buf, int n)+{+ int got = 0;+ while (got < n) {+ int r = recv(s, buf + got, n - got, 0);+ if (r <= 0) return SOCKET_ERROR; // 0 = peer closed, <0 = error/timeout+ got += r;+ }+ return n;+}+static UINT32 parse_ipv4(const char *ip);static UINT32 resolve_hostname(const char *hostname);static PROXY_CONFIG* find_proxy_config(UINT32 config_id);@@ -397,7 +480,11 @@ static BOOL is_broadcast_or_multicast(UINT32 ip);static BOOL is_ipv6_multicast_or_linklocal(const UINT8 ip6[16]);static DWORD get_cached_pid(UINT32 src_ip, UINT16 src_port, BOOL is_udp);static void cache_pid(UINT32 src_ip, UINT16 src_port, DWORD pid, BOOL is_udp);+static void remove_cached_pid(UINT32 src_ip, UINT16 src_port, BOOL is_udp);static void clear_pid_cache(void);+static void cleanup_stale_pid_cache(void);+static void cleanup_stale_dns_cache(void);+static int recv_n(SOCKET s, char *buf, int n);static void update_has_active_rules(void);static void base64_encode(const char* input, char* output, size_t output_size);@@ -504,10 +591,17 @@ static DWORD WINAPI packet_processor(LPVOID arg)if (g_connection_callback != NULL && pid6u > 0){+ // Fold the 128-bit destination into a 32-bit key so the log-dedup+ // table distinguishes different IPv6 hosts. Passing 0 (as before)+ // collapsed every IPv6 destination on the same port/action into one+ // key, causing distinct hosts to be dropped as duplicates.+ const UINT32 *dw6 = (const UINT32 *)ipv6_header->DstAddr;+ UINT32 v6key = dw6[0] ^ dw6[1] ^ dw6[2] ^ dw6[3];+char pname[MAX_PROCESS_NAME];if (get_process_name_from_pid(pid6u, pname, sizeof(pname))){- if (!is_connection_already_logged(pid6u, 0, dp, action6u))+ if (!is_connection_already_logged(pid6u, v6key, dp, action6u)){char dstr[64];inet_ntop(AF_INET6, ipv6_header->DstAddr, dstr, sizeof(dstr));@@ -516,7 +610,7 @@ static DWORD WINAPI packet_processor(LPVOID arg)else if(action6u==RULE_ACTION_DIRECT) snprintf(pinfo,sizeof(pinfo),"Direct (UDP)");else snprintf(pinfo,sizeof(pinfo),"Blocked (UDP)");g_connection_callback(extract_filename(pname),pid6u,dstr,dp,pinfo);- if(g_traffic_logging_enabled) add_logged_connection(pid6u,0,dp,action6u);+ if(g_traffic_logging_enabled) add_logged_connection(pid6u,v6key,dp,action6u);}}}@@ -551,6 +645,13 @@ static DWORD WINAPI packet_processor(LPVOID arg)}goto ipv6u_send;}+ else+ {+ // DIRECT (or no matching rule): unmodified packet, send as-is without+ // recomputing checksums (wasteful + can clash with NIC offload, #161).+ WinDivertSend(windivert_handle, packet, packet_len, NULL, &addr);+ continue;+ }}else{@@ -674,10 +775,15 @@ static DWORD WINAPI packet_processor(LPVOID arg)if (g_connection_callback != NULL && tcp_header->Syn && !tcp_header->Ack && pid6 > 0){+ // Fold the 128-bit destination into a 32-bit dedup key so different+ // IPv6 hosts aren't collapsed into one entry (see IPv6 UDP path).+ const UINT32 *dw6 = (const UINT32 *)ipv6_header->DstAddr;+ UINT32 v6key = dw6[0] ^ dw6[1] ^ dw6[2] ^ dw6[3];+char process_name[MAX_PROCESS_NAME];if (get_process_name_from_pid(pid6, process_name, sizeof(process_name))){- if (!is_connection_already_logged(pid6, 0, dp, action6))+ if (!is_connection_already_logged(pid6, v6key, dp, action6)){char dest_ip_str[64];const UINT8 *d6 = (const UINT8*)ipv6_header->DstAddr;@@ -703,7 +809,7 @@ static DWORD WINAPI packet_processor(LPVOID arg)g_connection_callback(display_name, pid6, dest_ip_str, dp, proxy_info);if (g_traffic_logging_enabled)- add_logged_connection(pid6, 0, dp, action6);+ add_logged_connection(pid6, v6key, dp, action6);}}}@@ -918,6 +1024,14 @@ static DWORD WINAPI packet_processor(LPVOID arg)// for loopback we need keep as outbound (127.x.x.x -> 127.0.0.1)// for a fucking stupid reason i missed this part for 6 months}+ else+ {+ // DIRECT (or no matching rule): the packet is unmodified, so send it+ // as-is. Recomputing checksums on an untouched packet is wasteful and+ // can conflict with NIC checksum offload on some drivers (see #161).+ WinDivertSend(windivert_handle, packet, packet_len, NULL, &addr);+ continue;+ }}}else@@ -959,6 +1073,15 @@ static DWORD WINAPI packet_processor(LPVOID arg)// Part of this taken from Cluade to fix windivert packet error{UINT16 sp = ntohs(tcp_header->SrcPort);++ // A fresh SYN starts a new connection that may be reusing an ephemeral+ // port whose previous owner has closed. Evict any PID cached for this+ // port so the rule decision is re-derived against the correct current+ // process instead of a stale one (prevents wrong-app rule matching for+ // up to PID_CACHE_TTL_MS after a port is recycled).+ if (tcp_header->Syn && !tcp_header->Ack)+ remove_cached_pid(ip_header->SrcAddr, sp, FALSE);+if (port_is_decided(sp)){if (tcp_header->Fin || tcp_header->Rst)@@ -1218,6 +1341,31 @@ static UINT32 resolve_hostname(const char *hostname)return resolved_ip;}+// Reusable grow-only scratch buffer for the TCP/UDP owner-PID tables. These lookups run+// only on the single packet-processor thread (NUM_PACKET_THREADS == 1) and each call+// finishes scanning before the next one, so one shared buffer is safe and lets us skip a+// malloc/free of the (potentially tens-of-KB) table on every new connection. Freed in Stop.+static char *g_pidtbl_buf = NULL;+static DWORD g_pidtbl_cap = 0;++static void *pidtbl_reserve(DWORD need)+{+ if (need > g_pidtbl_cap)+ {+ DWORD newcap = g_pidtbl_cap ? g_pidtbl_cap : 16384;+ while (newcap < need)+ {+ if (newcap > (0xFFFFFFFFu / 2)) { newcap = need; break; }+ newcap *= 2;+ }+ char *nb = (char *)realloc(g_pidtbl_buf, newcap);+ if (nb == NULL) return NULL; // keep the old buffer intact on failure+ g_pidtbl_buf = nb;+ g_pidtbl_cap = newcap;+ }+ return g_pidtbl_buf;+}+static DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port){// check cache first@@ -1225,7 +1373,6 @@ static DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port)if (cached_pid != 0)return cached_pid;- MIB_TCPTABLE_OWNER_PID *tcp_table = NULL;DWORD size = 0;DWORD pid = 0;@@ -1235,7 +1382,7 @@ static DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port)return 0;}- tcp_table = (MIB_TCPTABLE_OWNER_PID *)malloc(size);+ MIB_TCPTABLE_OWNER_PID *tcp_table = (MIB_TCPTABLE_OWNER_PID *)pidtbl_reserve(size);if (tcp_table == NULL){return 0;@@ -1244,8 +1391,7 @@ static DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port)if (GetExtendedTcpTable(tcp_table, &size, FALSE, AF_INET,TCP_TABLE_OWNER_PID_ALL, 0) != NO_ERROR){- free(tcp_table);- return 0;+ return 0; // buffer is reused, not freed}for (DWORD i = 0; i < tcp_table->dwNumEntries; i++)@@ -1260,8 +1406,6 @@ static DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port)}}- free(tcp_table);-// store cache the resultif (pid != 0)cache_pid(src_ip, src_port, pid, FALSE);@@ -1276,7 +1420,6 @@ static DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port)if (cached_pid != 0)return cached_pid;- MIB_UDPTABLE_OWNER_PID *udp_table = NULL;DWORD size = 0;DWORD pid = 0;@@ -1286,7 +1429,7 @@ static DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port)return 0;}- udp_table = (MIB_UDPTABLE_OWNER_PID *)malloc(size);+ MIB_UDPTABLE_OWNER_PID *udp_table = (MIB_UDPTABLE_OWNER_PID *)pidtbl_reserve(size);if (udp_table == NULL){return 0;@@ -1295,8 +1438,7 @@ static DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port)if (GetExtendedUdpTable(udp_table, &size, FALSE, AF_INET,UDP_TABLE_OWNER_PID, 0) != NO_ERROR){- free(udp_table);- return 0;+ return 0; // buffer is reused, not freed}// First pass: Try exact match (IP + port)@@ -1329,8 +1471,6 @@ static DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port)}}- free(udp_table);-if (pid != 0)cache_pid(src_ip, src_port, pid, TRUE);@@ -1339,12 +1479,11 @@ static DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port)static DWORD get_process_id_from_connection_v6(const UINT8 src_ip6[16], UINT16 src_port){- MIB_TCP6TABLE_OWNER_PID *tcp_table = NULL;DWORD size = 0, pid = 0;if (GetExtendedTcpTable(NULL, &size, FALSE, AF_INET6, TCP_TABLE_OWNER_PID_ALL, 0) != ERROR_INSUFFICIENT_BUFFER)return 0;- tcp_table = (MIB_TCP6TABLE_OWNER_PID *)malloc(size);+ MIB_TCP6TABLE_OWNER_PID *tcp_table = (MIB_TCP6TABLE_OWNER_PID *)pidtbl_reserve(size);if (!tcp_table) return 0;if (GetExtendedTcpTable(tcp_table, &size, FALSE, AF_INET6, TCP_TABLE_OWNER_PID_ALL, 0) == NO_ERROR){@@ -1359,18 +1498,16 @@ static DWORD get_process_id_from_connection_v6(const UINT8 src_ip6[16], UINT16 s}}}- free(tcp_table);- return pid;+ return pid; // buffer is reused, not freed}static DWORD get_process_id_from_udp_connection_v6(const UINT8 src_ip6[16], UINT16 src_port){- MIB_UDP6TABLE_OWNER_PID *udp_table = NULL;DWORD size = 0, pid = 0;if (GetExtendedUdpTable(NULL, &size, FALSE, AF_INET6, UDP_TABLE_OWNER_PID, 0) != ERROR_INSUFFICIENT_BUFFER)return 0;- udp_table = (MIB_UDP6TABLE_OWNER_PID *)malloc(size);+ MIB_UDP6TABLE_OWNER_PID *udp_table = (MIB_UDP6TABLE_OWNER_PID *)pidtbl_reserve(size);if (!udp_table) return 0;if (GetExtendedUdpTable(udp_table, &size, FALSE, AF_INET6, UDP_TABLE_OWNER_PID, 0) == NO_ERROR){@@ -1386,8 +1523,7 @@ static DWORD get_process_id_from_udp_connection_v6(const UINT8 src_ip6[16], UINT}}}- free(udp_table);- return pid;+ return pid; // buffer is reused, not freed}static BOOL is_ipv6_multicast_or_linklocal(const UINT8 ip6[16])@@ -1749,12 +1885,26 @@ static BOOL match_process_list(const char *process_list, const char *process_namif (process_list == NULL || process_list[0] == '\0' || strcmp(process_list, "*") == 0)return TRUE;- size_t len = strnlen_s(process_list, MAX_LIST_SIZE) + 1;- char *list_copy = (char *)malloc(len);- if (list_copy == NULL)- return FALSE;+ // Stack buffer for the common short case; malloc only for unusually long lists.+ char stackbuf[256];+ size_t len = strnlen_s(process_list, MAX_LIST_SIZE) + 1;+ size_t dstsz = len;+ char *list_copy;+ BOOL on_heap = FALSE;+ if (len <= sizeof(stackbuf))+ {+ list_copy = stackbuf;+ dstsz = sizeof(stackbuf);+ }+ else+ {+ list_copy = (char *)malloc(len);+ if (list_copy == NULL)+ return FALSE;+ on_heap = TRUE;+ }- strncpy_s(list_copy, len, process_list, _TRUNCATE);+ strncpy_s(list_copy, dstsz, process_list, _TRUNCATE);BOOL matched = FALSE;char *context = NULL;@@ -1790,7 +1940,8 @@ static BOOL match_process_list(const char *process_list, const char *process_nam}token = strtok_s(NULL, ",;", &context);}- free(list_copy);+ if (on_heap)+ free(list_copy);return matched;}@@ -1828,12 +1979,26 @@ static BOOL match_domain_list(const char *domain_list, const char *domain)if (domain == NULL || domain[0] == '\0')return FALSE;- size_t len = strnlen_s(domain_list, MAX_LIST_SIZE) + 1;- char *list_copy = (char *)malloc(len);- if (list_copy == NULL)- return FALSE;+ // Stack buffer for the common short case; malloc only for unusually long lists.+ char stackbuf[256];+ size_t len = strnlen_s(domain_list, MAX_LIST_SIZE) + 1;+ size_t dstsz = len;+ char *list_copy;+ BOOL on_heap = FALSE;+ if (len <= sizeof(stackbuf))+ {+ list_copy = stackbuf;+ dstsz = sizeof(stackbuf);+ }+ else+ {+ list_copy = (char *)malloc(len);+ if (list_copy == NULL)+ return FALSE;+ on_heap = TRUE;+ }- strncpy_s(list_copy, len, domain_list, _TRUNCATE);+ strncpy_s(list_copy, dstsz, domain_list, _TRUNCATE);BOOL matched = FALSE;char *context = NULL;char *token = strtok_s(list_copy, ",;", &context);@@ -1852,7 +2017,8 @@ static BOOL match_domain_list(const char *domain_list, const char *domain)}token = strtok_s(NULL, ",;", &context);}- free(list_copy);+ if (on_heap)+ free(list_copy);return matched;}@@ -1907,7 +2073,9 @@ static BOOL is_broadcast_or_multicast(UINT32 ip)// Unified rule matching function for both TCP and UDP// Matches rules by process name, IP, port, and protocol-static RuleAction match_rule(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)+// Inner matcher — caller MUST hold g_rules_lock (shared) for the whole traversal so+// rules_list and the strings it points to cannot be freed/edited mid-match.+static RuleAction match_rule_inner(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id){PROCESS_RULE *rule = rules_list;PROCESS_RULE *wildcard_rule = NULL; // Save fully wildcard rule for last@@ -2007,10 +2175,21 @@ static RuleAction match_rule(const char *process_name, UINT32 dest_ip, UINT16 dereturn RULE_ACTION_DIRECT;}+// Public matcher — takes the shared rules lock so a concurrent AddRule/EditRule/+// DeleteRule/MoveRule from the GUI thread cannot free a node/string mid-match.+static RuleAction match_rule(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)+{+ AcquireSRWLockShared(&g_rules_lock);+ RuleAction action = match_rule_inner(process_name, dest_ip, dest_port, is_udp, out_proxy_config_id);+ ReleaseSRWLockShared(&g_rules_lock);+ return action;+}+// IPv6 variant of match_rule — uses match_ip_list_v6 for host patterns.// Supports exact addresses ("::1"), CIDR ("2001:db8::/32"), and wildcards ("*").// IPv4-format patterns in target_hosts are silently skipped for IPv6 traffic.-static RuleAction match_rule_v6(const char *process_name, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)+// Caller MUST hold g_rules_lock (shared) — see match_rule_v6 wrapper below.+static RuleAction match_rule_v6_inner(const char *process_name, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id){PROCESS_RULE *rule = rules_list;PROCESS_RULE *wildcard_rule = NULL;@@ -2087,6 +2266,14 @@ static RuleAction match_rule_v6(const char *process_name, const UINT8 dest_ip6[1return RULE_ACTION_DIRECT;}+static RuleAction match_rule_v6(const char *process_name, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)+{+ AcquireSRWLockShared(&g_rules_lock);+ RuleAction action = match_rule_v6_inner(process_name, dest_ip6, dest_port, is_udp, out_proxy_config_id);+ ReleaseSRWLockShared(&g_rules_lock);+ return action;+}+static RuleAction check_process_rule(UINT32 src_ip, UINT16 src_port, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, DWORD *out_pid, UINT32 *out_proxy_config_id){DWORD pid;@@ -2397,7 +2584,7 @@ static int socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_porif (use_auth) { buf[1] = 0x02; buf[2] = SOCKS5_AUTH_NONE; buf[3] = 0x02; if (send(s, (char*)buf, 4, 0) != 4) return -1; }else { buf[1] = 0x01; buf[2] = SOCKS5_AUTH_NONE; if (send(s, (char*)buf, 3, 0) != 3) return -1; }- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != SOCKS5_VERSION) return -1;if (buf[1] == 0x02)@@ -2411,7 +2598,7 @@ static int socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_porbuf[2 + user_len] = (unsigned char)pass_len;memcpy(&buf[3 + user_len], cfg->password, pass_len);if (send(s, (char*)buf, (int)(3 + user_len + pass_len), 0) != (int)(3 + user_len + pass_len)) return -1;- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != 0x01 || buf[1] != 0x00) return -1;}else if (buf[1] != SOCKS5_AUTH_NONE) return -1;@@ -2433,7 +2620,7 @@ static int socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_porif (send(s, (char*)buf, req_len, 0) != req_len) return -1;// Read the 4-byte response header: VER REP RSV ATYP- len = recv(s, (char*)buf, 4, 0);+ len = recv_n(s, (char*)buf, 4);if (len < 4 || buf[0] != SOCKS5_VERSION || buf[1] != 0x00){log_message("SOCKS5 domain: CONNECT failed (reply=%d)", len > 1 ? buf[1] : -1);@@ -2494,7 +2681,7 @@ static int socks5_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROX}}- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != SOCKS5_VERSION){log_message("SOCKS5: Invalid auth response");@@ -2531,7 +2718,7 @@ static int socks5_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXreturn -1;}- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != 0x01 || buf[1] != 0x00){log_message("SOCKS5: Authentication failed");@@ -2562,7 +2749,7 @@ static int socks5_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXreturn -1;}- len = recv(s, (char*)buf, 10, 0);+ len = recv_n(s, (char*)buf, 10);if (len < 10 || buf[0] != SOCKS5_VERSION || buf[1] != 0x00){log_message("SOCKS5: CONNECT failed (reply=%d)", len > 1 ? buf[1] : -1);@@ -2582,7 +2769,7 @@ static int socks5_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_porif (use_auth) { buf[1] = 0x02; buf[2] = SOCKS5_AUTH_NONE; buf[3] = 0x02; if (send(s, (char*)buf, 4, 0) != 4) return -1; }else { buf[1] = 0x01; buf[2] = SOCKS5_AUTH_NONE; if (send(s, (char*)buf, 3, 0) != 3) return -1; }- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != SOCKS5_VERSION) return -1;if (buf[1] == 0x02)@@ -2596,7 +2783,7 @@ static int socks5_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_porbuf[2 + ul] = (unsigned char)pl;memcpy(&buf[3 + ul], cfg->password, pl);if (send(s, (char*)buf, (int)(3 + ul + pl), 0) != (int)(3 + ul + pl)) return -1;- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != 0x01 || buf[1] != 0x00) return -1;}else if (buf[1] != SOCKS5_AUTH_NONE) return -1;@@ -2612,7 +2799,7 @@ static int socks5_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_porif (send(s, (char*)buf, 22, 0) != 22) return -1;// response: VER(1)+REP(1)+RSV(1)+ATYP(1)+BND.ADDR(16)+BND.PORT(2) = 22- len = recv(s, (char*)buf, 22, 0);+ len = recv_n(s, (char*)buf, 22);if (len < 4 || buf[0] != SOCKS5_VERSION || buf[1] != 0x00){log_message("SOCKS5 IPv6: CONNECT failed (reply=%d)", len > 1 ? buf[1] : -1);@@ -2802,7 +2989,7 @@ static int socks5_udp_associate_with_config(SOCKET s, struct sockaddr_in *relay_return -1;}- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != SOCKS5_VERSION)return -1;@@ -2825,7 +3012,7 @@ static int socks5_udp_associate_with_config(SOCKET s, struct sockaddr_in *relay_if (send(s, (char*)buf, 3 + user_len + pass_len, 0) != (int)(3 + user_len + pass_len))return -1;- len = recv(s, (char*)buf, 2, 0);+ len = recv_n(s, (char*)buf, 2);if (len != 2 || buf[0] != 0x01 || buf[1] != 0x00)return -1;}@@ -2848,7 +3035,7 @@ static int socks5_udp_associate_with_config(SOCKET s, struct sockaddr_in *relay_if (send(s, (char*)buf, 10, 0) != 10)return -1;- len = recv(s, (char*)buf, 10, 0);+ len = recv_n(s, (char*)buf, 10);if (len < 10 || buf[0] != SOCKS5_VERSION || buf[1] != 0x00)return -1;@@ -2859,6 +3046,29 @@ static int socks5_udp_associate_with_config(SOCKET s, struct sockaddr_in *relay_return 0;}+// TRUE if any enabled PROXY rule routes traffic through this proxy config. Used to skip+// proactively establishing UDP ASSOCIATE for configs that no rule uses — otherwise the+// relay wastes time (and can stall on a dead/unreachable host) connecting to proxies that+// will never carry traffic. A rule with proxy_config_id 0 means "first available", which+// could resolve to any config, so its presence marks all configs as potentially used.+static BOOL is_proxy_config_referenced(UINT32 config_id)+{+ BOOL referenced = FALSE;+ AcquireSRWLockShared(&g_rules_lock);+ for (PROCESS_RULE *r = rules_list; r != NULL; r = r->next)+ {+ if (!r->enabled || r->action != RULE_ACTION_PROXY)+ continue;+ if (r->proxy_config_id == config_id || r->proxy_config_id == 0)+ {+ referenced = TRUE;+ break;+ }+ }+ ReleaseSRWLockShared(&g_rules_lock);+ return referenced;+}+// connect UDP ASSOCIATE with SOCKS5 proxy (per proxy config)static BOOL establish_udp_associate_for_config(PROXY_CONFIG *cfg){@@ -2909,7 +3119,10 @@ static BOOL establish_udp_associate_for_config(PROXY_CONFIG *cfg)socks_addr.sin_addr.s_addr = socks5_ip;socks_addr.sin_port = htons(cfg->port);- if (connect(tcp_sock, (struct sockaddr *)&socks_addr, sizeof(socks_addr)) == SOCKET_ERROR)+ // Bounded connect: a dead/unreachable proxy config fails in ~2s instead of stalling+ // the single-threaded relay for the full OS SYN timeout (~21s), which was delaying+ // real packets that use a *different*, working proxy config.+ if (connect_with_timeout(tcp_sock, (struct sockaddr *)&socks_addr, sizeof(socks_addr), 2000) == SOCKET_ERROR){closesocket(tcp_sock);return FALSE;@@ -3019,10 +3232,12 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)}}- // Try initial UDP ASSOCIATE for all SOCKS5 configs+ // Try initial UDP ASSOCIATE only for SOCKS5 configs that an enabled rule actually uses.+ // Skipping unreferenced configs avoids stalling the relay on dead/unused proxies.for (int i = 0; i < g_proxy_config_count; i++){- if (g_proxy_configs[i].type == PROXY_TYPE_SOCKS5)+ if (g_proxy_configs[i].type == PROXY_TYPE_SOCKS5 &&+ is_proxy_config_referenced(g_proxy_configs[i].config_id)){establish_udp_associate_for_config(&g_proxy_configs[i]);}@@ -3059,7 +3274,8 @@ static DWORD WINAPI udp_relay_server(LPVOID arg)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)+ if (rc->type == PROXY_TYPE_SOCKS5 && !rc->udp_connected &&+ is_proxy_config_referenced(rc->config_id))establish_udp_associate_for_config(rc);}continue;@@ -4182,6 +4398,7 @@ PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char*// Append to tail so rules are evaluated in the order they were added,// matching the visual top-to-bottom order in the GUI (fixes issue #93).+ AcquireSRWLockExclusive(&g_rules_lock);if (rules_list == NULL){rules_list = rule;@@ -4193,11 +4410,13 @@ PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char*tail = tail->next;tail->next = rule;}+ UINT32 new_id = rule->rule_id;+ ReleaseSRWLockExclusive(&g_rules_lock);update_has_active_rules();- log_message("Added rule ID: %u for process '%s' (Protocol: %d, Action: %d, ProxyConfigId: %u, Domains: %s)", rule->rule_id, process_name, protocol, action, proxy_config_id, rule->target_domains);+ log_message("Added rule ID: %u for process '%s' (Protocol: %d, Action: %d, ProxyConfigId: %u, Domains: %s)", new_id, process_name, protocol, action, proxy_config_id, target_domains ? target_domains : "*");- return rule->rule_id;+ return new_id;}PROXYBRIDGE_API BOOL ProxyBridge_EnableRule(UINT32 rule_id)@@ -4205,19 +4424,27 @@ PROXYBRIDGE_API BOOL ProxyBridge_EnableRule(UINT32 rule_id)if (rule_id == 0)return FALSE;+ BOOL found = FALSE;+ AcquireSRWLockExclusive(&g_rules_lock);PROCESS_RULE *rule = rules_list;while (rule != NULL){if (rule->rule_id == rule_id){rule->enabled = TRUE;- update_has_active_rules();- log_message("Enabled rule ID: %u", rule_id);- return TRUE;+ found = TRUE;+ break;}rule = rule->next;}- return FALSE;+ ReleaseSRWLockExclusive(&g_rules_lock);++ if (found)+ {+ update_has_active_rules();+ log_message("Enabled rule ID: %u", rule_id);+ }+ return found;}PROXYBRIDGE_API BOOL ProxyBridge_DisableRule(UINT32 rule_id)@@ -4225,19 +4452,27 @@ PROXYBRIDGE_API BOOL ProxyBridge_DisableRule(UINT32 rule_id)if (rule_id == 0)return FALSE;+ BOOL found = FALSE;+ AcquireSRWLockExclusive(&g_rules_lock);PROCESS_RULE *rule = rules_list;while (rule != NULL){if (rule->rule_id == rule_id){rule->enabled = FALSE;- update_has_active_rules(); // Phase 1: Update fast-path flag- log_message("Disabled rule ID: %u", rule_id);- return TRUE;+ found = TRUE;+ break;}rule = rule->next;}- return FALSE;+ ReleaseSRWLockExclusive(&g_rules_lock);++ if (found)+ {+ update_has_active_rules(); // Phase 1: Update fast-path flag+ log_message("Disabled rule ID: %u", rule_id);+ }+ return found;}PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)@@ -4245,6 +4480,8 @@ PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)if (rule_id == 0)return FALSE;+ BOOL found = FALSE;+ AcquireSRWLockExclusive(&g_rules_lock);PROCESS_RULE *rule = rules_list;PROCESS_RULE *prev = NULL;@@ -4257,6 +4494,8 @@ PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)elseprev->next = rule->next;+ // Unlink + free under the exclusive lock so no reader can be mid-traversal+ // on this node (readers hold the shared lock; exclusive waits them out).if (rule->target_hosts != NULL)free(rule->target_hosts);if (rule->target_ports != NULL)@@ -4264,15 +4503,20 @@ PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)if (rule->target_domains != NULL)free(rule->target_domains);free(rule);-- update_has_active_rules();- log_message("Deleted rule ID: %u", rule_id);- return TRUE;+ found = TRUE;+ break;}prev = rule;rule = rule->next;}- return FALSE;+ ReleaseSRWLockExclusive(&g_rules_lock);++ if (found)+ {+ update_has_active_rules();+ log_message("Deleted rule ID: %u", rule_id);+ }+ return found;}PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_name, const char* target_hosts, const char* target_ports, const char* target_domains, RuleProtocol protocol, RuleAction action, UINT32 proxy_config_id)@@ -4283,23 +4527,26 @@ PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_na// NULL/"" domains means "no restriction" -> stored as "*"const char *domains_in = (target_domains != NULL && target_domains[0] != '\0') ? target_domains : "*";+ // Allocate the three replacements up front (outside the lock) so a failure leaves the+ // rule untouched and the lock is held only for the pointer swap.+ char *new_hosts = _strdup(target_hosts);+ char *new_ports = _strdup(target_ports);+ char *new_domains = _strdup(domains_in);+ if (new_hosts == NULL || new_ports == NULL || new_domains == NULL)+ {+ free(new_hosts);+ free(new_ports);+ free(new_domains);+ return FALSE;+ }++ BOOL found = FALSE;+ AcquireSRWLockExclusive(&g_rules_lock);PROCESS_RULE *rule = rules_list;while (rule != NULL){if (rule->rule_id == rule_id){- // Allocate all three replacements up front so a failure leaves the rule untouched.- char *new_hosts = _strdup(target_hosts);- char *new_ports = _strdup(target_ports);- char *new_domains = _strdup(domains_in);- if (new_hosts == NULL || new_ports == NULL || new_domains == NULL)- {- free(new_hosts);- free(new_ports);- free(new_domains);- return FALSE;- }-strncpy_s(rule->process_name, MAX_PROCESS_NAME, process_name, _TRUNCATE);free(rule->target_hosts);@@ -4312,14 +4559,25 @@ PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_narule->protocol = protocol;rule->action = action;rule->proxy_config_id = proxy_config_id;-- update_has_active_rules();- log_message("Updated rule ID: %u (ProxyConfigId: %u, Domains: %s)", rule_id, proxy_config_id, rule->target_domains);- return TRUE;+ found = TRUE;+ break;}rule = rule->next;}- return FALSE;+ ReleaseSRWLockExclusive(&g_rules_lock);++ if (!found)+ {+ // rule_id not found — the pre-allocated strings were never installed, free them.+ free(new_hosts);+ free(new_ports);+ free(new_domains);+ return FALSE;+ }++ update_has_active_rules();+ log_message("Updated rule ID: %u (ProxyConfigId: %u, Domains: %s)", rule_id, proxy_config_id, domains_in);+ return TRUE;}PROXYBRIDGE_API UINT32 ProxyBridge_GetRulePosition(UINT32 rule_id)@@ -4328,15 +4586,21 @@ PROXYBRIDGE_API UINT32 ProxyBridge_GetRulePosition(UINT32 rule_id)return 0;UINT32 position = 1;+ UINT32 result = 0;+ AcquireSRWLockShared(&g_rules_lock);PROCESS_RULE *rule = rules_list;while (rule != NULL){if (rule->rule_id == rule_id)- return position;+ {+ result = position;+ break;+ }position++;rule = rule->next;}- return 0;+ ReleaseSRWLockShared(&g_rules_lock);+ return result;}PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_position)@@ -4344,6 +4608,9 @@ PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_pif (rule_id == 0 || new_position == 0)return FALSE;+ // Relink under the exclusive lock so packet-path readers never see a half-moved list.+ AcquireSRWLockExclusive(&g_rules_lock);+// first rule and remove it from current positionPROCESS_RULE *rule = rules_list;PROCESS_RULE *prev = NULL;@@ -4357,7 +4624,10 @@ PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_p}if (rule == NULL)+ {+ ReleaseSRWLockExclusive(&g_rules_lock);return FALSE;+ }// Remove from current positionif (prev == NULL)@@ -4404,6 +4674,8 @@ PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_p}}+ ReleaseSRWLockExclusive(&g_rules_lock);+log_message("Moved rule ID %u to position %u", rule_id, new_position);return TRUE;}@@ -4631,13 +4903,10 @@ static void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, R{AcquireSRWLockExclusive(&lock);- int count = 0;- LOGGED_CONNECTION *temp = logged_connections;- while (temp != NULL) { count++; temp = temp->next; }-- if (count >= 100)+ // Use the running counter instead of re-walking the whole list on every add.+ if (g_logged_count >= 100){- temp = logged_connections;+ LOGGED_CONNECTION *temp = logged_connections;for (int i = 0; i < 98 && temp != NULL; i++)temp = temp->next;@@ -4646,14 +4915,17 @@ static void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RLOGGED_CONNECTION *to_free_list = temp->next;temp->next = NULL;+ int freed = 0;ReleaseSRWLockExclusive(&lock);while (to_free_list != NULL){LOGGED_CONNECTION *next = to_free_list->next;free(to_free_list);to_free_list = next;+ freed++;}AcquireSRWLockExclusive(&lock);+ g_logged_count -= freed;}}@@ -4666,6 +4938,7 @@ static void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, Rlogged->action = action;logged->next = logged_connections;logged_connections = logged;+ g_logged_count++;}ReleaseSRWLockExclusive(&lock);@@ -4681,6 +4954,7 @@ static void clear_logged_connections(void)logged_connections = logged_connections->next;free(to_free);}+ g_logged_count = 0;ReleaseSRWLockExclusive(&lock);}@@ -4774,6 +5048,99 @@ static void clear_pid_cache(void)ReleaseSRWLockExclusive(&lock);}+// Evict the cached PID for a specific (src_ip, src_port). Called when a new TCP+// connection begins (fresh SYN) on a port: Windows reuses ephemeral ports, so any+// PID cached for that port may belong to the previous, now-closed process. Without+// this, a reused port could be matched against the wrong application's rule for up to+// PID_CACHE_TTL_MS. Forcing a re-lookup here keeps the decision correct.+static void remove_cached_pid(UINT32 src_ip, UINT16 src_port, BOOL is_udp)+{+ UINT32 hash = pid_cache_hash(src_ip, src_port, is_udp);++ AcquireSRWLockExclusive(&lock);+ PID_CACHE_ENTRY **pp = &pid_cache[hash];+ while (*pp != NULL)+ {+ if ((*pp)->src_ip == src_ip && (*pp)->src_port == src_port && (*pp)->is_udp == is_udp)+ {+ PID_CACHE_ENTRY *to_free = *pp;+ *pp = (*pp)->next;+ free(to_free);+ break;+ }+ pp = &(*pp)->next;+ }+ ReleaseSRWLockExclusive(&lock);+}++// Prune expired PID-cache entries so the table doesn't grow without bound over a long+// session (entries were previously only ignored on lookup, never freed).+static void cleanup_stale_pid_cache(void)+{+ ULONGLONG now = GetTickCount64();++ AcquireSRWLockExclusive(&lock);+ for (int i = 0; i < PID_CACHE_SIZE; i++)+ {+ PID_CACHE_ENTRY **pp = &pid_cache[i];+ while (*pp != NULL)+ {+ if (now - (*pp)->timestamp >= PID_CACHE_TTL_MS)+ {+ PID_CACHE_ENTRY *to_free = *pp;+ *pp = (*pp)->next;+ free(to_free);+ }+ else+ {+ pp = &(*pp)->next;+ }+ }+ }+ ReleaseSRWLockExclusive(&lock);+}++// Prune expired DNS-snoop entries (IPv4 + IPv6) so the caches don't grow without bound.+static void cleanup_stale_dns_cache(void)+{+ ULONGLONG now = GetTickCount64();++ AcquireSRWLockExclusive(&g_dns_cache_lock);+ for (int i = 0; i < DNS_CACHE_BUCKETS; i++)+ {+ DNS_CACHE_ENTRY **pp = &g_dns_cache[i];+ while (*pp != NULL)+ {+ if ((*pp)->expire_tick <= now)+ {+ DNS_CACHE_ENTRY *to_free = *pp;+ *pp = (*pp)->next;+ free(to_free);+ }+ else+ {+ pp = &(*pp)->next;+ }+ }++ DNS_CACHE_ENTRY_V6 **pp6 = &g_dns_cache_v6[i];+ while (*pp6 != NULL)+ {+ if ((*pp6)->expire_tick <= now)+ {+ DNS_CACHE_ENTRY_V6 *to_free = *pp6;+ *pp6 = (*pp6)->next;+ free(to_free);+ }+ else+ {+ pp6 = &(*pp6)->next;+ }+ }+ }+ ReleaseSRWLockExclusive(&g_dns_cache_lock);+}+// Dedicated cleanup thread - runs independently without blocking packet processingstatic DWORD WINAPI cleanup_worker(LPVOID arg){@@ -4783,6 +5150,8 @@ static DWORD WINAPI cleanup_worker(LPVOID arg)if (running){cleanup_stale_connections();+ cleanup_stale_pid_cache();+ cleanup_stale_dns_cache();}}return 0;@@ -4805,10 +5174,15 @@ static void flush_dns_resolver_cache(void)FreeLibrary(dnsapi);}+// Recomputes the fast-path flags. Takes the shared rules lock itself, so callers must+// NOT already hold g_rules_lock exclusive (SRW locks are non-recursive) — the rule API+// functions below release their exclusive lock before calling this.static void update_has_active_rules(void){BOOL has_active = FALSE;BOOL has_domain = FALSE;++ AcquireSRWLockShared(&g_rules_lock);PROCESS_RULE *rule = rules_list;while (rule != NULL){@@ -4823,6 +5197,8 @@ static void update_has_active_rules(void)}rule = rule->next;}+ ReleaseSRWLockShared(&g_rules_lock);+g_has_active_rules = has_active;// Edge trigger: when domain rules first become active, flush the OS DNS cache so@@ -5073,6 +5449,11 @@ PROXYBRIDGE_API BOOL ProxyBridge_Stop(void)clear_pid_cache();+ // Release the reusable owner-PID table scratch buffer (packet thread has stopped).+ free(g_pidtbl_buf);+ g_pidtbl_buf = NULL;+ g_pidtbl_cap = 0;+// Reset per-port decision cache so stale entries don't carry over// if ProxyBridge is stopped and restarted with different rules.memset((void*)port_decided_bitmap, 0, sizeof(port_decided_bitmap));@@ -5109,6 +5490,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)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; }}+ AcquireSRWLockExclusive(&g_rules_lock);while (rules_list != NULL){PROCESS_RULE *to_free = rules_list;@@ -5123,6 +5505,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)free(to_free);}+ ReleaseSRWLockExclusive(&g_rules_lock);break;}return TRUE;