Skip to content

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

fix(windows): don't drop short-lived connections; log failed re-injection

AnoF-Cyber90727da86810
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index ba2ee37..a5fa171 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -134,6 +134,29 @@ static void reset_stale_tcp_state(UINT16 sp, BOOL is_ipv6)
}
}
+// Re-injection wrapper. Every WinDivertSend() in the packet loop used to ignore its result, so a
+// failed injection dropped the packet without a trace and the sender only noticed on its next
+// retransmission (~1 s later). Failures are now counted and logged (first few, then rarely).
+static volatile LONG g_send_fail = 0;
+static BOOL pb_divert_send(HANDLE h, PVOID pkt, UINT len, const WINDIVERT_ADDRESS *addr)
+{
+ if (WinDivertSend(h, pkt, len, NULL, addr))
+ return TRUE;
+ DWORD err = GetLastError();
+ LONG n = InterlockedIncrement(&g_send_fail);
+ if (n <= 10 || n % 500 == 0)
+ {
+ PWINDIVERT_IPHDR ip4 = NULL; PWINDIVERT_IPV6HDR ip6 = NULL; PWINDIVERT_TCPHDR t = NULL; PWINDIVERT_UDPHDR u = NULL;
+ WinDivertHelperParsePacket(pkt, len, &ip4, &ip6, NULL, NULL, NULL, &t, &u, NULL, NULL, NULL, NULL);
+ log_message("WinDivertSend failed (error %lu, %ld total): %s %s %u bytes%s%s%s",
+ err, n, ip4 ? "IPv4" : ip6 ? "IPv6" : "?", t ? "TCP" : u ? "UDP" : "?", len,
+ addr->Outbound ? " outbound" : " inbound", addr->Loopback ? " loopback" : "",
+ (t && t->Syn) ? " SYN" : (t && t->Fin) ? " FIN" : (t && t->Rst) ? " RST" : "");
+ }
+ return FALSE;
+}
+#define WinDivertSend(h, p, l, s, a) pb_divert_send((h), (p), (l), (a))
+
DWORD WINAPI packet_processor(LPVOID arg)
{
unsigned char packet[MAXBUF];
@@ -390,13 +413,15 @@ DWORD WINAPI packet_processor(LPVOID arg)
memcpy(ipv6_header->SrcAddr, tmp, 16);
addr.Outbound = FALSE;
}
- if (tcp_header->Fin || tcp_header->Rst) remove_connection(client_sp, FALSE, TRUE);
+ if (tcp_header->Rst) remove_connection(client_sp, FALSE, TRUE);
+ else if (tcp_header->Fin) connection_fin(client_sp, TRUE, 2);
goto ipv6_send;
}
if (is_connection_tracked(sp, FALSE, TRUE))
{
- if (tcp_header->Fin || tcp_header->Rst) { remove_connection(sp, FALSE, TRUE); port_clear(sp); }
+ if (tcp_header->Rst) { remove_connection(sp, FALSE, TRUE); port_clear(sp); }
+ else if (tcp_header->Fin) connection_fin(sp, TRUE, 1);
tcp_header->DstPort = htons(g_local_relay_port);
static const UINT8 _lb6t[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
@@ -816,18 +841,22 @@ DWORD WINAPI packet_processor(LPVOID arg)
addr.Outbound = FALSE;
}
- if (tcp_header->Fin || tcp_header->Rst)
+ if (tcp_header->Rst)
remove_connection(dst_port, FALSE, FALSE);
+ else if (tcp_header->Fin)
+ connection_fin(dst_port, FALSE, 2);
}
else if (is_connection_tracked(ntohs(tcp_header->SrcPort), FALSE, FALSE))
{
UINT16 src_port = ntohs(tcp_header->SrcPort);
- if (tcp_header->Fin || tcp_header->Rst)
+ if (tcp_header->Rst)
{
remove_connection(src_port, FALSE, FALSE);
port_clear(src_port);
}
+ else if (tcp_header->Fin)
+ connection_fin(src_port, FALSE, 1);
tcp_header->DstPort = htons(g_local_relay_port);
diff --git a/Windows/src/pb_conntrack.c b/Windows/src/pb_conntrack.c
index 8327a1c..667c61a 100644
--- a/Windows/src/pb_conntrack.c
+++ b/Windows/src/pb_conntrack.c
@@ -54,6 +54,7 @@ void add_connection(UINT16 src_port, BOOL is_udp, UINT32 src_ip, UINT32 dest_ip,
existing->orig_dest_port = dest_port;
existing->proxy_config_id = proxy_config_id;
existing->is_tracked = TRUE;
+ existing->fin_flags = 0;
existing->last_activity = GetTickCount64();
rev_insert(existing);
ReleaseSRWLockExclusive(&lock);
@@ -100,6 +101,7 @@ void add_connection_v6(UINT16 src_port, BOOL is_udp, const UINT8 src_ip6[16], co
existing->orig_dest_port = dest_port;
existing->proxy_config_id = proxy_config_id;
existing->is_tracked = TRUE;
+ existing->fin_flags = 0;
existing->last_activity = GetTickCount64();
rev_insert(existing);
ReleaseSRWLockExclusive(&lock);
@@ -371,6 +373,26 @@ void remove_connection(UINT16 src_port, BOOL is_udp, BOOL is_ipv6)
ReleaseSRWLockExclusive(&lock);
}
+void connection_fin(UINT16 src_port, BOOL is_ipv6, UINT8 bit)
+{
+ AcquireSRWLockExclusive(&lock);
+ CONNECTION_INFO **pp = &connection_hash_table[src_port % CONNECTION_HASH_SIZE];
+ while (*pp != NULL && !((*pp)->src_port == src_port && !(*pp)->is_udp && (*pp)->is_ipv6 == is_ipv6))
+ pp = &(*pp)->next;
+ CONNECTION_INFO *c = *pp;
+ if (c != NULL)
+ {
+ c->fin_flags |= bit;
+ if (c->fin_flags == 3)
+ {
+ *pp = c->next;
+ rev_unlink(c);
+ free(c);
+ }
+ }
+ ReleaseSRWLockExclusive(&lock);
+}
+
void cleanup_stale_connections(void)
{
ULONGLONG now = GetTickCount64();
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index 7e224d3..889d166 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -108,6 +108,7 @@ typedef struct CONNECTION_INFO {
struct CONNECTION_INFO *rev_next; // chain in the reverse table (keyed by orig_dest)
UINT32 rev_bucket; // reverse bucket this entry is currently linked in
BOOL in_rev; // TRUE while linked in the reverse table
+ UINT8 fin_flags; // TCP: 1 = app sent FIN, 2 = relay sent FIN (entry dropped when both seen)
} CONNECTION_INFO;
typedef struct {
@@ -355,6 +356,12 @@ BOOL udp_direct_cached(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 ds
void udp_direct_remember(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port);
void udp_direct_reset(void);
+// TCP close handling. A FIN must NOT delete the entry on the spot: a client that sends and closes
+// at once can have its FIN seen before the relay has accept()ed - the relay's lookup then fails
+// and the connection (and its data) is silently dropped. The entry is kept until both sides have
+// sent their FIN (bit 1 = app, bit 2 = relay); a reset still removes it immediately.
+void connection_fin(UINT16 src_port, BOOL is_ipv6, UINT8 bit);
+
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);