Skip to content

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

fix(windows): the service crashed every time the GUI disconnected (threads outliving the engine)

AnoF-Cyber1da45241ce9f
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 5a5e670..00a75df 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -1051,7 +1051,9 @@ DWORD WINAPI cleanup_worker(LPVOID arg)
{
while (running)
{
- Sleep(30000); // 30 seconds
+ // 30 seconds, but in short slices: ProxyBridge_Stop() only waits briefly for this thread, and a
+ // thread still sleeping inside the DLL when it is unloaded would crash the host process.
+ for (int i = 0; i < 300 && running; i++) Sleep(100);
if (running)
{
cleanup_stale_connections();
@@ -1086,6 +1088,8 @@ static DWORD fw_run(const wchar_t *args, DWORD wait_ms)
return code;
}
+static HANDLE g_fw_thread = NULL;
+
static DWORD WINAPI fw_ensure_thread(LPVOID arg)
{
(void)arg;
@@ -1146,7 +1150,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_Start(void)
relay_pool_start(); // optional: connections fall back to blocking relay threads without it
- { HANDLE ft = CreateThread(NULL, 0, fw_ensure_thread, NULL, 0, NULL); if (ft) CloseHandle(ft); }
+ g_fw_thread = CreateThread(NULL, 0, fw_ensure_thread, NULL, 0, NULL);
proxy_thread = CreateThread(NULL, 1, local_proxy_server, NULL, 0, NULL);
if (proxy_thread == NULL)
@@ -1333,7 +1337,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_Stop(void)
{
if (packet_thread[i] != NULL)
{
- WaitForSingleObject(packet_thread[i], 1000); // 1 second timeout
+ WaitForSingleObject(packet_thread[i], 8000); // generous: a thread outliving Stop would run code from an unloaded DLL
CloseHandle(packet_thread[i]);
packet_thread[i] = NULL;
}
@@ -1341,25 +1345,36 @@ PROXYBRIDGE_API BOOL ProxyBridge_Stop(void)
if (proxy_thread != NULL)
{
- WaitForSingleObject(proxy_thread, 1000); // 1 second timeout
+ WaitForSingleObject(proxy_thread, 8000); // generous: a thread outliving Stop would run code from an unloaded DLL
CloseHandle(proxy_thread);
proxy_thread = NULL;
}
if (cleanup_thread != NULL)
{
- WaitForSingleObject(cleanup_thread, 1000); // 1 second timeout
+ WaitForSingleObject(cleanup_thread, 8000); // generous: a thread outliving Stop would run code from an unloaded DLL
CloseHandle(cleanup_thread);
cleanup_thread = NULL;
}
if (udp_relay_thread != NULL)
{
- WaitForSingleObject(udp_relay_thread, 1000); // 1 second timeout
+ WaitForSingleObject(udp_relay_thread, 8000); // generous: a thread outliving Stop would run code from an unloaded DLL
CloseHandle(udp_relay_thread);
udp_relay_thread = NULL;
}
+ // Handshake threads (connect to the proxy + SOCKS/HTTP negotiation) can still be mid-flight.
+ for (int i = 0; i < 700 && g_handler_threads > 0; i++) Sleep(50); // up to 35 s
+ if (g_handler_threads > 0) log_message("Stop: %ld connection thread(s) still running", (long)g_handler_threads);
+
+ if (g_fw_thread != NULL)
+ {
+ WaitForSingleObject(g_fw_thread, 20000); // netsh calls are bounded (8 s + 15 s)
+ CloseHandle(g_fw_thread);
+ g_fw_thread = NULL;
+ }
+
// Close every live relay and stop the pool's worker threads (the accept loop has ended,
// so no new relays arrive). Must finish before the engine can be unloaded.
relay_pool_stop();
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index d84918f..b660382 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -383,6 +383,7 @@ BOOL relay_pool_start(void); // IOCP relay pool (pb_relay.
void relay_pool_stop(void);
BOOL relay_start(SOCKET client, SOCKET proxy);
extern volatile LONG g_active_relays;
+extern volatile LONG g_handler_threads; // per-connection handshake threads still running (Stop waits for them)
// ---- ProxyBridge.c ----
DWORD WINAPI packet_processor(LPVOID arg);
diff --git a/Windows/src/pb_relay.c b/Windows/src/pb_relay.c
index 2ab85db..bd36c2e 100644
--- a/Windows/src/pb_relay.c
+++ b/Windows/src/pb_relay.c
@@ -500,24 +500,6 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
if (select(0, &read_fds, NULL, NULL, &timeout) <= 0)
continue;
- // helper lambda-like macro to accept and dispatch a connection
- #define ACCEPT_AND_DISPATCH(sock, saddr_type, addr_field) do { \
- saddr_type ca; int cl = sizeof(ca); \
- SOCKET cs = accept(sock, (struct sockaddr*)&ca, &cl); \
- if (cs == INVALID_SOCKET) break; \
- CONNECTION_CONFIG *cc = (CONNECTION_CONFIG*)malloc(sizeof(CONNECTION_CONFIG)); \
- if (cc == NULL) { closesocket(cs); break; } \
- cc->client_socket = cs; \
- UINT16 cp = ntohs(((saddr_type*)&ca)->addr_field); \
- BOOL ok = cc->is_ipv6 ? \
- get_connection_full_v6(cp, FALSE, cc->orig_dest_ip6, &cc->orig_dest_port, &cc->proxy_config_id) : \
- get_connection_full(cp, FALSE, &cc->orig_dest_ip, &cc->orig_dest_port, &cc->proxy_config_id); \
- if (!ok) { closesocket(cs); free(cc); break; } \
- HANDLE t = CreateThread(NULL, 1, connection_handler, (LPVOID)cc, 0, NULL); \
- if (t == NULL) { closesocket(cs); free(cc); break; } \
- CloseHandle(t); \
- } while(0)
-
if (FD_ISSET(listen_sock, &read_fds))
{
struct sockaddr_in client_addr;
@@ -536,9 +518,10 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
if (get_connection_full(client_port, FALSE, &conn_config->orig_dest_ip, &conn_config->orig_dest_port, &conn_config->proxy_config_id) &&
relay_peer_ok_v4(client_addr.sin_addr.s_addr, conn_config->orig_dest_ip))
{
+ InterlockedIncrement(&g_handler_threads);
HANDLE conn_thread = CreateThread(NULL, 1, connection_handler, (LPVOID)conn_config, 0, NULL);
if (conn_thread != NULL) { CloseHandle(conn_thread); }
- else { closesocket(client_sock); free(conn_config); }
+ else { InterlockedDecrement(&g_handler_threads); closesocket(client_sock); free(conn_config); }
}
else { closesocket(client_sock); free(conn_config); }
}
@@ -564,9 +547,10 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
if (get_connection_full_v6(client_port, FALSE, conn_config->orig_dest_ip6, &conn_config->orig_dest_port, &conn_config->proxy_config_id) &&
relay_peer_ok_v6(&client_addr6.sin6_addr, conn_config->orig_dest_ip6))
{
+ InterlockedIncrement(&g_handler_threads);
HANDLE conn_thread = CreateThread(NULL, 1, connection_handler, (LPVOID)conn_config, 0, NULL);
if (conn_thread != NULL) { CloseHandle(conn_thread); }
- else { closesocket(client_sock6); free(conn_config); }
+ else { InterlockedDecrement(&g_handler_threads); closesocket(client_sock6); free(conn_config); }
}
else { closesocket(client_sock6); free(conn_config); }
}
@@ -575,8 +559,6 @@ DWORD WINAPI local_proxy_server(LPVOID arg)
}
}
- #undef ACCEPT_AND_DISPATCH
-
closesocket(listen_sock);
if (listen_sock6 != INVALID_SOCKET) closesocket(listen_sock6);
WSACleanup();
@@ -594,7 +576,21 @@ static void reset_close(SOCKET s)
closesocket(s);
}
+volatile LONG g_handler_threads = 0;
+
+static DWORD WINAPI connection_handler_impl(LPVOID arg);
+
+// Every handshake thread is counted from the moment it is created until it returns, so that
+// ProxyBridge_Stop() can wait for all of them. A thread still running inside the DLL when the DLL
+// is unloaded (the service reloads the engine for every GUI connection) crashes the process.
DWORD WINAPI connection_handler(LPVOID arg)
+{
+ DWORD r = connection_handler_impl(arg);
+ InterlockedDecrement(&g_handler_threads);
+ return r;
+}
+
+static DWORD WINAPI connection_handler_impl(LPVOID arg)
{
CONNECTION_CONFIG *config = (CONNECTION_CONFIG *)arg;
SOCKET client_sock = config->client_socket;