feat: debug options

This commit is contained in:
FUFSoB committed 2026-10-11 11:18:48 +05:00
1 parent cacd51595c
commit e293efd1c1
33 files changed
+965 -77

No files matched your search

@@ -19,6 +19,8 @@ let
perAppPinSlots
perAppPinTproxy
perAppPinTun
singBoxLogLevel
connectionLogEnabled
;
inherit (constants)
xrayDnsBridgePorts
@@ -188,7 +190,7 @@ let
lib.recursiveUpdate
(
{
log.level = "warn";
log.level = singBoxLogLevel;
dns = mkDnsConfig {
localDetour = if forceLocalDnsViaProxy then "proxy" else null;
@@ -297,8 +299,8 @@ let
// lib.optionalAttrs enableClashApi (clashApiBlock clashApiPort)
)
# Fake addresses handed out survive a restart, so apps still holding one keep working. The
# start script creates the directory; nothing else is stored, since TUN configs have no
# Clash API to switch a selector with.
# start script creates the directory; nothing else is stored, since no selector of a TUN
# config is switched through the Clash API.
(
lib.optionalAttrs fakeIp {
experimental.cache_file = {
@@ -330,7 +332,9 @@ in
tunAutoRedirect = true;
tunStrictRoute = true;
forceLocalDnsViaProxy = false;
enableClashApi = false;
# Only for debug.connectionLog to read the connections.
enableClashApi = connectionLogEnabled;
clashApiPort = constants.globalTunClashApiPort;
xrayDnsBridgePort = xrayDnsBridgePorts.tun;
fakeIpCache = "tun";
};
@@ -341,8 +345,8 @@ in
tunAddress = perAppRoutingTun.address;
tunIPv6Address = constants.perAppTunIPv6Address;
pins = if perAppPinTun then tunPins else null;
# The pins' selectors are switched through it.
enableClashApi = perAppPinTun;
# The pins' selectors are switched through it; debug.connectionLog reads the connections.
enableClashApi = perAppPinTun || connectionLogEnabled;
clashApiPort = constants.perAppTunClashApiPort;
tunMtu = perAppRoutingTun.mtu;
tunAutoRoute = false;
@@ -13,6 +13,7 @@ let
globalTun
globalTproxy
perAppRoutingTun
xrayLogLevel
;
inherit (constants)
globalTunIPv6Address
@@ -296,7 +297,7 @@ let
++ [ (finalRule (if (proxyCfg.routing.default == "proxy") then "proxy" else "direct")) ];
in
{
log.loglevel = "warning";
log.loglevel = xrayLogLevel;
dns = mkDnsConfig { fakeDns = enableTunFakeDns; };
inbounds =
lib.optional enableMixed {
+16
View File
@@ -628,6 +628,17 @@ let
collapseNamedOutbounds = selectionMode == "first";
# Always on with sing-box: `proxy-ctl proxy outbounds test` needs it in every selection mode.
clashApiEnabled = singBoxEnabled;
# debug.logLevel, in each backend's words.
singBoxLogLevel = cfg.debug.logLevel;
xrayLogLevel =
{
warn = "warning";
info = "info";
debug = "debug";
trace = "debug";
}
.${cfg.debug.logLevel};
connectionLogEnabled = cfg.enable && clashApiEnabled && cfg.debug.connectionLog.enable;
perAppZapretEnabled = perAppZapretCfg.enable;
# The global units each per-app backend steps aside for (constants.refuseUnderGlobal), as
# systemctl patterns: they stop it as they start, and the standby unit waits them out.
@@ -1159,6 +1170,8 @@ let
ipv6 = "fd66:21::${lib.toLower (lib.toHexString (slot + 2))}";
};
perAppTunClashApiPort = 19180;
# The global TUN backend's, for debug.connectionLog alone.
globalTunClashApiPort = 19181;
# The DNS forwarders of per-app TUN and TProxy apps (proxy-suite-per-app-<route>-dns): the
# route's at the base, pin slot n's at base + 1 + n; they ask perAppDnsUpstream with its mark.
perAppTproxyDnsBasePort = 19200;
@@ -1230,6 +1243,9 @@ in
inherit
proxyCfg
localProxy
singBoxLogLevel
xrayLogLevel
connectionLogEnabled
singBoxCfg
xrayCfg
proxyEnabled
+97
View File
@@ -0,0 +1,97 @@
{ lib, ... }:
let
inherit (lib) mkOption types;
inherit (import ./lib.nix { inherit lib; }) bool;
in
{
# Troubleshooting only: everything here logs more, some of it every site visited.
options.services.proxy-suite.debug = {
logLevel = mkOption {
type = types.enum [
"warn"
"info"
"debug"
"trace"
];
default = "warn";
description = ''
How much the proxy backends log to the journal (`proxy-ctl logs proxy-suite-socks`):
sing-box, and XRay where it runs (XRay has no "trace": it logs at "debug" then).
"info" logs every connection with its destination and the rule that routed it.
`/run/proxy-suite/xray-loglevel` still overrides XRay's level at the next start.
'';
example = "info";
};
connectionLog = {
enable = bool false ''
Record the backends' connections to the journal: each one as it opens, with its
listener, site, rule and exit, and as it closes, with the bytes each way and how long
it lasted. For failures that come and go: look back at the time one happened. A unit
per backend, up while it is: proxy-suite-connection-log for the local proxy and
global or per-app TProxy, proxy-suite-connection-log-tun for the global TUN and
proxy-suite-connection-log-per-app-tun for per-app TUN, whose Clash APIs this turns
on. The same view live: `proxy-ctl debug conns`. Polls the Clash API, so it needs the
sing-box or hybrid backend; a connection shorter than `interval` can be missed.
'';
hosts = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Record only connections to these domains and their subdomains, or these addresses.
Empty: every connection.
'';
example = [
"twimg.com"
"x.com"
];
};
inbounds = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
Record only connections that came in on listeners whose tag starts with one of
these: "tproxy" for global and per-app TProxy, "tun-in" for the global and per-app
TUN, "per-app-pin" for apps run `--via` an outbound through TProxy, "mixed-in" for
the local proxy. Empty: every listener.
'';
example = [ "tproxy" ];
};
stallAfter = mkOption {
type = types.ints.unsigned;
default = 10;
description = ''
Seconds a connection may wait on an answer: one that has sent since it last
received, and received nothing for this long, is logged as stalled, and again
when it resumes. Several requests share a connection (HTTP/2), so a stall shows
only when all of them wait. 0: never.
'';
};
interval = mkOption {
type = types.numbers.positive;
default = 0.5;
description = "Seconds between looks at the connections.";
};
};
perAppDns = bool false ''
Log every lookup the per-app DNS forwarders send on (per-app TUN and TProxy, and apps
run through an AmneziaWG interface): the name, its type, the answer's code and how long
it took, and the lookups that got no answer. In the journals of
proxy-suite-per-app-tun-dns, proxy-suite-per-app-tproxy-dns and
proxy-suite-per-app-via@.
'';
zapret2 = {
packets = bool false "Log everything nfqws2 does to the journal, every packet included. Very verbose.";
autoHostlist = bool false "Log why sites are or are not learned (nfqws2's own learner).";
};
tgWsProxy = bool false "Log the Telegram proxy's debug messages.";
};
}
+1
View File
@@ -25,6 +25,7 @@
./user-control.nix
./kill-switch.nix
./host.nix
./debug.nix
];
options.services.proxy-suite.enable = lib.mkEnableOption "proxy-suite";
+8 -1
View File
@@ -23,6 +23,13 @@ in
(lib.mkRenamedOptionModule (p "tray.autostart") (p "gui.autostart"))
(lib.mkRenamedOptionModule (p "tray.pollInterval") (p "gui.refreshInterval"))
# Troubleshooting switches gathered under debug.
(lib.mkRenamedOptionModule (p "zapret.zapret2.debug") (p "debug.zapret2.packets"))
(lib.mkRenamedOptionModule (p "zapret.zapret2.autoHostlist.debugLog") (
p "debug.zapret2.autoHostlist"
))
(lib.mkRenamedOptionModule (p "tgWsProxy.log.verbose") (p "debug.tgWsProxy"))
# These were aliases; they are removal notices now, so there is one
# compatibility policy instead of two.
(rm "proxyInbounds" "Use services.proxy-suite.inbounds.")
@@ -65,7 +72,7 @@ in
(rm "tgWsProxy.cfProxyFallback" "Use services.proxy-suite.tgWsProxy.cloudflare.fallback.")
(rm "tgWsProxy.bufKb" "Use services.proxy-suite.tgWsProxy.bufferKiB.")
(rm "tgWsProxy.routingMark" "Use services.proxy-suite.tgWsProxy.fwmark.")
(rm "tgWsProxy.verbose" "Use services.proxy-suite.tgWsProxy.log.verbose.")
(rm "tgWsProxy.verbose" "Use services.proxy-suite.debug.tgWsProxy.")
(rm "tgWsProxy.logFile" "Use services.proxy-suite.tgWsProxy.log.file.")
(rm "tgWsProxy.logMaxMb" "Use services.proxy-suite.tgWsProxy.log.maxSizeMiB.")
(rm "tgWsProxy.logBackups" "Use services.proxy-suite.tgWsProxy.log.keep.")
@@ -94,12 +94,6 @@ in
};
log = {
verbose = mkOption {
type = types.bool;
default = false;
description = "Log debug messages.";
};
file = mkOption {
type = types.nullOr types.str;
default = null;
+2 -13
View File
@@ -120,16 +120,11 @@ in
default = true;
description = ''
Log each counted failure and strategy switch to the journal, per site
(`proxy-ctl logs proxy-suite-zapret`). Without it nfqws2 logs them only with `debug`.
(`proxy-ctl logs proxy-suite-zapret`). Without it nfqws2 logs them only with
`debug.zapret2.packets`.
'';
};
debug = mkOption {
type = types.bool;
default = false;
description = "Log everything nfqws2 does to the journal, every packet included. Very verbose: for troubleshooting.";
};
ipv6 = mkOption {
type = types.bool;
default = false;
@@ -160,12 +155,6 @@ in
udpOut = int 4 "UDP packets sent, with at most `udpIn` replies, that count as a failure.";
udpIn = int 1 "Most UDP replies that still count as no answer (see `udpOut`).";
debugLog = mkOption {
type = types.bool;
default = false;
description = "Log why sites are or are not learned (nfqws2's own learner).";
};
extendedDetection = mkOption {
type = types.bool;
default = true;
@@ -289,6 +289,9 @@ let
(mkAssertion (!(proxyCfg.autoProxy.enable && pureXrayEnabled))
"proxy-suite: proxy.autoProxy needs the sing-box backend: learned routes are local rule-sets that sing-box reloads from disk, and pure XRay has no equivalent"
)
(mkAssertion (!cfg.debug.connectionLog.enable || singBoxEnabled)
"proxy-suite: debug.connectionLog reads sing-box's Clash API: it needs proxy.enable = true and proxy.backend = \"sing-box\" or \"hybrid\""
)
(mkAssertion
(!cfg.sshProxy.enable || (cfg.sshProxy.server.user != null && cfg.sshProxy.server.host != null))
"proxy-suite: sshProxy.server.user and sshProxy.server.host are required when sshProxy.enable = true"
+3
View File
@@ -68,6 +68,9 @@ in
withCurlImpersonate = cfg.tools.curlImpersonate.enable;
env = {
CLASH_API = "http://127.0.0.1:${toString singBoxCfg.clashApiPort}";
# The TUN backends' own, root's alone (`debug conns --backend`).
CLASH_API_TUN = "http://127.0.0.1:${toString constants.globalTunClashApiPort}";
CLASH_API_PER_APP_TUN = "http://127.0.0.1:${toString constants.perAppTunClashApiPort}";
SELECTION = selectionMode;
SUB_TAGS_FILE = toString subscriptionTagsFile;
SUB_CACHE_DIR = subscriptionCacheDir;
+93
View File
@@ -223,6 +223,98 @@ let
};
};
# debug.connectionLog: `proxy-ctl debug conns` into the journal, a unit per sing-box
# instance, up while it is.
connectionLogUnit =
let
logCfg = cfg.debug.connectionLog;
instances = [
{
unit = "proxy-suite-connection-log";
backend = "socks";
service = "proxy-suite-socks.service";
}
]
++ lib.optional globalTun.enable {
unit = "proxy-suite-connection-log-tun";
backend = "tun";
service = "proxy-suite-tun.service";
}
++ lib.optional perAppRoutingTun.enable {
unit = "proxy-suite-connection-log-per-app-tun";
backend = "per-app-tun";
service = "proxy-suite-per-app-tun.service";
};
in
lib.mkIf derived.connectionLogEnabled {
services.proxy-suite.internal.services = lib.listToAttrs (
map (instance: {
name = instance.unit;
value = mkConnectionLog logCfg instance;
}) instances
);
};
mkConnectionLog =
logCfg:
{ backend, service, ... }:
{
description = "proxy-suite - record the ${backend} backend's connections (debug.connectionLog)";
after = [ service ];
# Not BindsTo: per-app TUN stops when no app needs it, and this must not count as one.
partOf = [ service ];
wantedBy = [ service ];
serviceConfig = {
ExecStart = lib.escapeShellArgs (
[
"${control.proxyCtl}/bin/proxy-ctl"
"debug"
"conns"
"--backend"
backend
"--stall"
(toString logCfg.stallAfter)
"--interval"
(toString logCfg.interval)
]
++ lib.concatMap (h: [
"--host"
h
]) logCfg.hosts
++ lib.concatMap (i: [
"--inbound"
i
]) logCfg.inbounds
);
Restart = "on-failure";
RestartSec = 5;
# Root for the Clash API secret's sake (its file is root's), with no capability at all.
CapabilityBoundingSet = [ "" ];
NoNewPrivileges = true;
PrivateTmp = true;
# Root without capabilities still owns the disks' device nodes.
PrivateDevices = true;
ProtectSystem = "strict";
ProtectHome = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
LockPersonality = true;
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
# The API is on loopback; nothing else to reach.
IPAddressAllow = [ "localhost" ];
IPAddressDeny = [ "any" ];
};
};
# userControl.groups cannot own the spools the way userControl.group does: POSIX ACLs let
# them in, set again on every boot and whenever the groups change (a changed unit
# restarts), so a group dropped from the configuration loses what it had. With
@@ -261,6 +353,7 @@ lib.mkMerge [
autoProxyUnits
outboundGroupsUnit
clashBrokerUnit
connectionLogUnit
aclUnit
{
services.proxy-suite.internal = {
@@ -257,7 +257,7 @@ let
set -euo pipefail
${resolveInstance}
${dnsServers}
exec ${python3} ${proxySuiteScriptsDir}/per_app_dns.py --port "$dns_port" --mark "$mark" "''${servers[@]}"
exec ${python3} ${proxySuiteScriptsDir}/per_app_dns.py --port "$dns_port" --mark "$mark" ${lib.optionalString cfg.debug.perAppDns "--log "}"''${servers[@]}"
'';
# A failure keeps the marking and the unreachable rule, so the apps are turned away rather
@@ -13,6 +13,7 @@ let
xrayEnabled
hybridEnabled
pureXrayEnabled
xrayLogLevel
constants
zapretCutoffProxyFallback
zapret2DirectSync
@@ -196,7 +197,8 @@ let
RUNTIME_DIR="${runtimeDir}"
ROUTE_MODE_STATE_FILE="${routeModeStateFile}"
BACKEND_JQ_FILTER=${lib.escapeShellArg backendJqFilterFile}
XRAY_LOGLEVEL=""
# debug.logLevel; empty keeps each config's own "warning".
XRAY_LOGLEVEL=${lib.escapeShellArg (lib.optionalString (xrayLogLevel != "warning") xrayLogLevel)}
XRAY_SINGLE_PROXY_TAG=""
mkdir -p "$RUNTIME_DIR"
OUTBOUNDS_JSON='[]'
+1
View File
@@ -193,6 +193,7 @@ let
]
++ map (l: "--listen ${l}") listeners
++ map (a: "--bind ${a}") bind
++ lib.optional ctx.cfg.debug.perAppDns "--log"
++ [ ctx.constants.perAppDnsUpstream ]
);
runtimeDirectory = "proxy-suite-per-app-${route}-dns";
+1 -1
View File
@@ -39,7 +39,7 @@ let
else
"args+=(--secret=${lib.escapeShellArg t.secret})"
}
${dcArgs}${mkFlagArg t.log.verbose "--verbose"}${
${dcArgs}${mkFlagArg cfg.debug.tgWsProxy "--verbose"}${
mkOptionalValueArg (t.log.file != null) "--log-file" t.log.file
}${lib.optionalString (t.log.file != null) (mkRawValueArg "--log-max-mb" t.log.maxSizeMiB)}${
lib.optionalString (t.log.file != null) (mkRawValueArg "--log-backups" t.log.keep)
+2 -2
View File
@@ -176,7 +176,7 @@ let
# (an app or a domain sent through a tunnel) it would otherwise take the default route,
# away from the connection it was meant to precede; without such routing it is a no-op.
optPrefix = lib.concatStringsSep " " (
lib.optional zapret2Cfg.debug "--debug=1"
lib.optional cfg.debug.zapret2.packets "--debug=1"
++ [
"--bind-fix4"
"--bind-fix6"
@@ -265,7 +265,7 @@ let
"AUTOHOSTLIST_INCOMING_MAXSEQ=${toString autoCfg.incomingMaxseq}"
"AUTOHOSTLIST_UDP_OUT=${toString autoCfg.udpOut}"
"AUTOHOSTLIST_UDP_IN=${toString autoCfg.udpIn}"
"AUTOHOSTLIST_DEBUGLOG=${if autoCfg.debugLog then "1" else "0"}"
"AUTOHOSTLIST_DEBUGLOG=${if cfg.debug.zapret2.autoHostlist then "1" else "0"}"
""
]
)