From 12a5c063c47b7066aa1183bde2e40d28ff2b4da0 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Sun, 12 Apr 2026 18:00:19 +0500 Subject: [PATCH] update urltest logic --- README.md | 33 +++++++++++++------ modules/proxy-suite/options.nix | 40 ++++++++++++++++++++++- modules/proxy-suite/service.nix | 5 ++- nix/checks.nix | 56 +++++++++++++++++++++++++++++++-- 4 files changed, 119 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6dbafa8..25f6705 100644 --- a/README.md +++ b/README.md @@ -182,10 +182,15 @@ singBox = { ]; # urltest is strongly recommended for subscriptions: sing-box probes all proxies - # every 3 minutes and routes through the fastest working one. With selection = "first" + # concurrently and routes through the fastest working one. With selection = "first" # a single dead server in position 0 breaks everything. selection = "urltest"; subscriptionUpdateInterval = "6h"; # refresh every 6 hours (default: 1d) + + # Optional: test against a URL that is actually blocked in your region. + # Only proxies that bypass the block will win — not just any reachable server. + # Defaults to https://www.gstatic.com/generate_204 (reachability check only). + urlTest.url = "https://telegram.org/"; }; ``` @@ -206,6 +211,14 @@ subscriptions = [{ **Periodic refresh:** a systemd timer (`proxy-suite-subscription-update`) fires on boot (after 5 minutes) and then every `subscriptionUpdateInterval`. On success it updates the cache files and restarts `proxy-suite-socks` (and `proxy-suite-tun` if it is active). +### urltest startup behavior + +When `selection = "urltest"`, sing-box starts probing all proxies **concurrently** the moment the service comes up. Until the first test result arrives (typically within a few seconds), traffic uses the first proxy in the list. As tests complete, sing-box progressively switches to faster winners. Dead proxies are skipped as their tests time out. + +**Practical consequence:** the first few requests after a (re)start may go through a slow or unreachable proxy. This resolves quickly — usually within 5–20 seconds — without any manual action. + +To minimize this window, set `urlTest.url` to a URL that must actually be reachable through a working proxy (e.g. a blocked site in your region). This way the first working test result corresponds to a proxy that genuinely serves your traffic, not just any reachable server. + ### Tag generation Each proxy URI in the subscription gets a sing-box outbound tag derived from its `#remark` fragment: @@ -226,7 +239,7 @@ singBox = { selection = "urltest"; # These domains go through whichever community proxy wins the latency test. - routing.proxy.domains = [ "youtube.com" "twitter.com" ]; + routing.proxy.domains = [ "telegram.org" "twitter.com" ]; }; ``` @@ -431,14 +444,14 @@ tgWsProxy = { ## Services -| Service | Auto-starts | Description | -| -------------------------------------- | -------------------- | ------------------------------------------------ | -| `proxy-suite-socks` | yes | SOCKS5/HTTP proxy, always running | -| `proxy-suite-tproxy` | no | TProxy transparent mode (start manually) | -| `proxy-suite-tun` | no | TUN mode (start manually, conflicts with tproxy) | -| `proxy-suite-tg-ws-proxy` | yes (if enabled) | Telegram proxy | -| `proxy-suite-zapret-vm-exempt` | yes (if enabled) | VM subnet exemption | -| `proxy-suite-subscription-update` | timer (if sub set) | Refresh subscription caches and restart | +| Service | Auto-starts | Description | +| --------------------------------- | ------------------ | ------------------------------------------------ | +| `proxy-suite-socks` | yes | SOCKS5/HTTP proxy, always running | +| `proxy-suite-tproxy` | no | TProxy transparent mode (start manually) | +| `proxy-suite-tun` | no | TUN mode (start manually, conflicts with tproxy) | +| `proxy-suite-tg-ws-proxy` | yes (if enabled) | Telegram proxy | +| `proxy-suite-zapret-vm-exempt` | yes (if enabled) | VM subnet exemption | +| `proxy-suite-subscription-update` | timer (if sub set) | Refresh subscription caches and restart | TProxy and TUN are mutually exclusive. Start one, the other refuses to start. diff --git a/modules/proxy-suite/options.nix b/modules/proxy-suite/options.nix index e2f3763..9bf7324 100644 --- a/modules/proxy-suite/options.nix +++ b/modules/proxy-suite/options.nix @@ -272,10 +272,48 @@ in How to pick between multiple outbounds: - "first": use the first outbound only (no overhead, simplest) - "selector": expose a Clash-compatible API for manual switching - - "urltest": measure latency every 3 minutes and auto-switch to the fastest + - "urltest": measure latency periodically and auto-switch to the fastest ''; }; + urlTest = { + url = mkOption { + type = types.str; + default = "https://www.gstatic.com/generate_204"; + description = '' + URL that sing-box fetches through each proxy to measure latency. + Only used when selection = "urltest". + + Set this to a URL that is actually blocked in your region (e.g. + "https://telegram.org") so that only proxies that bypass the + blocking get selected. If left at the default, any responding proxy + wins — including ones that might not unblock your target site. + ''; + example = "https://telegram.org"; + }; + + interval = mkOption { + type = types.str; + default = "3m"; + description = '' + How often sing-box re-tests all outbounds. Accepts a Go duration + string (e.g. "1m", "3m", "10m"). Only used when selection = "urltest". + ''; + example = "1m"; + }; + + tolerance = mkOption { + type = types.int; + default = 50; + description = '' + Latency tolerance in milliseconds. The current proxy is only replaced + when a competing one is faster by more than this value. + Only used when selection = "urltest". + ''; + example = 100; + }; + }; + clashApiPort = mkOption { type = types.port; default = 9090; diff --git a/modules/proxy-suite/service.nix b/modules/proxy-suite/service.nix index 50f6a35..7809066 100644 --- a/modules/proxy-suite/service.nix +++ b/modules/proxy-suite/service.nix @@ -145,7 +145,10 @@ let TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON") WRAPPER=$(${jq} -n \ --argjson tags "$TAGS" \ - '{type:"urltest",tag:"proxy",outbounds:$tags,url:"https://www.gstatic.com/generate_204",interval:"3m",tolerance:50}') + --arg url ${lib.escapeShellArg sb.urlTest.url} \ + --arg interval ${lib.escapeShellArg sb.urlTest.interval} \ + --argjson tolerance ${toString sb.urlTest.tolerance} \ + '{type:"urltest",tag:"proxy",outbounds:$tags,url:$url,interval:$interval,tolerance:$tolerance}') OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON") ''; in diff --git a/nix/checks.nix b/nix/checks.nix index 4dcc9bd..66deaf2 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -545,6 +545,29 @@ let } ]; + urlTestCustomFixture = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + singBox = { + outbounds = [ + { + tag = "test-proxy"; + url = "http://proxy.example.com:8080"; + } + ]; + selection = "urltest"; + urlTest = { + url = "https://telegram.org"; + interval = "1m"; + tolerance = 100; + }; + }; + }; + } + ]; + # Validation failures subscriptionBothSources = forceEval ( (evalProxySuite [ @@ -844,14 +867,16 @@ let # -- subscription: StateDirectory set on socks service -- ( - assert subscriptionOnlyFixture.config.systemd.services."proxy-suite-socks".serviceConfig.StateDirectory + assert + subscriptionOnlyFixture.config.systemd.services."proxy-suite-socks".serviceConfig.StateDirectory == "proxy-suite"; true ) # -- subscription: custom update interval flows through to timer -- ( - assert subscriptionWithStaticFixture.config.systemd.timers."proxy-suite-subscription-update".timerConfig.OnUnitActiveSec + assert + subscriptionWithStaticFixture.config.systemd.timers."proxy-suite-subscription-update".timerConfig.OnUnitActiveSec == "6h"; true ) @@ -859,7 +884,8 @@ let # -- subscription: selection=first renames first subscription outbound to "proxy" -- ( let - startScript = subscriptionFirstSelectionFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart; + startScript = + subscriptionFirstSelectionFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart; scriptText = builtins.readFile startScript; in assert builtins.match ".*proxy.*" scriptText != null; @@ -899,6 +925,30 @@ let assert noOutboundsNoSubscriptions.success == false; true ) + + # -- urlTest: custom url/interval/tolerance propagate to start script -- + ( + let + startScript = + urlTestCustomFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart; + scriptText = builtins.readFile startScript; + in + assert builtins.match ".*telegram\\.org.*" scriptText != null; + assert builtins.match ".*1m.*" scriptText != null; + assert builtins.match ".*100.*" scriptText != null; + true + ) + + # -- urlTest: defaults are sane -- + ( + let + cfg = minimal.config.services.proxy-suite.singBox.urlTest; + in + assert cfg.url == "https://www.gstatic.com/generate_204"; + assert cfg.interval == "3m"; + assert cfg.tolerance == 50; + true + ) ]; in {