Skip to content

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

feat(windows): multi-folder Browse and an 'Add running process' picker for rule Applications

AnoF-Cybercfa961c6f0bf
diff --git a/Windows/cli/main.c b/Windows/cli/main.c
index 55dc31e..65f6657 100644
--- a/Windows/cli/main.c
+++ b/Windows/cli/main.c
@@ -59,7 +59,7 @@ typedef struct {
} PBProxyConfig;
typedef struct {
- char process_name[256];
+ char process_name[8192]; // Applications list: many programs separated by ";"
char target_hosts[512];
char target_ports[256];
char target_domains[8192]; // "*", "google.com", "*.google.com;*.gstatic.com" (long lists)
diff --git a/Windows/gui/loc/loc.h b/Windows/gui/loc/loc.h
index bff4d79..4fa61e0 100644
--- a/Windows/gui/loc/loc.h
+++ b/Windows/gui/loc/loc.h
@@ -27,6 +27,8 @@ enum {
S_DLG_SERVERS, S_DLG_SERVER, S_G_SERVER, S_G_AUTH, S_L_ADDR, S_CHK_ENABLE, S_CHK_ENABLED,
S_DLG_RULE, S_BTN_ADDDOTS, S_BTN_EDITDOTS, S_BTN_REMOVE, S_BTN_CLONE, S_BTN_UP, S_BTN_DOWN,
S_COL_ADDR, S_COL_TYPE, S_RULES_HINT, S_RULES_SEARCH, S_RULES_COUNT,
+ S_BTN_RUNNING, S_DLG_PROCPICK, S_PP_SEARCH, S_PP_REFRESH, S_PP_FULLPATH, S_PP_NOTE, S_PP_STATUS,
+ S_PP_COL_NAME, S_PP_COL_PID, S_PP_COL_PATH, S_PP_FULL,
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_UDP_NOTE, S_EX_LOCALHOST,
@@ -137,6 +139,18 @@ static const wchar_t* const g_strings[S_COUNT][2] = {
L"规则从上到下依次匹配;使用复选框启用/禁用规则。" },
/* S_RULES_SEARCH */ { L"Search rules (name, apps, hosts, ports, proxy)...", L"搜索规则 (名称、应用、主机、端口、代理)..." },
/* S_RULES_COUNT */ { L"%d of %d rules - clear the search to reorder", L"%d / %d 条规则 - 清除搜索后可调整顺序" },
+ /* S_BTN_RUNNING */ { L"Running...", L"运行中..." },
+ /* S_DLG_PROCPICK */ { L"Add running process", L"添加正在运行的进程" },
+ /* S_PP_SEARCH */ { L"Search by name or path...", L"按名称或路径搜索..." },
+ /* S_PP_REFRESH */ { L"Refresh", L"刷新" },
+ /* S_PP_FULLPATH */ { L"Use full executable path when available", L"尽可能使用完整的可执行文件路径" },
+ /* S_PP_NOTE */ { L"Rules match programs by name: only the name (or the full path, if ticked) is added. The PID is shown for reference only.",
+ L"规则按程序名称匹配: 只会添加名称 (勾选后为完整路径)。PID 仅供参考。" },
+ /* S_PP_STATUS */ { L"%d program(s) running. Programs whose path cannot be read are listed by name.", L"%d 个程序正在运行。无法读取路径的程序仅按名称列出。" },
+ /* S_PP_COL_NAME */ { L"Process name", L"进程名称" },
+ /* S_PP_COL_PID */ { L"PID", L"PID" },
+ /* S_PP_COL_PATH */ { L"Executable path", L"可执行文件路径" },
+ /* S_PP_FULL */ { L"The Applications list is full (maximum %d characters): %d program(s) could not be added.", L"应用程序列表已满 (最多 %d 个字符): 有 %d 个程序无法添加。" },
/* S_BTN_CHECK */ { L"Proxy Test", L"代理测试" },
/* S_TEST_OK */ { L"Proxy is reachable.\n\n%s", L"代理可访问。\n\n%s" },
/* S_TEST_FAIL */ { L"Proxy test failed.\n\n%s", L"代理测试失败。\n\n%s" },
diff --git a/Windows/gui/main.c b/Windows/gui/main.c
index 1336675..3b0b2df 100644
--- a/Windows/gui/main.c
+++ b/Windows/gui/main.c
@@ -18,6 +18,8 @@
#include <tlhelp32.h>
#include <commctrl.h>
#include <commdlg.h>
+#define COBJMACROS
+#include <shobjidl.h>
#include <dwmapi.h>
#include <uxtheme.h>
#include <stdio.h>
@@ -33,6 +35,8 @@
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "comdlg32.lib")
+#pragma comment(lib, "ole32.lib")
+#pragma comment(lib, "uuid.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "uxtheme.lib")
diff --git a/Windows/gui/profile/profile.h b/Windows/gui/profile/profile.h
index a4e4faa..425f4f3 100644
--- a/Windows/gui/profile/profile.h
+++ b/Windows/gui/profile/profile.h
@@ -16,7 +16,7 @@
#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
+#define PB_APPS_MAX 8192 // Applications field: a "; "-separated list of many programs (8191 characters + NUL)
typedef struct {
UINT32 storedId; // Id as written in the profile
diff --git a/Windows/gui/res/app.rc b/Windows/gui/res/app.rc
index d5b717b..d14858e 100644
--- a/Windows/gui/res/app.rc
+++ b/Windows/gui/res/app.rc
@@ -122,6 +122,24 @@ BEGIN
DEFPUSHBUTTON "Close", IDCANCEL, 572, 298, 60, 16
END
+// Running programs picker (adds program NAMES to a rule; PIDs are shown for reference only)
+IDD_PROCPICK DIALOGEX 0, 0, 420, 276
+STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Add running process"
+FONT 11, "Segoe UI", 400, 0, 0x0
+BEGIN
+ EDITTEXT IDC_PP_SEARCH, 8, 8, 332, 14, ES_AUTOHSCROLL | WS_TABSTOP
+ PUSHBUTTON "Refresh", IDC_PP_REFRESH, 346, 7, 66, 16
+ CONTROL "", IDC_PP_LIST, "SysListView32",
+ LVS_REPORT | LVS_SHOWSELALWAYS | WS_BORDER | WS_TABSTOP,
+ 8, 28, 404, 188
+ AUTOCHECKBOX "Use full executable path when available", IDC_PP_FULLPATH, 8, 222, 300, 10
+ LTEXT "", IDC_PP_NOTE, 8, 234, 404, 20
+ LTEXT "", IDC_PP_STATUS, 8, 258, 270, 10
+ DEFPUSHBUTTON "Add", IDOK, 284, 256, 60, 16
+ PUSHBUTTON "Cancel", IDCANCEL, 352, 256, 60, 16
+END
+
// Proxification Rule (edit) sub-dialog
IDD_RULE DIALOGEX 0, 0, 480, 314
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
@@ -133,7 +151,8 @@ BEGIN
AUTOCHECKBOX "Enabled", IDC_RE_ENABLED, 410, 10, 62, 10
LTEXT "Applications:", IDC_RE_L_APPS, 8, 32, 80, 10
- EDITTEXT IDC_RE_APPS, 92, 30, 316, 13, ES_AUTOHSCROLL | WS_TABSTOP
+ EDITTEXT IDC_RE_APPS, 92, 30, 250, 13, ES_AUTOHSCROLL | WS_TABSTOP
+ PUSHBUTTON "Running...", IDC_RE_RUNNING, 346, 29, 62, 14
PUSHBUTTON "Browse...", IDC_RE_BROWSE, 412, 29, 60, 14
LTEXT "", IDC_RE_EX_APPS, 92, 46, 380, 9
diff --git a/Windows/gui/res/resource.h b/Windows/gui/res/resource.h
index 211a6c1..1a25815 100644
--- a/Windows/gui/res/resource.h
+++ b/Windows/gui/res/resource.h
@@ -137,8 +137,18 @@
#define IDC_RE_L_NAME 443 // "Name:" label
#define IDC_RE_LOCALHOST 444 // "also proxy localhost" checkbox
#define IDC_RE_EX_LOCAL 445 // its hint text
+#define IDC_RE_RUNNING 446 // "Running..." (pick from running programs)
// Name-input dialog (new profile)
+// Running-programs picker
+#define IDD_PROCPICK 800
+#define IDC_PP_SEARCH 801
+#define IDC_PP_REFRESH 802
+#define IDC_PP_LIST 803
+#define IDC_PP_FULLPATH 804
+#define IDC_PP_STATUS 805
+#define IDC_PP_NOTE 806
+
#define IDD_NAME 500
#define IDC_NAME_EDIT 501
#define IDC_NAME_PROMPT 502
diff --git a/Windows/gui/ui/dialogs.h b/Windows/gui/ui/dialogs.h
index c4ca081..e0dbfa0 100644
--- a/Windows/gui/ui/dialogs.h
+++ b/Windows/gui/ui/dialogs.h
@@ -607,16 +607,285 @@ static LRESULT CALLBACK RuleListProc(HWND h, UINT m, WPARAM w, LPARAM l, UINT_PT
return DefSubclassProc(h, m, w, l);
}
-// Append one application name to a "; "-separated list, respecting the existing separator.
-static void AppendApp(wchar_t* out, int cap, const wchar_t* name)
+// ---- Applications list: adding programs from Browse / Running ----
+// Rules match programs by NAME (or by full path), never by PID: everything added here is a file name or a path.
+typedef struct { wchar_t** v; int n; } AppList;
+
+static void AppListAdd(AppList* l, const wchar_t* s)
+{
+ for (int i = 0; i < l->n; i++) if (!_wcsicmp(l->v[i], s)) return; // no duplicates
+ wchar_t* c = _wcsdup(s);
+ wchar_t** nv = c ? (wchar_t**)realloc(l->v, ((size_t)l->n + 1) * sizeof(wchar_t*)) : NULL;
+ if (!nv) { free(c); return; }
+ l->v = nv; l->v[l->n++] = c;
+}
+static void AppListFree(AppList* l) { for (int i = 0; i < l->n; i++) free(l->v[i]); free(l->v); l->v = NULL; l->n = 0; }
+
+// Does the ';' / ',' separated list already contain `name` (case-insensitive; blanks and surrounding quotes ignored)?
+static BOOL AppListContains(const wchar_t* list, const wchar_t* name)
+{
+ size_t nl = wcslen(name);
+ const wchar_t* p = list;
+ while (*p)
+ {
+ while (*p == L';' || *p == L',' || *p == L' ' || *p == L'\t') p++;
+ const wchar_t* s = p; while (*p && *p != L';' && *p != L',') p++;
+ const wchar_t* e = p; while (e > s && (e[-1] == L' ' || e[-1] == L'\t')) e--;
+ if (e - s >= 2 && *s == L'"' && e[-1] == L'"') { s++; e--; }
+ if ((size_t)(e - s) == nl && _wcsnicmp(s, name, nl) == 0) return TRUE;
+ }
+ return FALSE;
+}
+
+// Appends names to the Applications box ("a.exe; b.exe"). A name already in the list is skipped, and a name that is
+// empty, contains a list separator (';' ',' or a quote), or would not fit in PB_APPS_MAX is skipped as a whole - never
+// cut in the middle. Returns how many were added; *skipped counts the ones that could not be (list full / unusable name).
+static int AppendApps(HWND dlg, const wchar_t* const* names, int n, int* skipped, int* duplicates)
+{
+ int added = 0, bad = 0, dup = 0;
+ HWND e = GetDlgItem(dlg, IDC_RE_APPS);
+ wchar_t out[PB_APPS_MAX];
+ GetWindowTextW(e, out, PB_APPS_MAX);
+ if (out[0] == L'*' && out[1] == 0) out[0] = 0; // "any program" gives way to the first real entry
+ for (int i = 0; i < n; i++)
+ {
+ const wchar_t* nm = names[i];
+ size_t len = out[0] ? wcslen(out) : 0, nl = nm ? wcslen(nm) : 0;
+ if (!nm || nl == 0 || wcspbrk(nm, L";,\"")) { bad++; continue; }
+ if (AppListContains(out, nm)) { dup++; continue; }
+ const wchar_t* sep = len == 0 ? L"" : (out[len - 1] == L';') ? L" " : L"; ";
+ if (len + wcslen(sep) + nl >= PB_APPS_MAX) { bad++; continue; }
+ _snwprintf_s(out + len, PB_APPS_MAX - len, _TRUNCATE, L"%s%s", sep, nm);
+ added++;
+ }
+ if (added) SetWindowTextW(e, out);
+ if (skipped) *skipped = bad;
+ if (duplicates) *duplicates = dup;
+ return added;
+}
+
+// AppendApps + the "list is full" message (kept apart so tests can call AppendApps without a message box)
+static void ApplyApps(HWND dlg, const AppList* l)
+{
+ int bad = 0;
+ AppendApps(dlg, (const wchar_t* const*)l->v, l->n, &bad, NULL);
+ if (bad > 0)
+ {
+ wchar_t m[256]; _snwprintf_s(m, ARRAYSIZE(m), _TRUNCATE, T(S_PP_FULL), PB_APPS_MAX - 1, bad);
+ MessageBoxW(dlg, m, APP_TITLE, MB_OK | MB_ICONWARNING);
+ }
+}
+
+// Browse: the modern Windows file dialog. Unlike the old GetOpenFileName multi-select it accepts files from DIFFERENT
+// folders (for example the results of a search), and has no fixed buffer to overflow. Adds the file NAMES.
+static BOOL BrowseExecutables(HWND owner, AppList* out)
+{
+ BOOL got = FALSE;
+ HRESULT co = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
+ IFileOpenDialog* fd = NULL;
+ if (SUCCEEDED(CoCreateInstance(&CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog, (void**)&fd)))
+ {
+ DWORD opt = 0; IFileOpenDialog_GetOptions(fd, &opt);
+ IFileOpenDialog_SetOptions(fd, opt | FOS_ALLOWMULTISELECT | FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST | FOS_PATHMUSTEXIST);
+ COMDLG_FILTERSPEC spec[2] = { { L"Executables", L"*.exe" }, { L"All Files", L"*.*" } };
+ IFileOpenDialog_SetFileTypes(fd, 2, spec);
+ IFileOpenDialog_SetFileTypeIndex(fd, 1);
+ IFileOpenDialog_SetTitle(fd, L"Select Process Executable(s)");
+ if (SUCCEEDED(IFileOpenDialog_Show(fd, owner)))
+ {
+ IShellItemArray* arr = NULL;
+ if (SUCCEEDED(IFileOpenDialog_GetResults(fd, &arr)))
+ {
+ DWORD cnt = 0; IShellItemArray_GetCount(arr, &cnt);
+ for (DWORD i = 0; i < cnt; i++)
+ {
+ IShellItem* it = NULL;
+ if (FAILED(IShellItemArray_GetItemAt(arr, i, &it))) continue;
+ PWSTR path = NULL;
+ if (SUCCEEDED(IShellItem_GetDisplayName(it, SIGDN_FILESYSPATH, &path)) && path)
+ {
+ const wchar_t* base = path;
+ for (const wchar_t* q = path; *q; q++) if (*q == L'\\' || *q == L'/') base = q + 1;
+ if (*base) { AppListAdd(out, base); got = TRUE; }
+ CoTaskMemFree(path);
+ }
+ IShellItem_Release(it);
+ }
+ IShellItemArray_Release(arr);
+ }
+ }
+ IFileOpenDialog_Release(fd);
+ }
+ if (SUCCEEDED(co)) CoUninitialize();
+ return got;
+}
+
+// ---- Running-programs picker ----
+// One row per running PROGRAM (same executable path; or same name when the path cannot be read). The PID shown is
+// the lowest one of that program and is for reference only - nothing here ever writes a PID into a rule.
+typedef struct { wchar_t name[260]; wchar_t path[MAX_PATH]; DWORD pid; int instances; } PPRow;
+
+static int PPCompare(const void* a, const void* b)
+{
+ const PPRow* x = (const PPRow*)a; const PPRow* y = (const PPRow*)b;
+ int c = _wcsicmp(x->name, y->name);
+ return c ? c : _wcsicmp(x->path, y->path);
+}
+
+static int PPEnumerateReal(PPRow** out)
+{
+ *out = NULL;
+ HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+ if (snap == INVALID_HANDLE_VALUE) return 0;
+ int cap = 256, n = 0;
+ PPRow* rows = (PPRow*)calloc((size_t)cap, sizeof(PPRow));
+ if (!rows) { CloseHandle(snap); return 0; }
+ PROCESSENTRY32W pe; pe.dwSize = sizeof(pe);
+ for (BOOL ok = Process32FirstW(snap, &pe); ok; ok = Process32NextW(snap, &pe))
+ {
+ if (pe.th32ProcessID == 0 || pe.th32ProcessID == 4) continue; // Idle and System are not programs
+ wchar_t path[MAX_PATH]; path[0] = 0;
+ HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pe.th32ProcessID);
+ if (h) { DWORD len = MAX_PATH; if (!QueryFullProcessImageNameW(h, 0, path, &len)) path[0] = 0; CloseHandle(h); }
+ int found = -1;
+ for (int i = 0; i < n && found < 0; i++)
+ if (path[0] ? (rows[i].path[0] && !_wcsicmp(rows[i].path, path)) : (!rows[i].path[0] && !_wcsicmp(rows[i].name, pe.szExeFile))) found = i;
+ if (found >= 0) { rows[found].instances++; if (pe.th32ProcessID < rows[found].pid) rows[found].pid = pe.th32ProcessID; continue; }
+ if (n == cap)
+ {
+ PPRow* g = (PPRow*)realloc(rows, (size_t)cap * 2 * sizeof(PPRow));
+ if (!g) break;
+ rows = g; cap *= 2;
+ }
+ PPRow* r = &rows[n++]; ZeroMemory(r, sizeof(*r));
+ lstrcpynW(r->name, pe.szExeFile, 260); lstrcpynW(r->path, path, MAX_PATH); r->pid = pe.th32ProcessID; r->instances = 1;
+ }
+ CloseHandle(snap);
+ qsort(rows, (size_t)n, sizeof(PPRow), PPCompare);
+ *out = rows;
+ return n;
+}
+static int (*g_ppEnumerate)(PPRow**) = PPEnumerateReal; // replaced by a fake list in the tests
+
+typedef struct { AppList* result; PPRow* rows; int n; int* shown; int shownN; } PPState;
+
+static void PPRefill(HWND dlg, PPState* st)
+{
+ HWND lv = GetDlgItem(dlg, IDC_PP_LIST);
+ wchar_t q[256]; GetDlgItemTextW(dlg, IDC_PP_SEARCH, q, ARRAYSIZE(q));
+ wchar_t words[8][64]; int nw = 0; // every word must appear in the name or the path
+ for (const wchar_t* p = q; *p && nw < 8; )
+ {
+ while (*p && *p <= L' ') p++;
+ if (!*p) break;
+ int k = 0; while (*p && *p > L' ') { if (k < 63) words[nw][k++] = *p; p++; }
+ words[nw++][k] = 0;
+ }
+ SendMessageW(lv, WM_SETREDRAW, FALSE, 0);
+ ListView_DeleteAllItems(lv);
+ st->shownN = 0;
+ for (int i = 0; i < st->n; i++)
+ {
+ const PPRow* r = &st->rows[i];
+ BOOL ok = TRUE;
+ for (int w = 0; w < nw && ok; w++) ok = ContainsCI(r->name, words[w]) || ContainsCI(r->path, words[w]);
+ if (!ok) continue;
+ int row = st->shownN++;
+ st->shown[row] = i;
+ LVITEMW it; ZeroMemory(&it, sizeof(it)); it.mask = LVIF_TEXT; it.iItem = row; it.pszText = (LPWSTR)r->name;
+ ListView_InsertItem(lv, &it);
+ wchar_t pid[48];
+ if (r->instances > 1) _snwprintf_s(pid, ARRAYSIZE(pid), _TRUNCATE, L"%lu (+%d)", r->pid, r->instances - 1);
+ else _snwprintf_s(pid, ARRAYSIZE(pid), _TRUNCATE, L"%lu", r->pid);
+ ListView_SetItemText(lv, row, 1, pid);
+ ListView_SetItemText(lv, row, 2, r->path[0] ? (LPWSTR)r->path : (LPWSTR)L"\x2014");
+ }
+ SendMessageW(lv, WM_SETREDRAW, TRUE, 0);
+ EnableWindow(GetDlgItem(dlg, IDOK), ListView_GetSelectedCount(lv) > 0);
+ wchar_t status[200]; _snwprintf_s(status, ARRAYSIZE(status), _TRUNCATE, T(S_PP_STATUS), st->n);
+ SetDlgItemTextW(dlg, IDC_PP_STATUS, status);
+}
+
+static void PPReload(HWND dlg, PPState* st)
{
- if (!name || !name[0]) return;
- size_t n = wcslen(out);
- if (n == 0) { lstrcpynW(out, name, cap); return; }
- const wchar_t* sep = (out[n - 1] == L';') ? L" " : L"; ";
- _snwprintf_s(out + n, (size_t)cap - n, _TRUNCATE, L"%s%s", sep, name);
+ free(st->rows); free(st->shown);
+ st->rows = NULL; st->shown = NULL; st->n = 0; st->shownN = 0;
+ st->n = g_ppEnumerate(&st->rows);
+ if (st->n < 0 || !st->rows) st->n = 0;
+ st->shown = (int*)calloc((size_t)(st->n > 0 ? st->n : 1), sizeof(int));
+ if (!st->shown) { free(st->rows); st->rows = NULL; st->n = 0; st->shown = (int*)calloc(1, sizeof(int)); }
+ PPRefill(dlg, st);
}
+// Only ever a name, or a path when "full path" is ticked and the path is usable in the list ("," ";" and quotes are not).
+static const wchar_t* PPChoose(const PPRow* r, BOOL fullPath)
+{
+ if (fullPath && r->path[0] && !wcspbrk(r->path, L";,\"")) return r->path;
+ return r->name;
+}
+
+INT_PTR CALLBACK ProcPickDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
+{
+ PPState* st = (PPState*)GetWindowLongPtrW(dlg, GWLP_USERDATA);
+ switch (msg)
+ {
+ case WM_INITDIALOG:
+ {
+ st = (PPState*)calloc(1, sizeof(PPState));
+ st->result = (AppList*)lp;
+ SetWindowLongPtrW(dlg, GWLP_USERDATA, (LONG_PTR)st);
+ SetWindowTextW(dlg, T(S_DLG_PROCPICK));
+ SetDlgItemTextW(dlg, IDC_PP_REFRESH, T(S_PP_REFRESH));
+ SetDlgItemTextW(dlg, IDC_PP_FULLPATH, T(S_PP_FULLPATH));
+ SetDlgItemTextW(dlg, IDC_PP_NOTE, T(S_PP_NOTE));
+ SetDlgItemTextW(dlg, IDOK, T(S_BTN_ADD));
+ SetDlgItemTextW(dlg, IDCANCEL, T(S_BTN_CANCEL));
+ SendDlgItemMessageW(dlg, IDC_PP_SEARCH, EM_LIMITTEXT, 255, 0);
+ SendDlgItemMessageW(dlg, IDC_PP_SEARCH, EM_SETCUEBANNER, TRUE, (LPARAM)T(S_PP_SEARCH));
+ InitDarkMode(dlg);
+ HWND lv = GetDlgItem(dlg, IDC_PP_LIST);
+ DarkListView(lv, 0);
+ struct { int s; int w; } cols[] = { {S_PP_COL_NAME, 150}, {S_PP_COL_PID, 80}, {S_PP_COL_PATH, 380} };
+ LVCOLUMNW c; c.mask = LVCF_TEXT | LVCF_WIDTH;
+ for (int i = 0; i < 3; i++) { c.pszText = (LPWSTR)T(cols[i].s); c.cx = cols[i].w; ListView_InsertColumn(lv, i, &c); }
+ PPReload(dlg, st);
+ return TRUE;
+ }
+ PB_DARK_CTLCOLORS;
+ case WM_NOTIFY:
+ {
+ LPNMHDR nh = (LPNMHDR)lp;
+ if (nh->idFrom == IDC_PP_LIST && nh->code == LVN_ITEMCHANGED)
+ EnableWindow(GetDlgItem(dlg, IDOK), ListView_GetSelectedCount(GetDlgItem(dlg, IDC_PP_LIST)) > 0);
+ else if (nh->idFrom == IDC_PP_LIST && nh->code == NM_DBLCLK && ListView_GetSelectedCount(GetDlgItem(dlg, IDC_PP_LIST)) > 0)
+ PostMessageW(dlg, WM_COMMAND, IDOK, 0);
+ return TRUE;
+ }
+ case WM_COMMAND:
+ switch (LOWORD(wp))
+ {
+ case IDC_PP_SEARCH: if (HIWORD(wp) == EN_CHANGE && st) PPRefill(dlg, st); return TRUE;
+ case IDC_PP_REFRESH: if (st) PPReload(dlg, st); return TRUE;
+ case IDOK:
+ {
+ HWND lv = GetDlgItem(dlg, IDC_PP_LIST);
+ BOOL full = IsDlgButtonChecked(dlg, IDC_PP_FULLPATH) == BST_CHECKED;
+ for (int row = ListView_GetNextItem(lv, -1, LVNI_SELECTED); row >= 0 && st; row = ListView_GetNextItem(lv, row, LVNI_SELECTED))
+ if (row < st->shownN) AppListAdd(st->result, PPChoose(&st->rows[st->shown[row]], full));
+ EndDialog(dlg, (st && st->result->n > 0) ? 1 : 0);
+ return TRUE;
+ }
+ case IDCANCEL: EndDialog(dlg, 0); return TRUE;
+ }
+ return FALSE;
+ case WM_DESTROY:
+ if (st) { free(st->rows); free(st->shown); free(st); SetWindowLongPtrW(dlg, GWLP_USERDATA, 0); }
+ return FALSE;
+ }
+ return FALSE;
+}
+
+
// Edit sub-dialog. lParam is a PBRule* seeded with current values; on OK it is written back
// (name/proc/hosts/ports/domains/proto/action/cfgStoredId/enabled) and the dialog returns 1.
// "Also proxy localhost traffic" only means something for a PROXY rule (DIRECT / BLOCK never
@@ -647,6 +916,7 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
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_RUNNING, T(S_BTN_RUNNING));
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));
@@ -728,41 +998,16 @@ INT_PTR CALLBACK RuleEditDlgProc(HWND dlg, UINT msg, WPARAM wp, LPARAM lp)
return TRUE;
case IDC_RE_BROWSE:
{
- // Pick one or more .exe files and append each file name to the list.
- // Multi-select needs a buffer big enough for the folder + every file name.
- static wchar_t files[8192];
- files[0] = 0;
- OPENFILENAMEW ofn; ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = dlg;
- ofn.lpstrFilter = L"Executables\0*.exe\0All Files\0*.*\0";
- ofn.lpstrFile = files; ofn.nMaxFile = ARRAYSIZE(files);
- ofn.lpstrTitle = L"Select Process Executable(s)";
- ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
- OFN_ALLOWMULTISELECT | OFN_EXPLORER;
- if (GetOpenFileNameW(&ofn))
- {
- wchar_t out[PB_APPS_MAX];
- GetDlgItemTextW(dlg, IDC_RE_APPS, out, PB_APPS_MAX);
- if (!out[0] || (out[0] == L'*' && out[1] == 0)) out[0] = 0; // replace empty/"*"
-
- // OFN_EXPLORER multi-select layout:
- // single: "C:\dir\app.exe\0\0" (one full path)
- // multi: "C:\dir\0a.exe\0b.exe\0...\0\0" (folder, then bare names)
- const wchar_t* dir = files;
- const wchar_t* p = files + wcslen(files) + 1;
- if (*p == 0)
- {
- const wchar_t* base = dir; // single: strip to file name
- for (const wchar_t* q = dir; *q; q++) if (*q == L'\\' || *q == L'/') base = q + 1;
- AppendApp(out, ARRAYSIZE(out), base);
- }
- else
- {
- for (; *p; p += wcslen(p) + 1) // multi: each p is a bare file name
- AppendApp(out, ARRAYSIZE(out), p);
- }
- SetDlgItemTextW(dlg, IDC_RE_APPS, out);
- }
+ AppList l = { 0 }; // file names only; files from different folders are fine
+ if (BrowseExecutables(dlg, &l)) ApplyApps(dlg, &l);
+ AppListFree(&l);
+ return TRUE;
+ }
+ case IDC_RE_RUNNING:
+ {
+ AppList l = { 0 }; // program names (never PIDs) from the running programs
+ if (DialogBoxParamW(g_hInst, MAKEINTRESOURCEW(IDD_PROCPICK), dlg, ProcPickDlgProc, (LPARAM)&l) == 1) ApplyApps(dlg, &l);
+ AppListFree(&l);
return TRUE;
}
case IDOK:
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index 570ed91..5e2dd44 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -24,7 +24,8 @@
#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
+#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.2.0"
#define PID_CACHE_SIZE 1024
#define PID_CACHE_TTL_MS 30000
@@ -46,7 +47,7 @@
typedef struct PROCESS_RULE {
UINT32 rule_id;
- char process_name[MAX_PROCESS_NAME];
+ 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)
diff --git a/Windows/src/pb_rules.c b/Windows/src/pb_rules.c
index 7ec6f97..d9532ee 100644
--- a/Windows/src/pb_rules.c
+++ b/Windows/src/pb_rules.c
@@ -830,7 +830,8 @@ RuleAction check_process_rule(UINT32 src_ip, UINT16 src_port, UINT32 dest_ip, UI
PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char* target_hosts, const char* target_ports, const char* target_domains, RuleProtocol protocol, RuleAction action, UINT32 proxy_config_id, BOOL localhost_via_proxy)
{
- if (process_name == NULL || process_name[0] == '\0')
+ // a list that does not fit is refused: cutting it would silently drop or mangle programs
+ if (process_name == NULL || process_name[0] == '\0' || strnlen_s(process_name, MAX_RULE_PROCESS_LIST) >= MAX_RULE_PROCESS_LIST)
return 0;
PROCESS_RULE *rule = (PROCESS_RULE *)malloc(sizeof(PROCESS_RULE));
@@ -838,7 +839,7 @@ PROXYBRIDGE_API UINT32 ProxyBridge_AddRule(const char* process_name, const char*
return 0;
rule->rule_id = g_next_rule_id++;
- strncpy_s(rule->process_name, MAX_PROCESS_NAME, process_name, _TRUNCATE);
+ strncpy_s(rule->process_name, sizeof(rule->process_name), process_name, _TRUNCATE);
rule->protocol = protocol;
rule->proxy_config_id = proxy_config_id;
rule->localhost_via_proxy = localhost_via_proxy;
@@ -1051,7 +1052,8 @@ PROXYBRIDGE_API BOOL ProxyBridge_DeleteRule(UINT32 rule_id)
PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_name, const char* target_hosts, const char* target_ports, const char* target_domains, RuleProtocol protocol, RuleAction action, UINT32 proxy_config_id, BOOL localhost_via_proxy)
{
- if (rule_id == 0 || process_name == NULL || target_hosts == NULL || target_ports == NULL)
+ if (rule_id == 0 || process_name == NULL || target_hosts == NULL || target_ports == NULL ||
+ strnlen_s(process_name, MAX_RULE_PROCESS_LIST) >= MAX_RULE_PROCESS_LIST)
return FALSE;
// NULL/"" domains means "no restriction" -> stored as "*"
@@ -1077,7 +1079,7 @@ PROXYBRIDGE_API BOOL ProxyBridge_EditRule(UINT32 rule_id, const char* process_na
{
if (rule->rule_id == rule_id)
{
- strncpy_s(rule->process_name, MAX_PROCESS_NAME, process_name, _TRUNCATE);
+ strncpy_s(rule->process_name, sizeof(rule->process_name), process_name, _TRUNCATE);
free(rule->target_hosts);
free(rule->target_ports);
diff --git a/Windows/tests/run_tests.ps1 b/Windows/tests/run_tests.ps1
index 7e65d57..d020288 100644
--- a/Windows/tests/run_tests.ps1
+++ b/Windows/tests/run_tests.ps1
@@ -74,7 +74,7 @@ $clUni = "/utf-8 /DUNICODE /D_UNICODE /D_CRT_SECURE_NO_WARNINGS"
$clCore = "/Isrc $wdInc /DPROXYBRIDGE_EXPORTS /D_CRT_SECURE_NO_WARNINGS /D_WINSOCK_DEPRECATED_NO_WARNINGS"
$lnCore = "$wdLib WinDivert.lib ws2_32.lib iphlpapi.lib advapi32.lib"
$lnProf = 'user32.lib advapi32.lib shell32.lib'
-$lnGui = '/SUBSYSTEM:CONSOLE /ENTRY:wWinMainCRTStartup user32.lib gdi32.lib comctl32.lib shell32.lib comdlg32.lib winhttp.lib advapi32.lib dwmapi.lib uxtheme.lib'
+$lnGui = '/SUBSYSTEM:CONSOLE /ENTRY:wWinMainCRTStartup user32.lib gdi32.lib comctl32.lib shell32.lib comdlg32.lib winhttp.lib advapi32.lib dwmapi.lib uxtheme.lib ole32.lib uuid.lib'
$prof = 'gui\profile\profile.c'
$res = "$Out\app.res"
diff --git a/Windows/tests/test_dialogs_ui.c b/Windows/tests/test_dialogs_ui.c
index 14302d9..b4ec6d9 100644
--- a/Windows/tests/test_dialogs_ui.c
+++ b/Windows/tests/test_dialogs_ui.c
@@ -19,6 +19,19 @@ static void Combo(int id, int sel) { SendDlgItemMessageW(g_dlg, id, CB_SETCURSEL
static int ComboSel(int id) { return (int)SendDlgItemMessageW(g_dlg, id, CB_GETCURSEL, 0, 0); }
static int ComboCount(int id) { return (int)SendDlgItemMessageW(g_dlg, id, CB_GETCOUNT, 0, 0); }
+static PPRow* g_fakeRows; static AppList g_ppResult;
+static int FakeEnum(PPRow** out) { *out = (PPRow*)malloc(3 * sizeof(PPRow)); memcpy(*out, g_fakeRows, 3 * sizeof(PPRow)); return 3; }
+static VOID CALLBACK PickTimer(HWND h, UINT m, UINT_PTR id, DWORD t)
+{
+ (void)h; (void)m; (void)t;
+ HWND d = FindWindowW(L"#32770", T(S_DLG_PROCPICK));
+ if (!d) return;
+ KillTimer(NULL, id);
+ HWND lv = GetDlgItem(d, IDC_PP_LIST);
+ for (int i = 0; i < ListView_GetItemCount(lv); i++) ListView_SetItemState(lv, i, LVIS_SELECTED, LVIS_SELECTED);
+ SendMessageW(d, WM_COMMAND, MAKEWPARAM(IDOK, BN_CLICKED), (LPARAM)GetDlgItem(d, IDOK));
+}
+
static HWND OpenRuleEditor(PBRule* r)
{
HWND d = CreateDialogParamW(g_hInst, MAKEINTRESOURCEW(IDD_RULE), NULL, RuleEditDlgProc, (LPARAM)r);
@@ -105,6 +118,56 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR cmd, int show)
CHECK(!wcscmp(r.name, L"keep") && !wcscmp(r.hosts, L"1.1.1.1"));
DestroyWindow(g_dlg); }
+ /* ================= Applications list: Browse / Running helpers ================= */
+ { PBRule r; ZeroMemory(&r, sizeof r); 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);
+ g_dlg = OpenRuleEditor(&r);
+ static wchar_t t[PB_APPS_MAX]; int bad = -1, dup = -1;
+ const wchar_t* one[] = { L"curl.exe", L"Chrome.exe" };
+ CHECK(AppendApps(g_dlg, one, 2, &bad, &dup) == 2 && bad == 0); /* "*" gives way to the first entry */
+ Get(IDC_RE_APPS, t, PB_APPS_MAX); CHECK(!wcscmp(t, L"curl.exe; Chrome.exe"));
+ const wchar_t* two[] = { L"CHROME.EXE", L"edge.exe", L"a;b.exe", L"" };
+ CHECK(AppendApps(g_dlg, two, 4, &bad, &dup) == 1 && bad == 2 && dup == 1); /* duplicate (any case) skipped, unusable names refused */
+ Get(IDC_RE_APPS, t, PB_APPS_MAX); CHECK(!wcscmp(t, L"curl.exe; Chrome.exe; edge.exe"));
+ /* a nearly full list: a name that does not fit is refused whole, never cut */
+ static wchar_t big[PB_APPS_MAX]; int k = 0; for (; k < PB_APPS_MAX - 12; k++) big[k] = L'x'; big[k] = 0;
+ SendDlgItemMessageW(g_dlg, IDC_RE_APPS, EM_LIMITTEXT, 0, 0);
+ Set(IDC_RE_APPS, big);
+ const wchar_t* tail[] = { L"averylongname.exe", L"a.exe" };
+ CHECK(AppendApps(g_dlg, tail, 2, &bad, &dup) == 1 && bad == 1);
+ Get(IDC_RE_APPS, t, PB_APPS_MAX); CHECK(wcslen(t) < PB_APPS_MAX && wcsstr(t, L"; a.exe") && !wcsstr(t, L"averylong"));
+ DestroyWindow(g_dlg); }
+
+ /* ================= running-programs picker (fake process list) ================= */
+ {
+ static PPRow fake[3]; g_fakeRows = fake;
+ lstrcpynW(fake[0].name, L"alpha.exe", 260); lstrcpynW(fake[0].path, L"C:\\Apps\\alpha.exe", MAX_PATH); fake[0].pid = 4242; fake[0].instances = 3;
+ lstrcpynW(fake[1].name, L"beta.exe", 260); lstrcpynW(fake[1].path, L"C:\\My, Apps\\beta.exe", MAX_PATH); fake[1].pid = 77; fake[1].instances = 1; /* comma: unusable as a path in the list */
+ lstrcpynW(fake[2].name, L"gamma.exe", 260); fake[2].pid = 9; fake[2].instances = 1; /* path unreadable */
+ g_ppEnumerate = FakeEnum;
+ g_dlg = CreateDialogParamW(g_hInst, MAKEINTRESOURCEW(IDD_PROCPICK), NULL, ProcPickDlgProc, (LPARAM)&g_ppResult);
+ ShowWindow(g_dlg, SW_SHOW);
+ HWND lv = GetDlgItem(g_dlg, IDC_PP_LIST);
+ CHECK(ListView_GetItemCount(lv) == 3 && !IsWindowEnabled(GetDlgItem(g_dlg, IDOK))); /* Add needs a selection */
+ wchar_t t[64]; ListView_GetItemText(lv, 0, 1, t, 64); CHECK(!wcscmp(t, L"4242 (+2)")); /* PID shown for reference only */
+ Set(IDC_PP_SEARCH, L"APPS alp"); CHECK(ListView_GetItemCount(lv) == 1); /* every word, name or path, any case */
+ Set(IDC_PP_SEARCH, L""); CHECK(ListView_GetItemCount(lv) == 3);
+ ListView_SetItemState(lv, 0, LVIS_SELECTED, LVIS_SELECTED);
+ CHECK(IsWindowEnabled(GetDlgItem(g_dlg, IDOK)));
+ CHECK(!wcscmp(PPChoose(&fake[0], FALSE), L"alpha.exe") && !wcscmp(PPChoose(&fake[0], TRUE), L"C:\\Apps\\alpha.exe"));
+ CHECK(!wcscmp(PPChoose(&fake[1], TRUE), L"beta.exe") && !wcscmp(PPChoose(&fake[2], TRUE), L"gamma.exe")); /* fall back to the name */
+ DestroyWindow(g_dlg);
+ /* modal run: select everything and press Add - the result holds names, never PIDs */
+ SetTimer(NULL, 1, 50, PickTimer);
+ CHECK(DialogBoxParamW(g_hInst, MAKEINTRESOURCEW(IDD_PROCPICK), NULL, ProcPickDlgProc, (LPARAM)&g_ppResult) == 1);
+ CHECK(g_ppResult.n == 3 && !wcscmp(g_ppResult.v[0], L"alpha.exe") && !wcscmp(g_ppResult.v[1], L"beta.exe") && !wcscmp(g_ppResult.v[2], L"gamma.exe"));
+ AppListFree(&g_ppResult);
+ /* the real enumerator finds this very program with its path */
+ PPRow* rows = NULL; int n = PPEnumerateReal(&rows); BOOL me = FALSE;
+ wchar_t exe[MAX_PATH]; GetModuleFileNameW(NULL, exe, MAX_PATH);
+ for (int i = 0; i < n; i++) if (!_wcsicmp(rows[i].path, exe)) me = TRUE;
+ CHECK(n > 3 && me); free(rows);
+ }
+
/* ================= proxy servers list ================= */
g_dlg = CreateDialogParamW(hInst, MAKEINTRESOURCEW(IDD_SERVERS), NULL, ServersDlgProc, 0);
CHECK(g_dlg != NULL);
diff --git a/Windows/tests/test_rules_match.c b/Windows/tests/test_rules_match.c
index c8f1d2e..6c8ef5f 100644
--- a/Windows/tests/test_rules_match.c
+++ b/Windows/tests/test_rules_match.c
@@ -112,5 +112,19 @@ int main(void)
CHECK(big != 0 && m4("longlist.exe", "10.5.249.1", 1) == RULE_ACTION_BLOCK && m4("longlist.exe", "10.5.249.2", 1) == RULE_ACTION_DIRECT);
ProxyBridge_DeleteRule(big); }
+ /* a long Applications list (many programs, ~7000 characters) is kept whole and still matches its last entry */
+ { char procs[8192]; size_t o = 0; for (int i = 0; i < 600; i++) o += sprintf_s(procs + o, sizeof procs - o, "prog%03d.exe;", i);
+ sprintf_s(procs + o, sizeof procs - o, "lastone.exe");
+ UINT32 id = ProxyBridge_AddRule(procs, "*", "*", "*", RULE_PROTOCOL_BOTH, RULE_ACTION_BLOCK, 0, FALSE);
+ CHECK(id != 0 && m4("lastone.exe", "8.8.8.8", 80) == RULE_ACTION_BLOCK && m4("prog300.exe", "8.8.8.8", 80) == RULE_ACTION_BLOCK && m4("lastone2.exe", "8.8.8.8", 80) == RULE_ACTION_DIRECT);
+ ProxyBridge_DeleteRule(id); }
+
+ /* a list that cannot fit is refused, never cut in the middle of a program name */
+ { static char huge[MAX_RULE_PROCESS_LIST + 16]; memset(huge, 'a', sizeof huge - 1); huge[sizeof huge - 1] = 0;
+ CHECK(ProxyBridge_AddRule(huge, "*", "*", "*", RULE_PROTOCOL_BOTH, RULE_ACTION_BLOCK, 0, FALSE) == 0);
+ UINT32 ok = ProxyBridge_AddRule("ok.exe", "*", "*", "*", RULE_PROTOCOL_BOTH, RULE_ACTION_BLOCK, 0, FALSE);
+ CHECK(ok != 0 && !ProxyBridge_EditRule(ok, huge, "*", "*", "*", RULE_PROTOCOL_BOTH, RULE_ACTION_BLOCK, 0, FALSE) && m4("ok.exe", "8.8.8.8", 80) == RULE_ACTION_BLOCK);
+ ProxyBridge_DeleteRule(ok); }
+
return pbtest_done("test_rules_match");
}