Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic
C ccybersecurityhttp3httpproxylinuxlinux-proxymacosmacos-appmacos-proxyproxifierproxifier-alternativeproxyclienttcpthick-clientudpwindivertwindows
#177 add support for DNS resolution configuration in proxy settings
AnoF-Cyberd773d3de216f
diff --git a/Windows/cli/main.c b/Windows/cli/main.cindex fab50ff..da3cc2e 100644--- a/Windows/cli/main.c+++ b/Windows/cli/main.c@@ -22,7 +22,8 @@typedef uint32_t (*pfnAddProxyConfig)(int type, const char* ip, uint16_t port,- const char* user, const char* pass);+ const char* user, const char* pass,+ int send_domain_to_proxy);typedef uint32_t (*pfnAddRule)(const char* process, const char* hosts,const char* ports, const char* domains,int protocol, int action,@@ -55,6 +56,7 @@ typedef struct {int port;char username[256];char password[256];+ int send_domain_to_proxy; // 1 = proxy resolves DNS (default), 0 = send IP} PBProxyConfig;typedef struct {@@ -345,6 +347,10 @@ static bool load_profile(const char* path, PBProfile* prof)jstr(buf, "Username", c->username, sizeof(c->username));jstr(buf, "Password", c->password, sizeof(c->password));+ // Boolean; default ON when the field is absent (older profiles).+ const char* sd = jfind(buf, "SendDomainToProxy");+ c->send_domain_to_proxy = (!sd || strncmp(sd, "true", 4) == 0) ? 1 : 0;+free(buf);if (c->host[0] != '\0' && c->port > 0 && c->port <= 65535)@@ -885,7 +891,7 @@ int main(int argc, char* argv[]){PBProxyConfig* c = &prof.configs[i];uint32_t did = g_AddProxyConfig(c->type, c->host, (uint16_t)c->port,- c->username, c->password);+ c->username, c->password, c->send_domain_to_proxy);if (did == 0){fprintf(stderr, " [%d] ERROR: Failed to add %s %s:%d\n",diff --git a/Windows/gui/api/pb_api.h b/Windows/gui/api/pb_api.hindex 8385471..386b5aa 100644--- a/Windows/gui/api/pb_api.h+++ b/Windows/gui/api/pb_api.h@@ -20,8 +20,8 @@ typedef void (*PBConnectionCallback)(const char* process_name, DWORD pid,const char* proxy_info);// exported function pointer typedefs-typedef UINT32 (*PFN_AddProxyConfig)(PBProxyType, const char*, unsigned short, const char*, const char*);-typedef BOOL (*PFN_EditProxyConfig)(UINT32, PBProxyType, const char*, unsigned short, const char*, const char*);+typedef UINT32 (*PFN_AddProxyConfig)(PBProxyType, const char*, unsigned short, const char*, const char*, BOOL);+typedef BOOL (*PFN_EditProxyConfig)(UINT32, PBProxyType, const char*, unsigned short, const char*, const char*, BOOL);typedef BOOL (*PFN_DeleteProxyConfig)(UINT32);typedef int (*PFN_TestProxyConfig)(UINT32, const char*, unsigned short, char*, size_t);typedef void (*PBTestLogCallback)(const char* line, void* user);diff --git a/Windows/gui/loc/loc.h b/Windows/gui/loc/loc.hindex 78defb6..3aaa9f0 100644--- a/Windows/gui/loc/loc.h+++ b/Windows/gui/loc/loc.h@@ -36,7 +36,7 @@ enum {S_AB_VERSION, S_AB_DESC, S_AB_AUTHOR, S_AB_WEB, S_AB_GITHUB, S_AB_LICENSE,S_M_UPDATE, S_UPD_TITLE, S_UPD_AVAIL, S_UPD_NOTES, S_UPD_NOW, S_UPD_LATER,S_UPD_DONTASK, S_UPD_LATEST, S_UPD_ERR, S_UPD_DLING, S_UPD_DLFAIL,- S_L_NAME, S_COL_NAME,+ S_L_NAME, S_COL_NAME, S_CHK_SENDDOMAIN,S_COUNT};@@ -193,6 +193,7 @@ static const wchar_t* const g_strings[S_COUNT][2] = {/* S_UPD_DLFAIL */ { L"Download failed.", L"下载失败。" },/* S_L_NAME */ { L"Name:", L"名称:" },/* S_COL_NAME */ { L"Name", L"名称" },+ /* S_CHK_SENDDOMAIN */ { L"Let proxy resolve DNS (send hostname)", L"由代理解析 DNS (发送主机名)" },};static const wchar_t* T(int id) { return g_strings[id][g_lang ? 1 : 0]; }diff --git a/Windows/gui/main.c b/Windows/gui/main.cindex e556b1d..93617af 100644--- a/Windows/gui/main.c+++ b/Windows/gui/main.c@@ -229,7 +229,7 @@ static void ApplyConfigs(void)int type = (_wcsicmp(c->type, L"HTTP") == 0) ? PB_PROXY_HTTP : PB_PROXY_SOCKS5;char h[256], u[256], p[256];W2Ux(c->host, h, sizeof(h)); W2Ux(c->user, u, sizeof(u)); W2Ux(c->pass, p, sizeof(p));- c->nativeId = g_api.AddProxyConfig((PBProxyType)type, h, (unsigned short)_wtoi(c->port), u, p);+ c->nativeId = g_api.AddProxyConfig((PBProxyType)type, h, (unsigned short)_wtoi(c->port), u, p, c->sendDomain ? TRUE : FALSE);if (c->storedId == 0) c->storedId = c->nativeId;}}diff --git a/Windows/gui/profile/profile.c b/Windows/gui/profile/profile.cindex 826686a..64ab4f7 100644--- a/Windows/gui/profile/profile.c+++ b/Windows/gui/profile/profile.c@@ -179,6 +179,7 @@ void PB_ProfileLoad(const wchar_t* name, PBProfile* p)u2w(json_str(c, "Port", ""), cf->port, 16);u2w(json_str(c, "Username", ""), cf->user, 128);u2w(json_str(c, "Password", ""), cf->pass, 128);+ cf->sendDomain = json_bool(c, "SendDomainToProxy", 1); // default on}JVal* rules = json_get(root, "ProxyRules");@@ -249,7 +250,8 @@ BOOL PB_ProfileSave(const wchar_t* name, const PBProfile* p)put_kv_str(&b, " ", "Host", c->host, ",\n");put_kv_str(&b, " ", "Port", c->port, ",\n");put_kv_str(&b, " ", "Username", c->user, ",\n");- put_kv_str(&b, " ", "Password", c->pass, "\n");+ put_kv_str(&b, " ", "Password", c->pass, ",\n");+ sb_put(&b, " \"SendDomainToProxy\": "); sb_put(&b, c->sendDomain ? "true" : "false"); sb_put(&b, "\n");sb_put(&b, " }");}sb_put(&b, p->cfgCount ? "\n ],\n" : "],\n");diff --git a/Windows/gui/profile/profile.h b/Windows/gui/profile/profile.hindex 79debc9..f43351d 100644--- a/Windows/gui/profile/profile.h+++ b/Windows/gui/profile/profile.h@@ -26,6 +26,7 @@ typedef struct {wchar_t port[16];wchar_t user[128];wchar_t pass[128];+ int sendDomain; // 1 = proxy resolves DNS (send hostname), 0 = send IP} PBConfig;typedef struct {diff --git a/Windows/gui/res/app.rc b/Windows/gui/res/app.rcindex dee33c3..7ea904c 100644--- a/Windows/gui/res/app.rc+++ b/Windows/gui/res/app.rc@@ -72,6 +72,7 @@ BEGINEDITTEXT IDC_SE_PORT, 268, 44, 36, 12, ES_AUTOHSCROLL | ES_NUMBER | WS_TABSTOPLTEXT "Protocol:", IDC_SE_L_PROTO, 18, 70, 44, 10COMBOBOX IDC_SE_PROTO, 66, 68, 168, 80, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP+ AUTOCHECKBOX "Let proxy resolve DNS", IDC_SE_SENDDOMAIN, 18, 82, 288, 10, WS_TABSTOPGROUPBOX "Authentication", IDC_SE_G_AUTH, 8, 102, 304, 66AUTOCHECKBOX "Enable", IDC_SE_AUTH, 18, 118, 60, 10LTEXT "Username:", IDC_SE_L_USER, 18, 136, 44, 10diff --git a/Windows/gui/res/resource.h b/Windows/gui/res/resource.hindex 74a57ee..b994c3c 100644--- a/Windows/gui/res/resource.h+++ b/Windows/gui/res/resource.h@@ -86,6 +86,7 @@#define IDC_SE_G_AUTH 336 // "Authentication" group box#define IDC_SE_NAME 327 // friendly name edit#define IDC_SE_L_NAME 337 // "Name:" label+#define IDC_SE_SENDDOMAIN 328 // "let proxy resolve DNS" checkbox// Proxy Checker dialog#define IDD_CHECKER 340diff --git a/Windows/gui/ui/dialogs.h b/Windows/gui/ui/dialogs.hindex 85f9d0b..ee05296 100644--- a/Windows/gui/ui/dialogs.h+++ b/Windows/gui/ui/dialogs.h@@ -139,6 +139,7 @@ INT_PTR CALLBACK ServerEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)SetDlgItemTextW(dlg, IDC_SE_L_PORT, T(S_L_PORT));SetDlgItemTextW(dlg, IDC_SE_L_PROTO, T(S_L_PROTO));SetDlgItemTextW(dlg, IDC_SE_AUTH, T(S_CHK_ENABLE));+ SetDlgItemTextW(dlg, IDC_SE_SENDDOMAIN, T(S_CHK_SENDDOMAIN));SetDlgItemTextW(dlg, IDC_SE_L_USER, T(S_L_USER));SetDlgItemTextW(dlg, IDC_SE_L_PASS, T(S_L_PASS));SetDlgItemTextW(dlg, IDOK, T(S_BTN_OK));@@ -152,6 +153,7 @@ INT_PTR CALLBACK ServerEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)if (c->port[0]) SetDlgItemTextW(dlg, IDC_SE_PORT, c->port); else SetDlgItemInt(dlg, IDC_SE_PORT, 1080, FALSE);SetDlgItemTextW(dlg, IDC_SE_USER, c->user);SetDlgItemTextW(dlg, IDC_SE_PASS, c->pass);+ CheckDlgButton(dlg, IDC_SE_SENDDOMAIN, c->sendDomain ? BST_CHECKED : BST_UNCHECKED);BOOL hasAuth = c->user[0] != 0;CheckDlgButton(dlg, IDC_SE_AUTH, hasAuth ? BST_CHECKED : BST_UNCHECKED);EnableWindow(GetDlgItem(dlg, IDC_SE_USER), hasAuth);@@ -177,6 +179,7 @@ INT_PTR CALLBACK ServerEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)lstrcpynW(c->type, isHttp ? L"HTTP" : L"SOCKS5", 16);GetDlgItemTextW(dlg, IDC_SE_NAME, c->name, 128);GetDlgItemTextW(dlg, IDC_SE_ADDR, c->host, 128);+ c->sendDomain = (IsDlgButtonChecked(dlg, IDC_SE_SENDDOMAIN) == BST_CHECKED) ? 1 : 0;if (!c->name[0]) lstrcpynW(c->name, c->host[0] ? c->host : L"Proxy Server", 128);GetDlgItemTextW(dlg, IDC_SE_PORT, c->port, 16);if (IsDlgButtonChecked(dlg, IDC_SE_AUTH) == BST_CHECKED)@@ -321,11 +324,12 @@ INT_PTR CALLBACK ServersDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)if (g_profile.cfgCount >= PB_MAX_CFG) { MessageBoxW(dlg, L"Config limit reached.", APP_TITLE, MB_OK); return TRUE; }PBConfig c; ZeroMemory(&c, sizeof(c));lstrcpynW(c.name, L"Proxy Server", 128);+ c.sendDomain = 1; // default: let the proxy resolve DNSif (DialogBoxParamW(g_hInst, MAKEINTRESOURCEW(IDD_SERVER), dlg, ServerEditDlgProc, (LPARAM)&c) == 1){char h[256], u[256], p[256]; W2Ux(c.host, h, sizeof(h)); W2Ux(c.user, u, sizeof(u)); W2Ux(c.pass, p, sizeof(p));UINT32 id = g_api.AddProxyConfig((_wcsicmp(c.type, L"HTTP") == 0) ? PB_PROXY_HTTP : PB_PROXY_SOCKS5,- h, (unsigned short)_wtoi(c.port), u, p);+ h, (unsigned short)_wtoi(c.port), u, p, c.sendDomain ? TRUE : FALSE);if (id > 0) { c.nativeId = id; c.storedId = id; g_profile.cfg[g_profile.cfgCount++] = c; SaveActive(); RefreshServerList(lv); }else MessageBoxW(dlg, T(S_ERR_ADDCFG), APP_TITLE, MB_OK | MB_ICONERROR);}@@ -341,7 +345,7 @@ INT_PTR CALLBACK ServersDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)g_profile.cfg[sel] = c;char h[256], u[256], p[256]; W2Ux(c.host, h, sizeof(h)); W2Ux(c.user, u, sizeof(u)); W2Ux(c.pass, p, sizeof(p));g_api.EditProxyConfig(c.nativeId, (_wcsicmp(c.type, L"HTTP") == 0) ? PB_PROXY_HTTP : PB_PROXY_SOCKS5,- h, (unsigned short)_wtoi(c.port), u, p);+ h, (unsigned short)_wtoi(c.port), u, p, c.sendDomain ? TRUE : FALSE);SaveActive(); RefreshServerList(lv);ListView_SetItemState(lv, sel, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);}diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.cindex 85fd577..587225d 100644--- a/Windows/src/ProxyBridge.c+++ b/Windows/src/ProxyBridge.c@@ -147,6 +147,7 @@ typedef struct {UINT16 port;char username[256];char password[256];+ BOOL send_domain_to_proxy; // TRUE = proxy resolves DNS (send hostname), FALSE = send IPUINT32 resolved_ip; // cached at add/edit time - avoids DNS per connectionULONGLONG last_udp_attempt;SOCKET udp_tcp_ctrl;@@ -2825,11 +2826,11 @@ static int http_connect_v6(SOCKET s, const UINT8 dest_ip6[16], UINT16 dest_port,char addr_str[64];inet_ntop(AF_INET6, dest_ip6, addr_str, sizeof(addr_str));- // Use cached domain name if available so the proxy sees the hostname+ // Use the cached hostname only if this config opts to let the proxy resolve DNS.char cached_domain[256];const char *host_part;char host_buf[270]; // big enough for [ipv6]:port or domain- if (dns_cache_lookup_v6(dest_ip6, cached_domain, sizeof(cached_domain)))+ if (cfg->send_domain_to_proxy && dns_cache_lookup_v6(dest_ip6, cached_domain, sizeof(cached_domain))){host_part = cached_domain;strncpy_s(host_buf, sizeof(host_buf), cached_domain, _TRUNCATE);@@ -2897,11 +2898,11 @@ static int http_connect(SOCKET s, UINT32 dest_ip, UINT16 dest_port, const PROXY_int status_code;BOOL use_auth = (cfg != NULL && cfg->username[0] != '\0');- // Use cached domain name if available so the proxy sees the hostname+ // Use the cached hostname only if this config opts to let the proxy resolve DNS.char cached_domain[256];char ip_str[32];const char *host_part;- if (dns_cache_lookup(dest_ip, cached_domain, sizeof(cached_domain)))+ if (cfg->send_domain_to_proxy && dns_cache_lookup(dest_ip, cached_domain, sizeof(cached_domain))){host_part = cached_domain;}@@ -3804,16 +3805,18 @@ static DWORD WINAPI connection_handler(LPVOID arg){int rc;char cached_domain[256];+ // Per-config: only hand the hostname to the proxy (socks5h) when this config+ // opts in; otherwise send the locally-resolved IP (socks5).if (is_ipv6){- if (dns_cache_lookup_v6(dest_ip6, cached_domain, sizeof(cached_domain)))+ if (proxy->send_domain_to_proxy && dns_cache_lookup_v6(dest_ip6, cached_domain, sizeof(cached_domain)))rc = socks5_connect_domain(socks_sock, cached_domain, dest_port, proxy);elserc = socks5_connect_v6(socks_sock, dest_ip6, dest_port, proxy);}else{- if (dns_cache_lookup(dest_ip, cached_domain, sizeof(cached_domain)))+ if (proxy->send_domain_to_proxy && dns_cache_lookup(dest_ip, cached_domain, sizeof(cached_domain)))rc = socks5_connect_domain(socks_sock, cached_domain, dest_port, proxy);elserc = socks5_connect(socks_sock, dest_ip, dest_port, proxy);@@ -4686,7 +4689,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_preturn TRUE;}-PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password)+PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password, BOOL send_domain_to_proxy){if (proxy_ip == NULL || proxy_ip[0] == '\0' || proxy_port == 0)return 0;@@ -4703,6 +4706,7 @@ PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* prcfg->config_id = g_next_config_id++;cfg->type = (type == PROXY_TYPE_HTTP) ? PROXY_TYPE_HTTP : PROXY_TYPE_SOCKS5;cfg->port = proxy_port;+ cfg->send_domain_to_proxy = send_domain_to_proxy;strncpy_s(cfg->host, sizeof(cfg->host), proxy_ip, _TRUNCATE);cfg->resolved_ip = resolve_hostname(proxy_ip);if (username != NULL) strncpy_s(cfg->username, sizeof(cfg->username), username, _TRUNCATE);@@ -4716,7 +4720,7 @@ PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* prreturn cfg->config_id;}-PROXYBRIDGE_API BOOL ProxyBridge_EditProxyConfig(UINT32 config_id, ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password)+PROXYBRIDGE_API BOOL ProxyBridge_EditProxyConfig(UINT32 config_id, ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password, BOOL send_domain_to_proxy){if (proxy_ip == NULL || proxy_ip[0] == '\0' || proxy_port == 0)return FALSE;@@ -4737,6 +4741,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_EditProxyConfig(UINT32 config_id, ProxyType typcfg->type = (type == PROXY_TYPE_HTTP) ? PROXY_TYPE_HTTP : PROXY_TYPE_SOCKS5;cfg->port = proxy_port;+ cfg->send_domain_to_proxy = send_domain_to_proxy;strncpy_s(cfg->host, sizeof(cfg->host), proxy_ip, _TRUNCATE);cfg->resolved_ip = resolved;cfg->username[0] = '\0';diff --git a/Windows/src/ProxyBridge.h b/Windows/src/ProxyBridge.hindex d94db7e..2d40db6 100644--- a/Windows/src/ProxyBridge.h+++ b/Windows/src/ProxyBridge.h@@ -37,8 +37,10 @@ typedef enum {// Multiple proxy config management// proxy_ip can be IP address or hostname; returns config_id (>0) on success, 0 on failure-PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password);-PROXYBRIDGE_API BOOL ProxyBridge_EditProxyConfig(UINT32 config_id, ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password);+// send_domain_to_proxy: TRUE = the proxy resolves DNS (send hostname; socks5h / CONNECT domain),+// FALSE = resolve locally and send the IP (socks5 / CONNECT ip). Per-config.+PROXYBRIDGE_API UINT32 ProxyBridge_AddProxyConfig(ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password, BOOL send_domain_to_proxy);+PROXYBRIDGE_API BOOL ProxyBridge_EditProxyConfig(UINT32 config_id, ProxyType type, const char* proxy_ip, UINT16 proxy_port, const char* username, const char* password, BOOL send_domain_to_proxy);PROXYBRIDGE_API BOOL ProxyBridge_DeleteProxyConfig(UINT32 config_id);PROXYBRIDGE_API int ProxyBridge_TestProxyConfig(UINT32 config_id, const char* target_host, UINT16 target_port, char* result_buffer, size_t buffer_size);// Detailed multi-step proxy check (like Proxifier's Proxy Checker). Streams human-readable