diff --git a/option/template.go b/option/template.go index b35ef13..5fb1454 100644 --- a/option/template.go +++ b/option/template.go @@ -28,18 +28,19 @@ type _Template struct { RemoteResolve bool `json:"remote_resolve,omitempty"` // DNS - CustomDNSServers []option.DNSServerOptions `json:"custom_dns_servers,omitempty"` - DNS string `json:"dns,omitempty"` - DNSLocal string `json:"dns_local,omitempty"` - EnableFakeIP bool `json:"enable_fakeip,omitempty"` - DisableDNSLeak bool `json:"disable_dns_leak,omitempty"` - PreDNSRules []option.DNSRule `json:"pre_dns_rules,omitempty"` - CustomDNSRules []option.DNSRule `json:"custom_dns_rules,omitempty"` - CustomFakeIP *option.DNSFakeIPOptions `json:"custom_fakeip,omitempty"` - CustomDNSTag string `json:"custom_dns_tag,omitempty"` - CustomDNSLocalTag string `json:"custom_dns_local_tag,omitempty"` - CustomDNSLocalSetupTag string `json:"custom_dns_local_setup_tag,omitempty"` - CustomDNSFakeIPTag string `json:"custom_dns_fakeip_tag,omitempty"` + CustomDNSServers []option.DNSServerOptions `json:"custom_dns_servers,omitempty"` + DNS string `json:"dns,omitempty"` + DNSLocal string `json:"dns_local,omitempty"` + DNSSetup string `json:"dns_setup,omitempty"` + EnableFakeIP bool `json:"enable_fakeip,omitempty"` + DisableDNSLeak bool `json:"disable_dns_leak,omitempty"` + PreDNSRules []option.DNSRule `json:"pre_dns_rules,omitempty"` + CustomDNSRules []option.DNSRule `json:"custom_dns_rules,omitempty"` + CustomFakeIP *option.DNSFakeIPOptions `json:"custom_fakeip,omitempty"` + CustomDNSTag string `json:"custom_dns_tag,omitempty"` + CustomDNSLocalTag string `json:"custom_dns_local_tag,omitempty"` + CustomDNSSetupTag string `json:"custom_dns_setup_tag,omitempty"` + CustomDNSFakeIPTag string `json:"custom_dns_fakeip_tag,omitempty"` // Inbound Inbounds []option.Inbound `json:"inbounds,omitempty"` diff --git a/template/render_dns.go b/template/render_dns.go index 05adbbb..6a82497 100644 --- a/template/render_dns.go +++ b/template/render_dns.go @@ -1,6 +1,7 @@ package template import ( + "errors" "net/netip" "net/url" @@ -22,6 +23,13 @@ func getOrDefault(v string, d string) string { return d } +func ifElse[T any](b bool, v1 T, v2 T) T { + if b { + return v1 + } + return v2 +} + func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error { var ( domainStrategy option.DomainStrategy @@ -45,11 +53,13 @@ func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error dnsTag := getOrDefault(t.CustomDNSTag, DefaultDNSTag) dnsLocalTag := getOrDefault(t.CustomDNSLocalTag, DefaultDNSLocalTag) - dnsLocalSetupTag := getOrDefault(t.CustomDNSLocalSetupTag, DefaultDNSLocalSetupTag) + dnsSetupTag := getOrDefault(t.CustomDNSSetupTag, DefaultDNSSetupTag) dnsFakeIPTag := getOrDefault(t.CustomDNSFakeIPTag, DefaultDNSFakeIPTag) dns := getOrDefault(t.DNS, DefaultDNS) dnsLocal := getOrDefault(t.DNSLocal, DefaultDNSLocal) + dnsSetup := getOrDefault(t.DNSSetup, DefaultDNSSetup) directTag := getOrDefault(t.DirectTag, DefaultDirectTag) + dnsSetupDetour := ifElse(dnsSetup != "local", "", directTag) options.DNS = &option.DNSOptions{ Servers: []option.DNSServerOptions{}, @@ -68,15 +78,21 @@ func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error defaultDNSOptions.AddressResolver = dnsLocalTag } if t.RemoteResolve { - defaultDNSOptions.Detour = getOrDefault(t.DefaultTag, DefaultDefaultTag) + defaultDNSOptions.Detour = getOrDefault(t.DefaultTag, DefaultProxyTag) } options.DNS.Servers = append(options.DNS.Servers, defaultDNSOptions) + dnsSetupUrl, err := url.Parse(dnsSetup) + if err != nil || BM.IsDomainName(dnsSetupUrl.Hostname()) { + return errors.New("dns_setup must be ip based dns or local") + } + if t.DisableTrafficBypass { options.DNS.Servers = append(options.DNS.Servers, option.DNSServerOptions{ Tag: dnsLocalTag, - Address: "local", + Address: dnsSetup, Strategy: domainStrategyLocal, + Detour: dnsSetupDetour, }) } else { localDNSOptions := []option.DNSServerOptions{{ @@ -86,11 +102,12 @@ func (t *Template) renderDNS(metadata M.Metadata, options *option.Options) error Strategy: domainStrategyLocal, }} if dnsLocalUrl, err := url.Parse(dnsLocal); err == nil && BM.IsDomainName(dnsLocalUrl.Hostname()) { - localDNSOptions[0].AddressResolver = dnsLocalSetupTag + localDNSOptions[0].AddressResolver = dnsSetupTag localDNSOptions = append(localDNSOptions, option.DNSServerOptions{ - Tag: dnsLocalSetupTag, - Address: "local", + Tag: dnsSetupTag, + Address: dnsSetup, Strategy: domainStrategyLocal, + Detour: dnsSetupDetour, }) } options.DNS.Servers = append(options.DNS.Servers, localDNSOptions...) diff --git a/template/render_outbounds.go b/template/render_outbounds.go index e69fb61..5932377 100644 --- a/template/render_outbounds.go +++ b/template/render_outbounds.go @@ -21,7 +21,7 @@ func (t *Template) renderOutbounds(metadata M.Metadata, options *boxOption.Optio disableRuleAction := t.DisableRuleAction || (metadata.Version != nil && metadata.Version.LessThan(semver.ParseVersion("1.11.0-alpha.7"))) defaultTag := t.DefaultTag if defaultTag == "" { - defaultTag = DefaultDefaultTag + defaultTag = DefaultProxyTag } directTag := t.DirectTag if directTag == "" { diff --git a/template/render_route.go b/template/render_route.go index 6343e5d..31b4dfb 100644 --- a/template/render_route.go +++ b/template/render_route.go @@ -102,7 +102,7 @@ func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) err directTag = DefaultDirectTag } if defaultTag == "" { - defaultTag = DefaultDefaultTag + defaultTag = DefaultProxyTag } options.Route.Rules = append(options.Route.Rules, option.Rule{ Type: C.RuleTypeDefault, diff --git a/template/template.go b/template/template.go index 82891ef..d6dd768 100644 --- a/template/template.go +++ b/template/template.go @@ -13,18 +13,19 @@ import ( ) const ( - DefaultMixedPort = 8080 - DefaultDNSTag = "default" - DefaultDNSLocalTag = "local" - DefaultDNSLocalSetupTag = "local_setup" - DefaultDNSFakeIPTag = "remote" - DefaultDNS = "tls://8.8.8.8" - DefaultDNSLocal = "https://223.5.5.5/dns-query" - DefaultDefaultTag = "default" - DefaultDirectTag = "direct" - DefaultBlockTag = "block" - DNSTag = "dns" - DefaultURLTestTag = "URLTest" + DefaultMixedPort = 8080 + DefaultDNSTag = "default" + DefaultDNSLocalTag = "local" + DefaultDNSSetupTag = "local_setup" + DefaultDNSFakeIPTag = "remote" + DefaultDNS = "tls://8.8.8.8" + DefaultDNSLocal = "https://223.5.5.5/dns-query" + DefaultDNSSetup = "local" + DefaultProxyTag = "proxy" + DefaultDirectTag = "direct" + DefaultBlockTag = "block" + DNSTag = "dns" + DefaultURLTestTag = "URLTest" ) var Default = new(Template)