Skip to content

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

feat(windows): one Target Hosts field for IPs and domain names, longer lists, multiline editor

AnoF-Cyber3047bc702205
diff --git a/Windows/README.md b/Windows/README.md
index 231553b..afd5d26 100644
--- a/Windows/README.md
+++ b/Windows/README.md
@@ -116,8 +116,18 @@ ProxyBridge supports multiple proxy server configurations simultaneously. Each p
- IPv6 exact: `::1`, `2001:db8::1`
- IPv6 CIDR: `2001:db8::/32`, `fe80::/10`
- IPv6 range: `2001:db8::1-2001:db8::ff`
+ - Domain names: `github.com`, `*.github.com`
- Leave empty or use `*` for all hosts
+ IP addresses and domain names go in the **same** field and can be mixed: `10.*.*.*; github.com; *.google.com`
+ matches a connection when its IP matches an IP entry **or** its domain matches a domain entry. The box is
+ multiline, so a long list can be written one entry per line; line breaks are only for readability and
+ `;` separates the entries (up to 8191 characters).
+
+ Domain names are learned from the application's own DNS lookups, so they only work with unencrypted DNS
+ (turn off "Secure DNS" / DNS over HTTPS in the browser). In profile files the entries are saved as
+ `TargetHosts` (IPs) and `TargetDomains` (domain names), which other ProxyBridge apps read the same way.
+
**Target Ports (Optional):**
- Specific ports: `80; 8080`
- Port range: `80-8000`
diff --git a/Windows/cli/main.c b/Windows/cli/main.c
index 8702296..d5a5e2e 100644
--- a/Windows/cli/main.c
+++ b/Windows/cli/main.c
@@ -62,7 +62,7 @@ typedef struct {
char process_name[256];
char target_hosts[512];
char target_ports[256];
- char target_domains[512]; // "*", "google.com", "*.google.com;*.gstatic.com"
+ char target_domains[8192]; // "*", "google.com", "*.google.com;*.gstatic.com" (long lists)
int protocol; // 0=TCP, 1=UDP, 2=BOTH
int action; // 0=PROXY, 1=DIRECT, 2=BLOCK
int is_enabled;
@@ -257,6 +257,97 @@ static const char* jfind(const char* json, const char* key)
}
// Extract a JSON string value for key into out[size]. Returns true on success.
+// Same cleanup the GUI does for Target Domains: drop line breaks/control characters, trim blanks around
+// ';' / ',', drop empty entries, join with ';'. In place; the text never grows.
+static void normalize_list(char* s)
+{
+ char* w = s; const char* r = s; bool any = false;
+ while (*r)
+ {
+ while (*r && (*r == ';' || *r == ',' || (unsigned char)*r <= ' ')) r++;
+ if (!*r) break;
+ if (any) *w++ = ';';
+ char* lastKept = w;
+ while (*r && *r != ';' && *r != ',')
+ {
+ char c = *r++;
+ if ((unsigned char)c < ' ') continue;
+ *w++ = c;
+ if (c != ' ') lastKept = w;
+ }
+ w = lastKept;
+ any = true;
+ }
+ *w = '\0';
+}
+
+// IPs and domain names are one list in the GUI but two keys in the profile (TargetHosts / TargetDomains) and in
+// the engine. An entry is an address pattern when it is made of digits and . - / * (or, with ':', hex digits and
+// : . - / *); everything else is a domain name.
+static bool is_ip_token(const char* s, size_t n)
+{
+ bool colon = false, digit_or_star = false;
+ for (size_t i = 0; i < n; i++) if (s[i] == ':') colon = true;
+ for (size_t i = 0; i < n; i++)
+ {
+ char c = s[i];
+ bool digit = (c >= '0' && c <= '9');
+ bool hex = (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+ bool punct = (c == '.' || c == '-' || c == '/' || c == '*' || c == ':');
+ if (digit || c == '*') digit_or_star = true;
+ if (!(digit || punct || (colon && hex))) return false;
+ }
+ return digit_or_star;
+}
+
+static void copy_list(char* dst, size_t cap, const char* src)
+{
+ if (strlen(src) < cap) { strcpy(dst, src); return; }
+ memcpy(dst, src, cap - 1); dst[cap - 1] = '\0'; // too long: keep whole entries only
+ char* semi = strrchr(dst, ';');
+ if (semi) *semi = '\0'; else dst[0] = '\0';
+}
+
+// Re-sorts both lists by entry type, so a domain written in TargetHosts (or an address in TargetDomains)
+// still works. Each result is "*" when it has no entries, and both are "*" when the list contains "*".
+static void sort_hosts(char* hosts, size_t hcap, char* doms, size_t dcap)
+{
+ size_t cap = strlen(hosts) + strlen(doms) + 4;
+ char* ips = (char*)malloc(cap); char* dm = (char*)malloc(cap);
+ if (!ips || !dm) { free(ips); free(dm); return; }
+ ips[0] = dm[0] = '\0';
+ size_t io = 0, dd = 0; bool any = false;
+ // A side that is empty or just "*" means "no restriction from this key" and adds no entries; a "*" next
+ // to other entries means any host.
+ bool has_hosts = hosts[0] && strcmp(hosts, "*") != 0;
+ bool has_doms = doms[0] && strcmp(doms, "*") != 0;
+ for (int pass = 0; pass < 2; pass++)
+ {
+ if (pass == 0 ? !has_hosts : !has_doms) continue;
+ const char* p = pass ? doms : hosts;
+ while (*p)
+ {
+ while (*p == ';' || *p == ',' || (*p && (unsigned char)*p <= ' ')) p++;
+ if (!*p) break;
+ const char* st = p;
+ while (*p && *p != ';' && *p != ',') p++;
+ const char* e = p;
+ while (e > st && (unsigned char)e[-1] <= ' ') e--;
+ size_t n = (size_t)(e - st);
+ if (n == 0) continue;
+ if (n == 1 && st[0] == '*') { any = true; continue; }
+ bool ip = is_ip_token(st, n);
+ char* out = ip ? ips : dm; size_t* pos = ip ? &io : &dd;
+ if (*pos) out[(*pos)++] = ';';
+ memcpy(out + *pos, st, n); *pos += n; out[*pos] = '\0';
+ }
+ }
+ if (any) io = dd = 0;
+ copy_list(hosts, hcap, io ? ips : "*");
+ copy_list(doms, dcap, dd ? dm : "*");
+ free(ips); free(dm);
+}
+
static bool jstr(const char* json, const char* key, char* out, size_t size)
{
if (size == 0) return false;
@@ -461,6 +552,9 @@ static bool load_profile(const char* path, PBProfile* prof)
jstr(buf, "TargetHosts", r->target_hosts, sizeof(r->target_hosts));
jstr(buf, "TargetPorts", r->target_ports, sizeof(r->target_ports));
jstr(buf, "TargetDomains", r->target_domains, sizeof(r->target_domains));
+ normalize_list(r->target_domains); // profiles may hold line breaks (formatting only)
+ normalize_list(r->target_hosts);
+ sort_hosts(r->target_hosts, sizeof(r->target_hosts), r->target_domains, sizeof(r->target_domains));
char ps[16] = {0};
jstr(buf, "Protocol", ps, sizeof(ps));
@@ -938,7 +1032,7 @@ int main(int argc, char* argv[])
// ── Load profile ──────────────────────────────────────────────────────────
printf("Loading profile: %s\n", profile_path);
- PBProfile prof;
+ static PBProfile prof; // large (many rules x long domain lists): keep it off the stack
if (!load_profile(profile_path, &prof))
return 1;
diff --git a/Windows/gui/loc/loc.h b/Windows/gui/loc/loc.h
index 77856df..b450cef 100644
--- a/Windows/gui/loc/loc.h
+++ b/Windows/gui/loc/loc.h
@@ -18,9 +18,9 @@ enum {
S_TAB_CONN, S_TAB_ACT, S_TRAY_SHOW, S_TRAY_EXIT,
S_DLG_SETTINGS, S_L_TYPE, S_L_HOST, S_L_PORT, S_L_USER, S_L_PASS, S_L_CFGLIST,
S_BTN_ADD, S_BTN_UPDATE, S_BTN_DELETE, S_BTN_CLOSE,
- S_DLG_RULES, S_L_APPS, S_L_HOSTS, S_L_PORTS, S_L_DOMAINS, S_L_PROTO, S_L_ACTION, S_L_CFGID,
+ S_DLG_RULES, S_L_APPS, S_L_HOSTS, S_L_PORTS, S_L_PROTO, S_L_ACTION, S_L_CFGID,
S_BTN_ADDRULE, S_BTN_TOGGLE,
- S_COL_ON, S_COL_APPS, S_COL_HOSTS, S_COL_PORTS, S_COL_DOMAINS, S_COL_PROTO, S_COL_ACTION, S_COL_CFG,
+ S_COL_ON, S_COL_APPS, S_COL_HOSTS, S_COL_PORTS, S_COL_PROTO, S_COL_ACTION, S_COL_CFG,
S_DLG_NEWPROFILE, S_L_PROFNAME, S_BTN_OK, S_BTN_CANCEL,
S_ABOUT_TITLE, S_ABOUT_BODY, S_STARTED, S_STARTFAIL,
S_ERR_HOST, S_ERR_ADDCFG, S_ERR_ADDRULE, S_INFO_NODELDEFAULT, S_ERR_IMPORT, S_ERR_EXPORT,
@@ -29,7 +29,7 @@ enum {
S_COL_ADDR, S_COL_TYPE, S_RULES_HINT,
S_BTN_CHECK, S_TEST_OK, S_TEST_FAIL, S_TEST_TITLE,
S_DLG_CHECKER, S_BTN_RETEST, S_TEST_RUNNING, S_CK_HOST, S_BTN_BROWSE,
- S_EX_APPS, S_EX_HOSTS, S_EX_PORTS, S_EX_DOMAINS, S_UDP_NOTE, S_EX_LOCALHOST,
+ S_EX_APPS, S_EX_HOSTS, S_EX_PORTS, S_UDP_NOTE, S_EX_LOCALHOST,
S_M_AUTOCLEAR, S_M_RENAME, S_M_LOGFILTERS, S_DLG_RENAME,
S_DLG_FILTERS, S_DLG_FILTER, S_FL_DESC, S_L_MODE, S_L_PROC, S_L_IP,
S_COL_MODE, S_COL_PROC, S_COL_IP, S_INFO_NORENAMEDEFAULT, S_ERR_RUNNING,
@@ -87,7 +87,6 @@ static const wchar_t* const g_strings[S_COUNT][2] = {
/* S_L_APPS */ { L"Applications:", L"应用程序:" },
/* S_L_HOSTS */ { L"Target Hosts:", L"目标主机:" },
/* S_L_PORTS */ { L"Target Ports:", L"目标端口:" },
- /* S_L_DOMAINS */ { L"Target Domains:", L"目标域名:" },
/* S_L_PROTO */ { L"Protocol:", L"协议:" },
/* S_L_ACTION */ { L"Action:", L"操作:" },
/* S_L_CFGID */ { L"Proxy Config Id:", L"代理配置 ID:" },
@@ -97,7 +96,6 @@ static const wchar_t* const g_strings[S_COUNT][2] = {
/* S_COL_APPS */ { L"Applications", L"应用程序" },
/* S_COL_HOSTS */ { L"Hosts", L"主机" },
/* S_COL_PORTS */ { L"Ports", L"端口" },
- /* S_COL_DOMAINS */ { L"Domains", L"域名" },
/* S_COL_PROTO */ { L"Proto", L"协议" },
/* S_COL_ACTION */ { L"Action", L"操作" },
/* S_COL_CFG */ { L"Proxy", L"代理" },
@@ -146,11 +144,9 @@ static const wchar_t* const g_strings[S_COUNT][2] = {
/* S_BTN_BROWSE */ { L"Browse...", L"浏览..." },
/* S_EX_APPS */ { L"Example: iexplore.exe; \"C:\\some app.exe\"; fire*.exe; *.bin",
L"示例: iexplore.exe; \"C:\\某个应用.exe\"; fire*.exe; *.bin" },
- /* S_EX_HOSTS */ { L"Example: 127.0.0.1; 192.168.1.*; 10.1.0.0-10.5.255.255; ::1; 2001:db8::/32; fe80::1-fe80::ff",
- L"示例: 127.0.0.1; 192.168.1.*; 10.1.0.0-10.5.255.255; ::1; 2001:db8::/32; fe80::1-fe80::ff" },
+ /* S_EX_HOSTS */ { L"Example: 192.168.1.*; 10.0.0.0/8; 10.1.0.0-10.5.255.255; ::1; 2001:db8::/32; github.com; *.github.com (domain names need unencrypted DNS). One entry per line is fine; separate entries with ;",
+ L"示例: 192.168.1.*; 10.0.0.0/8; 10.1.0.0-10.5.255.255; ::1; 2001:db8::/32; github.com; *.github.com (域名需要未加密的 DNS)。可以每行一个条目,条目之间用 ; 分隔" },
/* S_EX_PORTS */ { L"Example: 80; 8000-9000; 3128", L"示例: 80; 8000-9000; 3128" },
- /* S_EX_DOMAINS */ { L"Example: google.com; *.google.com; *.gstatic.com (requires unencrypted DNS)",
- L"示例: google.com; *.google.com; *.gstatic.com (需要未加密的 DNS)" },
/* S_UDP_NOTE */ { L"\x26A0 UDP only works with SOCKS5 proxy. Not all SOCKS5 proxies support UDP by default. "
L"SOCKS5 UDP support does not guarantee QUIC or HTTP/3 support, verify your proxy supports these protocols separately.",
L"\x26A0 UDP 仅支持 SOCKS5 代理。并非所有 SOCKS5 代理默认支持 UDP。SOCKS5 UDP 支持并不保证支持 QUIC 或 HTTP/3 -- 请单独确认您的代理是否支持这些协议。" },
diff --git a/Windows/gui/main.c b/Windows/gui/main.c
index 7d73061..22e1381 100644
--- a/Windows/gui/main.c
+++ b/Windows/gui/main.c
@@ -139,7 +139,11 @@ static HMENU g_switchMenu = NULL; // the Profile > Switch submenu (rebuilt
// small helpers
static void W2Ux(const wchar_t* w, char* out, int cch)
-{ WideCharToMultiByte(CP_UTF8, 0, w ? w : L"", -1, out, cch, NULL, NULL); }
+{
+ if (cch <= 0) return;
+ out[0] = 0;
+ if (!WideCharToMultiByte(CP_UTF8, 0, w ? w : L"", -1, out, cch, NULL, NULL)) out[0] = 0; // too small: empty, never unterminated
+}
static wchar_t* U2Wdup(const char* s)
{
@@ -243,43 +247,61 @@ static void ApplyConfigs(void)
}
}
+// The UTF-8 strings of one rule, in a single heap block (worst case 3 bytes per UTF-16 unit, so nothing is
+// ever cut). The GUI keeps IPs and domain names in one list; the engine takes them as two.
+typedef struct { char proc[PB_APPS_MAX * 3 + 4]; char ports[128 * 3 + 4]; char ips[PB_HOSTS_MAX * 3 + 4]; char doms[PB_HOSTS_MAX * 3 + 4]; } EngineStr;
+
+static EngineStr* EngineStrings(const PBRule* r)
+{
+ EngineStr* s = (EngineStr*)malloc(sizeof(*s));
+ size_t n = wcslen(r->hosts) + 2;
+ wchar_t* wi = (wchar_t*)malloc(n * sizeof(wchar_t));
+ wchar_t* wd = (wchar_t*)malloc(n * sizeof(wchar_t));
+ if (!s || !wi || !wd) { free(s); free(wi); free(wd); return NULL; }
+ PB_HostsSplit(r->hosts, wi, n, wd, n);
+ W2Ux(r->proc, s->proc, sizeof(s->proc)); W2Ux(r->ports, s->ports, sizeof(s->ports));
+ W2Ux(wi, s->ips, sizeof(s->ips)); W2Ux(wd, s->doms, sizeof(s->doms));
+ free(wi); free(wd);
+ return s;
+}
+
static void ApplyRules(void)
{
for (int i = 0; i < g_profile.ruleCount; i++)
{
PBRule* r = &g_profile.rule[i];
- char proc[2048], hosts[512], ports[256], domains[512];
- W2Ux(r->proc, proc, sizeof(proc)); W2Ux(r->hosts, hosts, sizeof(hosts));
- W2Ux(r->ports, ports, sizeof(ports)); W2Ux(r->domains, domains, sizeof(domains));
- r->nativeId = g_api.AddRule(proc, hosts, ports, domains,
+ EngineStr* s = EngineStrings(r);
+ if (!s) continue;
+ r->nativeId = g_api.AddRule(s->proc, s->ips, s->ports, s->doms,
(PBRuleProtocol)ProtoIdx(r->proto), (PBRuleAction)ActionIdx(r->action),
ResolveNativeCfg(r->cfgStoredId), r->localhost != 0);
if (r->nativeId && !r->enabled) g_api.DisableRule(r->nativeId);
+ free(s);
}
}
// Add one rule to the engine, returning its (stable) native id. Enable state applied too.
static UINT32 EngineAddRule(PBRule* r)
{
- char proc[2048], hosts[512], ports[256], domains[512];
- W2Ux(r->proc, proc, sizeof(proc)); W2Ux(r->hosts, hosts, sizeof(hosts));
- W2Ux(r->ports, ports, sizeof(ports)); W2Ux(r->domains, domains, sizeof(domains));
- UINT32 id = g_api.AddRule(proc, hosts, ports, domains,
+ EngineStr* s = EngineStrings(r);
+ if (!s) return 0;
+ UINT32 id = g_api.AddRule(s->proc, s->ips, s->ports, s->doms,
(PBRuleProtocol)ProtoIdx(r->proto), (PBRuleAction)ActionIdx(r->action),
ResolveNativeCfg(r->cfgStoredId), r->localhost != 0);
if (id && !r->enabled) g_api.DisableRule(id);
+ free(s);
return id;
}
// Edit an existing rule in place - keeps the same native id and list position.
static void EngineEditRule(PBRule* r)
{
- char proc[2048], hosts[512], ports[256], domains[512];
- W2Ux(r->proc, proc, sizeof(proc)); W2Ux(r->hosts, hosts, sizeof(hosts));
- W2Ux(r->ports, ports, sizeof(ports)); W2Ux(r->domains, domains, sizeof(domains));
- g_api.EditRule(r->nativeId, proc, hosts, ports, domains,
+ EngineStr* s = EngineStrings(r);
+ if (!s) return;
+ g_api.EditRule(r->nativeId, s->proc, s->ips, s->ports, s->doms,
(PBRuleProtocol)ProtoIdx(r->proto), (PBRuleAction)ActionIdx(r->action),
ResolveNativeCfg(r->cfgStoredId), r->localhost != 0);
if (r->enabled) g_api.EnableRule(r->nativeId); else g_api.DisableRule(r->nativeId);
+ free(s);
}
static void UnapplyProfile(void)
diff --git a/Windows/gui/profile/profile.c b/Windows/gui/profile/profile.c
index 92a35c5..344246f 100644
--- a/Windows/gui/profile/profile.c
+++ b/Windows/gui/profile/profile.c
@@ -11,13 +11,147 @@
// UTF-8 <-> UTF-16
static void u2w(const char* s, wchar_t* out, int cch)
{
- if (!s) { if (cch) out[0] = 0; return; }
- MultiByteToWideChar(CP_UTF8, 0, s, -1, out, cch);
+ if (cch <= 0) return;
+ out[0] = 0;
+ if (!s) return;
+ // Convert at most cch-1 source bytes: a UTF-16 string is never longer than its UTF-8 source, so the
+ // result always fits (an over-long value is cut, never overflows or stays unterminated).
+ size_t sl = strlen(s);
+ if (sl > (size_t)(cch - 1)) sl = (size_t)(cch - 1);
+ if (sl == 0) return;
+ int n = MultiByteToWideChar(CP_UTF8, 0, s, (int)sl, out, cch - 1);
+ out[n > 0 ? n : 0] = 0;
}
static void w2u(const wchar_t* w, char* out, int cch)
{
- if (!w) { if (cch) out[0] = 0; return; }
- WideCharToMultiByte(CP_UTF8, 0, w, -1, out, cch, NULL, NULL);
+ if (cch <= 0) return;
+ out[0] = 0;
+ if (!w) return;
+ if (!WideCharToMultiByte(CP_UTF8, 0, w, -1, out, cch, NULL, NULL)) out[0] = 0; // too small: empty, never unterminated
+}
+
+// ---- Target Domains list helpers (see profile.h) ----
+void PB_ListNormalize(wchar_t* s)
+{
+ if (!s) return;
+ wchar_t* w = s; // write position; never passes the read position, so this is in place
+ const wchar_t* r = s;
+ BOOL any = FALSE;
+ while (*r)
+ {
+ while (*r && (*r == L';' || *r == L',' || *r <= L' ')) r++; // separators, blanks, line breaks, controls
+ if (!*r) break;
+ if (any) *w++ = L';';
+ wchar_t* lastKept = w; // end of the entry without trailing blanks
+ while (*r && *r != L';' && *r != L',')
+ {
+ wchar_t c = *r++;
+ if (c < L' ') continue; // a line break/tab inside an entry is ignored
+ *w++ = c;
+ if (c != L' ') lastKept = w;
+ }
+ w = lastKept;
+ any = TRUE;
+ }
+ *w = 0;
+}
+
+void PB_ListFormat(const wchar_t* in, wchar_t* out, size_t cch)
+{
+ if (!out || cch == 0) return;
+ size_t o = 0;
+ for (const wchar_t* p = in ? in : L""; *p && o + 1 < cch; p++)
+ {
+ out[o++] = *p;
+ if (*p == L';')
+ {
+ if (o + 3 > cch) break;
+ out[o++] = L'\r'; out[o++] = L'\n';
+ }
+ }
+ out[o] = 0;
+}
+
+// ---- Target Hosts: IPs and domains as one list (see profile.h) ----
+// An entry is an address pattern when it is made of digits and . - / * (IPv4 forms such as 10.0.0.0/8,
+// 192.168.*.*, 10.1-5.*.7, 1.2.3.4-1.2.3.9) or, if it contains ':', of hex digits and : . - / * (IPv6).
+// Everything else (github.com, *.github.com) is a domain name.
+static BOOL IsIpToken(const wchar_t* s, size_t n)
+{
+ BOOL colon = FALSE, digitOrStar = FALSE;
+ for (size_t i = 0; i < n; i++) if (s[i] == L':') colon = TRUE;
+ for (size_t i = 0; i < n; i++)
+ {
+ wchar_t c = s[i];
+ BOOL digit = (c >= L'0' && c <= L'9');
+ BOOL hex = (c >= L'a' && c <= L'f') || (c >= L'A' && c <= L'F');
+ BOOL punct = (c == L'.' || c == L'-' || c == L'/' || c == L'*' || c == L':');
+ if (digit || c == L'*') digitOrStar = TRUE;
+ if (!(digit || punct || (colon && hex))) return FALSE;
+ }
+ return digitOrStar;
+}
+
+static BOOL HostsPresent(const wchar_t* s)
+{
+ return s && s[0] && !(s[0] == L'*' && s[1] == 0);
+}
+
+void PB_HostsMerge(const wchar_t* ips, const wchar_t* doms, wchar_t* out, size_t cch)
+{
+ if (!out || cch == 0) return;
+ out[0] = 0;
+ BOOL a = HostsPresent(ips), d = HostsPresent(doms);
+ if (!a && !d) { if (cch >= 2) { out[0] = L'*'; out[1] = 0; } return; }
+ size_t la = a ? wcslen(ips) : 0, ld = d ? wcslen(doms) : 0;
+ size_t total = la + ((a && d) ? 1 : 0) + ld;
+ wchar_t* t = (wchar_t*)malloc((total + 1) * sizeof(wchar_t));
+ if (!t) { if (cch >= 2) { out[0] = L'*'; out[1] = 0; } return; }
+ size_t o = 0;
+ if (a) { memcpy(t, ips, la * sizeof(wchar_t)); o = la; }
+ if (a && d) t[o++] = L';';
+ if (d) { memcpy(t + o, doms, ld * sizeof(wchar_t)); o += ld; }
+ t[o] = 0;
+ if (total <= cch - 1) memcpy(out, t, (total + 1) * sizeof(wchar_t));
+ else
+ {
+ // too long for the buffer: keep whole entries only
+ memcpy(out, t, (cch - 1) * sizeof(wchar_t));
+ out[cch - 1] = 0;
+ if (t[cch - 1] != L';') { wchar_t* semi = wcsrchr(out, L';'); if (semi) *semi = 0; else out[0] = 0; }
+ }
+ free(t);
+ if (!out[0] && cch >= 2) { out[0] = L'*'; out[1] = 0; }
+}
+
+void PB_HostsSplit(const wchar_t* merged, wchar_t* ips, size_t ipsCch, wchar_t* doms, size_t domsCch)
+{
+ if (ipsCch) ips[0] = 0;
+ if (domsCch) doms[0] = 0;
+ size_t io = 0, dd = 0;
+ BOOL any = FALSE;
+ const wchar_t* p = merged ? merged : L"";
+ while (*p)
+ {
+ while (*p == L';' || *p == L',' || (*p && *p <= L' ')) p++;
+ if (!*p) break;
+ const wchar_t* s = p;
+ while (*p && *p != L';' && *p != L',') p++;
+ const wchar_t* e = p;
+ while (e > s && e[-1] <= L' ') e--;
+ size_t n = (size_t)(e - s);
+ if (n == 0) continue;
+ if (n == 1 && s[0] == L'*') { any = TRUE; continue; }
+ BOOL isIp = IsIpToken(s, n);
+ wchar_t* out = isIp ? ips : doms; size_t* pos = isIp ? &io : &dd; size_t cch = isIp ? ipsCch : domsCch;
+ size_t need = n + (*pos ? 1 : 0);
+ if (*pos + need + 1 > cch) continue; // cannot happen with correctly sized buffers; never overflows
+ if (*pos) out[(*pos)++] = L';';
+ memcpy(out + *pos, s, n * sizeof(wchar_t)); *pos += n; out[*pos] = 0;
+ }
+ if (any || (!io && !dd)) io = dd = 0;
+ if (!io && ipsCch >= 2) { ips[0] = L'*'; ips[1] = 0; }
+ if (!dd && domsCch >= 2) { doms[0] = L'*'; doms[1] = 0; }
}
// paths
@@ -220,9 +354,21 @@ void PB_ProfileLoad(const wchar_t* name, PBProfile* p)
PBRule* ru = &p->rule[p->ruleCount++];
u2w(json_str(r, "Name", "ProxyBridge Rule"), ru->name, 128);
u2w(json_str(r, "ProcessName", "*"), ru->proc, PB_APPS_MAX);
- u2w(json_str(r, "TargetHosts", "*"), ru->hosts, 256);
u2w(json_str(r, "TargetPorts", "*"), ru->ports, 128);
- u2w(json_str(r, "TargetDomains", "*"), ru->domains, 256);
+ {
+ // the file keeps IPs and domains in two keys; the GUI shows them as one list
+ wchar_t* wi = (wchar_t*)malloc(PB_HOSTS_MAX * sizeof(wchar_t));
+ wchar_t* wd = (wchar_t*)malloc(PB_HOSTS_MAX * sizeof(wchar_t));
+ if (wi && wd)
+ {
+ u2w(json_str(r, "TargetHosts", "*"), wi, PB_HOSTS_MAX);
+ u2w(json_str(r, "TargetDomains", "*"), wd, PB_HOSTS_MAX);
+ PB_ListNormalize(wi); PB_ListNormalize(wd); // older/hand-edited files may hold stray line breaks
+ PB_HostsMerge(wi, wd, ru->hosts, PB_HOSTS_MAX);
+ }
+ else lstrcpynW(ru->hosts, L"*", PB_HOSTS_MAX);
+ free(wi); free(wd);
+ }
u2w(json_str(r, "Protocol", "TCP"), ru->proto, 8);
u2w(json_str(r, "Action", "PROXY"), ru->action, 16);
ru->enabled = json_bool(r, "IsEnabled", 1);
@@ -252,9 +398,13 @@ void PB_ProfileLoad(const wchar_t* name, PBProfile* p)
static void put_kv_str(StrBuf* b, const char* indent, const char* key, const wchar_t* wval, const char* trail)
{
- char tmp[PB_APPS_MAX * 3 + 4]; w2u(wval, tmp, sizeof(tmp)); // UTF-8 worst case ~3 bytes/char
+ // UTF-8 worst case is 3 bytes per UTF-16 unit; sized from the actual string so nothing is cut or overflows
+ size_t cap = (wval ? wcslen(wval) : 0) * 3 + 4;
+ char* tmp = (char*)malloc(cap);
+ if (tmp) w2u(wval, tmp, (int)cap);
sb_put(b, indent); sb_put(b, "\""); sb_put(b, key); sb_put(b, "\": ");
- sb_json_str(b, tmp); sb_put(b, trail);
+ sb_json_str(b, tmp ? tmp : ""); sb_put(b, trail);
+ free(tmp);
}
BOOL PB_ProfileSave(const wchar_t* name, const PBProfile* p)
@@ -297,9 +447,18 @@ BOOL PB_ProfileSave(const wchar_t* name, const PBProfile* p)
sb_put(&b, i ? ",\n" : "\n"); sb_put(&b, " {\n");
put_kv_str(&b, " ", "Name", r->name, ",\n");
put_kv_str(&b, " ", "ProcessName", r->proc, ",\n");
- put_kv_str(&b, " ", "TargetHosts", r->hosts, ",\n");
- put_kv_str(&b, " ", "TargetPorts", r->ports, ",\n");
- put_kv_str(&b, " ", "TargetDomains", r->domains, ",\n");
+ {
+ // IPs -> TargetHosts, domain names -> TargetDomains (the format macOS, Linux and older versions read)
+ size_t hn = wcslen(r->hosts) + 2;
+ wchar_t* wi = (wchar_t*)malloc(hn * sizeof(wchar_t));
+ wchar_t* wd = (wchar_t*)malloc(hn * sizeof(wchar_t));
+ if (wi && wd) PB_HostsSplit(r->hosts, wi, hn, wd, hn);
+ else { if (wi) wcscpy_s(wi, hn, L"*"); if (wd) wcscpy_s(wd, hn, L"*"); }
+ put_kv_str(&b, " ", "TargetHosts", wi ? wi : L"*", ",\n");
+ put_kv_str(&b, " ", "TargetPorts", r->ports, ",\n");
+ put_kv_str(&b, " ", "TargetDomains", wd ? wd : L"*", ",\n");
+ free(wi); free(wd);
+ }
put_kv_str(&b, " ", "Protocol", r->proto, ",\n");
put_kv_str(&b, " ", "Action", r->action, ",\n");
sb_put(&b, " \"IsEnabled\": "); sb_put(&b, r->enabled ? "true" : "false"); sb_put(&b, ",\n");
diff --git a/Windows/gui/profile/profile.h b/Windows/gui/profile/profile.h
index 171a270..2980bf6 100644
--- a/Windows/gui/profile/profile.h
+++ b/Windows/gui/profile/profile.h
@@ -15,6 +15,7 @@
#define PB_MAX_RULE 128
#define PB_MAX_FILTER 64
#define PB_NAME_MAX 64
+#define PB_HOSTS_MAX 8192 // Target Hosts: one ";"-separated list of IPs and domain names (8191 characters + NUL)
#define PB_APPS_MAX 1024 // Applications field: holds a "; "-separated list of many exes
typedef struct {
@@ -33,9 +34,8 @@ typedef struct {
UINT32 nativeId; // id returned by AddRule after apply
wchar_t name[128]; // friendly label (GUI-only; not sent to the core)
wchar_t proc[PB_APPS_MAX];
- wchar_t hosts[256];
+ wchar_t hosts[PB_HOSTS_MAX]; // IPs, ranges and domain names together ("*" = any host)
wchar_t ports[128];
- wchar_t domains[256];
wchar_t proto[8]; // "TCP" | "UDP" | "BOTH"
wchar_t action[16]; // "PROXY" | "DIRECT" | "BLOCK"
int enabled;
@@ -65,6 +65,21 @@ typedef struct PBProfile {
PBFilter filter[PB_MAX_FILTER]; int filterCount;
} PBProfile;
+// Target Hosts list helpers. Line breaks only exist in the editor for readability:
+// PB_ListNormalize - in place; removes line breaks/tabs/control characters, trims blanks around the
+// separators (';' or ','), drops empty entries and joins with ';'. Never grows the text.
+// PB_ListFormat - writes the list with a line break after every ';' (cch must be >= 3 * len + 1).
+void PB_ListNormalize(wchar_t* s);
+void PB_ListFormat(const wchar_t* in, wchar_t* out, size_t cch);
+
+// The profile file (and the engine) keep IPs and domain names in two keys, TargetHosts and TargetDomains; the
+// GUI shows them as one list.
+// PB_HostsMerge - TargetHosts + TargetDomains (either may be "" or "*") -> one list ("*" if both are empty).
+// PB_HostsSplit - one list -> IP-like entries and domain entries. Each output is "*" when it has no entries,
+// and both are "*" when the list contains "*". cch of each output must be >= len(merged) + 2.
+void PB_HostsMerge(const wchar_t* ips, const wchar_t* doms, wchar_t* out, size_t cch);
+void PB_HostsSplit(const wchar_t* merged, wchar_t* ips, size_t ipsCch, wchar_t* doms, size_t domsCch);
+
// settings.json (active profile pointer). Other settings fields are preserved on write.
void PB_GetActiveProfile(wchar_t* out, int cch);
void PB_SetActiveProfile(const wchar_t* name);
diff --git a/Windows/gui/res/app.rc b/Windows/gui/res/app.rc
index 94343c4..84fbe59 100644
--- a/Windows/gui/res/app.rc
+++ b/Windows/gui/res/app.rc
@@ -120,7 +120,7 @@ BEGIN
END
// Proxification Rule (edit) sub-dialog
-IDD_RULE DIALOGEX 0, 0, 480, 294
+IDD_RULE DIALOGEX 0, 0, 480, 314
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Proxy Rule"
FONT 11, "Segoe UI", 400, 0, 0x0
@@ -135,29 +135,25 @@ BEGIN
LTEXT "", IDC_RE_EX_APPS, 92, 46, 380, 9
LTEXT "Target Hosts:", IDC_RE_L_HOSTS, 8, 62, 80, 10
- EDITTEXT IDC_RE_HOSTS, 92, 60, 380, 13, ES_AUTOHSCROLL | WS_TABSTOP
- LTEXT "", IDC_RE_EX_HOSTS, 92, 76, 380, 9
+ EDITTEXT IDC_RE_HOSTS, 92, 60, 380, 52, ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN | WS_VSCROLL | WS_TABSTOP
+ LTEXT "", IDC_RE_EX_HOSTS, 92, 116, 380, 18
- LTEXT "Target Ports:", IDC_RE_L_PORTS, 8, 92, 80, 10
- EDITTEXT IDC_RE_PORTS, 92, 90, 380, 13, ES_AUTOHSCROLL | WS_TABSTOP
- LTEXT "", IDC_RE_EX_PORTS, 92, 106, 380, 9
+ LTEXT "Target Ports:", IDC_RE_L_PORTS, 8, 142, 80, 10
+ EDITTEXT IDC_RE_PORTS, 92, 140, 380, 13, ES_AUTOHSCROLL | WS_TABSTOP
+ LTEXT "", IDC_RE_EX_PORTS, 92, 156, 380, 9
- LTEXT "Target Domains:", IDC_RE_L_DOMAINS, 8, 122, 80, 10
- EDITTEXT IDC_RE_DOMAINS, 92, 120, 380, 13, ES_AUTOHSCROLL | WS_TABSTOP
- LTEXT "", IDC_RE_EX_DOMAINS,92, 136, 380, 9
+ LTEXT "Protocol:", IDC_RE_L_PROTO, 8, 176, 80, 10
+ COMBOBOX IDC_RE_PROTO, 92, 174, 120, 80, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
+ LTEXT "", IDC_RE_UDPNOTE, 92, 192, 380, 40
- LTEXT "Protocol:", IDC_RE_L_PROTO, 8, 152, 80, 10
- COMBOBOX IDC_RE_PROTO, 92, 150, 120, 80, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
- LTEXT "", IDC_RE_UDPNOTE, 92, 168, 380, 40
+ LTEXT "Action:", IDC_RE_L_ACTION, 8, 238, 80, 10
+ COMBOBOX IDC_RE_ACTION, 92, 236, 380,160, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
- LTEXT "Action:", IDC_RE_L_ACTION, 8, 214, 80, 10
- COMBOBOX IDC_RE_ACTION, 92, 212, 380,160, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
+ AUTOCHECKBOX "Also proxy localhost traffic", IDC_RE_LOCALHOST, 92, 258, 380, 10
+ LTEXT "", IDC_RE_EX_LOCAL, 92, 271, 380, 20
- AUTOCHECKBOX "Also proxy localhost traffic", IDC_RE_LOCALHOST, 92, 234, 380, 10
- LTEXT "", IDC_RE_EX_LOCAL, 92, 247, 380, 20
-
- DEFPUSHBUTTON "OK", IDOK, 362, 272, 52, 14
- PUSHBUTTON "Cancel", IDCANCEL, 418, 272, 52, 14
+ DEFPUSHBUTTON "OK", IDOK, 362, 294, 52, 14
+ PUSHBUTTON "Cancel", IDCANCEL, 418, 294, 52, 14
END
// Log Filters (list) dialog
diff --git a/Windows/gui/res/resource.h b/Windows/gui/res/resource.h
index eb726de..83890b1 100644
--- a/Windows/gui/res/resource.h
+++ b/Windows/gui/res/resource.h
@@ -116,20 +116,17 @@
#define IDC_RE_APPS 422
#define IDC_RE_HOSTS 423
#define IDC_RE_PORTS 424
-#define IDC_RE_DOMAINS 425
#define IDC_RE_PROTO 426
#define IDC_RE_ACTION 427
#define IDC_RE_L_APPS 430
#define IDC_RE_L_HOSTS 431
#define IDC_RE_L_PORTS 432
-#define IDC_RE_L_DOMAINS 433
#define IDC_RE_L_PROTO 434
#define IDC_RE_L_ACTION 435
#define IDC_RE_BROWSE 436
#define IDC_RE_EX_APPS 437
#define IDC_RE_EX_HOSTS 438
#define IDC_RE_EX_PORTS 439
-#define IDC_RE_EX_DOMAINS 440
#define IDC_RE_UDPNOTE 441
#define IDC_RE_NAME 442 // friendly name edit
#define IDC_RE_L_NAME 443 // "Name:" label
diff --git a/Windows/gui/ui/dialogs.h b/Windows/gui/ui/dialogs.h
index b8159dc..b2f2e5f 100644
--- a/Windows/gui/ui/dialogs.h
+++ b/Windows/gui/ui/dialogs.h
@@ -388,13 +388,13 @@ static void InitRulesList(HWND lv)
// Checkboxes give the enable/disable toggle in the first column.
DarkListView(lv, LVS_EX_CHECKBOXES);
struct { int s; int w; } cols[] = {
- {S_COL_ON, 36}, {S_COL_NAME, 130}, {S_COL_APPS, 160}, {S_COL_HOSTS, 70}, {S_COL_PORTS, 55},
- {S_COL_DOMAINS, 90}, {S_COL_PROTO, 55}, {S_COL_ACTION, 65}, {S_COL_CFG, 190}
+ {S_COL_ON, 36}, {S_COL_NAME, 130}, {S_COL_APPS, 160}, {S_COL_HOSTS, 200}, {S_COL_PORTS, 60},
+ {S_COL_PROTO, 55}, {S_COL_ACTION, 65}, {S_COL_CFG, 190}
};
LVCOLUMNW c; c.mask = LVCF_TEXT | LVCF_WIDTH;
- for (int i = 0; i < 9; i++) { c.pszText = (LPWSTR)T(cols[i].s); c.cx = cols[i].w; ListView_InsertColumn(lv, i, &c); }
+ for (int i = 0; i < 8; i++) { c.pszText = (LPWSTR)T(cols[i].s); c.cx = cols[i].w; ListView_InsertColumn(lv, i, &c); }
SetWindowSubclass(lv, ListDarkSubProc, 3, 0);
- FillColumns(lv, 9);
+ FillColumns(lv, 8);
}
static void RefreshRulesList(HWND lv)
{
@@ -410,9 +410,8 @@ static void RefreshRulesList(HWND lv)
ListView_SetItemText(lv, i, 2, r->proc);
ListView_SetItemText(lv, i, 3, r->hosts);
ListView_SetItemText(lv, i, 4, r->ports);
- ListView_SetItemText(lv, i, 5, r->domains);
- ListView_SetItemText(lv, i, 6, r->proto);
- ListView_SetItemText(lv, i, 7, r->action);
+ ListView_SetItemText(lv, i, 5, r->proto);
+ ListView_SetItemText(lv, i, 6, r->action);
wchar_t cfg[200];
if (_wcsicmp(r->action, L"PROXY") == 0)
{
@@ -424,7 +423,7 @@ static void RefreshRulesList(HWND lv)
cfg[199] = 0;
}
else lstrcpynW(cfg, L"\x2014", 200); // em dash for Direct/Block
- ListView_SetItemText(lv, i, 8, cfg);
+ ListView_SetItemText(lv, i, 7, cfg);
ListView_SetCheckState(lv, i, r->enabled ? TRUE : FALSE);
}
g_rulesRefreshing = FALSE;
@@ -467,14 +466,12 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
SetDlgItemTextW(dlg, IDC_RE_L_APPS, T(S_L_APPS));
SetDlgItemTextW(dlg, IDC_RE_L_HOSTS, T(S_L_HOSTS));
SetDlgItemTextW(dlg, IDC_RE_L_PORTS, T(S_L_PORTS));
- SetDlgItemTextW(dlg, IDC_RE_L_DOMAINS,T(S_L_DOMAINS));
SetDlgItemTextW(dlg, IDC_RE_L_PROTO, T(S_L_PROTO));
SetDlgItemTextW(dlg, IDC_RE_L_ACTION, T(S_L_ACTION));
SetDlgItemTextW(dlg, IDC_RE_BROWSE, T(S_BTN_BROWSE));
SetDlgItemTextW(dlg, IDC_RE_EX_APPS, T(S_EX_APPS));
SetDlgItemTextW(dlg, IDC_RE_EX_HOSTS, T(S_EX_HOSTS));
SetDlgItemTextW(dlg, IDC_RE_EX_PORTS, T(S_EX_PORTS));
- SetDlgItemTextW(dlg, IDC_RE_EX_DOMAINS, T(S_EX_DOMAINS));
SetDlgItemTextW(dlg, IDC_RE_UDPNOTE, T(S_UDP_NOTE));
SetDlgItemTextW(dlg, IDC_RE_LOCALHOST, T(S_CHK_LOCALHOST));
SetDlgItemTextW(dlg, IDC_RE_EX_LOCAL, T(S_EX_LOCALHOST));
@@ -502,9 +499,16 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
SetDlgItemTextW(dlg, IDC_RE_NAME, r->name[0] ? r->name : L"ProxyBridge Rule");
SendDlgItemMessageW(dlg, IDC_RE_APPS, EM_LIMITTEXT, PB_APPS_MAX - 1, 0);
SetDlgItemTextW(dlg, IDC_RE_APPS, r->proc[0] ? r->proc : L"*");
- SetDlgItemTextW(dlg, IDC_RE_HOSTS, r->hosts[0] ? r->hosts : L"*");
SetDlgItemTextW(dlg, IDC_RE_PORTS, r->ports[0] ? r->ports : L"*");
- SetDlgItemTextW(dlg, IDC_RE_DOMAINS, r->domains[0] ? r->domains : L"*");
+ {
+ // multiline editor: one entry per line (the line breaks are display-only, removed again on save)
+ HWND he = GetDlgItem(dlg, IDC_RE_HOSTS);
+ SendMessageW(he, EM_LIMITTEXT, PB_HOSTS_MAX * 3, 0);
+ size_t fcap = wcslen(r->hosts) * 3 + 4;
+ wchar_t* fmt = (wchar_t*)malloc(fcap * sizeof(wchar_t));
+ if (fmt) { PB_ListFormat(r->hosts[0] ? r->hosts : L"*", fmt, fcap); SetWindowTextW(he, fmt); free(fmt); }
+ else SetWindowTextW(he, L"*");
+ }
SendMessageW(pr, CB_SETCURSEL, ProtoIdx(r->proto[0] ? r->proto : L"BOTH"), 0);
int target = 0;
if (!_wcsicmp(r->action, L"BLOCK")) target = 1;
@@ -534,7 +538,7 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
SetBkColor(dc, C_BG);
if (id == IDC_RE_UDPNOTE) SetTextColor(dc, C_WARN);
else if (id == IDC_RE_EX_APPS || id == IDC_RE_EX_HOSTS ||
- id == IDC_RE_EX_PORTS || id == IDC_RE_EX_DOMAINS || id == IDC_RE_EX_LOCAL) SetTextColor(dc, C_DIM);
+ id == IDC_RE_EX_PORTS || id == IDC_RE_EX_LOCAL) SetTextColor(dc, C_DIM);
else SetTextColor(dc, C_TEXT);
return (INT_PTR)g_brBg;
}
@@ -589,13 +593,28 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
GetDlgItemTextW(dlg, IDC_RE_NAME, r->name, 128);
if (!r->name[0]) lstrcpynW(r->name, L"ProxyBridge Rule", 128);
GetDlgItemTextW(dlg, IDC_RE_APPS, r->proc, PB_APPS_MAX);
- GetDlgItemTextW(dlg, IDC_RE_HOSTS, r->hosts, 256);
GetDlgItemTextW(dlg, IDC_RE_PORTS, r->ports, 128);
- GetDlgItemTextW(dlg, IDC_RE_DOMAINS, r->domains, 256);
+ {
+ // read the whole text (line breaks included), clean it up, and refuse - not cut - a list that is too long
+ HWND he = GetDlgItem(dlg, IDC_RE_HOSTS);
+ int hn = GetWindowTextLengthW(he);
+ wchar_t* htxt = (wchar_t*)malloc(((size_t)hn + 1) * sizeof(wchar_t));
+ if (!htxt) { MessageBoxW(dlg, L"Out of memory.", APP_TITLE, MB_OK | MB_ICONERROR); return TRUE; }
+ GetWindowTextW(he, htxt, hn + 1);
+ PB_ListNormalize(htxt);
+ if (wcslen(htxt) >= PB_HOSTS_MAX)
+ {
+ wchar_t m[96]; _snwprintf_s(m, ARRAYSIZE(m), _TRUNCATE, L"Target Hosts is too long (maximum %d characters).", PB_HOSTS_MAX - 1);
+ MessageBoxW(dlg, m, APP_TITLE, MB_OK | MB_ICONWARNING);
+ free(htxt);
+ return TRUE;
+ }
+ lstrcpynW(r->hosts, htxt, PB_HOSTS_MAX);
+ free(htxt);
+ }
if (!r->proc[0]) lstrcpynW(r->proc, L"*", 256);
- if (!r->hosts[0]) lstrcpynW(r->hosts, L"*", 256);
+ if (!r->hosts[0]) lstrcpynW(r->hosts, L"*", PB_HOSTS_MAX);
if (!r->ports[0]) lstrcpynW(r->ports, L"*", 128);
- if (!r->domains[0]) lstrcpynW(r->domains, L"*", 256);
lstrcpynW(r->proto, ProtoName((int)SendMessageW(GetDlgItem(dlg, IDC_RE_PROTO), CB_GETCURSEL, 0, 0)), 8);
HWND ac = GetDlgItem(dlg, IDC_RE_ACTION);
int asel = (int)SendMessageW(ac, CB_GETCURSEL, 0, 0);
@@ -688,8 +707,8 @@ INT_PTR CALLBACK RulesDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
if (g_profile.ruleCount >= PB_MAX_RULE) { MessageBoxW(dlg, L"Rule limit reached.", APP_TITLE, MB_OK); return TRUE; }
PBRule r; ZeroMemory(&r, sizeof(r));
lstrcpynW(r.name, L"ProxyBridge Rule", 128);
- lstrcpynW(r.proc, L"*", 256); lstrcpynW(r.hosts, L"*", 256);
- lstrcpynW(r.ports, L"*", 128); lstrcpynW(r.domains, L"*", 256);
+ lstrcpynW(r.proc, L"*", 256); lstrcpynW(r.hosts, L"*", PB_HOSTS_MAX);
+ lstrcpynW(r.ports, L"*", 128);
lstrcpynW(r.proto, L"BOTH", 8); lstrcpynW(r.action, L"DIRECT", 16); r.enabled = 1;
if (DialogBoxParamW(g_hInst, MAKEINTRESOURCEW(IDD_RULE), dlg, RuleEditDlgProc, (LPARAM)&r) == 1)
{
diff --git a/Windows/src/pb_rules.c b/Windows/src/pb_rules.c
index 60701dc..d54747a 100644
--- a/Windows/src/pb_rules.c
+++ b/Windows/src/pb_rules.c
@@ -506,6 +506,34 @@ BOOL match_domain_filter(const PROCESS_RULE *rule, const char *domain)
return match_domain_list(rule->target_domains, domain);
}
+// Target Hosts (IPs, ranges, patterns) and Target Domains (hostnames) act as ONE list of hosts: a
+// connection matches when its IP matches an IP entry OR its resolved name matches a domain entry.
+// A side that is empty or "*" adds no restriction by itself; when both are, every host matches.
+static BOOL rule_has_ip_filter(const PROCESS_RULE *rule)
+{
+ return rule->target_hosts != NULL && rule->target_hosts[0] != '\0' && strcmp(rule->target_hosts, "*") != 0;
+}
+
+static BOOL rule_host_match(const PROCESS_RULE *rule, UINT32 dest_ip, const char *dst_domain)
+{
+ BOOL has_ip = rule_has_ip_filter(rule), has_dom = rule_has_domain_filter(rule);
+ if (!has_ip && !has_dom)
+ return TRUE;
+ if (has_ip && match_ip_list(rule->target_hosts, dest_ip))
+ return TRUE;
+ return has_dom && dst_domain != NULL && dst_domain[0] != '\0' && match_domain_list(rule->target_domains, dst_domain);
+}
+
+static BOOL rule_host_match_v6(const PROCESS_RULE *rule, const UINT8 dest_ip6[16], const char *dst_domain)
+{
+ BOOL has_ip = rule_has_ip_filter(rule), has_dom = rule_has_domain_filter(rule);
+ if (!has_ip && !has_dom)
+ return TRUE;
+ if (has_ip && match_ip_list_v6(rule->target_hosts, dest_ip6))
+ return TRUE;
+ return has_dom && dst_domain != NULL && dst_domain[0] != '\0' && match_domain_list(rule->target_domains, dst_domain);
+}
+
BOOL is_broadcast_or_multicast(UINT32 ip)
{
// note: Localhost (127.x.x.x) is now supported for proxying
@@ -586,9 +614,8 @@ RuleAction match_rule_inner(const char *process_name, UINT32 dest_ip, UINT16 des
if (has_ip_filter || has_port_filter || has_domain_filter)
{
// Filtered wildcard - check if it matches
- if (match_ip_list(rule->target_hosts, dest_ip) &&
- match_port_list(rule->target_ports, dest_port) &&
- match_domain_filter(rule, dst_domain))
+ if (rule_host_match(rule, dest_ip, dst_domain) &&
+ match_port_list(rule->target_ports, dest_port))
{
// Matched! Return this rule's action
g_tl_rule_localhost = rule->localhost_via_proxy;
@@ -613,9 +640,8 @@ RuleAction match_rule_inner(const char *process_name, UINT32 dest_ip, UINT16 des
if (match_process_list(rule->process_name, process_name))
{
// Process matched! Check IP, port and domain filters
- if (match_ip_list(rule->target_hosts, dest_ip) &&
- match_port_list(rule->target_ports, dest_port) &&
- match_domain_filter(rule, dst_domain))
+ if (rule_host_match(rule, dest_ip, dst_domain) &&
+ match_port_list(rule->target_ports, dest_port))
{
// All filters matched! Return this rule's action
g_tl_rule_localhost = rule->localhost_via_proxy;
@@ -691,9 +717,8 @@ RuleAction match_rule_v6_inner(const char *process_name, const UINT8 dest_ip6[16
if (has_ip_filter || has_port_filter || has_domain_filter)
{
- if (match_ip_list_v6(rule->target_hosts, dest_ip6) &&
- match_port_list(rule->target_ports, dest_port) &&
- match_domain_filter(rule, dst_domain))
+ if (rule_host_match_v6(rule, dest_ip6, dst_domain) &&
+ match_port_list(rule->target_ports, dest_port))
{
g_tl_rule_localhost = rule->localhost_via_proxy;
if (out_proxy_config_id != NULL) *out_proxy_config_id = rule->proxy_config_id;
@@ -711,9 +736,8 @@ RuleAction match_rule_v6_inner(const char *process_name, const UINT8 dest_ip6[16
if (match_process_list(rule->process_name, process_name))
{
- if (match_ip_list_v6(rule->target_hosts, dest_ip6) &&
- match_port_list(rule->target_ports, dest_port) &&
- match_domain_filter(rule, dst_domain))
+ if (rule_host_match_v6(rule, dest_ip6, dst_domain) &&
+ match_port_list(rule->target_ports, dest_port))
{
g_tl_rule_localhost = rule->localhost_via_proxy;
if (out_proxy_config_id != NULL) *out_proxy_config_id = rule->proxy_config_id;