Skip to content

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

219 - Added Stop and Pause

AnoF-Cyber263fe0bba9dc
diff --git a/Windows/README.md b/Windows/README.md
index 74aef2d..c5b38d2 100644
--- a/Windows/README.md
+++ b/Windows/README.md
@@ -225,6 +225,12 @@ just start typing while the list has focus, **Enter** or **Down** to go to the m
**x** button) to clear the search. Edit, Clone, Remove and the enable check boxes work on the rules you see;
Move Up / Move Down are only available when no search is active, because the order of a partial list is ambiguous.
+**Pausing ProxyBridge (main switch):** untick **Proxy → Enable ProxyBridge** (or right-click the tray icon and untick
+**Enable ProxyBridge**) to switch every rule off without closing the app. While paused, new connections go out
+directly: proxy rules **and block rules** do not apply, and the window title and tray tooltip show "(PAUSED)".
+Connections that were already being proxied keep working until they end. Tick the item again to apply the rules
+to new connections. ProxyBridge always starts enabled; the switch is not remembered between runs.
+
**Note:** Adding a rule with action **PROXY** while no proxy is configured will fall back to a direct connection. Configure at least one proxy server before using PROXY rules.
### Command Line Interface (CLI)
diff --git a/Windows/gui/api/pb_api.h b/Windows/gui/api/pb_api.h
index 52f6c7e..6d15471 100644
--- a/Windows/gui/api/pb_api.h
+++ b/Windows/gui/api/pb_api.h
@@ -36,6 +36,7 @@ typedef UINT32 (*PFN_GetRulePosition)(UINT32);
typedef void (*PFN_SetLogCallback)(PBLogCallback);
typedef void (*PFN_SetConnectionCallback)(PBConnectionCallback);
typedef void (*PFN_SetTrafficLoggingEnabled)(BOOL);
+typedef void (*PFN_SetEnabled)(BOOL);
typedef void (*PFN_ClearConnectionLogs)(void);
typedef BOOL (*PFN_Start)(void);
typedef BOOL (*PFN_Stop)(void);
@@ -58,6 +59,7 @@ typedef struct {
PFN_SetLogCallback SetLogCallback;
PFN_SetConnectionCallback SetConnectionCallback;
PFN_SetTrafficLoggingEnabled SetTrafficLoggingEnabled;
+ PFN_SetEnabled SetEnabled;
PFN_ClearConnectionLogs ClearConnectionLogs;
PFN_Start Start;
PFN_Stop Stop;
@@ -102,6 +104,7 @@ static BOOL PB_LoadDirect(PBApi* api)
PB_BIND(SetLogCallback, "SetLogCallback");
PB_BIND(SetConnectionCallback, "SetConnectionCallback");
PB_BIND(SetTrafficLoggingEnabled, "SetTrafficLoggingEnabled");
+ PB_BIND(SetEnabled, "SetEnabled");
PB_BIND(ClearConnectionLogs, "ClearConnectionLogs");
PB_BIND(Start, "Start");
PB_BIND(Stop, "Stop");
diff --git a/Windows/gui/api/pb_ipc.h b/Windows/gui/api/pb_ipc.h
index 8beeb41..e327a29 100644
--- a/Windows/gui/api/pb_ipc.h
+++ b/Windows/gui/api/pb_ipc.h
@@ -128,6 +128,7 @@ static BOOL IpcMoveRule(UINT32 id, UINT32 pos) { PBIPC_BEGIN(PBOP_MOVE_RULE); Pb
static UINT32 IpcGetRulePos(UINT32 id) { PBIPC_BEGIN(PBOP_GET_RULE_POS); PbW_U32(&_w, id); PBIPC_END(); }
static void IpcSetTrafficLog(BOOL v) { PBIPC_BEGIN(PBOP_SET_TRAFFIC_LOG); PbW_U32(&_w, v != 0); PbIpcCall(_b, _w.len); }
static void IpcClearConnLogs(void) { PBIPC_BEGIN(PBOP_CLEAR_CONN_LOGS); PbIpcCall(_b, _w.len); }
+static void IpcSetEnabled(BOOL v) { PBIPC_BEGIN(PBOP_SET_ENABLED); PbW_U32(&_w, v != 0); PbIpcCall(_b, _w.len); }
static void IpcSetLogCallback(PBLogCallback cb) { g_ipcLogCb = cb; }
static void IpcSetConnectionCallback(PBConnectionCallback cb) { g_ipcConnCb = cb; }
static BOOL IpcStart(void) { PBIPC_BEGIN(PBOP_START); PBIPC_END(); }
@@ -244,7 +245,7 @@ static int PB_LoadService(PBApi* api)
api->MoveRuleToPosition = IpcMoveRule; api->GetRulePosition = IpcGetRulePos;
api->SetLogCallback = IpcSetLogCallback;
api->SetConnectionCallback = IpcSetConnectionCallback;
- api->SetTrafficLoggingEnabled = IpcSetTrafficLog;
+ api->SetTrafficLoggingEnabled = IpcSetTrafficLog; api->SetEnabled = IpcSetEnabled;
api->ClearConnectionLogs = IpcClearConnLogs; api->Start = IpcStart; api->Stop = IpcStop;
return PB_IPC_OK;
}
diff --git a/Windows/gui/loc/loc.h b/Windows/gui/loc/loc.h
index 5e4fa7e..77e0262 100644
--- a/Windows/gui/loc/loc.h
+++ b/Windows/gui/loc/loc.h
@@ -10,7 +10,7 @@
#define APP_VERSION PB_VER_STRW // defined in res/resource.h (single source of truth)
enum {
- S_M_PROXY, S_M_PROXY_SETTINGS, S_M_PROXY_RULES,
+ S_M_PROXY, S_M_PROXY_SETTINGS, S_M_PROXY_RULES, S_M_ENABLE, S_TITLE_PAUSED,
S_M_PROFILE, S_M_NEWPROFILE, S_M_DELPROFILE, S_M_IMPORT, S_M_EXPORT, S_M_SWITCH,
S_M_SETTINGS, S_CHK_LOCALHOST, S_M_TRAFFIC, S_M_TRAY, S_M_LANG, S_M_LANG_EN, S_M_LANG_ZH,
S_M_CLEARCONN, S_M_CLEARACT, S_M_HELP, S_M_ABOUT, S_M_DOCS, S_M_STARTUP,
@@ -47,6 +47,8 @@ static const wchar_t* const g_strings[S_COUNT][2] = {
/* S_M_PROXY */ { L"&Proxy", L"代理(&P)" },
/* S_M_PROXY_SETTINGS */ { L"Proxy &Settings...", L"代理设置(&S)..." },
/* S_M_PROXY_RULES */ { L"Proxy &Rules...", L"代理规则(&R)..." },
+ /* S_M_ENABLE */ { L"&Enable ProxyBridge", L"启用 ProxyBridge(&E)" },
+ /* S_TITLE_PAUSED */ { L" (PAUSED)", L" (已暂停)" },
/* S_M_PROFILE */ { L"P&rofile", L"配置文件(&R)" },
/* S_M_NEWPROFILE */ { L"&New Profile...", L"新建配置(&N)..." },
/* S_M_DELPROFILE */ { L"&Delete Current Profile", L"删除当前配置(&D)" },
diff --git a/Windows/gui/main.c b/Windows/gui/main.c
index 22e1381..a31f878 100644
--- a/Windows/gui/main.c
+++ b/Windows/gui/main.c
@@ -126,6 +126,7 @@ static BOOL g_started = FALSE;
static PBProfile g_profile;
static wchar_t g_activeProfile[PB_NAME_MAX] = L"Default";
static BOOL g_trafficLog = TRUE, g_closeToTray = TRUE;
+static BOOL g_proxyEnabled = TRUE; // main switch; not saved: ProxyBridge always starts enabled
static BOOL g_startup = FALSE; // "Run at Startup" (schtasks logon task)
static BOOL g_autoClear = TRUE; // auto-clear logs past a line threshold to save memory
#define AUTO_CLEAR_LINES 500
@@ -331,7 +332,7 @@ static void SwitchToProfile(const wchar_t* name)
static void UpdateTitle(void)
{
- wchar_t t[128]; _snwprintf_s(t, 128, _TRUNCATE, L"%s - %s", APP_TITLE, g_activeProfile);
+ wchar_t t[160]; _snwprintf_s(t, 160, _TRUNCATE, L"%s - %s%s", APP_TITLE, g_activeProfile, g_proxyEnabled ? L"" : T(S_TITLE_PAUSED));
SetWindowTextW(g_hMain, t);
}
@@ -356,6 +357,8 @@ static void BuildMainMenu(HWND hwnd)
HMENU bar = CreateMenu();
HMENU proxy = CreatePopupMenu();
+ AppendMenuW(proxy, MF_STRING, IDM_PROXY_ENABLE, T(S_M_ENABLE)); // main on/off switch
+ AppendMenuW(proxy, MF_SEPARATOR, 0, NULL);
AppendMenuW(proxy, MF_STRING, IDM_PROXY_SETTINGS, T(S_M_PROXY_SETTINGS));
AppendMenuW(proxy, MF_STRING, IDM_PROXY_RULES, T(S_M_PROXY_RULES));
AppendMenuW(proxy, MF_STRING, IDM_LOG_FILTERS, T(S_M_LOGFILTERS));
@@ -453,6 +456,14 @@ static void TrayAdd(HWND hwnd)
// broadcast re-adds the icon once the taskbar actually appears
g_trayAdded = Shell_NotifyIconW(NIM_ADD, &g_tray);
}
+static void TrayUpdateTip(void)
+{
+ if (!g_trayAdded) return;
+ _snwprintf_s(g_tray.szTip, ARRAYSIZE(g_tray.szTip), _TRUNCATE, L"%s%s", APP_TITLE, g_proxyEnabled ? L"" : T(S_TITLE_PAUSED));
+ g_tray.uFlags = NIF_TIP;
+ Shell_NotifyIconW(NIM_MODIFY, &g_tray);
+ g_tray.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
+}
static void TrayRemove(void) { if (g_trayAdded) { Shell_NotifyIconW(NIM_DELETE, &g_tray); g_trayAdded = FALSE; } }
static void ShowMainWindow(HWND hwnd) { ShowWindow(hwnd, SW_SHOW); ShowWindow(hwnd, SW_RESTORE); SetForegroundWindow(hwnd); }
@@ -556,6 +567,7 @@ static void SelectTab(int idx)
static void SyncMenuChecks(HWND hwnd)
{
HMENU m = GetMenu(hwnd);
+ CheckMenuItem(m, IDM_PROXY_ENABLE, MF_BYCOMMAND | (g_proxyEnabled ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(m, IDM_SET_TRAFFICLOG, MF_BYCOMMAND | (g_trafficLog ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(m, IDM_SET_CLOSETOTRAY, MF_BYCOMMAND | (g_closeToTray? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(m, IDM_SET_STARTUP, MF_BYCOMMAND | (g_startup ? MF_CHECKED : MF_UNCHECKED));
@@ -872,6 +884,7 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
POINT pt; GetCursorPos(&pt);
HMENU pm = CreatePopupMenu();
AppendMenuW(pm, MF_STRING, IDM_TRAY_SHOW, T(S_TRAY_SHOW));
+ AppendMenuW(pm, MF_STRING | (g_proxyEnabled ? MF_CHECKED : MF_UNCHECKED), IDM_PROXY_ENABLE, T(S_M_ENABLE));
AppendMenuW(pm, MF_SEPARATOR, 0, NULL);
AppendMenuW(pm, MF_STRING, IDM_TRAY_EXIT, T(S_TRAY_EXIT));
SetForegroundWindow(hwnd);
@@ -893,6 +906,11 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
case IDM_PROXY_SETTINGS: DialogBoxW(g_hInst, MAKEINTRESOURCEW(IDD_SERVERS), hwnd, ServersDlgProc); return 0;
case IDM_PROXY_RULES: DialogBoxW(g_hInst, MAKEINTRESOURCEW(IDD_RULES), hwnd, RulesDlgProc); return 0;
+ case IDM_PROXY_ENABLE:
+ g_proxyEnabled = !g_proxyEnabled;
+ g_api.SetEnabled(g_proxyEnabled);
+ UpdateTitle(); TrayUpdateTip(); SyncMenuChecks(hwnd);
+ return 0;
case IDM_SET_TRAFFICLOG:
g_trafficLog = !g_trafficLog; g_profile.trafficLogging = g_trafficLog;
g_api.SetTrafficLoggingEnabled(g_trafficLog); SaveActive(); SyncMenuChecks(hwnd); return 0;
@@ -1183,6 +1201,7 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR cmd, int show)
// Wire callbacks + toggles, add configs (before Start so the UDP relay starts), start,
// then add rules.
g_api.SetTrafficLoggingEnabled(g_trafficLog);
+ g_api.SetEnabled(g_proxyEnabled);
g_api.SetLogCallback(PBLogCb);
g_api.SetConnectionCallback(PBConnCb);
ApplyConfigs();
diff --git a/Windows/gui/res/resource.h b/Windows/gui/res/resource.h
index 085268f..ee33599 100644
--- a/Windows/gui/res/resource.h
+++ b/Windows/gui/res/resource.h
@@ -26,6 +26,7 @@
#define IDM_ZOOM_IN 223
#define IDM_ZOOM_OUT 224
#define IDM_ZOOM_RESET 225
+#define IDM_PROXY_ENABLE 226 // main on/off switch
#define IDM_PROFILE_RENAME 244
// Log Filters (list) dialog
diff --git a/Windows/src/ProxyBridge.c b/Windows/src/ProxyBridge.c
index 48e5115..ba202f9 100644
--- a/Windows/src/ProxyBridge.c
+++ b/Windows/src/ProxyBridge.c
@@ -34,6 +34,7 @@ volatile BOOL g_has_active_rules = FALSE;
// Set when at least one enabled rule carries a domain filter. Gates the DNS-cache
// lookup in match_rule so setups without domain rules pay zero extra cost.
volatile BOOL g_has_domain_rules = FALSE;
+volatile BOOL g_proxy_enabled = TRUE;
SOCKET udp_relay_socket = INVALID_SOCKET;
SOCKET udp_relay_socket6 = INVALID_SOCKET;
volatile BOOL running = FALSE;
@@ -1035,6 +1036,18 @@ DWORD WINAPI packet_processor(LPVOID arg)
return 0;
}
+// Main on/off switch. FALSE pauses ProxyBridge without stopping the capture driver: every NEW connection is
+// sent directly, and no rule (proxy or block) applies. Connections that are already being proxied keep working
+// until they end. TRUE (the default) applies the rules again.
+PROXYBRIDGE_API void ProxyBridge_SetEnabled(BOOL enable)
+{
+ BOOL was = g_proxy_enabled;
+ g_proxy_enabled = enable ? TRUE : FALSE;
+ if (was != g_proxy_enabled)
+ log_message(enable ? "ProxyBridge enabled: rules apply to new connections"
+ : "ProxyBridge paused: new connections go direct (connections already proxied continue)");
+}
+
PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback)
{
g_log_callback = callback;
diff --git a/Windows/src/ProxyBridge.h b/Windows/src/ProxyBridge.h
index 2ba289e..a051b51 100644
--- a/Windows/src/ProxyBridge.h
+++ b/Windows/src/ProxyBridge.h
@@ -61,6 +61,7 @@ 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);
PROXYBRIDGE_API BOOL ProxyBridge_MoveRuleToPosition(UINT32 rule_id, UINT32 new_position); // Move rule to specific position (1=first, 2=second, etc)
PROXYBRIDGE_API UINT32 ProxyBridge_GetRulePosition(UINT32 rule_id); // Get current position of rule in list (1-based)
+PROXYBRIDGE_API void ProxyBridge_SetEnabled(BOOL enable); // main switch: FALSE pauses all rules for new connections (default TRUE)
PROXYBRIDGE_API void ProxyBridge_SetLogCallback(LogCallback callback);
PROXYBRIDGE_API void ProxyBridge_SetConnectionCallback(ConnectionCallback callback);
PROXYBRIDGE_API void ProxyBridge_SetTrafficLoggingEnabled(BOOL enable);
diff --git a/Windows/src/pb_internal.h b/Windows/src/pb_internal.h
index da1262c..13f15a0 100644
--- a/Windows/src/pb_internal.h
+++ b/Windows/src/pb_internal.h
@@ -197,6 +197,7 @@ extern HANDLE cleanup_thread;
extern PID_CACHE_ENTRY *pid_cache[PID_CACHE_SIZE];
extern volatile BOOL g_has_active_rules;
extern volatile BOOL g_has_domain_rules;
+extern volatile BOOL g_proxy_enabled; // FALSE = paused: new connections are neither proxied nor blocked (existing ones carry on)
extern SOCKET udp_relay_socket;
extern SOCKET udp_relay_socket6;
extern volatile BOOL running;
diff --git a/Windows/src/pb_rules.c b/Windows/src/pb_rules.c
index d54747a..7ec6f97 100644
--- a/Windows/src/pb_rules.c
+++ b/Windows/src/pb_rules.c
@@ -671,6 +671,12 @@ RuleAction match_rule_inner(const char *process_name, UINT32 dest_ip, UINT16 des
// DeleteRule/MoveRule from the GUI thread cannot free a node/string mid-match.
RuleAction match_rule(const char *process_name, UINT32 dest_ip, UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)
{
+ if (!g_proxy_enabled) // paused: no rule applies
+ {
+ g_tl_rule_localhost = FALSE;
+ if (out_proxy_config_id != NULL) *out_proxy_config_id = 0;
+ return RULE_ACTION_DIRECT;
+ }
AcquireSRWLockShared(&g_rules_lock);
RuleAction action = match_rule_inner(process_name, dest_ip, dest_port, is_udp, out_proxy_config_id);
ReleaseSRWLockShared(&g_rules_lock);
@@ -762,6 +768,12 @@ RuleAction match_rule_v6_inner(const char *process_name, const UINT8 dest_ip6[16
RuleAction match_rule_v6(const char *process_name, const UINT8 dest_ip6[16], UINT16 dest_port, BOOL is_udp, UINT32 *out_proxy_config_id)
{
+ if (!g_proxy_enabled) // paused: no rule applies
+ {
+ g_tl_rule_localhost = FALSE;
+ if (out_proxy_config_id != NULL) *out_proxy_config_id = 0;
+ return RULE_ACTION_DIRECT;
+ }
AcquireSRWLockShared(&g_rules_lock);
RuleAction action = match_rule_v6_inner(process_name, dest_ip6, dest_port, is_udp, out_proxy_config_id);
ReleaseSRWLockShared(&g_rules_lock);
diff --git a/Windows/src/service/pb_proto.h b/Windows/src/service/pb_proto.h
index 619e4ef..2795876 100644
--- a/Windows/src/service/pb_proto.h
+++ b/Windows/src/service/pb_proto.h
@@ -19,7 +19,7 @@
#define PB_SERVICE_NAME L"ProxyBridgeSvc"
#define PB_PIPE_CTL L"\\\\.\\pipe\\ProxyBridgeCtl"
#define PB_PIPE_EVT L"\\\\.\\pipe\\ProxyBridgeEvt"
-#define PB_PROTO_VERSION 2 // 2: per-rule localhost flag; PBOP_SET_LOCALHOST removed
+#define PB_PROTO_VERSION 3 // 3: PBOP_SET_ENABLED (main switch); 2: per-rule localhost flag
#define PB_MSG_MAX (64u * 1024u)
#define PB_STR_MAX (32u * 1024u)
#define PB_NULL_STR 0xFFFFFFFFu
@@ -40,7 +40,8 @@ enum {
PBOP_SET_TRAFFIC_LOG, // bool -> 0
PBOP_CLEAR_CONN_LOGS, // () -> 0
PBOP_START, // () -> bool
- PBOP_STOP // () -> bool
+ PBOP_STOP, // () -> bool
+ PBOP_SET_ENABLED // bool -> 0 (main switch: 0 pauses all rules for new connections)
};
enum {
diff --git a/Windows/src/service/pb_service.c b/Windows/src/service/pb_service.c
index 7cbaf45..1b2e27f 100644
--- a/Windows/src/service/pb_service.c
+++ b/Windows/src/service/pb_service.c
@@ -171,6 +171,7 @@ static BOOL Dispatch(const BYTE *req, DWORD reqLen, PbW *resp)
case PBOP_SET_TRAFFIC_LOG: { UINT32 v = PbR_U32(&r); if (!r.ok) return FALSE; g_api.SetTrafficLoggingEnabled(v != 0); break; }
case PBOP_CLEAR_CONN_LOGS: g_api.ClearConnectionLogs(); break;
+ case PBOP_SET_ENABLED: { UINT32 v = PbR_U32(&r); if (!r.ok) return FALSE; g_api.SetEnabled(v != 0); break; }
case PBOP_START: ret = g_api.Start(); break;
case PBOP_STOP: ret = g_api.Stop(); break;
default: return FALSE;