From 433a13ccad89a5e937641305f66e207e0bd17a64 Mon Sep 17 00:00:00 2001 From: FUFSoB Date: Tue, 9 Jun 2026 19:35:48 +0500 Subject: [PATCH] fix: truly fixing xray tun --- modules/proxy-suite/config.nix | 155 ++++++++++++++--- modules/proxy-suite/default.nix | 6 + modules/proxy-suite/service/default.nix | 31 +++- .../proxy-suite/service/per-app-routing.nix | 42 ++++- modules/proxy-suite/service/scripts.nix | 43 ++++- nix/checks.nix | 164 ++++++++++++++++-- 6 files changed, 392 insertions(+), 49 deletions(-) diff --git a/modules/proxy-suite/config.nix b/modules/proxy-suite/config.nix index f8b4f62..d9180b5 100644 --- a/modules/proxy-suite/config.nix +++ b/modules/proxy-suite/config.nix @@ -19,6 +19,19 @@ let ; perAppTun = derived.perAppRoutingTun; direct = rules.direct; + xrayGlobalTunIPv6Address = "fd66:19::1/64"; + xrayPerAppTunIPv6Address = "fd66:20::1/64"; + xrayFakeDnsPools = [ + { + ipPool = "198.18.0.0/15"; + poolSize = 32768; + } + { + ipPool = "fc00::/18"; + poolSize = 32768; + } + ]; + stripCidr = cidr: builtins.head (lib.splitString "/" cidr); mkSingBoxDnsServer = tag: upstream: detour: @@ -154,13 +167,74 @@ let else "${base}:${toString upstream.port}"; - xraySniffing = { - enabled = true; - destOverride = [ - "http" - "tls" - "quic" - ]; + mkXrayDnsServer = + tag: upstream: + { + address = xrayDnsAddress upstream; + inherit tag; + queryStrategy = "UseIP"; + }; + + mkXraySniffing = + { + fakeDnsOnly ? false, + }: + if fakeDnsOnly then + { + enabled = true; + destOverride = [ "fakedns" ]; + metadataOnly = true; + } + else + { + enabled = true; + destOverride = [ + "http" + "tls" + "quic" + ]; + }; + + xraySniffing = mkXraySniffing { }; + + mkXrayTunDnsConfig = + { + preferRemote ? proxyCfg.proxyByDefault, + }: + let + primaryTag = if preferRemote then "remote" else "local"; + secondaryTag = if preferRemote then "local" else "remote"; + primaryUpstream = if preferRemote then proxyCfg.dns.remote else proxyCfg.dns.local; + secondaryUpstream = if preferRemote then proxyCfg.dns.local else proxyCfg.dns.remote; + in + { + queryStrategy = "UseIP"; + tag = "dns-in"; + servers = [ + { + address = "fakedns"; + tag = "fakedns"; + } + (mkXrayDnsServer primaryTag primaryUpstream) + (mkXrayDnsServer secondaryTag secondaryUpstream) + ]; + }; + + xrayTunDnsHijackRule = { + type = "field"; + inboundTag = [ "tun-in" ]; + network = "tcp,udp"; + port = 53; + outboundTag = "dns-out"; + ruleTag = "dns-hijack"; + }; + + xrayTunDnsUpstreamRule = { + type = "field"; + inboundTag = [ "dns-in" ]; + network = "tcp,udp"; + outboundTag = "direct"; + ruleTag = "dns-upstream-direct"; }; xrayFinalRule = @@ -198,17 +272,55 @@ let enableTun ? false, tunInterface ? globalTun.interface, tunAddress ? globalTun.address, + tunIPv6Address ? null, tunMtu ? globalTun.mtu, useOutboundRoutingMark ? false, enableUrlTest ? proxyCfg.selection == "urltest", domainStrategy ? "IPIfNonMatch", + enableTunFakeDns ? false, }: + let + tunDnsServers = [ + (if proxyCfg.proxyByDefault then proxyCfg.dns.remote.address else proxyCfg.dns.local.address) + (if proxyCfg.proxyByDefault then proxyCfg.dns.local.address else proxyCfg.dns.remote.address) + ]; + xrayTunSniffing = mkXraySniffing { fakeDnsOnly = enableTunFakeDns; }; + xrayDnsConfig = + if enableTunFakeDns then + mkXrayTunDnsConfig { } + else + { + servers = [ + (xrayDnsAddress proxyCfg.dns.remote) + (xrayDnsAddress proxyCfg.dns.local) + ]; + }; + xrayTunOutbounds = lib.optionals enableTunFakeDns [ + { + protocol = "dns"; + tag = "dns-out"; + settings = { + userLevel = 0; + rules = [ + { + action = "direct"; + qType = "2-27,29-65535"; + } + ]; + }; + } + ]; + xrayRoutingRules = + lib.optionals enableTunFakeDns [ + xrayTunDnsUpstreamRule + xrayTunDnsHijackRule + ] + ++ rules.xrayRoutingRules + ++ [ (xrayFinalRule (if proxyCfg.proxyByDefault then "proxy" else "direct")) ]; + in { log.loglevel = "warning"; - dns.servers = [ - (xrayDnsAddress proxyCfg.dns.remote) - (xrayDnsAddress proxyCfg.dns.local) - ]; + dns = xrayDnsConfig; inbounds = lib.optional enableMixed { tag = "mixed-in"; @@ -240,14 +352,14 @@ let settings = { name = tunInterface; mtu = tunMtu; - gateway = [ tunAddress ]; - dns = [ proxyCfg.dns.local.address ]; + gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ]; + dns = tunDnsServers; userLevel = 0; autoOutboundsInterface = "auto"; }; - sniffing = xraySniffing; + sniffing = xrayTunSniffing; }; - outbounds = [ + outbounds = xrayTunOutbounds ++ [ (xrayDirectOutbound useOutboundRoutingMark) { protocol = "blackhole"; @@ -258,7 +370,7 @@ let routing = { domainStrategy = domainStrategy; domainMatcher = "hybrid"; - rules = xrayRouteRules; + rules = xrayRoutingRules; } // lib.optionalAttrs enableUrlTest { balancers = [ @@ -276,7 +388,8 @@ let probeUrl = proxyCfg.urlTest.url; probeInterval = proxyCfg.urlTest.interval; }; - }; + } + // lib.optionalAttrs enableTunFakeDns { fakedns = xrayFakeDnsPools; }; singBoxTproxyTemplate = mkSingBoxConfig { enableMixed = true; @@ -319,18 +432,20 @@ let enableTun = true; tunInterface = globalTun.interface; tunAddress = globalTun.address; + tunIPv6Address = xrayGlobalTunIPv6Address; tunMtu = globalTun.mtu; useOutboundRoutingMark = true; - domainStrategy = "AsIs"; + enableTunFakeDns = true; }; xrayPerAppTunTemplate = mkXrayConfig { enableTun = true; tunInterface = perAppTun.interface; tunAddress = perAppTun.address; + tunIPv6Address = xrayPerAppTunIPv6Address; tunMtu = perAppTun.mtu; - useOutboundRoutingMark = globalTproxy.enable; - domainStrategy = "AsIs"; + useOutboundRoutingMark = true; + enableTunFakeDns = true; }; tproxyTemplate = if xrayEnabled then xrayTproxyTemplate else singBoxTproxyTemplate; diff --git a/modules/proxy-suite/default.nix b/modules/proxy-suite/default.nix index b38147d..9f794e1 100644 --- a/modules/proxy-suite/default.nix +++ b/modules/proxy-suite/default.nix @@ -39,6 +39,12 @@ in }; in lib.mkMerge [ + { + warnings = lib.optional (cfg.proxy.enable && cfg.proxy.xray.enable) '' + proxy-suite: XRay backend is still in test mode; sing-box should be preferable for now. + ''; + } + (import ./service { inherit config diff --git a/modules/proxy-suite/service/default.nix b/modules/proxy-suite/service/default.nix index a01473d..b46f1f9 100644 --- a/modules/proxy-suite/service/default.nix +++ b/modules/proxy-suite/service/default.nix @@ -99,13 +99,18 @@ let # packets intended for per-app routing tables. xrayTunPerAppTproxyRulePriority = 8996; xrayTunPerAppTunRulePriority = 8997; + xrayGlobalTunIPv6Address = "fd66:19::1/64"; + xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64"; xrayTunUpScript = pkgs.writeShellScript "proxy-suite-xray-tun-up" '' set -euo pipefail tun_cidr=${lib.escapeShellArg globalTun.address} + tun6_cidr=${lib.escapeShellArg xrayGlobalTunIPv6Address} + tun6_route_prefix=${lib.escapeShellArg xrayGlobalTunIPv6RoutePrefix} tun_addr="" tun_route_prefix="" + uplink_addr="" cidr_network() { local cidr="$1" @@ -142,23 +147,45 @@ let exit 1 fi + uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${pkgs.gawk}/bin/awk ' + /src/ { + for (i = 1; i <= NF; i++) { + if ($i == "src" && i + 1 <= NF) { + print $(i + 1) + exit + } + } + } + ')" + if [ -z "$uplink_addr" ]; then + echo "proxy-suite: could not determine the default uplink IPv4 address for XRay TUN" >&2 + exit 1 + fi + while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done while ${ip} -4 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done + while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done + while ${ip} -6 rule del pref ${toString tunAutoRouteRulePriority} 2>/dev/null; do :; done tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" ${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg globalTun.interface} + ${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg globalTun.interface} ${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg globalTun.interface} src "$tun_addr" table ${toString tunAutoRouteTableIndex} - ${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex} + ${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} src "$uplink_addr" table ${toString tunAutoRouteTableIndex} + ${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex} + ${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex} ${lib.optionalString perAppRoutingTproxy.enable '' ${ip} -4 rule add pref ${toString xrayTunPerAppTproxyRulePriority} fwmark ${toString perAppRoutingTproxy.fwmark} table ${toString perAppRoutingTproxy.routeTable} ''} ${lib.optionalString perAppRoutingTun.enable '' ${ip} -4 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} + ${ip} -6 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} ''} ${ip} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex} + ${ip} -6 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex} ''; tproxyUpScript = pkgs.writeShellScript "proxy-suite-tproxy-up" '' @@ -196,10 +223,12 @@ let while ${ip} -6 rule del table ${toString tunAutoRouteTableIndex} 2>/dev/null; do :; done while ${ip} -4 rule del pref ${toString xrayTunPerAppTproxyRulePriority} 2>/dev/null; do :; done while ${ip} -4 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done + while ${ip} -6 rule del pref ${toString xrayTunPerAppTunRulePriority} 2>/dev/null; do :; done ${ip} -4 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true ${ip} -6 route flush table ${toString tunAutoRouteTableIndex} 2>/dev/null || true ${ip} link del dev ${lib.escapeShellArg globalTun.interface} 2>/dev/null || true + ${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true ''; systemServiceEntries = [ diff --git a/modules/proxy-suite/service/per-app-routing.nix b/modules/proxy-suite/service/per-app-routing.nix index daa65fa..b497808 100644 --- a/modules/proxy-suite/service/per-app-routing.nix +++ b/modules/proxy-suite/service/per-app-routing.nix @@ -98,6 +98,8 @@ let hasZapretProfiles = builtins.any ( profile: profile.route == "zapret" ) effectivePerAppRoutingProfiles; + xrayPerAppTunIPv6Address = "fd66:20::1/64"; + xrayPerAppTunIPv6RoutePrefix = "fd66:20::/64"; perAppTunWaitForInterface = pkgs.writeShellScript "proxy-suite-per-app-tun-wait-for-interface" '' set -euo pipefail @@ -115,8 +117,11 @@ let set -euo pipefail tun_cidr=${lib.escapeShellArg perAppRoutingTun.address} + tun6_cidr=${lib.escapeShellArg xrayPerAppTunIPv6Address} + tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix} tun_addr="" tun_route_prefix="" + uplink_addr="" cidr_network() { local cidr="$1" @@ -142,16 +147,36 @@ let } ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true - while ${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done - ${ip} route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done + while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done + ${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + ${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ${nft} -f ${perAppTunChainFile} ${perAppTunWaitForInterface} + uplink_addr="$(${ip} -4 route get 1.1.1.1 2>/dev/null | ${awk} ' + /src/ { + for (i = 1; i <= NF; i++) { + if ($i == "src" && i + 1 <= NF) { + print $(i + 1) + exit + } + } + } + ')" + if [ -z "$uplink_addr" ]; then + echo "proxy-suite: could not determine the default uplink IPv4 address for app TUN" >&2 + exit 1 + fi tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" ${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} - ${ip} route replace "$tun_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$tun_addr" table ${toString perAppRoutingTun.routeTable} - ${ip} route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} - ${ip} rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + ${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} + ${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$tun_addr" table ${toString perAppRoutingTun.routeTable} + ${ip} -4 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} src "$uplink_addr" table ${toString perAppRoutingTun.routeTable} + ${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} + ${ip} -6 route replace default dev ${lib.escapeShellArg perAppRoutingTun.interface} table ${toString perAppRoutingTun.routeTable} + ${ip} -4 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + ${ip} -6 rule add fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ''; perAppTunDownScript = pkgs.writeShellScript "proxy-suite-per-app-tun-down" '' @@ -159,9 +184,12 @@ let # Best-effort cleanup for graceful stops and for unclean previous exits. ${nft} delete table inet proxy_suite_per_app_tun 2>/dev/null || true - while ${ip} rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done - ${ip} route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + while ${ip} -4 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done + while ${ip} -6 rule del fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable} 2>/dev/null; do :; done + ${ip} -4 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true + ${ip} -6 route flush table ${toString perAppRoutingTun.routeTable} 2>/dev/null || true ${ip} link del dev ${lib.escapeShellArg perAppRoutingTun.interface} 2>/dev/null || true + ${pkgs.systemd}/bin/resolvectl flush-caches 2>/dev/null || true ''; perAppTproxyUpScript = pkgs.writeShellScript "proxy-suite-per-app-tproxy-up" '' diff --git a/modules/proxy-suite/service/scripts.nix b/modules/proxy-suite/service/scripts.nix index 725f8f2..35d97a2 100644 --- a/modules/proxy-suite/service/scripts.nix +++ b/modules/proxy-suite/service/scripts.nix @@ -354,7 +354,7 @@ let if xrayEnabled then '' def xray_bind_outbound($interface): - if $interface == "" or .protocol == "blackhole" then + if $interface == "" or .protocol == "blackhole" or .protocol == "dns" then . else .streamSettings = (.streamSettings // {}) @@ -377,20 +377,47 @@ let else . end; + def xray_preserved_rules: + [.routing.rules[] + | select(((.ruleTag? // "") == "dns-hijack") or ((.ruleTag? // "") == "dns-upstream-direct"))]; def xray_final_rule($tag; $single_tag): if $tag == "proxy" and "${selectionMode}" == "urltest" then {type:"field",network:"tcp,udp",ruleTag:"final-default"} + xray_proxy_rule_target($single_tag) else {type:"field",network:"tcp,udp",ruleTag:"final-default",outboundTag:$tag} end; + def xray_dns_server_order($dns_final): + if $dns_final == "" then + .dns.servers + else + ([.dns.servers[] | select((.tag? // "") == "fakedns")] + + [.dns.servers[] | select((.tag? // "") == $dns_final)] + + [.dns.servers[] | select((.tag? // "") != "fakedns" and (.tag? // "") != $dns_final)]) + end; + def xray_tun_dns_addresses($dns_final; $local_addr; $remote_addr): + if $dns_final == "local" then + [$local_addr, $remote_addr] + elif $dns_final == "remote" then + [$remote_addr, $local_addr] + else + [$remote_addr, $local_addr] + end; .outbounds = (($obs + .outbounds) | map(xray_bind_outbound($xray_bind_interface))) | 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}] else . end + | if $xray_tun_dns_runtime then + .dns.servers = xray_dns_server_order($dns_final) + | if $dns_final == "" then + . + else + (.inbounds[] | select(.protocol == "tun" and .tag == "tun-in") | .settings.dns) = xray_tun_dns_addresses($dns_final; $xray_dns_local_client; $xray_dns_remote_client) + end + else . end | if $route_enabled then - .routing.rules = ($route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)]) + .routing.rules = (xray_preserved_rules + $route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)]) else . end | if $xray_single_proxy_tag == "" then . @@ -413,6 +440,7 @@ let | if $clear_dns_rules then .dns.rules = [] else . end else . end ''; + backendJqFilterFile = pkgs.writeText "proxy-suite-${backend}-backend-filter.jq" mkBackendJqFilter; mkStartScript = { @@ -422,11 +450,13 @@ let routingMark ? null, enableLocalProxyAuth ? false, xrayTunEgressBinding ? false, + xrayTunDnsRuntime ? false, }: pkgs.writeShellScript name '' set -euo pipefail RUNTIME_DIR="${runtimeDir}" ROUTE_MODE_STATE_FILE="${routeModeStateFile}" + BACKEND_JQ_FILTER=${lib.escapeShellArg backendJqFilterFile} XRAY_LOGLEVEL="" XRAY_TUN_BIND_INTERFACE="" XRAY_SINGLE_PROXY_TAG="" @@ -483,7 +513,10 @@ let --arg xray_loglevel "$XRAY_LOGLEVEL" \ --arg xray_bind_interface "$XRAY_TUN_BIND_INTERFACE" \ --arg xray_single_proxy_tag "$XRAY_SINGLE_PROXY_TAG" \ - '${mkBackendJqFilter}' \ + --argjson xray_tun_dns_runtime ${if xrayTunDnsRuntime then "true" else "false"} \ + --arg xray_dns_local_client ${lib.escapeShellArg proxyCfg.dns.local.address} \ + --arg xray_dns_remote_client ${lib.escapeShellArg proxyCfg.dns.remote.address} \ + -f "$BACKEND_JQ_FILTER" \ "${configFile}" > "$RUNTIME_DIR/config.json" ${lib.optionalString enableLocalProxyAuth '' chmod 600 "$RUNTIME_DIR/config.json" @@ -506,14 +539,16 @@ let configFile = tunFile; routingMark = if xrayEnabled then globalTproxy.proxyMark else null; xrayTunEgressBinding = xrayEnabled; + xrayTunDnsRuntime = xrayEnabled; }; startPerAppTun = mkStartScript { name = "proxy-suite-start-per-app-tun"; runtimeDir = "/run/proxy-suite-per-app-tun"; configFile = perAppTunFile; - routingMark = if globalTproxy.enable then globalTproxy.proxyMark else null; + routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null; xrayTunEgressBinding = xrayEnabled; + xrayTunDnsRuntime = xrayEnabled; }; mkSubscriptionFetchBlock = diff --git a/nix/checks.nix b/nix/checks.nix index 293fc2c..cbd85b3 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -276,6 +276,64 @@ let xrayPerAppTunCleanupScript = builtins.readFile ( xrayFixture.config.systemd.services."proxy-suite-per-app-tun".serviceConfig.ExecStopPost ); + xrayStartBackendJqFilterFile = shellValueByPrefix xrayStartScript "BACKEND_JQ_FILTER="; + xrayTunBackendJqFilterFile = shellValueByPrefix xrayTunStartScript "BACKEND_JQ_FILTER="; + xrayPerAppTunBackendJqFilterFile = shellValueByPrefix xrayPerAppTunStartScript "BACKEND_JQ_FILTER="; + xrayBackendJqFilter = builtins.readFile xrayTunBackendJqFilterFile; + xrayDnsLocalClient = xrayFixture.config.services.proxy-suite.proxy.dns.local.address; + xrayDnsRemoteClient = xrayFixture.config.services.proxy-suite.proxy.dns.remote.address; + xrayTunConfigJson = pkgs.writeText "proxy-suite-xray-tun-check.json" (builtins.toJSON xrayTunConfig); + xrayPerAppTunConfigJson = + pkgs.writeText "proxy-suite-xray-per-app-tun-check.json" (builtins.toJSON xrayPerAppTunConfig); + xrayJqFilterRuntimeCheck = pkgs.runCommand "proxy-suite-xray-jq-filter-runtime-check" { nativeBuildInputs = [ pkgs.jq ]; } '' + OBS='[{"protocol":"freedom","tag":"proxy-suite-ob-primary"}]' + + check_runtime() { + local name="$1" + local input="$2" + local output="$TMPDIR/$name.json" + + jq \ + --argjson obs "$OBS" \ + --argjson auth_enabled false \ + --arg user "" \ + --arg password "" \ + --argjson route_enabled true \ + --argjson route_rules '[]' \ + --arg route_final "proxy" \ + --arg dns_final "remote" \ + --argjson clear_dns_rules false \ + --arg xray_loglevel "" \ + --arg xray_bind_interface "eth0" \ + --arg xray_single_proxy_tag "proxy-suite-ob-primary" \ + --argjson xray_tun_dns_runtime true \ + --arg xray_dns_local_client ${pkgs.lib.escapeShellArg xrayDnsLocalClient} \ + --arg xray_dns_remote_client ${pkgs.lib.escapeShellArg xrayDnsRemoteClient} \ + -f ${pkgs.lib.escapeShellArg xrayTunBackendJqFilterFile} \ + "$input" > "$output" + + jq -e \ + --arg remote ${pkgs.lib.escapeShellArg xrayDnsRemoteClient} \ + --arg local ${pkgs.lib.escapeShellArg xrayDnsLocalClient} \ + ' + type == "object" + and (.dns.servers | length) >= 3 + and .dns.servers[0].tag == "fakedns" + and ([.routing.rules[] | select((.ruleTag? // "") == "dns-hijack")] | length) == 1 + and ([.routing.rules[] | select((.ruleTag? // "") == "dns-upstream-direct")] | length) == 1 + and ([.outbounds[] | select(.tag == "proxy-suite-ob-primary" and (.streamSettings.sockopt.interface? // "") == "eth0")] | length) == 1 + and ([.outbounds[] | select(.tag == "dns-out" and ((.streamSettings.sockopt.interface? // "") == ""))] | length) == 1 + and ([.inbounds[] | select(.tag == "tun-in") | .settings.dns] | length) == 1 + and ([.inbounds[] | select(.tag == "tun-in") | .settings.dns][0] == [$remote, $local]) + and ((.routing | has("balancers")) | not) + and ((has("observatory")) | not) + ' "$output" >/dev/null + } + + check_runtime global ${pkgs.lib.escapeShellArg xrayTunConfigJson} + check_runtime per-app ${pkgs.lib.escapeShellArg xrayPerAppTunConfigJson} + touch "$out" + ''; xraySubscriptionFixture = evalProxySuite [ { system.stateVersion = "26.05"; @@ -399,6 +457,8 @@ let routeModeStartScript = builtins.readFile ( routeModeFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart ); + routeModeBackendJqFilterFile = shellValueByPrefix routeModeStartScript "BACKEND_JQ_FILTER="; + routeModeBackendJqFilter = builtins.readFile routeModeBackendJqFilterFile; routeModeRules = mkRouteModeRules routeModeFixture; tgSecretFile = evalProxySuite [ @@ -1090,6 +1150,8 @@ let localProxyAuthStartScript = builtins.readFile ( localProxyAuthFixture.config.systemd.services."proxy-suite-socks".serviceConfig.ExecStart ); + localProxyAuthBackendJqFilterFile = shellValueByPrefix localProxyAuthStartScript "BACKEND_JQ_FILTER="; + localProxyAuthBackendJqFilter = builtins.readFile localProxyAuthBackendJqFilterFile; localProxyAuthPasswordFileFixture = evalProxySuite [ baseModule @@ -1600,9 +1662,11 @@ let # -- proxy.auth: authenticated mixed inbound is injected at runtime -- ( assert pkgs.lib.hasInfix ''LOCAL_PROXY_PASSWORD="$(cat "'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' localProxyAuthStartScript; assert pkgs.lib.hasInfix ''--arg user local-user'' localProxyAuthStartScript; assert pkgs.lib.hasInfix ''--arg password "$LOCAL_PROXY_PASSWORD"'' localProxyAuthStartScript; - assert pkgs.lib.hasInfix ''select(.type == "mixed" and .tag == "mixed-in") | .users'' localProxyAuthStartScript; + assert pkgs.lib.hasInfix ''select(.type == "mixed" and .tag == "mixed-in") | .users'' localProxyAuthBackendJqFilter; assert pkgs.lib.hasInfix ''chmod 600 "$RUNTIME_DIR/config.json"'' localProxyAuthStartScript; true ) @@ -1685,6 +1749,9 @@ let balancer = builtins.head xrayTproxyConfig.routing.balancers; in assert xrayFixture.config.services.proxy-suite.proxy.xray.enable; + assert builtins.any ( + warning: pkgs.lib.hasInfix "XRay backend is still in test mode" warning + ) xrayFixture.config.warnings; assert xrayFixture.config.systemd.services ? "proxy-suite-socks"; assert xrayFixture.config.systemd.services ? "proxy-suite-tproxy"; assert xrayFixture.config.systemd.services ? "proxy-suite-tun"; @@ -1717,44 +1784,104 @@ let tunDirectOutbound = builtins.head ( builtins.filter (outbound: outbound.tag == "direct") xrayTunConfig.outbounds ); + perAppTunDirectOutbound = builtins.head ( + builtins.filter (outbound: outbound.tag == "direct") xrayPerAppTunConfig.outbounds + ); + tunDnsOutbound = builtins.head ( + builtins.filter (outbound: outbound.tag == "dns-out") xrayTunConfig.outbounds + ); tunHasFinalRuleTag = builtins.any (rule: (rule ? ruleTag) && rule.ruleTag == "final-default") xrayTunConfig.routing.rules; tunHasDirectGeositeRule = builtins.any (rule: (rule ? ruleTag) && rule.ruleTag == "direct-geosite") xrayTunConfig.routing.rules; + tunHasDnsHijackRule = builtins.any (rule: (rule ? ruleTag) && rule.ruleTag == "dns-hijack") xrayTunConfig.routing.rules; + tunHasDnsUpstreamRule = builtins.any (rule: (rule ? ruleTag) && rule.ruleTag == "dns-upstream-direct") xrayTunConfig.routing.rules; in assert tunInbound.protocol == "tun"; assert tunInbound.settings.name == "singtun0"; - assert tunInbound.settings.gateway == [ "172.19.0.1/30" ]; + assert tunInbound.settings.gateway == [ "172.19.0.1/30" "fd66:19::1/64" ]; + assert builtins.length tunInbound.settings.dns == 2; assert tunInbound.settings.autoOutboundsInterface == "auto"; - assert !(tunInbound.sniffing ? routeOnly); - assert xrayTunConfig.routing.domainStrategy == "AsIs"; + assert tunInbound.sniffing.destOverride == [ "fakedns" ]; + assert tunInbound.sniffing.metadataOnly == true; + assert xrayTunConfig.routing.domainStrategy == "IPIfNonMatch"; assert tunHasFinalRuleTag; assert tunHasDirectGeositeRule; + assert tunHasDnsHijackRule; + assert tunHasDnsUpstreamRule; + assert xrayTunConfig.dns.queryStrategy == "UseIP"; + assert xrayTunConfig.dns.tag == "dns-in"; + assert (builtins.head xrayTunConfig.dns.servers).address == "fakedns"; + assert (builtins.head xrayTunConfig.dns.servers).tag == "fakedns"; + assert (builtins.elemAt xrayTunConfig.dns.servers 1).tag == "remote"; + assert (builtins.elemAt xrayTunConfig.dns.servers 2).tag == "local"; + assert builtins.length xrayTunConfig.fakedns == 2; + assert tunDnsOutbound.protocol == "dns"; + assert tunDnsOutbound.settings.rules == [ { action = "direct"; qType = "2-27,29-65535"; } ]; assert tunDirectOutbound.streamSettings.sockopt.mark == 2; + assert xrayStartBackendJqFilterFile == xrayTunBackendJqFilterFile; + assert xrayTunBackendJqFilterFile == xrayPerAppTunBackendJqFilterFile; + assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" xrayStartScript; + assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' xrayStartScript; 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 ".streamSettings.sockopt.interface = $interface" xrayTunStartScript; - assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayTunStartScript; - assert pkgs.lib.hasInfix "del(.observatory)" xrayTunStartScript; - assert pkgs.lib.hasInfix "xray_rewrite_proxy_rule" 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; + assert pkgs.lib.hasInfix "xray_preserved_rules" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "dns-upstream-direct" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "xray_tun_dns_addresses" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "dns-hijack" xrayBackendJqFilter; + assert pkgs.lib.hasInfix ".streamSettings.sockopt.interface = $interface" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "del(.observatory)" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "xray_rewrite_proxy_rule" xrayBackendJqFilter; + assert pkgs.lib.hasInfix ".protocol == \"dns\"" xrayBackendJqFilter; assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayTunUpScript; + assert pkgs.lib.hasInfix "fd66:19::1/64" xrayTunUpScript; + assert pkgs.lib.hasInfix ''uplink_addr="$('' xrayTunUpScript; assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev singtun0'' xrayTunUpScript; + assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev singtun0'' xrayTunUpScript; assert pkgs.lib.hasInfix ''route replace "$tun_route_prefix" dev singtun0 src "$tun_addr" table 2022'' xrayTunUpScript; + assert pkgs.lib.hasInfix ''route replace default dev singtun0 src "$uplink_addr" table 2022'' xrayTunUpScript; + assert pkgs.lib.hasInfix ''-6 route replace "$tun6_route_prefix" dev singtun0 table 2022'' xrayTunUpScript; + assert pkgs.lib.hasInfix ''-6 route replace default dev singtun0 table 2022'' xrayTunUpScript; assert pkgs.lib.hasInfix "rule add pref 8996 fwmark 17 table 102" xrayTunUpScript; assert pkgs.lib.hasInfix "rule add pref 8997 fwmark 16 table 101" xrayTunUpScript; assert pkgs.lib.hasInfix "rule add pref 9000 not fwmark 2 table 2022" xrayTunUpScript; + assert pkgs.lib.hasInfix "-6 rule add pref 8997 fwmark 16 table 101" xrayTunUpScript; + assert pkgs.lib.hasInfix "-6 rule add pref 9000 not fwmark 2 table 2022" xrayTunUpScript; assert perAppTunInbound.settings.name == "psperapptun0"; - assert perAppTunInbound.settings.gateway == [ "172.20.0.1/30" ]; - assert !(perAppTunInbound.sniffing ? routeOnly); - assert xrayPerAppTunConfig.routing.domainStrategy == "AsIs"; + assert perAppTunInbound.settings.gateway == [ "172.20.0.1/30" "fd66:20::1/64" ]; + assert builtins.length perAppTunInbound.settings.dns == 2; + assert perAppTunInbound.sniffing.destOverride == [ "fakedns" ]; + assert perAppTunInbound.sniffing.metadataOnly == true; + assert xrayPerAppTunConfig.routing.domainStrategy == "IPIfNonMatch"; + assert (builtins.head xrayPerAppTunConfig.dns.servers).address == "fakedns"; + assert xrayPerAppTunConfig.dns.tag == "dns-in"; + assert builtins.length xrayPerAppTunConfig.fakedns == 2; + 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 ".streamSettings.sockopt.interface = $interface" xrayPerAppTunStartScript; - assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayPerAppTunStartScript; - assert pkgs.lib.hasInfix "del(.observatory)" 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; + assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayPerAppTunStartScript; + assert pkgs.lib.hasInfix ".streamSettings.sockopt.interface = $interface" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "del(.routing.balancers)" xrayBackendJqFilter; + assert pkgs.lib.hasInfix "del(.observatory)" xrayBackendJqFilter; assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix "fd66:20::1/64" xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''uplink_addr="$('' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''addr replace "$tun_cidr" dev psperapptun0'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''-6 addr replace "$tun6_cidr" dev psperapptun0'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''route replace "$tun_route_prefix" dev psperapptun0 src "$tun_addr" table 101'' xrayPerAppTunUpScript; - assert pkgs.lib.hasInfix ''route flush table 101'' xrayPerAppTunCleanupScript; + assert pkgs.lib.hasInfix ''route replace default dev psperapptun0 src "$uplink_addr" table 101'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''-6 route replace "$tun6_route_prefix" dev psperapptun0 table 101'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''-6 route replace default dev psperapptun0 table 101'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''-6 rule add fwmark 16 table 101'' xrayPerAppTunUpScript; + assert pkgs.lib.hasInfix ''-4 route flush table 101'' xrayPerAppTunCleanupScript; + assert pkgs.lib.hasInfix ''-6 route flush table 101'' xrayPerAppTunCleanupScript; + assert pkgs.lib.hasInfix "resolvectl flush-caches" xrayPerAppTunCleanupScript; true ) ( @@ -2235,10 +2362,12 @@ let # -- runtime route mode: socks start script includes all override branches and uses volatile state -- ( assert pkgs.lib.hasInfix ''ROUTE_MODE_STATE_FILE="/run/proxy-suite/route-mode"'' routeModeStartScript; + assert pkgs.lib.hasInfix "BACKEND_JQ_FILTER=" routeModeStartScript; + assert pkgs.lib.hasInfix ''-f "$BACKEND_JQ_FILTER"'' routeModeStartScript; assert pkgs.lib.hasInfix ''all-proxy)'' routeModeStartScript; assert pkgs.lib.hasInfix ''all-bypass)'' routeModeStartScript; - assert pkgs.lib.hasInfix ''.route.rules = $route_rules'' routeModeStartScript; - assert pkgs.lib.hasInfix ''.dns.rules = []'' routeModeStartScript; + assert pkgs.lib.hasInfix ''.route.rules = $route_rules'' routeModeBackendJqFilter; + assert pkgs.lib.hasInfix ''.dns.rules = []'' routeModeBackendJqFilter; true ) @@ -2722,6 +2851,7 @@ in { proxy-suite-module = builtins.seq validated (pkgs.writeText "proxy-suite-module-check" "ok"); proxy-suite-tray-build = suitePkgs.proxy-suite-tray; + xray-jq-filter-runtime = xrayJqFilterRuntimeCheck; zapret-hostlist-rules = pkgs.runCommand "proxy-suite-zapret-hostlist-rules-check" { } '' grep -F -- '--hostlist="${zapretSyncExtraListsBase}/hostlists/list-twitter.txt"' "${zapretSyncExtraListsBase}/config"