From ce86e5669b679fab435832db355284fb2854c382 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Thu, 18 Jun 2026 12:53:35 +0500 Subject: [PATCH] fix: different xray fixes --- docs/options.md | 4 - modules/proxy-suite/config.nix | 18 ++-- modules/proxy-suite/service-assertions.nix | 3 + modules/proxy-suite/service/scripts.nix | 10 +- nix/checks.nix | 108 +++++++++++++++++++++ scripts/proxy_parsing.py | 15 ++- scripts/test-build-outbound.py | 19 ++++ 7 files changed, 156 insertions(+), 21 deletions(-) diff --git a/docs/options.md b/docs/options.md index 6fe1492..7b65371 100644 --- a/docs/options.md +++ b/docs/options.md @@ -534,9 +534,6 @@ services.proxy-suite = { package = pkgs.xray; }; }; - singBox = { - _freeformOptions = { }; - }; tgWsProxy = { bypassTransparentProxy = true; dcIps = { @@ -4456,7 +4453,6 @@ true - [modules/proxy-suite/options/zapret\.nix](https://github.com/FUFSoB/proxy-suite-flake/blob/main/modules/proxy-suite/options/zapret.nix) - ## services\.proxy-suite\.zapret\.cidrExemption\.enable diff --git a/modules/proxy-suite/config.nix b/modules/proxy-suite/config.nix index d9180b5..91f0b7f 100644 --- a/modules/proxy-suite/config.nix +++ b/modules/proxy-suite/config.nix @@ -155,23 +155,17 @@ let xrayDnsAddress = upstream: - let - base = - if upstream.type == "tls" then - "tls://${upstream.address}" - else - upstream.address; - in - if upstream.port == 53 && upstream.type != "tls" then - base + if upstream.type == "tcp" then + "tcp://${upstream.address}" else - "${base}:${toString upstream.port}"; + upstream.address; mkXrayDnsServer = tag: upstream: { address = xrayDnsAddress upstream; inherit tag; + port = upstream.port; queryStrategy = "UseIP"; }; @@ -291,8 +285,8 @@ let else { servers = [ - (xrayDnsAddress proxyCfg.dns.remote) - (xrayDnsAddress proxyCfg.dns.local) + (mkXrayDnsServer "remote" proxyCfg.dns.remote) + (mkXrayDnsServer "local" proxyCfg.dns.local) ]; }; xrayTunOutbounds = lib.optionals enableTunFakeDns [ diff --git a/modules/proxy-suite/service-assertions.nix b/modules/proxy-suite/service-assertions.nix index 533f554..5f6824e 100644 --- a/modules/proxy-suite/service-assertions.nix +++ b/modules/proxy-suite/service-assertions.nix @@ -68,6 +68,9 @@ let (mkAssertion ( !(xrayEnabled && proxyCfg.selection == "selector") ) "proxy-suite: proxy.selection = \"selector\" is only available with proxy.singBox.enable = true") + (mkAssertion ( + !(xrayEnabled && (proxyCfg.dns.local.type == "tls" || proxyCfg.dns.remote.type == "tls")) + ) "proxy-suite: proxy.dns.*.type = \"tls\" is not supported with proxy.xray.enable = true; use udp/tcp DNS for XRay") (requireEnabled globalTun.enable proxyEnabled "proxy-suite: proxy.tun.enable requires proxy.enable = true" ) diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 35d97a2..3c28ad9 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -119,8 +119,12 @@ let { streamSettings.sockopt.mark = routingMark; } else { routing_mark = routingMark; }; + taggedRaw = backendRaw // { inherit tag; }; in - backendRaw // { inherit tag; } // markAttrs; + if xrayEnabled then + lib.recursiveUpdate taggedRaw markAttrs + else + taggedRaw // markAttrs; mkOutboundBlock = ob: routingMark: tag: @@ -406,7 +410,7 @@ let | if $xray_loglevel == "" then . else .log.loglevel = $xray_loglevel end | if $auth_enabled then (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.auth) = "password" - | (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.users) = [{user:$user,pass:$password}] + | (.inbounds[] | select(.protocol == "socks" and .tag == "mixed-in") | .settings.accounts) = [{user:$user,pass:$password}] else . end | if $xray_tun_dns_runtime then .dns.servers = xray_dns_server_order($dns_final) @@ -480,7 +484,7 @@ let fi ''} ${lib.optionalString xrayTunEgressBinding '' - XRAY_TUN_BIND_INTERFACE="$(${pkgs.iproute2}/bin/ip -4 route get 1.1.1.1 2>/dev/null | ${pkgs.gawk}/bin/awk ' + XRAY_TUN_BIND_INTERFACE="$(${pkgs.iproute2}/bin/ip -4 route get 1.1.1.1 mark ${toString globalTproxy.proxyMark} 2>/dev/null | ${pkgs.gawk}/bin/awk ' /dev/ { for (i = 1; i <= NF; i++) { if ($i == "dev" && i + 1 <= NF) { diff --git a/nix/checks.nix b/nix/checks.nix index cbd85b3..b4b3318 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -137,6 +137,18 @@ let builtins.head ( builtins.filter (line: pkgs.lib.hasPrefix prefix line) (pkgs.lib.splitString "\n" text) ); + lineContaining = + text: infix: + builtins.head ( + builtins.filter (line: pkgs.lib.hasInfix infix line) (pkgs.lib.splitString "\n" text) + ); + staticOutboundJsonFromScript = + script: + let + line = lineContaining script "OB_JSON=$(cat \""; + parts = pkgs.lib.splitString "\"" line; + in + builtins.fromJSON (builtins.readFile (builtins.elemAt parts 1)); normalizeExec = value: if builtins.isList value then builtins.head value else value; readExecScripts = value: @@ -350,6 +362,80 @@ let xraySubscriptionStartScript = builtins.readFile ( xraySubscriptionFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart ); + xrayRawStreamFixture = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + xray.enable = true; + outbounds = [ + { + tag = "raw-stream"; + xrayJson = { + protocol = "vless"; + settings = { + address = "example.com"; + port = 443; + id = "00000000-0000-0000-0000-000000000000"; + encryption = "none"; + }; + streamSettings = { + network = "ws"; + security = "tls"; + tlsSettings.serverName = "cdn.example.com"; + wsSettings.path = "/ws"; + sockopt.tcpFastOpen = true; + }; + }; + } + ]; + }; + }; + } + ]; + xrayRawStreamStartScript = builtins.readFile ( + xrayRawStreamFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart + ); + xrayRawStreamOutbound = staticOutboundJsonFromScript xrayRawStreamStartScript; + xrayDnsTcpFixture = evalProxySuite [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + xray.enable = true; + dns.local = { + type = "tcp"; + address = "9.9.9.9"; + port = 5353; + }; + outbounds = [ { tag = "primary"; url = "http://proxy.example.com:8080"; } ]; + }; + }; + } + ]; + xrayDnsTcpConfig = mkTProxyConfig xrayDnsTcpFixture; + xrayDnsTlsUnavailable = mkBadFixtureRaw [ + { + system.stateVersion = "26.05"; + services.proxy-suite = { + enable = true; + proxy = { + enable = true; + xray.enable = true; + dns.remote = { + type = "tls"; + address = "1.1.1.1"; + port = 853; + }; + outbounds = [ { tag = "primary"; url = "http://proxy.example.com:8080"; } ]; + }; + }; + } + ]; xrayBothBackends = mkBadFixtureRaw [ { system.stateVersion = "26.05"; @@ -1821,9 +1907,12 @@ let assert xrayTunBackendJqFilterFile == xrayPerAppTunBackendJqFilterFile; assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" xrayStartScript; assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' xrayStartScript; + assert pkgs.lib.hasInfix ''select(.protocol == "socks" and .tag == "mixed-in") | .settings.accounts'' xrayBackendJqFilter; + assert !(pkgs.lib.hasInfix ''select(.protocol == "socks" and .tag == "mixed-in") | .settings.users'' xrayBackendJqFilter); assert pkgs.lib.hasInfix "xray-loglevel" xrayTunStartScript; assert pkgs.lib.hasInfix "XRAY_SINGLE_PROXY_TAG=" xrayTunStartScript; assert pkgs.lib.hasInfix "ip -4 route get 1.1.1.1" xrayTunStartScript; + assert pkgs.lib.hasInfix "ip -4 route get 1.1.1.1 mark 2" xrayTunStartScript; assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" xrayTunStartScript; assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' xrayTunStartScript; assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayTunStartScript; @@ -1862,6 +1951,7 @@ let assert perAppTunDirectOutbound.streamSettings.sockopt.mark == 2; assert pkgs.lib.hasInfix "XRAY_SINGLE_PROXY_TAG=" xrayPerAppTunStartScript; assert pkgs.lib.hasInfix "ip -4 route get 1.1.1.1" xrayPerAppTunStartScript; + assert pkgs.lib.hasInfix "ip -4 route get 1.1.1.1 mark 2" xrayPerAppTunStartScript; assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" xrayPerAppTunStartScript; assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' xrayPerAppTunStartScript; assert pkgs.lib.hasInfix "--routing-mark 2" xrayPerAppTunStartScript; @@ -1889,6 +1979,24 @@ let assert pkgs.lib.hasInfix "--backend xray" xraySubscriptionStartScript; true ) + # -- xray backend: raw stream settings and DNS encoding are preserved -- + ( + let + localDns = dnsServerByTag xrayDnsTcpConfig "local"; + in + assert xrayRawStreamOutbound.tag == "proxy"; + assert xrayRawStreamOutbound.streamSettings.network == "ws"; + assert xrayRawStreamOutbound.streamSettings.security == "tls"; + assert xrayRawStreamOutbound.streamSettings.tlsSettings.serverName == "cdn.example.com"; + assert xrayRawStreamOutbound.streamSettings.wsSettings.path == "/ws"; + assert xrayRawStreamOutbound.streamSettings.sockopt.tcpFastOpen == true; + assert xrayRawStreamOutbound.streamSettings.sockopt.mark == 2; + assert localDns.address == "tcp://9.9.9.9"; + assert localDns.port == 5353; + assert localDns.queryStrategy == "UseIP"; + assert xrayDnsTlsUnavailable.success == false; + true + ) # -- backend selection and backend-specific raw JSON options are enforced -- ( assert xrayBothBackends.success == false; diff --git a/scripts/proxy_parsing.py b/scripts/proxy_parsing.py index 0110bd7..93b7c88 100644 --- a/scripts/proxy_parsing.py +++ b/scripts/proxy_parsing.py @@ -53,7 +53,10 @@ def _xray_tls(tls: dict | None) -> tuple[str, dict]: if tls.get("alpn"): settings["alpn"] = tls["alpn"] if tls.get("insecure"): - settings["allowInsecure"] = True + raise ValueError( + "XRay no longer supports tlsSettings.allowInsecure; " + "remove insecure=1 or use a supported certificate verification option" + ) if tls.get("ech_config_list"): settings["echConfigList"] = tls["ech_config_list"] utls = tls.get("utls", {}) @@ -150,13 +153,21 @@ def render_xray_outbound(ob: dict, routing_mark: int | None = None) -> dict: stream = stream_settings() stream["network"] = "hysteria" stream["security"] = "tls" + stream["hysteriaSettings"] = {"version": 2} settings = { "version": 2, "address": ob["server"], "port": ob["server_port"], } if ob.get("password"): - settings["password"] = ob["password"] + stream["hysteriaSettings"]["auth"] = ob["password"] + if ob.get("obfs", {}).get("type") == "salamander": + stream.setdefault("finalmask", {}).setdefault("udp", []).append( + { + "type": "salamander", + "settings": {"password": ob["obfs"].get("password", "")}, + } + ) return {"protocol": "hysteria", "tag": tag, "settings": settings, "streamSettings": stream} if typ == "socks": diff --git a/scripts/test-build-outbound.py b/scripts/test-build-outbound.py index 5a9ef07..26101a3 100644 --- a/scripts/test-build-outbound.py +++ b/scripts/test-build-outbound.py @@ -182,6 +182,25 @@ class BuildOutboundTests(unittest.TestCase): self.assertTrue(ob["tls"]["insecure"]) self.assertEqual(ob["obfs"]["password"], "mask") + def test_xray_hysteria2(self): + ob = run_xray_parser( + "hy2://secret@example.com:443?sni=hy.example.com&obfs=salamander&obfs-password=mask" + ) + self.assertEqual(ob["protocol"], "hysteria") + self.assertEqual(ob["settings"]["address"], "example.com") + self.assertNotIn("password", ob["settings"]) + self.assertEqual(ob["streamSettings"]["network"], "hysteria") + self.assertEqual(ob["streamSettings"]["tlsSettings"]["serverName"], "hy.example.com") + self.assertEqual(ob["streamSettings"]["hysteriaSettings"]["auth"], "secret") + self.assertEqual( + ob["streamSettings"]["finalmask"]["udp"], + [{"type": "salamander", "settings": {"password": "mask"}}], + ) + + def test_xray_hysteria2_insecure_fails_loudly(self): + with self.assertRaisesRegex(ValueError, "allowInsecure"): + run_xray_parser("hy2://secret@example.com:443?sni=hy.example.com&insecure=1") + def test_tuic(self): ob = run_parser( "tuic://00000000-0000-0000-0000-000000000000:secret@example.com:443?sni=tuic.example.com&alpn=h3,hq-29"