update urltest logic

This commit is contained in:
FUFSoB committed 2026-04-12 18:00:19 +05:00
1 parent 2fb920d812
commit fe309b8889
4 files changed
+119 -15

No files matched your search

+23 -10
View File
@@ -182,10 +182,15 @@ singBox = {
]; ];
# urltest is strongly recommended for subscriptions: sing-box probes all proxies # 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. # a single dead server in position 0 breaks everything.
selection = "urltest"; selection = "urltest";
subscriptionUpdateInterval = "6h"; # refresh every 6 hours (default: 1d) 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). **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 ### Tag generation
Each proxy URI in the subscription gets a sing-box outbound tag derived from its `#remark` fragment: Each proxy URI in the subscription gets a sing-box outbound tag derived from its `#remark` fragment:
@@ -226,7 +239,7 @@ singBox = {
selection = "urltest"; selection = "urltest";
# These domains go through whichever community proxy wins the latency test. # 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 ## Services
| Service | Auto-starts | Description | | Service | Auto-starts | Description |
| -------------------------------------- | -------------------- | ------------------------------------------------ | | --------------------------------- | ------------------ | ------------------------------------------------ |
| `proxy-suite-socks` | yes | SOCKS5/HTTP proxy, always running | | `proxy-suite-socks` | yes | SOCKS5/HTTP proxy, always running |
| `proxy-suite-tproxy` | no | TProxy transparent mode (start manually) | | `proxy-suite-tproxy` | no | TProxy transparent mode (start manually) |
| `proxy-suite-tun` | no | TUN mode (start manually, conflicts with tproxy) | | `proxy-suite-tun` | no | TUN mode (start manually, conflicts with tproxy) |
| `proxy-suite-tg-ws-proxy` | yes (if enabled) | Telegram proxy | | `proxy-suite-tg-ws-proxy` | yes (if enabled) | Telegram proxy |
| `proxy-suite-zapret-vm-exempt` | yes (if enabled) | VM subnet exemption | | `proxy-suite-zapret-vm-exempt` | yes (if enabled) | VM subnet exemption |
| `proxy-suite-subscription-update` | timer (if sub set) | Refresh subscription caches and restart | | `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. TProxy and TUN are mutually exclusive. Start one, the other refuses to start.
+39 -1
View File
@@ -272,10 +272,48 @@ in
How to pick between multiple outbounds: How to pick between multiple outbounds:
- "first": use the first outbound only (no overhead, simplest) - "first": use the first outbound only (no overhead, simplest)
- "selector": expose a Clash-compatible API for manual switching - "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 { clashApiPort = mkOption {
type = types.port; type = types.port;
default = 9090; default = 9090;
+4 -1
View File
@@ -145,7 +145,10 @@ let
TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON") TAGS=$(${jq} '[.[].tag]' <<< "$OUTBOUNDS_JSON")
WRAPPER=$(${jq} -n \ WRAPPER=$(${jq} -n \
--argjson tags "$TAGS" \ --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") OUTBOUNDS_JSON=$(${jq} --argjson w "$WRAPPER" '[$w] + .' <<< "$OUTBOUNDS_JSON")
''; '';
in in
+53 -3
View File
@@ -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 # Validation failures
subscriptionBothSources = forceEval ( subscriptionBothSources = forceEval (
(evalProxySuite [ (evalProxySuite [
@@ -844,14 +867,16 @@ let
# -- subscription: StateDirectory set on socks service -- # -- 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"; == "proxy-suite";
true true
) )
# -- subscription: custom update interval flows through to timer -- # -- 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"; == "6h";
true true
) )
@@ -859,7 +884,8 @@ let
# -- subscription: selection=first renames first subscription outbound to "proxy" -- # -- subscription: selection=first renames first subscription outbound to "proxy" --
( (
let 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; scriptText = builtins.readFile startScript;
in in
assert builtins.match ".*proxy.*" scriptText != null; assert builtins.match ".*proxy.*" scriptText != null;
@@ -899,6 +925,30 @@ let
assert noOutboundsNoSubscriptions.success == false; assert noOutboundsNoSubscriptions.success == false;
true 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 in
{ {