Skip to content

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

21.4 KB
Raw Blame
#ifndef PB_INTERNAL_H
#define PB_INTERNAL_H

// Shared internal declarations for the ProxyBridge core, split across
// ProxyBridge.c (globals + lifecycle), pb_net.c, pb_rules.c, pb_conn.c.
// Defines, structs, extern globals and cross-module prototypes live here.

// select() sets hold 64 sockets by default and FD_SET silently drops the rest. The UDP relay watches two sockets per
// SOCKS5 proxy plus its own two, which is 66 with 32 proxies, so the sets must be bigger. Must come before winsock2.h.
#ifndef FD_SETSIZE
#define FD_SETSIZE 128
#endif
#include <winsock2.h>
#include <windows.h>
#include "ProxyBridge.h"
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <iphlpapi.h>
#include <psapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "windivert.h"

typedef char pb_fd_setsize_fits_all_proxies[(FD_SETSIZE >= 2 * MAX_PROXY_CONFIGS + 4) ? 1 : -1];   // compile error if MAX_PROXY_CONFIGS grows past it

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "advapi32.lib")   // token elevation check for the firewall rule

#define MAXBUF 0xFFFF
#define LOCAL_PROXY_PORT 34010
#define LOCAL_UDP_RELAY_PORT 34011  // its running UDP port still make sure to not run on same port as TCP, opening same port and tcp and udp cause issue and handling port at relay server response injection
#define MAX_PROCESS_NAME 1024        // one process image name / path read from the OS
#define MAX_RULE_PROCESS_LIST 24576  // a rule's Applications list in UTF-8: 8191 UTF-16 characters at up to 3 bytes each
#define VERSION "4.3.0"
#define PID_CACHE_SIZE 1024
#define PID_CACHE_TTL_MS 30000
// Single packet-processor thread eliminates TCP packet reordering.
// With multiple threads each racing to WinDivertRecv+WinDivertSend, thread N+1
// can re-inject its segment before thread N injects segment N, causing the
// relay's TCP stack to send DUPACKs back to the browser.  The browser
// interprets 3+ DUPACKs as loss, halves its congestion window, and upload
// throughput collapses 50% A single ordered thread prevents this entirely.
// One thread is fast enough at 200 Mbps with 1460-byte segments there are
// 17 000 packets/sec a single core processes well over 200000 packets/sec.
#define NUM_PACKET_THREADS 1
#define CONNECTION_HASH_SIZE 4096
#define SOCKS5_BUFFER_SIZE 1024
#define HTTP_BUFFER_SIZE 1024
#define FILTER_BUFFER_SIZE 1024
#define LOG_BUFFER_SIZE 1024
#define MAX_LIST_SIZE 65536  // max byte length for semicolon-delimited host/port/process lists

typedef struct PROCESS_RULE {
    UINT32 rule_id;
    char process_name[MAX_RULE_PROCESS_LIST];
    char *target_hosts;   // Dynamic: IP filter "*", "192.168.*.*", "10.0.0.1;172.16.0.0"
    char *target_ports;   // Dynamic: Port filter "*", "80", "80;443", "8000-9000"
    char *target_domains; // Dynamic: Domain filter "*", "google.com", "*.google.com;*.gstatic.com" ("" or "*" = no domain restriction)
    RuleProtocol protocol;  // TCP, UDP, or BOTH
    RuleAction action;
    UINT32 proxy_config_id;  // Which proxy config to route this rule through (0 = first available)
    BOOL localhost_via_proxy; // TRUE: connections to localhost (127.x / ::1) that match this rule are proxied; FALSE: they go direct
    BOOL enabled;
    struct PROCESS_RULE *next;
} PROCESS_RULE;

#define SOCKS5_VERSION 0x05
#define SOCKS5_CMD_CONNECT 0x01
#define SOCKS5_CMD_UDP_ASSOCIATE 0x03
#define SOCKS5_ATYP_IPV4   0x01
#define SOCKS5_ATYP_IPV6   0x04
#define SOCKS5_ATYP_DOMAIN 0x03  // send hostname to proxy rfc 1928
#define SOCKS5_AUTH_NONE   0x00

