Let setup dns can be editable

This commit is contained in:
Puqns67 2024-11-26 14:04:33 +08:00
parent 0a1889c438
commit 6066354b8c
5 changed files with 51 additions and 32 deletions

View File

@ -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"`

View File

@ -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...)

View File

@ -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 == "" {

View File

@ -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,

View File

@ -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)