diff --git a/README.md b/README.md index 3757559..1595075 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,24 @@ services.proxy-suite = { By default, `zapret` and `tgWsProxy` stay disabled until you enable them explicitly. +Example: override the built-in DNS upstreams while keeping proxy-suite's +default routing behavior: + +```nix +services.proxy-suite.singBox.dns = { + local = { + type = "tcp"; + address = "9.9.9.9"; + port = 53; + }; + remote = { + type = "tls"; + address = "1.1.1.1"; + port = 853; + }; +}; +``` + --- ## Options diff --git a/docs/options.md b/docs/options.md index e29423e..b466322 100644 --- a/docs/options.md +++ b/docs/options.md @@ -10,6 +10,18 @@ services.proxy-suite = { enable = false; singBox = { clashApiPort = 9090; + dns = { + local = { + address = "1.1.1.1"; + port = 53; + type = "udp"; + }; + remote = { + address = "1.1.1.1"; + port = 53; + type = "udp"; + }; + }; enable = true; fwmark = 1; listenAddress = "127.0.0.1"; @@ -188,6 +200,270 @@ Only used when selection is “selector” or “urltest”\. Ignored in +## services\.proxy-suite\.singBox\.dns\.local + + + +DNS upstream used for the built-in “local” resolver role\. +This resolver is also used as sing-box route\.default_domain_resolver\. + +The module keeps detour policy automatic: in mixed/TProxy mode, +“local” uses direct; in TUN mode, it is forced through the proxy to +avoid auto_redirect conflicts\. + + + +*Type:* +submodule + + + +*Default:* + +```nix +{ + address = "1.1.1.1"; + port = 53; + type = "udp"; +} +``` + + + +*Example:* + +```nix +{ + address = "9.9.9.9"; + port = 53; + type = "tcp"; +} +``` + +*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\.dns\.local\.address + + + +Resolver address or hostname used for this DNS upstream\. + + + +*Type:* +string matching the pattern \.+ + + + +*Example:* + +```nix +"1.1.1.1" +``` + +*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\.dns\.local\.port + + + +Destination port for this DNS upstream\. + + + +*Type:* +16 bit unsigned integer; between 0 and 65535 (both inclusive) + + + +*Default:* + +```nix +53 +``` + + + +*Example:* + +```nix +853 +``` + +*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\.dns\.local\.type + + + +sing-box DNS transport type for this upstream resolver\. + + + +*Type:* +one of “udp”, “tcp”, “tls” + + + +*Default:* + +```nix +"udp" +``` + + + +*Example:* + +```nix +"tls" +``` + +*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\.dns\.remote + + + +DNS upstream used for the built-in “remote” resolver role\. + +This resolver always detours through the proxy and becomes the +generated dns\.final target when singBox\.proxyByDefault = true\. + + + +*Type:* +submodule + + + +*Default:* + +```nix +{ + address = "1.1.1.1"; + port = 53; + type = "udp"; +} +``` + + + +*Example:* + +```nix +{ + address = "1.1.1.1"; + port = 853; + type = "tls"; +} +``` + +*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\.dns\.remote\.address + + + +Resolver address or hostname used for this DNS upstream\. + + + +*Type:* +string matching the pattern \.+ + + + +*Example:* + +```nix +"1.1.1.1" +``` + +*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\.dns\.remote\.port + + + +Destination port for this DNS upstream\. + + + +*Type:* +16 bit unsigned integer; between 0 and 65535 (both inclusive) + + + +*Default:* + +```nix +53 +``` + + + +*Example:* + +```nix +853 +``` + +*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\.dns\.remote\.type + + + +sing-box DNS transport type for this upstream resolver\. + + + +*Type:* +one of “udp”, “tcp”, “tls” + + + +*Default:* + +```nix +"udp" +``` + + + +*Example:* + +```nix +"tls" +``` + +*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\.fwmark diff --git a/modules/proxy-suite/config.nix b/modules/proxy-suite/config.nix index b3115d7..44d9aa0 100644 --- a/modules/proxy-suite/config.nix +++ b/modules/proxy-suite/config.nix @@ -11,6 +11,14 @@ let sb = cfg.singBox; direct = rules.direct; + mkDnsServer = + tag: upstream: detour: { + inherit tag detour; + type = upstream.type; + server = upstream.address; + server_port = upstream.port; + }; + clashApiBlock = lib.optionalAttrs (sb.selection != "first") { experimental = { clash_api = { @@ -21,20 +29,12 @@ let dnsConfig = { servers = [ - { - tag = "remote"; - type = "udp"; - server = "1.1.1.1"; - detour = "proxy"; - } - { - tag = "local"; - type = "udp"; - server = "1.1.1.1"; + (mkDnsServer "remote" sb.dns.remote "proxy") + ( # In TUN mode, routing DNS through direct can conflict with auto_redirect. # In TProxy/mixed mode, direct is fine for local traffic. - detour = "direct"; - } + mkDnsServer "local" sb.dns.local "direct" + ) ]; rules = lib.optional (builtins.elem "google" sb.routing.proxy.geosites) { diff --git a/modules/proxy-suite/options.nix b/modules/proxy-suite/options.nix index d633869..aee19d5 100644 --- a/modules/proxy-suite/options.nix +++ b/modules/proxy-suite/options.nix @@ -183,6 +183,40 @@ let }; }; + dnsUpstreamType = types.submodule { + options = { + type = mkOption { + type = types.enum [ + "udp" + "tcp" + "tls" + ]; + default = "udp"; + description = '' + sing-box DNS transport type for this upstream resolver. + ''; + example = "tls"; + }; + + address = mkOption { + type = types.strMatching ".+"; + description = '' + Resolver address or hostname used for this DNS upstream. + ''; + example = "1.1.1.1"; + }; + + port = mkOption { + type = types.port; + default = 53; + description = '' + Destination port for this DNS upstream. + ''; + example = 853; + }; + }; + }; + zapretPresetType = types.enum [ "general" "google" @@ -488,6 +522,50 @@ in example = true; }; + dns = { + local = mkOption { + type = dnsUpstreamType; + default = { + type = "udp"; + address = "1.1.1.1"; + port = 53; + }; + description = '' + DNS upstream used for the built-in "local" resolver role. + This resolver is also used as sing-box route.default_domain_resolver. + + The module keeps detour policy automatic: in mixed/TProxy mode, + "local" uses direct; in TUN mode, it is forced through the proxy to + avoid auto_redirect conflicts. + ''; + example = { + type = "tcp"; + address = "9.9.9.9"; + port = 53; + }; + }; + + remote = mkOption { + type = dnsUpstreamType; + default = { + type = "udp"; + address = "1.1.1.1"; + port = 53; + }; + description = '' + DNS upstream used for the built-in "remote" resolver role. + + This resolver always detours through the proxy and becomes the + generated dns.final target when singBox.proxyByDefault = true. + ''; + example = { + type = "tls"; + address = "1.1.1.1"; + port = 853; + }; + }; + }; + tun = { enable = mkEnableOption "TUN mode service (opt-in, mutually exclusive with proxy-suite-tproxy)"; diff --git a/nix/checks.nix b/nix/checks.nix index 5a45a4b..b6ec347 100644 --- a/nix/checks.nix +++ b/nix/checks.nix @@ -37,6 +37,17 @@ let }; in builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile configs.tproxyFile)); + mkTunConfig = + fixture: + let + cfg = fixture.config.services.proxy-suite; + rules = mkRouting fixture; + configs = import ../modules/proxy-suite/config.nix { + lib = pkgs.lib; + inherit pkgs cfg rules; + }; + in + builtins.fromJSON (builtins.unsafeDiscardStringContext (builtins.readFile configs.tunFile)); hasDirectDomain = rules: domain: builtins.any ( @@ -55,6 +66,9 @@ let dnsHasRuleSet = dnsRules: ruleSet: builtins.any (rule: (rule ? rule_set) && builtins.elem ruleSet rule.rule_set) dnsRules; + dnsServerByTag = + dnsConfig: tag: + builtins.head (builtins.filter (server: server.tag == tag) dnsConfig.dns.servers); mkZapretBase = fixture: let @@ -277,6 +291,31 @@ let ]; ruExplicitConfig = mkTProxyConfig ruExplicitFixture; + dnsLocalOverrideFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.dns.local = { + type = "tcp"; + address = "9.9.9.9"; + port = 5353; + }; + } + ]; + dnsLocalOverrideConfig = mkTProxyConfig dnsLocalOverrideFixture; + + dnsRemoteOverrideFixture = evalProxySuite [ + baseModule + { + services.proxy-suite.singBox.dns.remote = { + type = "tls"; + address = "1.0.0.1"; + port = 853; + }; + } + ]; + dnsRemoteOverrideConfig = mkTProxyConfig dnsRemoteOverrideFixture; + tunDefaultConfig = mkTunConfig tunManualFixture; + zapretSyncFixture = evalProxySuite [ baseModule { @@ -782,6 +821,21 @@ let assert (builtins.head routingOrGeoIPRules).outbound == "proxy"; true ) + ( + let + localDns = dnsServerByTag ruDefaultConfig "local"; + remoteDns = dnsServerByTag ruDefaultConfig "remote"; + in + assert localDns.type == "udp"; + assert localDns.server == "1.1.1.1"; + assert localDns.server_port == 53; + assert localDns.detour == "direct"; + assert remoteDns.type == "udp"; + assert remoteDns.server == "1.1.1.1"; + assert remoteDns.server_port == 53; + assert remoteDns.detour == "proxy"; + true + ) ( assert hasRuleSet ruDefaultRules "direct" "geosite-category-ru"; true @@ -810,6 +864,33 @@ let assert dnsHasRuleSet ruExplicitConfig.dns.rules "geosite-category-ru"; true ) + ( + let + localDns = dnsServerByTag dnsLocalOverrideConfig "local"; + in + assert localDns.type == "tcp"; + assert localDns.server == "9.9.9.9"; + assert localDns.server_port == 5353; + assert localDns.detour == "direct"; + true + ) + ( + let + remoteDns = dnsServerByTag dnsRemoteOverrideConfig "remote"; + in + assert remoteDns.type == "tls"; + assert remoteDns.server == "1.0.0.1"; + assert remoteDns.server_port == 853; + assert remoteDns.detour == "proxy"; + true + ) + ( + let + localDns = dnsServerByTag tunDefaultConfig "local"; + in + assert localDns.detour == "proxy"; + true + ) ( assert hasDirectDomain zapretSyncRules "discord.com"; true