From d7108abf0cfc3aba598c1bda40415f32526d49bf Mon Sep 17 00:00:00 2001 From: fufsob Date: Tue, 15 Sep 2026 04:08:18 +0000 Subject: [PATCH] feat: support warp config generators --- modules/proxy-suite/options/warp.nix | 15 ++++++++++++++- modules/proxy-suite/warp.nix | 19 +++++++++++++++++-- nix/checks/warp.nix | 18 ++++++++++++++++++ scripts/test-warp-outbound.py | 5 +++++ scripts/warp_outbound.py | 3 ++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/modules/proxy-suite/options/warp.nix b/modules/proxy-suite/options/warp.nix index aefaa66..317daf8 100644 --- a/modules/proxy-suite/options/warp.nix +++ b/modules/proxy-suite/options/warp.nix @@ -12,7 +12,7 @@ in default = null; description = '' Runtime path to a WireGuard profile for WARP, as written by `wgcf register && wgcf generate`. - Junk-packet lines (Jc, Jmin, Jmax) are honoured by the AmneziaWG profile only. + AmneziaWG lines (Jc, S1-S4, H1-H4, I1-I5, AWG 3 timing) are honoured by the AmneziaWG profile only. When null, proxy-suite-warp registers a device with wgcf (accepting Cloudflare's terms) into /var/lib/proxy-suite/warp, through the local proxy when proxy.enable is set, and @@ -21,6 +21,19 @@ in example = "/run/secrets/wgcf-profile.conf"; }; + generatorUrl = mkOption { + type = types.nullOr types.str; + default = null; + description = '' + HTTPS URL of a WARP profile generator, fetched directly when wgcf registration fails + (e.g. the Cloudflare API is blocked and there is no local proxy). It must return a + WireGuard profile, or JSON with the base64 profile in `.content`. + + The generator registers the device, so its operator sees the private key. + ''; + example = "https://valokda-amnezia.vercel.app/api/warp?mode=awg2"; + }; + asOutbound = mkOption { type = types.bool; default = false; diff --git a/modules/proxy-suite/warp.nix b/modules/proxy-suite/warp.nix index 9268f65..478ac2c 100644 --- a/modules/proxy-suite/warp.nix +++ b/modules/proxy-suite/warp.nix @@ -46,7 +46,20 @@ let [ -s wgcf-account.toml ] || wgcf register --accept-tos wgcf generate } + ${lib.optionalString (w.generatorUrl != null) '' + # Fallback: a third-party generator registers from abroad and hands the profile back. + generate() { + body=$(${pkgs.curl}/bin/curl --noproxy '*' -fsS --max-time 60 -A proxy-suite \ + ${lib.escapeShellArg w.generatorUrl}) || return 1 + case "$body" in + "[Interface]"*) printf '%s\n' "$body" ;; + *) ${pkgs.jq}/bin/jq -er '.content | @base64d' <<< "$body" ;; + esac > wgcf-profile.conf.tmp || return 1 + grep -q '^PrivateKey' wgcf-profile.conf.tmp || return 1 + mv wgcf-profile.conf.tmp wgcf-profile.conf + } + ''} ${ if cfg.proxy.enable then '' @@ -58,10 +71,12 @@ let done userinfo= ${userinfo} - HTTPS_PROXY="socks5://''${userinfo}${hostPart}:${toString listener.port}" register || register + HTTPS_PROXY="socks5://''${userinfo}${hostPart}:${toString listener.port}" register || register${ + lib.optionalString (w.generatorUrl != null) " || generate" + } '' else - "register" + "register${lib.optionalString (w.generatorUrl != null) " || generate"}" } ''; diff --git a/nix/checks/warp.nix b/nix/checks/warp.nix index c26a635..5151bbd 100644 --- a/nix/checks/warp.nix +++ b/nix/checks/warp.nix @@ -56,6 +56,16 @@ let configFile = null; }; autoTunnel = tunnelOf auto; + generator = mkFixture "sing-box" { + asOutbound = true; + configFile = null; + generatorUrl = "https://gen.example.com/api/warp?mode=awg2"; + }; + registerOf = + fixture: + generated.readDerivation fixture.config.systemd.services."proxy-suite-warp".serviceConfig.ExecStart; + autoRegister = registerOf auto; + generatorRegister = registerOf generator; amneziaWg = evalProxySuite [ { @@ -185,6 +195,14 @@ in && hasInfix "until [ -s \"$profile\" ]" autoTunnel; true ) + # A generator only backs up wgcf, fetched directly with the URL quoted. + ( + assert + hasInfix "register || register || generate" generatorRegister + && hasInfix "'https://gen.example.com/api/warp?mode=awg2'" generatorRegister + && !(hasInfix "generate()" autoRegister); + true + ) ( assert amneziaWg.config.services.proxy-suite.amneziaWg.profiles.warp.configFile == profile; true diff --git a/scripts/test-warp-outbound.py b/scripts/test-warp-outbound.py index 82cbc27..1fc6e62 100644 --- a/scripts/test-warp-outbound.py +++ b/scripts/test-warp-outbound.py @@ -33,6 +33,11 @@ class WarpOutboundTest(unittest.TestCase): self.assertEqual((ob["peers"][0]["address"], ob["peers"][0]["port"]), ("2606:4700:d0::a29f:c001", 500)) self.assertNotIn("routing_mark", ob) + def test_awg3_keepalive_range(self): + # Generator AWG 3 profiles range the keepalive; sing-box gets the lower bound. + ob = build(WGCF.replace("[Peer]", "RekeyAfterTime = 100-120\n[Peer]\nPersistentKeepalive = 25-35"), "warp", None) + self.assertEqual(ob["peers"][0]["persistent_keepalive_interval"], 25) + def test_rejects_missing_peer(self): with self.assertRaises(ConfigError): build(WGCF.split("[Peer]")[0], "warp", None) diff --git a/scripts/warp_outbound.py b/scripts/warp_outbound.py index 05f690b..7d5c590 100644 --- a/scripts/warp_outbound.py +++ b/scripts/warp_outbound.py @@ -54,7 +54,8 @@ def build(text: str, tag: str, routing_mark: int | None) -> dict[str, Any]: "allowed_ips": _split(peer.get("allowedips", "")) or ["0.0.0.0/0", "::/0"], } if peer.get("persistentkeepalive"): - sb_peer["persistent_keepalive_interval"] = int(peer["persistentkeepalive"]) + # AWG 3 profiles give a range ("25-35"); sing-box takes one value. + sb_peer["persistent_keepalive_interval"] = int(peer["persistentkeepalive"].split("-")[0]) if peer.get("presharedkey"): sb_peer["pre_shared_key"] = peer["presharedkey"] endpoint: dict[str, Any] = {