// Idle timeout before a connection-tracking entry is reaped. Clean closes are removed
// immediately on FIN/RST, so this only sweeps entries whose FIN/RST was missed. It MUST
// exceed the relay's TCP keepalive interval (5 min) and real idle periods (IMAP IDLE,
// voice channels, WebSocket, DB pools) - otherwise an open-but-idle proxied connection
// gets reaped mid-session and silently breaks until restart (issue: hours-long degradation).
#define CONNECTION_STALE_TIMEOUT_MS 1800000  // 30 minutes

// DNS snooping cache: maps intercepted A-record answers to their hostnames so
// that SOCKS5 conect can forward ATYP_DOMAIN instead of ATYP_IPV4, letting
// proxy servers that do their own name-resolution (e.g. mihomo) see the
// original hostname rather than a bare IP.  (Resolves issue #138.)
#define DNS_CACHE_BUCKETS 1024
#define DNS_CACHE_TTL_MS  300000  // 5 minutes

typedef struct DNS_CACHE_ENTRY {
    UINT32 ip;              // network-byte-order IPv4 
    char   domain[256];
    ULONGLONG expire_tick;
    struct DNS_CACHE_ENTRY *next;
} DNS_CACHE_ENTRY;

typedef struct DNS_CACHE_ENTRY_V6 {
    UINT8  ip6[16];         // raw IPv6 address
    char   domain[256];
    ULONGLONG expire_tick;
    struct DNS_CACHE_ENTRY_V6 *next;
} DNS_CACHE_ENTRY_V6;

typedef struct CONNECTION_INFO {
    UINT16 src_port;
    BOOL   is_udp;             // protocol - a TCP and a UDP flow may share a local port
    UINT32 src_ip;
    UINT32 orig_dest_ip;
    UINT16 orig_dest_port;
    BOOL   is_tracked;
    ULONGLONG last_activity;
    UINT32 proxy_config_id;
    BOOL   is_ipv6;
    UINT8  src_ip6[16];        // raw IPv6 src (only valid when is_ipv6)
    UINT8  orig_dest_ip6[16];  // raw IPv6 dest (only valid when is_ipv6)
    struct CONNECTION_INFO *next;      // chain in the forward table (keyed by src_port)
    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)
    DWORD  pid;                        // owning process (shown in the live connections view)
    UINT32 rule_id;                    // rule that matched (live connections view)
    volatile LONG64 up_bytes;          // UDP: payload bytes app -> proxy
    volatile LONG64 down_bytes;        // UDP: payload bytes proxy -> app
} CONNECTION_INFO;

typedef struct {
    SOCKET client_socket;
    UINT32 orig_dest_ip;
    UINT16 orig_dest_port;
    UINT32 proxy_config_id;
    BOOL   is_ipv6;
    UINT8  orig_dest_ip6[16];
    DWORD  pid;
    UINT32 rule_id;
} CONNECTION_CONFIG;

typedef struct {
    SOCKET from_socket;
    SOCKET to_socket;
} TRANSFER_CONFIG;

// Two-thread bidirectional relay: each direction runs in its own thread so
// a slow proxy (upload) never stalls the download pipe and vice-versa.
typedef struct {
    SOCKET sock_client;   // app-side socket
    SOCKET sock_proxy;    // proxy-side socket
    volatile LONG refs;   // ref-count; last thread out closes both sockets
} RELAY_PAIR;

typedef struct {
    RELAY_PAIR *pair;
    SOCKET from;
    SOCKET to;
} ONE_WAY_CONFIG;

typedef struct LOGGED_CONNECTION {
    DWORD pid;
    UINT32 dest_ip;
    UINT16 dest_port;
    RuleAction action;
    struct LOGGED_CONNECTION *next;
} LOGGED_CONNECTION;

