diff --git a/modules/proxy-suite/config-templates/xray.nix b/modules/proxy-suite/config-templates/xray.nix index b9a71c5..2ff1c9d 100644 --- a/modules/proxy-suite/config-templates/xray.nix +++ b/modules/proxy-suite/config-templates/xray.nix @@ -71,7 +71,10 @@ let { enabled = true; destOverride = [ "fakedns" ]; - metadataOnly = true; + # Fake IPs only live in memory: after a restart an app still holding one gets its + # domain back from TLS/HTTP/QUIC instead of a dead end. Real IPs are left alone; + # the cost is up to 200 ms before a server-speaks-first protocol (SSH, SMTP) starts. + metadataOnly = false; } else { @@ -142,10 +145,6 @@ let enableTunFakeDns ? false, }: let - tunDnsServers = [ - (if (proxyCfg.routing.default == "proxy") then proxyCfg.dns.remote.address else proxyCfg.dns.local.address) - (if (proxyCfg.routing.default == "proxy") then proxyCfg.dns.local.address else proxyCfg.dns.remote.address) - ]; tunSniffing = mkSniffing { fakeDnsOnly = enableTunFakeDns; }; dnsConfig = if enableTunFakeDns then @@ -215,9 +214,7 @@ let name = tunInterface; mtu = tunMtu; gateway = [ tunAddress ] ++ lib.optionals (tunIPv6Address != null) [ tunIPv6Address ]; - dns = tunDnsServers; userLevel = 0; - autoOutboundsInterface = "auto"; }; sniffing = tunSniffing; }; diff --git a/modules/proxy-suite/derived.nix b/modules/proxy-suite/derived.nix index 10197b0..318b0c8 100644 --- a/modules/proxy-suite/derived.nix +++ b/modules/proxy-suite/derived.nix @@ -280,11 +280,18 @@ let tunAutoRouteTableIndex = 2022; tunAutoRouteRulePriority = 9000; - # Keeps proxy-suite's own daemons (the inbound XRay, replies to its clients included) - # out of the pure-XRay global TUN. - xrayTunServiceUserRulePriority = 8995; - xrayTunPerAppTproxyRulePriority = 8996; - xrayTunPerAppTunRulePriority = 8997; + # Pure-XRay global TUN, below 8998/8999 (AmneziaWG and tg-ws-proxy bypasses). + # proxyMark sockets and proxy-suite's own daemons (the inbound XRay, replies to its + # clients included) stay out of the TUN. + xrayTunMarkBypassRulePriority = 8992; + xrayTunServiceUserRulePriority = 8993; + xrayTunPerAppTproxyRulePriority = 8994; + xrayTunPerAppTunRulePriority = 8995; + # DNS stays in the TUN for fakedns, even to a LAN resolver. + xrayTunDnsRulePriority = 8996; + # Routes more specific than default (LAN, container bridges) skip the TUN, so + # replies to connections from there leave the way they came. + xrayTunMainRulePriority = 8997; xrayGlobalTunIPv6Address = "fd66:19::1/64"; xrayGlobalTunIPv6RoutePrefix = "fd66:19::/64"; diff --git a/modules/proxy-suite/service/per-app-routing.nix b/modules/proxy-suite/service/per-app-routing.nix index 3443141..644e925 100644 --- a/modules/proxy-suite/service/per-app-routing.nix +++ b/modules/proxy-suite/service/per-app-routing.nix @@ -68,7 +68,6 @@ let perAppTproxyRulesFile ip nft - awk seqBin sleepBin ; diff --git a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix index b84edc7..ab9dc0a 100644 --- a/modules/proxy-suite/service/per-app-routing/backend-scripts.nix +++ b/modules/proxy-suite/service/per-app-routing/backend-scripts.nix @@ -11,17 +11,11 @@ perAppTproxyRulesFile, ip, nft, - awk, seqBin, sleepBin, }: let - defaultUplinkIPv4Source = builders.mkDefaultUplinkIPv4Source { - inherit ip awk; - errorMessage = "proxy-suite: could not determine the default uplink IPv4 address for app TUN"; - }; - inherit (constants) xrayPerAppTunIPv6Address xrayPerAppTunIPv6RoutePrefix @@ -47,7 +41,6 @@ let tun6_route_prefix=${lib.escapeShellArg xrayPerAppTunIPv6RoutePrefix} tun_addr="" tun_route_prefix="" - uplink_addr="" ${builders.cidrNetworkFunction} @@ -68,12 +61,12 @@ let ${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = perAppRoutingTun.routeTable; }} ${nft} -f ${perAppTunChainFile} ${perAppTunWaitForInterface} - ${defaultUplinkIPv4Source} tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" ${ip} -4 addr replace "$tun_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} + # No uplink src, as in xrayTunUpScript: it goes stale when the uplink address changes. + ${ip} -4 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 ${lib.optionalString pureXrayEnabled '' ${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg perAppRoutingTun.interface} diff --git a/modules/proxy-suite/service/routing-scripts.nix b/modules/proxy-suite/service/routing-scripts.nix index 471ca7b..6d27f04 100644 --- a/modules/proxy-suite/service/routing-scripts.nix +++ b/modules/proxy-suite/service/routing-scripts.nix @@ -16,17 +16,45 @@ let inherit (constants) tunAutoRouteTableIndex tunAutoRouteRulePriority + xrayTunMarkBypassRulePriority xrayTunServiceUserRulePriority xrayTunPerAppTproxyRulePriority xrayTunPerAppTunRulePriority + xrayTunDnsRulePriority + xrayTunMainRulePriority xrayGlobalTunIPv6Address xrayGlobalTunIPv6RoutePrefix ; - defaultUplinkIPv4Source = builders.mkDefaultUplinkIPv4Source { - inherit ip; - awk = "${pkgs.gawk}/bin/awk"; - errorMessage = "proxy-suite: could not determine the default uplink IPv4 address for XRay TUN"; + deleteXrayTunRules = lib.concatMapStrings ( + family: + lib.concatMapStrings (priority: builders.mkIpRuleDeleteByPriority { inherit ip family priority; }) [ + xrayTunMarkBypassRulePriority + xrayTunServiceUserRulePriority + xrayTunPerAppTproxyRulePriority + xrayTunPerAppTunRulePriority + xrayTunDnsRulePriority + xrayTunMainRulePriority + tunAutoRouteRulePriority + ] + ) [ "-4" "-6" ]; + + # Replies to connections from outside (sshd, a game server, anything listening) take + # proxyMark, so the fwmark rule sends them back out the way they came instead of into + # the TUN, where nothing expects them. Locally started connections stay "original". + xrayTunReplyTable = "proxy_suite_xray_tun"; + xrayTunReplyRules = pkgs.writeText "proxy-suite-xray-tun.nft" '' + table inet ${xrayTunReplyTable} { + chain output { + type route hook output priority mangle; policy accept; + ct direction reply meta mark 0 meta mark set ${toString globalTproxy.proxyMark} + } + } + ''; + deleteXrayTunReplyTable = builders.mkNftDeleteTable { + inherit nft; + family = "inet"; + table = xrayTunReplyTable; }; in { @@ -38,7 +66,6 @@ in tun6_route_prefix=${lib.escapeShellArg xrayGlobalTunIPv6RoutePrefix} tun_addr="" tun_route_prefix="" - uplink_addr="" ${builders.cidrNetworkFunction} @@ -54,15 +81,9 @@ in exit 1 fi - ${defaultUplinkIPv4Source} - - 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 xrayTunServiceUserRulePriority} 2>/dev/null; do :; done - while ${ip} -6 rule del pref ${toString xrayTunServiceUserRulePriority} 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 + ${deleteXrayTunRules} + ${deleteXrayTunReplyTable} + ${nft} -f ${xrayTunReplyRules} tun_addr="''${tun_cidr%%/*}" tun_route_prefix="$(cidr_network "$tun_cidr")" @@ -70,7 +91,9 @@ in ${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} src "$uplink_addr" table ${toString tunAutoRouteTableIndex} + # No uplink src: it goes stale when the uplink address changes, and XRay's own sockets + # (routed by uid and mark, not bound to an interface) pick theirs from main. + ${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} 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 '' @@ -83,6 +106,12 @@ in service_uid=$(${pkgs.coreutils}/bin/id -u ${constants.serviceUser}) ${ip} -4 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main ${ip} -6 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main + for family in -4 -6; do + ${ip} "$family" rule add pref ${toString xrayTunMarkBypassRulePriority} fwmark ${toString globalTproxy.proxyMark} lookup main + ${ip} "$family" rule add pref ${toString xrayTunDnsRulePriority} ipproto udp dport 53 table ${toString tunAutoRouteTableIndex} + ${ip} "$family" rule add pref ${toString xrayTunDnsRulePriority} ipproto tcp dport 53 table ${toString tunAutoRouteTableIndex} + ${ip} "$family" rule add pref ${toString xrayTunMainRulePriority} lookup main suppress_prefixlength 0 + done ${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} ''; @@ -127,31 +156,8 @@ in ${builders.mkNftDeleteTable { inherit nft; family = "inet"; table = "sing-box"; }} ${builders.mkIpRuleDeleteByTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }} ${builders.mkIpRuleDeleteByTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }} - ${builders.mkIpRuleDeleteByPriority { - inherit ip; - family = "-4"; - priority = xrayTunPerAppTproxyRulePriority; - }} - ${builders.mkIpRuleDeleteByPriority { - inherit ip; - family = "-4"; - priority = xrayTunPerAppTunRulePriority; - }} - ${builders.mkIpRuleDeleteByPriority { - inherit ip; - family = "-6"; - priority = xrayTunPerAppTunRulePriority; - }} - ${builders.mkIpRuleDeleteByPriority { - inherit ip; - family = "-4"; - priority = xrayTunServiceUserRulePriority; - }} - ${builders.mkIpRuleDeleteByPriority { - inherit ip; - family = "-6"; - priority = xrayTunServiceUserRulePriority; - }} + ${deleteXrayTunRules} + ${deleteXrayTunReplyTable} ${builders.mkIpRouteFlushTable { inherit ip; family = "-4"; table = tunAutoRouteTableIndex; }} ${builders.mkIpRouteFlushTable { inherit ip; family = "-6"; table = tunAutoRouteTableIndex; }} ${builders.mkIpLinkDelete { inherit ip; interface = globalTun.interface; }} diff --git a/modules/proxy-suite/service/script-blocks/backend-jq-filter.nix b/modules/proxy-suite/service/script-blocks/backend-jq-filter.nix index 90669ff..c9350a3 100644 --- a/modules/proxy-suite/service/script-blocks/backend-jq-filter.nix +++ b/modules/proxy-suite/service/script-blocks/backend-jq-filter.nix @@ -9,20 +9,6 @@ if pureXrayEnabled then '' - def xray_bind_outbound($interface): - # A loopback hop (the WARP tunnel, the OpenSSH listener) cannot leave through the uplink. - if $interface == "" or .protocol == "blackhole" or .protocol == "dns" - or ((.settings.address? // "") | test("^(127\\.|::1$|localhost$)")) then - . - else - .streamSettings = (.streamSettings // {}) - | .streamSettings.sockopt = (.streamSettings.sockopt // {}) - | if (.streamSettings.sockopt.interface? // "") == "" then - .streamSettings.sockopt.interface = $interface - else - . - end - end; def xray_proxy_rule_target($single_tag): if $single_tag != "" then {outboundTag:$single_tag} @@ -52,15 +38,7 @@ if pureXrayEnabled then + [.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))) + .outbounds = ($obs + .outbounds) | 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" @@ -68,11 +46,12 @@ if pureXrayEnabled then 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 + # Without a domainStrategy a dial resolves through the system resolver, which under + # the TUN (resolved's upstream queries included) hands out fake IPs. XRay's own DNS + # client skips fakedns. + | .outbounds |= map( + if .protocol == "blackhole" or .protocol == "dns" or .protocol == "loopback" then . + else .streamSettings.sockopt.domainStrategy //= "UseIPv4v6" end) else . end | if $route_enabled then .routing.rules = (xray_preserved_rules + $route_rules + [xray_final_rule($route_final; $xray_single_proxy_tag)]) @@ -99,7 +78,7 @@ else [.route.rules[] | select(((.action? // "") == "hijack-dns") and (((.inbound? // []) | index("xray-dns-in")) != null))]; - .outbounds = $obs + .outbounds + .outbounds = ($obs + .outbounds) | if $auth_enabled then (.inbounds[] | select(.type == "mixed" and .tag == "mixed-in") | .users) = [{username:$user,password:$password}] else . end diff --git a/modules/proxy-suite/service/script-blocks/hybrid-runtime-helpers.nix b/modules/proxy-suite/service/script-blocks/hybrid-runtime-helpers.nix index cff4cfa..4500aca 100644 --- a/modules/proxy-suite/service/script-blocks/hybrid-runtime-helpers.nix +++ b/modules/proxy-suite/service/script-blocks/hybrid-runtime-helpers.nix @@ -67,24 +67,11 @@ lib.optionalString hybridEnabled '' fi ${jq} -n \ --arg loglevel "$XRAY_LOGLEVEL" \ - --arg bind_interface "$XRAY_TUN_BIND_INTERFACE" \ --argjson dns_port "$XRAY_SIDECAR_DNS_PORT" \ --argjson inbounds "$XRAY_INBOUNDS_JSON" \ --argjson outbounds "$XRAY_OUTBOUNDS_JSON" \ --argjson route_rules "$XRAY_ROUTE_RULES_JSON" \ ' - def bind_xray_sidecar($interface): - if $interface == "" then - . - else - .streamSettings = (.streamSettings // {}) - | .streamSettings.sockopt = (.streamSettings.sockopt // {}) - | if (.streamSettings.sockopt.interface? // "") == "" then - .streamSettings.sockopt.interface = $interface - else - . - end - end; { log: { loglevel: (if $loglevel == "" then "warning" else $loglevel end), @@ -101,7 +88,7 @@ lib.optionalString hybridEnabled '' ] }, inbounds: $inbounds, - outbounds: (($outbounds | map(bind_xray_sidecar($bind_interface))) + [{protocol:"freedom",tag:"direct"}]), + outbounds: ($outbounds + [{protocol:"freedom",tag:"direct"}]), routing: { domainStrategy: "AsIs", rules: ($route_rules + [{type:"field",ip:["127.0.0.1"],port:$dns_port,outboundTag:"direct"}]) diff --git a/modules/proxy-suite/service/start-scripts.nix b/modules/proxy-suite/service/start-scripts.nix index 02fa7fa..fcacc9a 100644 --- a/modules/proxy-suite/service/start-scripts.nix +++ b/modules/proxy-suite/service/start-scripts.nix @@ -146,7 +146,6 @@ let configFile, routingMark ? null, enableLocalProxyAuth ? false, - xrayTunEgressBinding ? false, xrayTunDnsRuntime ? false, xraySidecarBasePort ? xraySidecarBasePorts.socks, xrayDnsBridgePort ? xrayDnsBridgePorts.socks, @@ -161,7 +160,6 @@ let ROUTE_MODE_STATE_FILE="${routeModeStateFile}" BACKEND_JQ_FILTER=${lib.escapeShellArg backendJqFilterFile} XRAY_LOGLEVEL="" - XRAY_TUN_BIND_INTERFACE="" XRAY_SINGLE_PROXY_TAG="" mkdir -p "$RUNTIME_DIR" OUTBOUNDS_JSON='[]' @@ -187,22 +185,6 @@ let XRAY_LOGLEVEL="$(tr -d '\r\n[:space:]' < "${xrayLoglevelFile}" 2>/dev/null || true)" fi ''} - ${lib.optionalString xrayTunEgressBinding '' - 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) { - print $(i + 1) - exit - } - } - } - ')" - if [ -z "$XRAY_TUN_BIND_INTERFACE" ]; then - echo "proxy-suite: could not determine the default uplink interface for XRay TUN" >&2 - exit 1 - fi - ''} ${routeModeCaseBlock} @@ -350,12 +332,9 @@ let --arg dns_final "$DNS_FINAL" \ --argjson clear_dns_rules "$CLEAR_DNS_RULES" \ --arg xray_loglevel "$XRAY_LOGLEVEL" \ - --arg xray_bind_interface "$XRAY_TUN_BIND_INTERFACE" \ --arg xray_single_proxy_tag "$XRAY_SINGLE_PROXY_TAG" \ --argjson xray_selectable "$SELECTABLE_TAGS_JSON" \ --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 excludeServiceUserFromTun '' @@ -446,7 +425,6 @@ let runtimeDir = "/run/proxy-suite-tun"; configFile = tunFile; routingMark = if pureXrayEnabled then globalTproxy.proxyMark else null; - xrayTunEgressBinding = xrayEnabled; xrayTunDnsRuntime = pureXrayEnabled; xraySidecarBasePort = xraySidecarBasePorts.tun; xrayDnsBridgePort = xrayDnsBridgePorts.tun; @@ -458,7 +436,6 @@ let runtimeDir = "/run/proxy-suite-per-app-tun"; configFile = perAppTunFile; routingMark = if xrayEnabled || globalTproxy.enable then globalTproxy.proxyMark else null; - xrayTunEgressBinding = xrayEnabled; xrayTunDnsRuntime = pureXrayEnabled; xraySidecarBasePort = xraySidecarBasePorts.perAppTun; xrayDnsBridgePort = xrayDnsBridgePorts.perAppTun; diff --git a/nix/checks/xray-backend-runtime.nix b/nix/checks/xray-backend-runtime.nix index 7ab8334..c8b3b58 100644 --- a/nix/checks/xray-backend-runtime.nix +++ b/nix/checks/xray-backend-runtime.nix @@ -23,8 +23,6 @@ let inherit (fixtures) xrayBackendJqFilter xrayBackendJqFilterFile - xrayDnsLocalClient - xrayDnsRemoteClient xrayFixture xrayPerAppTunBackendJqFilterFile xrayPerAppTunCleanupScript @@ -62,27 +60,22 @@ let --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 xrayBackendJqFilterFile} \ "$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 ([.outbounds[] | select(.streamSettings.sockopt.interface?)] | length) == 0 + and ([.outbounds[] | select(.protocol == "freedom" or .tag == "proxy-suite-ob-primary") | .streamSettings.sockopt.domainStrategy] | all(. == "UseIPv4v6")) + and ([.outbounds[] | select(.protocol == "dns") | .streamSettings.sockopt.domainStrategy?] | all(. == null)) + and ([.inbounds[] | select(.tag == "tun-in") | .settings | has("dns") or has("autoOutboundsInterface")] | any | not) and ((.routing | has("balancers")) | not) and ((has("observatory")) | not) ' "$output" >/dev/null diff --git a/nix/checks/xray-backend-runtime/fixtures.nix b/nix/checks/xray-backend-runtime/fixtures.nix index 0498bae..900065b 100644 --- a/nix/checks/xray-backend-runtime/fixtures.nix +++ b/nix/checks/xray-backend-runtime/fixtures.nix @@ -85,8 +85,6 @@ let selectionMode = xrayFixture.config.services.proxy-suite.proxy.selection; }; xrayBackendJqFilterFile = pkgs.writeText "proxy-suite-xray-backend-filter-check.jq" xrayBackendJqFilter; - 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 ); @@ -98,8 +96,6 @@ in inherit xrayBackendJqFilter xrayBackendJqFilterFile - xrayDnsLocalClient - xrayDnsRemoteClient xrayFixture xrayPerAppTunBackendJqFilterFile xrayPerAppTunCleanupScript diff --git a/nix/checks/xray-backend-runtime/tun/backend-filter.nix b/nix/checks/xray-backend-runtime/tun/backend-filter.nix index 16c4753..249b2e1 100644 --- a/nix/checks/xray-backend-runtime/tun/backend-filter.nix +++ b/nix/checks/xray-backend-runtime/tun/backend-filter.nix @@ -22,13 +22,11 @@ !(pkgs.lib.hasInfix ''select(.protocol == "socks" and .tag == "mixed-in") | .settings.users'' xrayBackendJqFilter); 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 "xray_dns_server_order" 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; true ) ]; diff --git a/nix/checks/xray-backend-runtime/tun/global.nix b/nix/checks/xray-backend-runtime/tun/global.nix index ff45dcb..82ffaca 100644 --- a/nix/checks/xray-backend-runtime/tun/global.nix +++ b/nix/checks/xray-backend-runtime/tun/global.nix @@ -40,10 +40,8 @@ "172.19.0.1/30" checkConstants.xrayGlobalTunIPv6Address ]; - assert builtins.length tunInbound.settings.dns == 2; - assert tunInbound.settings.autoOutboundsInterface == "auto"; assert tunInbound.sniffing.destOverride == [ "fakedns" ]; - assert tunInbound.sniffing.metadataOnly == true; + assert tunInbound.sniffing.metadataOnly == false; assert xrayTunConfig.routing.domainStrategy == "IPIfNonMatch"; assert tunHasFinalRuleTag; assert tunHasDirectGeositeRule; @@ -67,21 +65,18 @@ assert tunDirectOutbound.streamSettings.sockopt.mark == 2; 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; assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayTunUpScript; assert pkgs.lib.hasInfix checkConstants.xrayGlobalTunIPv6Address 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 ${toString checkConstants.tunAutoRouteTableIndex}'' xrayTunUpScript; assert pkgs.lib.hasInfix - ''route replace default dev singtun0 src "$uplink_addr" table ${toString checkConstants.tunAutoRouteTableIndex}'' + "-4 route replace default dev singtun0 table ${toString checkConstants.tunAutoRouteTableIndex}" xrayTunUpScript; assert pkgs.lib.hasInfix ''-6 route replace "$tun6_route_prefix" dev singtun0 table ${toString checkConstants.tunAutoRouteTableIndex}'' @@ -104,6 +99,33 @@ assert pkgs.lib.hasInfix "-6 rule add pref ${toString checkConstants.tunAutoRouteRulePriority} not fwmark 2 table ${toString checkConstants.tunAutoRouteTableIndex}" xrayTunUpScript; + assert pkgs.lib.hasInfix + ''"$family" rule add pref ${toString checkConstants.xrayTunMarkBypassRulePriority} fwmark 2 lookup main'' + xrayTunUpScript; + assert pkgs.lib.hasInfix + ''"$family" rule add pref ${toString checkConstants.xrayTunDnsRulePriority} ipproto udp dport 53 table ${toString checkConstants.tunAutoRouteTableIndex}'' + xrayTunUpScript; + assert pkgs.lib.hasInfix + ''"$family" rule add pref ${toString checkConstants.xrayTunMainRulePriority} lookup main suppress_prefixlength 0'' + xrayTunUpScript; + assert pkgs.lib.hasInfix "proxy-suite-xray-tun.nft" xrayTunUpScript; + assert pkgs.lib.hasInfix "delete table inet proxy_suite_xray_tun" xrayTunUpScript; + # No uplink src: it would go stale when the uplink address changes. + assert !(pkgs.lib.hasInfix "uplink_addr" xrayTunUpScript); + # Every rule the up script adds is flushed first, v4 and v6. + assert builtins.all ( + priority: + pkgs.lib.hasInfix "-4 rule del pref ${toString priority}" xrayTunUpScript + && pkgs.lib.hasInfix "-6 rule del pref ${toString priority}" xrayTunUpScript + ) [ + checkConstants.xrayTunMarkBypassRulePriority + checkConstants.xrayTunServiceUserRulePriority + checkConstants.xrayTunPerAppTproxyRulePriority + checkConstants.xrayTunPerAppTunRulePriority + checkConstants.xrayTunDnsRulePriority + checkConstants.xrayTunMainRulePriority + checkConstants.tunAutoRouteRulePriority + ]; true ) ]; diff --git a/nix/checks/xray-backend-runtime/tun/per-app.nix b/nix/checks/xray-backend-runtime/tun/per-app.nix index 8ec0dde..dfe5b90 100644 --- a/nix/checks/xray-backend-runtime/tun/per-app.nix +++ b/nix/checks/xray-backend-runtime/tun/per-app.nix @@ -25,30 +25,27 @@ "172.20.0.1/30" checkConstants.xrayPerAppTunIPv6Address ]; - assert builtins.length perAppTunInbound.settings.dns == 2; assert perAppTunInbound.sniffing.destOverride == [ "fakedns" ]; - assert perAppTunInbound.sniffing.metadataOnly == true; + assert perAppTunInbound.sniffing.metadataOnly == false; 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 "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; assert pkgs.lib.hasInfix "xray_tun_dns_runtime" xrayPerAppTunStartScript; assert pkgs.lib.hasInfix ''tun_route_prefix="$(cidr_network "$tun_cidr")"'' xrayPerAppTunUpScript; assert pkgs.lib.hasInfix checkConstants.xrayPerAppTunIPv6Address xrayPerAppTunUpScript; - assert pkgs.lib.hasInfix ''uplink_addr="$('' 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 replace default dev psperapptun0 src "$uplink_addr" table 101'' + assert pkgs.lib.hasInfix "-4 route replace default dev psperapptun0 table 101" xrayPerAppTunUpScript; assert pkgs.lib.hasInfix ''-6 route replace "$tun6_route_prefix" dev psperapptun0 table 101'' xrayPerAppTunUpScript;