Skip to content

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

fix(windows): proxy rules could point at the wrong proxy after delete/re-add (#203)

AnoF-Cyber9b7d1e7637e1
diff --git a/Windows/gui/main.c b/Windows/gui/main.c
index 546fe7b..cc44e96 100644
--- a/Windows/gui/main.c
+++ b/Windows/gui/main.c
@@ -239,7 +239,7 @@ static void ApplyConfigs(void)
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->sendDomain ? TRUE : FALSE);
- if (c->storedId == 0) c->storedId = c->nativeId;
+ if (c->storedId == 0) c->storedId = PB_AllocConfigId(&g_profile);
}
}
diff --git a/Windows/gui/profile/profile.c b/Windows/gui/profile/profile.c
index 5980691..930208b 100644
--- a/Windows/gui/profile/profile.c
+++ b/Windows/gui/profile/profile.c
@@ -152,6 +152,31 @@ void PB_ProfileDefaults(PBProfile* p, const wchar_t* name)
lstrcpynW(p->language, L"en", 8);
}
+// Makes proxy-config ids unique and non-zero and positions the allocator past every id in use.
+// Older profiles stored the engine's per-session id here, so duplicates and gaps are possible; a
+// duplicate keeps its id for the first config (rules referencing it stay with that one) and the
+// later ones get fresh ids.
+static void normalize_cfg_ids(PBProfile* p)
+{
+ UINT32 hi = 0;
+ for (int i = 0; i < p->cfgCount; i++) if (p->cfg[i].storedId > hi) hi = p->cfg[i].storedId;
+ for (int i = 0; i < p->ruleCount; i++) if (p->rule[i].cfgStoredId > hi) hi = p->rule[i].cfgStoredId;
+ if (p->nextCfgId <= hi) p->nextCfgId = hi + 1;
+ if (p->nextCfgId == 0) p->nextCfgId = 1;
+ for (int j = 0; j < p->cfgCount; j++)
+ {
+ BOOL dup = (p->cfg[j].storedId == 0);
+ for (int i = 0; i < j && !dup; i++) if (p->cfg[i].storedId == p->cfg[j].storedId) dup = TRUE;
+ if (dup) p->cfg[j].storedId = p->nextCfgId++;
+ }
+}
+
+UINT32 PB_AllocConfigId(PBProfile* p)
+{
+ if (p->nextCfgId == 0) normalize_cfg_ids(p);
+ return p->nextCfgId++;
+}
+
void PB_ProfileLoad(const wchar_t* name, PBProfile* p)
{
PB_ProfileDefaults(p, name);
@@ -165,6 +190,7 @@ void PB_ProfileLoad(const wchar_t* name, PBProfile* p)
p->trafficLogging = json_bool(root, "IsTrafficLoggingEnabled", 1);
p->autoClearLogs = json_bool(root, "AutoClearConnectionLogs", 1);
p->closeToTray = json_bool(root, "CloseToTray", 1);
+ p->nextCfgId = (UINT32)json_long(root, "NextProxyConfigId", 0);
p->fontZoom = (int)json_long(root, "FontZoom", 100);
if (p->fontZoom < 60 || p->fontZoom > 300) p->fontZoom = 100;
u2w(json_str(root, "Language", "en"), p->language, 8);
@@ -202,6 +228,8 @@ void PB_ProfileLoad(const wchar_t* name, PBProfile* p)
ru->cfgStoredId = (UINT32)json_long(r, "ProxyConfigId", 0);
}
+ normalize_cfg_ids(p);
+
JVal* filters = json_get(root, "LogFilters");
if (filters && filters->type == J_ARR)
for (JVal* f = filters->child; f && p->filterCount < PB_MAX_FILTER; f = f->next)
@@ -240,7 +268,8 @@ BOOL PB_ProfileSave(const wchar_t* name, const PBProfile* p)
sb_put(&b, " \"AutoClearConnectionLogs\": "); sb_put(&b, p->autoClearLogs ? "true" : "false"); sb_put(&b, ",\n");
put_kv_str(&b, " ", "Language", p->language, ",\n");
sb_put(&b, " \"CloseToTray\": "); sb_put(&b, p->closeToTray ? "true" : "false"); sb_put(&b, ",\n");
- { char zb[32]; _snprintf_s(zb, sizeof(zb), _TRUNCATE, " \"FontZoom\": %d,\n", p->fontZoom); sb_put(&b, zb); }
+ { char zb[48]; _snprintf_s(zb, sizeof(zb), _TRUNCATE, " \"FontZoom\": %d,\n", p->fontZoom); sb_put(&b, zb); }
+ { char nb[64]; _snprintf_s(nb, sizeof(nb), _TRUNCATE, " \"NextProxyConfigId\": %u,\n", (unsigned)(p->nextCfgId ? p->nextCfgId : 1)); sb_put(&b, nb); }
sb_put(&b, " \"ProxyConfigs\": [");
for (int i = 0; i < p->cfgCount; i++)
diff --git a/Windows/gui/profile/profile.h b/Windows/gui/profile/profile.h
index 0b26a4e..a4d2605 100644
--- a/Windows/gui/profile/profile.h
+++ b/Windows/gui/profile/profile.h
@@ -46,12 +46,19 @@ typedef struct {
wchar_t mode[16], proc[256], ip[64], port[16], proto[8], action[16];
} PBFilter;
-typedef struct {
+// Stable ids for proxy configs. These are owned by the profile and are NOT the id the engine returns
+// from AddProxyConfig (that one is only valid for the current engine session and is remapped on load).
+// Never reused, so a rule can never silently start pointing at a proxy added after its own was deleted.
+typedef struct PBProfile PBProfile;
+UINT32 PB_AllocConfigId(PBProfile* p);
+
+typedef struct PBProfile {
int localhostViaProxy;
int trafficLogging;
int autoClearLogs;
int closeToTray;
int fontZoom; // UI/log text zoom in percent (60-300), 100 = DPI default
+ UINT32 nextCfgId; // next stable proxy-config id to hand out (persisted; ids are never reused)
wchar_t language[8]; // "en" | "zh"
PBConfig cfg[PB_MAX_CFG]; int cfgCount;
PBRule rule[PB_MAX_RULE]; int ruleCount;
diff --git a/Windows/gui/ui/dialogs.h b/Windows/gui/ui/dialogs.h
index ee05296..59ad73a 100644
--- a/Windows/gui/ui/dialogs.h
+++ b/Windows/gui/ui/dialogs.h
@@ -330,7 +330,7 @@ INT_PTR CALLBACK ServersDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
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, c.sendDomain ? TRUE : FALSE);
- if (id > 0) { c.nativeId = id; c.storedId = id; g_profile.cfg[g_profile.cfgCount++] = c; SaveActive(); RefreshServerList(lv); }
+ if (id > 0) { c.nativeId = id; c.storedId = PB_AllocConfigId(&g_profile); g_profile.cfg[g_profile.cfgCount++] = c; SaveActive(); RefreshServerList(lv); }
else MessageBoxW(dlg, T(S_ERR_ADDCFG), APP_TITLE, MB_OK | MB_ICONERROR);
}
return TRUE;