typedef struct PID_CACHE_ENTRY {
    UINT32 src_ip;
    UINT16 src_port;
    DWORD pid;
    ULONGLONG timestamp;     // GetTickCount64() - a 32-bit field breaks the TTL math after ~49.7 days of uptime
    BOOL is_udp;
    struct PID_CACHE_ENTRY *next;
} PID_CACHE_ENTRY;

// Internal proxy configuration with per-config UDP SOCKS5 state
typedef struct {
    UINT32 config_id;           // Unique ID (1-based), 0 = unused slot
    ProxyType type;
    char host[256];
    UINT16 port;
    char username[256];
    char password[256];
    BOOL send_domain_to_proxy;  // TRUE = proxy resolves DNS (send hostname), FALSE = send IP
    UINT32 resolved_ip;         // cached at add/edit time - avoids DNS per connection
    ULONGLONG last_udp_attempt;
    SOCKET udp_tcp_ctrl;
    SOCKET udp_send_sock;
    struct sockaddr_in udp_relay_addr;
    BOOL udp_connected;
} PROXY_CONFIG;

typedef BOOL (*token_match_func)(const char *token, const void *data);

// ---- shared globals (defined in ProxyBridge.c) ----
extern PROXY_CONFIG g_proxy_configs[MAX_PROXY_CONFIGS];
extern int g_proxy_config_count;
extern UINT32 g_next_config_id;
extern CONNECTION_INFO *connection_hash_table[CONNECTION_HASH_SIZE];
extern CONNECTION_INFO *connection_rev_table[CONNECTION_HASH_SIZE];
extern LOGGED_CONNECTION *logged_connections;
extern int g_logged_count;  // running length of logged_connections (guarded by `lock`)
extern PROCESS_RULE *rules_list;
extern UINT32 g_next_rule_id;
extern SRWLOCK lock;
extern SRWLOCK g_rules_lock;
extern HANDLE windivert_handle;
extern HANDLE packet_thread[NUM_PACKET_THREADS];
extern HANDLE proxy_thread;
extern HANDLE udp_relay_thread;
extern HANDLE cleanup_thread;
extern PID_CACHE_ENTRY *pid_cache[PID_CACHE_SIZE];
extern volatile BOOL g_has_active_rules;
extern volatile BOOL g_has_domain_rules;
extern volatile BOOL g_log_every_connection;   // TRUE: the Connections log is not de-duplicated (every new TCP connection, every new UDP flow)
extern volatile BOOL g_proxy_enabled;   // FALSE = paused: new connections are neither proxied nor blocked (existing ones carry on)
extern SOCKET udp_relay_socket;
extern SOCKET udp_relay_socket6;
extern volatile BOOL running;
extern DWORD g_current_process_id;
extern BOOL g_traffic_logging_enabled;
extern DNS_CACHE_ENTRY    *g_dns_cache[DNS_CACHE_BUCKETS];
extern DNS_CACHE_ENTRY_V6 *g_dns_cache_v6[DNS_CACHE_BUCKETS];
extern SRWLOCK             g_dns_cache_lock;
extern volatile LONG port_decided_bitmap[2048];  // 8 KB
extern volatile LONG port_direct_bitmap[2048];  // 8 KB
extern UINT16 g_local_relay_port;
// Set by match_rule*_inner on the calling thread: the "localhost via proxy" flag of the rule that
// produced the last verdict (FALSE when nothing matched). Read right after check_process_rule*().
extern __declspec(thread) BOOL g_tl_rule_localhost;
extern __declspec(thread) UINT32 g_tl_rule_id;   // same for the id of the rule that produced the verdict (0 = none)
extern LogCallback g_log_callback;
extern ConnectionCallback g_connection_callback;
extern char  *g_pidtbl_buf;
extern DWORD  g_pidtbl_cap;

