Improve: support tcp dns server & return an error when parsing nameserver (#127)

This commit is contained in:
gVisor bot 2019-03-01 00:52:30 +08:00
parent de6d34aa27
commit b41d106f78
2 changed files with 24 additions and 17 deletions

View file

@ -108,7 +108,7 @@ external-controller: 127.0.0.1:9090
# - 114.114.114.114 # - 114.114.114.114
# - tls://dns.rubyfish.cn:853 # dns over tls # - tls://dns.rubyfish.cn:853 # dns over tls
# fallback: # concurrent request with nameserver, fallback used when GEOIP country isn't CN # fallback: # concurrent request with nameserver, fallback used when GEOIP country isn't CN
# - 8.8.8.8 # - tcp://1.1.1.1
Proxy: Proxy:

View file

@ -387,33 +387,40 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
for idx, server := range servers { for idx, server := range servers {
// parse without scheme .e.g 8.8.8.8:53 // parse without scheme .e.g 8.8.8.8:53
if host, err := hostWithDefaultPort(server, "53"); err == nil { if !strings.Contains(server, "://") {
nameservers = append( server = "udp://" + server
nameservers,
dns.NameServer{Addr: host},
)
continue
} }
u, err := url.Parse(server) u, err := url.Parse(server)
if err != nil { if err != nil {
return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error()) return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error())
} }
if u.Scheme != "tls" { var host, dnsNetType string
switch u.Scheme {
case "udp":
host, err = hostWithDefaultPort(u.Host, "53")
dnsNetType = "" // UDP
case "tcp":
host, err = hostWithDefaultPort(u.Host, "53")
dnsNetType = "tcp" // TCP
case "tls":
host, err = hostWithDefaultPort(u.Host, "853")
dnsNetType = "tcp-tls" // DNS over TLS
default:
return nil, fmt.Errorf("DNS NameServer[%d] unsupport scheme: %s", idx, u.Scheme) return nil, fmt.Errorf("DNS NameServer[%d] unsupport scheme: %s", idx, u.Scheme)
} }
if err != nil {
return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error())
}
host, err := hostWithDefaultPort(u.Host, "853")
nameservers = append( nameservers = append(
nameservers, nameservers,
dns.NameServer{ dns.NameServer{
Net: "tcp-tls", Net: dnsNetType,
Addr: host, Addr: host,
}, },
) )
} }
return nameservers, nil return nameservers, nil
} }
@ -427,13 +434,13 @@ func parseDNS(cfg rawDNS) (*DNS, error) {
Listen: cfg.Listen, Listen: cfg.Listen,
EnhancedMode: cfg.EnhancedMode, EnhancedMode: cfg.EnhancedMode,
} }
var err error
if nameserver, err := parseNameServer(cfg.NameServer); err == nil { if dnsCfg.NameServer, err = parseNameServer(cfg.NameServer); err != nil {
dnsCfg.NameServer = nameserver return nil, err
} }
if fallback, err := parseNameServer(cfg.Fallback); err == nil { if dnsCfg.Fallback, err = parseNameServer(cfg.Fallback); err != nil {
dnsCfg.Fallback = fallback return nil, err
} }
return dnsCfg, nil return dnsCfg, nil