diff --git a/README.md b/README.md index 16e18b4..356baa2 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@ The goal is to replace GUI clients like v2rayN and throne – you configure your ## What it gives you - **SOCKS5/HTTP proxy** on `127.0.0.1:1080` by default – always running, always available to apps -- **Transparent proxy (TProxy)** – redirect all system traffic through sing-box without configuring each app; start/stop on demand -- **TUN mode** – full tunnel via a virtual network interface; useful when TProxy doesn't cover something +- **Transparent proxy (TProxy)** – redirect all system traffic through sing-box without configuring each app; start/stop on demand or autostart at boot +- **TUN mode** – full tunnel via a virtual network interface; useful when TProxy doesn't cover something, with optional boot-time autostart - **Subscription URLs** – point at a v2rayN/Clash-format subscription endpoint; the module fetches, decodes, and imports all proxies automatically, with a periodic refresh timer - **Multiple outbounds** with automatic latency-based switching or manual selection - **Per-outbound routing** – route specific domains, IPs, or geo sets to specific servers diff --git a/docs/options.md b/docs/options.md index 712cc11..e12cbd4 100644 --- a/docs/options.md +++ b/docs/options.md @@ -44,6 +44,7 @@ services.proxy-suite = { subscriptionUpdateInterval = "1d"; subscriptions = [ ]; tproxy = { + autostart = false; enable = false; localSubnets = [ "192.168.0.0/16" @@ -52,6 +53,7 @@ services.proxy-suite = { tproxyPort = 1081; tun = { address = "172.19.0.1/30"; + autostart = false; enable = false; interface = "singtun0"; mtu = 1400; @@ -1674,6 +1676,41 @@ false +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options.nix) + + + +## services\.proxy-suite\.singBox\.tproxy\.autostart + + + +Whether to start proxy-suite-tproxy automatically during boot by +attaching it to multi-user\.target\. +Only relevant when singBox\.tproxy\.enable = true\. +Cannot be enabled together with singBox\.tun\.autostart\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + *Example:* ```nix @@ -1825,6 +1862,41 @@ string +## services\.proxy-suite\.singBox\.tun\.autostart + + + +Whether to start proxy-suite-tun automatically during boot by +attaching it to multi-user\.target\. +Only relevant when singBox\.tun\.enable = true\. +Cannot be enabled together with singBox\.tproxy\.autostart\. + + + +*Type:* +boolean + + + +*Default:* + +```nix +false +``` + + + +*Example:* + +```nix +true +``` + +*Declared by:* + - [modules/proxy-suite/options\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options.nix) + + + ## services\.proxy-suite\.singBox\.tun\.interface diff --git a/modules/proxy-suite/options.nix b/modules/proxy-suite/options.nix index 190cfc1..133a140 100644 --- a/modules/proxy-suite/options.nix +++ b/modules/proxy-suite/options.nix @@ -491,6 +491,18 @@ in tun = { enable = mkEnableOption "TUN mode service (opt-in, mutually exclusive with proxy-suite-tproxy)"; + autostart = mkOption { + type = types.bool; + default = false; + description = '' + Whether to start proxy-suite-tun automatically during boot by + attaching it to multi-user.target. + Only relevant when singBox.tun.enable = true. + Cannot be enabled together with singBox.tproxy.autostart. + ''; + example = true; + }; + interface = mkOption { type = types.str; default = "singtun0"; @@ -525,6 +537,18 @@ in tproxy = { enable = mkEnableOption "TProxy mode service (opt-in, transparent proxy via nftables)"; + autostart = mkOption { + type = types.bool; + default = false; + description = '' + Whether to start proxy-suite-tproxy automatically during boot by + attaching it to multi-user.target. + Only relevant when singBox.tproxy.enable = true. + Cannot be enabled together with singBox.tun.autostart. + ''; + example = true; + }; + localSubnets = mkOption { type = types.listOf types.str; default = [ "192.168.0.0/16" ]; diff --git a/modules/proxy-suite/service.nix b/modules/proxy-suite/service.nix index 7809066..cf5d0ec 100644 --- a/modules/proxy-suite/service.nix +++ b/modules/proxy-suite/service.nix @@ -420,6 +420,10 @@ in assertion = invalidRoutingTargets == [ ]; message = "proxy-suite: routing.rules reference unknown outbound tag(s): ${lib.concatStringsSep ", " invalidRoutingTargets}"; } + { + assertion = !(sb.tproxy.enable && sb.tproxy.autostart && sb.tun.enable && sb.tun.autostart); + message = "proxy-suite: singBox.tproxy.autostart and singBox.tun.autostart cannot both be enabled at the same time"; + } { assertion = !t.enable @@ -484,6 +488,7 @@ in "network.target" "proxy-suite-socks.service" ]; + wantedBy = lib.optionals sb.tproxy.autostart [ "multi-user.target" ]; requires = [ "proxy-suite-socks.service" ]; conflicts = [ "proxy-suite-tun.service" ]; serviceConfig = { @@ -510,6 +515,7 @@ in proxy-suite-tun = { description = "sing-box TUN proxy client"; after = [ "network-online.target" ]; + wantedBy = lib.optionals sb.tun.autostart [ "multi-user.target" ]; wants = [ "network-online.target" ]; conflicts = [ "proxy-suite-tproxy.service" ]; serviceConfig = { diff --git a/nix/checks.nix b/nix/checks.nix index f62e088..95b2c18 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -178,6 +178,58 @@ let } ]; + tproxyManualFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.tproxy.enable = true; + } + ]; + + tproxyAutostartFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.tproxy = { + enable = true; + autostart = true; + }; + } + ]; + + tunManualFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.tun.enable = true; + } + ]; + + tunAutostartFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.tun = { + enable = true; + autostart = true; + }; + } + ]; + + conflictingAutostartModes = forceEval ( + (evalProxySuite [ + baseModule + { + services.proxy-suite.singBox = { + tproxy = { + enable = true; + autostart = true; + }; + tun = { + enable = true; + autostart = true; + }; + }; + } + ]).config.system.build.toplevel.drvPath + ); + routingOrFixture = evalProxySuite [ { system.stateVersion = "26.05"; @@ -680,6 +732,32 @@ let assert tproxyWithFirewall.config.networking.firewall.enable; true ) + ( + assert tproxyManualFixture.config.services.proxy-suite.singBox.tproxy.autostart == false; + assert tproxyManualFixture.config.systemd.services."proxy-suite-tproxy".wantedBy == [ ]; + true + ) + ( + assert + tproxyAutostartFixture.config.systemd.services."proxy-suite-tproxy".wantedBy + == [ "multi-user.target" ]; + true + ) + ( + assert tunManualFixture.config.services.proxy-suite.singBox.tun.autostart == false; + assert tunManualFixture.config.systemd.services."proxy-suite-tun".wantedBy == [ ]; + true + ) + ( + assert + tunAutostartFixture.config.systemd.services."proxy-suite-tun".wantedBy + == [ "multi-user.target" ]; + true + ) + ( + assert conflictingAutostartModes.success == false; + true + ) ( assert builtins.length routingOrDomainRules == 1; true