// ---- per-source-port decision bitmaps: hot-path inline helpers ----
static __forceinline BOOL port_is_decided(UINT16 p)
{
    return (port_decided_bitmap[p >> 5] >> (p & 31)) & 1;
}
static __forceinline BOOL port_is_direct(UINT16 p)
{
    return (port_direct_bitmap[p >> 5] >> (p & 31)) & 1;
}
static __forceinline void port_set_direct(UINT16 p)
{
    InterlockedOr(&port_decided_bitmap[p >> 5], (LONG)(1u << (p & 31)));
    InterlockedOr(&port_direct_bitmap[p >> 5],  (LONG)(1u << (p & 31)));
}
static __forceinline void port_set_decided(UINT16 p)  // decided, but NOT direct (proxy/block)
{
    InterlockedOr(&port_decided_bitmap[p >> 5], (LONG)(1u << (p & 31)));
    // leave port_direct_bitmap bit at 0
}
static __forceinline void port_clear(UINT16 p)
{
    InterlockedAnd(&port_decided_bitmap[p >> 5], (LONG)~(1u << (p & 31)));
    InterlockedAnd(&port_direct_bitmap[p >> 5],  (LONG)~(1u << (p & 31)));
}

// ---- pb_util.c ----
void log_message(const char *msg, ...);
const char* extract_filename(const char* path);
char* skip_whitespace(char *str);
void format_ip_address(UINT32 ip, char *buffer, size_t size);
BOOL parse_token_list(const char *list, const char *delimiters, token_match_func match_func, const void *match_data);
void configure_tcp_socket(SOCKET sock, int bufsize, DWORD timeout);
int connect_with_timeout(SOCKET s, const struct sockaddr *addr, int addrlen, int timeout_ms);
void configure_udp_socket(SOCKET sock, int bufsize, DWORD timeout);
int send_all(SOCKET sock, const char *buf, int len);
int recv_n(SOCKET s, char *buf, int n);
UINT32 parse_ipv4(const char *ip);
UINT32 resolve_hostname(const char *hostname);
void base64_encode(const char* input, char* output, size_t output_size);

// ---- pb_process.c ----
void *pidtbl_reserve(DWORD need);

// pb_sockmap.c - owner PID of new outbound TCP connections from WinDivert's SOCKET layer
BOOL  sockmap_start(void);
void  sockmap_stop(void);
DWORD sockmap_lookup_tcp(UINT16 port);   // 0 = unknown, fall back to the table scan
void  sockmap_note_scan(void);
DWORD get_process_id_from_connection(UINT32 src_ip, UINT16 src_port);
DWORD get_process_id_from_udp_connection(UINT32 src_ip, UINT16 src_port);
DWORD get_process_id_from_connection_v6(const UINT8 src_ip6[16], UINT16 src_port);
DWORD get_process_id_from_udp_connection_v6(const UINT8 src_ip6[16], UINT16 src_port);
BOOL get_process_name_from_pid(DWORD pid, char *name, DWORD name_size);
UINT32 pid_cache_hash(UINT32 src_ip, UINT16 src_port, BOOL is_udp);
DWORD get_cached_pid(UINT32 src_ip, UINT16 src_port, BOOL is_udp);
void cache_pid(UINT32 src_ip, UINT16 src_port, DWORD pid, BOOL is_udp);
void clear_pid_cache(void);
void remove_cached_pid(UINT32 src_ip, UINT16 src_port, BOOL is_udp);
void cleanup_stale_pid_cache(void);

