Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
fix(windows): clearer SOCKS5 CONNECT failure log, collapse repeats, RST app on handshake failure
AnoF-Cybere82f70890fd8
diff --git a/Windows/src/pb_relay.c b/Windows/src/pb_relay.cindex fac5aee..be264df 100644--- a/Windows/src/pb_relay.c+++ b/Windows/src/pb_relay.c@@ -564,6 +564,17 @@ DWORD WINAPI local_proxy_server(LPVOID arg)return 0;}+// Abortive close (RST) for the app-side socket when the upstream handshake fails. The relay+// has already completed the TCP handshake with the app, so a plain close() looks like a+// normal connection that ended with no data; an RST tells the app the connection failed+// right now, so it errors out (or retries another address) instead of waiting.+static void reset_close(SOCKET s)+{+ struct linger lg = { 1, 0 };+ setsockopt(s, SOL_SOCKET, SO_LINGER, (const char*)&lg, sizeof(lg));+ closesocket(s);+}+DWORD WINAPI connection_handler(LPVOID arg){CONNECTION_CONFIG *config = (CONNECTION_CONFIG *)arg;@@ -648,7 +659,7 @@ DWORD WINAPI connection_handler(LPVOID arg)}if (rc != 0){- closesocket(client_sock);+ reset_close(client_sock);closesocket(socks_sock);return 0;}@@ -660,7 +671,7 @@ DWORD WINAPI connection_handler(LPVOID arg): http_connect(socks_sock, dest_ip, dest_port, proxy);if (rc != 0){- closesocket(client_sock);+ reset_close(client_sock);closesocket(socks_sock);return 0;}diff --git a/Windows/src/pb_socks5.c b/Windows/src/pb_socks5.cindex 2b3eda2..279255a 100644--- a/Windows/src/pb_socks5.c+++ b/Windows/src/pb_socks5.c@@ -30,6 +30,58 @@ int socks5_read_connect_reply(SOCKET s, int *reply)return 0;}+// Explains a failed CONNECT. reply == -1 means no reply was read at all (the proxy closed the+// connection or timed out during the handshake); otherwise it is the RFC 1928 REP code.+// Identical messages repeat in bursts (a browser retrying a destination the proxy cannot+// reach), so the same line is collapsed for a few seconds instead of flooding the log.+static void socks5_log_connect_failure(const char *kind, const char *dest, UINT16 port, int reply)+{+ static SRWLOCK lk = SRWLOCK_INIT;+ static char last[512];+ static ULONGLONG last_tick = 0;+ static int suppressed = 0;++ char why[160];+ if (reply < 0)+ snprintf(why, sizeof(why), "the proxy closed the connection or did not answer (it may be unable to reach this destination)");+ else+ {+ const char *t = "unknown error";+ switch (reply)+ {+ case 0x01: t = "general failure"; break;+ case 0x02: t = "not allowed by ruleset"; break;+ case 0x03: t = "network unreachable"; break;+ case 0x04: t = "host unreachable"; break;+ case 0x05: t = "connection refused"; break;+ case 0x06: t = "TTL expired"; break;+ case 0x07: t = "command not supported"; break;+ case 0x08: t = "address type not supported"; break;+ }+ snprintf(why, sizeof(why), "proxy replied 0x%02X (%s)", reply, t);+ }++ char line[512];+ snprintf(line, sizeof(line), "SOCKS5 %s: CONNECT to %s:%u failed - %s", kind, dest, (unsigned)port, why);++ ULONGLONG now = GetTickCount64();+ AcquireSRWLockExclusive(&lk);+ if (strcmp(line, last) == 0 && now - last_tick < 5000)+ {+ suppressed++;+ ReleaseSRWLockExclusive(&lk);+ return;+ }+ int n = suppressed;+ suppressed = 0;+ strncpy_s(last, sizeof(last), line, _TRUNCATE);+ last_tick = now;+ ReleaseSRWLockExclusive(&lk);++ if (n > 0) log_message("(previous SOCKS5 failure repeated %d more times)", n);+ log_message("%s", line);+}+// SOCKS5 CONNECT with ATYP_DOMAINint socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_port, const PROXY_CONFIG *cfg)@@ -80,7 +132,7 @@ int socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_port, consint reply;if (socks5_read_connect_reply(s, &reply) != 0){- log_message("SOCKS5 domain: CONNECT failed (reply=%d)", reply);+ socks5_log_connect_failure("domain", hostname, dest_port, reply);return -1;}return 0;@@ -186,7 +238,9 @@ int socks5_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXY_CONFIint reply;if (socks5_read_connect_reply(s, &reply) != 0){- log_message("SOCKS5: CONNECT failed (reply=%d)", reply);+ char ds[32];+ snprintf(ds, sizeof(ds), "%u.%u.%u.%u", (dest_ip >> 0) & 0xFF, (dest_ip >> 8) & 0xFF, (dest_ip >> 16) & 0xFF, (dest_ip >> 24) & 0xFF);+ socks5_log_connect_failure("IPv4", ds, dest_port, reply);return -1;}@@ -237,7 +291,11 @@ int socks5_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_port, consint reply;if (socks5_read_connect_reply(s, &reply) != 0){- log_message("SOCKS5 IPv6: CONNECT failed (reply=%d)", reply);+ char ds[64] = "?";+ InetNtopA(AF_INET6, dest_ip6, ds, sizeof(ds));+ char bracketed[80];+ snprintf(bracketed, sizeof(bracketed), "[%s]", ds);+ socks5_log_connect_failure("IPv6", bracketed, dest_port, reply);return -1;}return 0;