// ---- pb_rules.c ----
BOOL is_ipv6_multicast_or_linklocal(const UINT8 ip6[16]);
RuleAction check_process_rule_v6(const UINT8 src_ip6[16], UINT16 src_port, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, DWORD *out_pid, UINT32 *out_proxy_config_id);
BOOL match_ip_pattern(const char *pattern, UINT32 ip);
BOOL match_port_pattern(const char *pattern, UINT16 port);
BOOL ip_match_wrapper(const char *token, const void *data);
BOOL match_ip_list(const char *ip_list, UINT32 ip);
BOOL match_ip_pattern_v6(const char *pattern, const UINT8 ip6[16]);
BOOL ip_match_wrapper_v6(const char *token, const void *data);
BOOL match_ip_list_v6(const char *ip_list, const UINT8 ip6[16]);
BOOL port_match_wrapper(const char *token, const void *data);
BOOL match_port_list(const char *port_list, UINT16 port);
BOOL wildcard_match(const char *pattern, const char *text);
BOOL match_process_pattern(const char *pattern, const char *process_full_path);
BOOL match_process_list(const char *process_list, const char *process_name);
BOOL match_domain_pattern(const char *pattern, const char *domain);
BOOL match_domain_list(const char *domain_list, const char *domain);
BOOL rule_has_domain_filter(const PROCESS_RULE *rule);
BOOL match_domain_filter(const PROCESS_RULE *rule, const char *domain);
BOOL is_broadcast_or_multicast(UINT32 ip);
RuleAction match_rule_inner(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id);
RuleAction match_rule(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id);
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);
RuleAction match_rule_v6(const char *process_name, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id);
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);
void update_has_active_rules(void);

// ---- pb_proxy.c ----
PROXY_CONFIG* find_proxy_config(UINT32 config_id);
BOOL any_socks5_config(void);
BOOL is_proxy_config_referenced(UINT32 config_id);

// ---- pb_dns.c ----
void dns_cache_init(void);
UINT32 dns_bucket(UINT32 ip);
void dns_cache_store(UINT32 ip, const char *domain);
BOOL dns_cache_lookup(UINT32 ip, char *out_domain, size_t out_size);
UINT32 dns_bucket_v6(const UINT8 ip6[16]);
void dns_cache_store_v6(const UINT8 ip6[16], const char *domain);
BOOL dns_cache_lookup_v6(const UINT8 ip6[16], char *out_domain, size_t out_size);
BOOL dns_parse_name(const UINT8 *msg, int msg_len, int *offset, char *dst, int dst_len);
void snoop_dns_response(const UINT8 *payload, int payload_len);
void cleanup_stale_dns_cache(void);
void flush_dns_resolver_cache(void);

// ---- pb_socks5.c ----
int socks5_read_connect_reply(SOCKET s, int *reply);
int socks5_connect_domain(SOCKET s, const char *hostname, UINT16 dest_port, const PROXY_CONFIG *cfg);
int socks5_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXY_CONFIG *cfg);
int socks5_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_port, const PROXY_CONFIG *cfg);
int socks5_udp_associate_with_config(SOCKET s, struct sockaddr_in *relay_addr, const PROXY_CONFIG *cfg);
BOOL establish_udp_associate_for_config(PROXY_CONFIG *cfg);

// ---- pb_http.c ----
int http_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_port, const PROXY_CONFIG *cfg);
int http_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXY_CONFIG *cfg);

// ---- pb_conntrack.c ----
UINT32 rev_hash_v4(UINT32 dest_ip, UINT16 dest_port);
UINT32 rev_hash_v6(const UINT8 dest_ip6[16], UINT16 dest_port);
void rev_insert(CONNECTION_INFO *c);
void rev_unlink(CONNECTION_INFO *c);
void add_connection(UINT16 src_port, BOOL is_udp, UINT32 src_ip, UINT32 dest_ip, UINT16 dest_port, UINT32 proxy_config_id, DWORD pid, UINT32 rule_id);
void add_connection_v6(UINT16 src_port, BOOL is_udp, const UINT8 src_ip6[16], const UINT8 dest_ip6[16], UINT16 dest_port, UINT32 proxy_config_id, DWORD pid, UINT32 rule_id);
BOOL get_connection_full_v6(UINT16 src_port, BOOL is_udp, UINT8 dest_ip6[16], UINT16 *dest_port, UINT32 *proxy_config_id);
// acct_down: payload bytes to add to the winning flow's "received" counter (0 = none)
BOOL find_v6_udp_sender(const UINT8 orig_dest_ip6[16], UINT16 orig_dest_port, UINT8 src_ip6[16], UINT16 *src_port, UINT32 acct_down);
// UDP flow validation for the app->relay redirect. A UDP "connection" is keyed by source port
// only, so it must be re-checked on every datagram: (a) if the destination changed (a socket
// that moves to another server, e.g. a voice call migrating) the entry is retargeted, and
// (b) if the flow has been idle longer than UDP_REEVAL_IDLE_MS the source port may belong to a
// different socket now, so the entry is dropped and the caller re-evaluates the rules.
// Returns TRUE if a valid entry exists (and was refreshed), FALSE if not tracked / evicted.
#define UDP_REEVAL_IDLE_MS 20000
BOOL udp_flow_validate(UINT16 src_port, BOOL is_ipv6, UINT32 dest_ip, const UINT8 dest_ip6[16], UINT16 dest_port);

// Short-lived cache of DIRECT decisions for UDP, keyed by (src port, destination). UDP has no
// SYN to decide once per connection, so every datagram of a direct flow used to repeat the
// full process/rule lookup (OpenProcess per packet) on the single capture thread.
// Capture-thread only (NUM_PACKET_THREADS == 1), so no locking.
#define UDP_DIRECT_TTL_MS 5000
BOOL udp_direct_cached(BOOL is_ipv6, const void *dst, UINT16 src_port, UINT16 dst_port);
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);
// Same lookups, plus the owning pid (optional) and an optional byte count added to the flow's "sent" counter under the
// same lock (the UDP relay uses it so counting costs no extra lookup).
BOOL get_connection_full_x(UINT16 src_port, BOOL is_udp, UINT32 *dest_ip, UINT16 *dest_port, UINT32 *proxy_config_id, DWORD *pid, UINT32 *rule_id, UINT32 acct_up);
BOOL get_connection_full_v6_x(UINT16 src_port, BOOL is_udp, UINT8 dest_ip6[16], UINT16 *dest_port, UINT32 *proxy_config_id, DWORD *pid, UINT32 *rule_id, UINT32 acct_up);
// Live proxied UDP flows (active in the last UDP_FLOW_LIVE_MS), newest table order; raw fields only.
#define UDP_FLOW_LIVE_MS 30000
UINT32 udp_flows_snapshot(PB_FLOW *out, UINT32 max);
UINT32 get_connection_proxy_id(UINT16 src_port, BOOL is_udp);
void remove_connection(UINT16 src_port, BOOL is_udp, BOOL is_ipv6);
void cleanup_stale_connections(void);
BOOL is_connection_already_logged(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);
void add_logged_connection(DWORD pid, UINT32 dest_ip, UINT16 dest_port, RuleAction action);
// Should this connection get a row in the Connections log? Default: only the first of identical (process, destination,
// port, action). With g_log_every_connection: every new TCP connection, and one row per UDP flow (again after 30 s of silence).
BOOL connection_log_wanted(DWORD pid, UINT32 dest_key, UINT16 dest_port, UINT16 src_port, BOOL is_udp, RuleAction action);
void clear_logged_connections(void);

// ---- pb_relay.c ----
DWORD WINAPI udp_relay_server(LPVOID arg);
DWORD WINAPI local_proxy_server(LPVOID arg);
DWORD WINAPI connection_handler(LPVOID arg);
DWORD WINAPI one_way_relay(LPVOID arg);
DWORD WINAPI transfer_handler(LPVOID arg);
BOOL relay_pool_start(void);                       // IOCP relay pool (pb_relay.c)
void relay_pool_stop(void);
// What the live connections view shows about a relayed TCP connection (copied into the relay record).
typedef struct { DWORD pid; UINT32 proxy_id; UINT32 rule_id; UINT16 port; BOOL v6; UINT8 dest[16]; } FLOW_META;
BOOL relay_start(SOCKET client, SOCKET proxy, const FLOW_META *meta);
UINT32 relay_flows_snapshot(PB_FLOW *out, UINT32 max);   // live proxied TCP connections, newest first; raw fields only
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);
DWORD WINAPI cleanup_worker(LPVOID arg);
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved);

#endif // PB_INTERNAL_H