add chain support for resolver

This commit is contained in:
ginuerzh 2020-01-17 22:41:35 +08:00
parent f1bad4d07b
commit 4133cf30b4
2 changed files with 37 additions and 31 deletions

View File

@ -219,9 +219,17 @@ func parseResolver(cfg string) gost.Resolver {
continue continue
} }
if strings.HasPrefix(s, "https") { if strings.HasPrefix(s, "https") {
p := "https"
u, _ := url.Parse(s)
if u == nil || u.Scheme == "" {
continue
}
if u.Scheme == "https-chain" {
p = u.Scheme
}
ns := gost.NameServer{ ns := gost.NameServer{
Addr: s, Addr: s,
Protocol: "https", Protocol: p,
} }
nss = append(nss, ns) nss = append(nss, ns)
continue continue

View File

@ -59,47 +59,48 @@ func (ns *NameServer) Init(opts ...NameServerOption) error {
opt(&ns.options) opt(&ns.options)
} }
switch strings.ToLower(ns.Protocol) { options := []ExchangerOption{
case "tcp":
ns.exchanger = NewDNSTCPExchanger(
ns.Addr,
TimeoutExchangerOption(ns.options.timeout), TimeoutExchangerOption(ns.options.timeout),
ChainExchangerOption(ns.options.chain), }
) protocol := strings.ToLower(ns.Protocol)
case "tls": switch protocol {
case "tcp", "tcp-chain":
if protocol == "tcp-chain" {
options = append(options, ChainExchangerOption(ns.options.chain))
}
ns.exchanger = NewDNSTCPExchanger(ns.Addr, options...)
case "tls", "tls-chain":
if protocol == "tls-chain" {
options = append(options, ChainExchangerOption(ns.options.chain))
}
cfg := &tls.Config{ cfg := &tls.Config{
ServerName: ns.Hostname, ServerName: ns.Hostname,
} }
if cfg.ServerName == "" { if cfg.ServerName == "" {
cfg.InsecureSkipVerify = true cfg.InsecureSkipVerify = true
} }
ns.exchanger = NewDoTExchanger( ns.exchanger = NewDoTExchanger(ns.Addr, cfg, options...)
ns.Addr, cfg, case "https", "https-chain":
TimeoutExchangerOption(ns.options.timeout), if protocol == "https-chain" {
ChainExchangerOption(ns.options.chain), options = append(options, ChainExchangerOption(ns.options.chain))
) }
case "https":
u, err := url.Parse(ns.Addr) u, err := url.Parse(ns.Addr)
if err != nil { if err != nil {
return err return err
} }
cfg := &tls.Config{ServerName: u.Hostname()} u.Scheme = "https"
cfg := &tls.Config{ServerName: ns.Hostname}
if cfg.ServerName == "" { if cfg.ServerName == "" {
cfg.InsecureSkipVerify = true cfg.InsecureSkipVerify = true
} }
ns.exchanger = NewDoHExchanger( ns.exchanger = NewDoHExchanger(u, cfg, options...)
u, cfg, case "udp", "udp-chain":
TimeoutExchangerOption(ns.options.timeout),
ChainExchangerOption(ns.options.chain),
)
case "udp":
fallthrough fallthrough
default: default:
ns.exchanger = NewDNSExchanger( if protocol == "udp-chain" {
ns.Addr, options = append(options, ChainExchangerOption(ns.options.chain))
TimeoutExchangerOption(ns.options.timeout), }
ChainExchangerOption(ns.options.chain), ns.exchanger = NewDNSExchanger(ns.Addr, options...)
)
} }
return nil return nil
@ -108,9 +109,6 @@ func (ns *NameServer) Init(opts ...NameServerOption) error {
func (ns *NameServer) String() string { func (ns *NameServer) String() string {
addr := ns.Addr addr := ns.Addr
prot := ns.Protocol prot := ns.Protocol
if _, port, _ := net.SplitHostPort(addr); port == "" {
addr = net.JoinHostPort(addr, "53")
}
if prot == "" { if prot == "" {
prot = "udp" prot = "udp"
} }
@ -411,7 +409,7 @@ func (r *resolver) Reload(rd io.Reader) error {
ns.Hostname = ss[2] ns.Hostname = ss[2]
} }
if strings.HasPrefix(ns.Addr, "https") { if strings.HasPrefix(ns.Addr, "https") && ns.Protocol == "" {
ns.Protocol = "https" ns.Protocol = "https"
} }
nss = append(nss, ns) nss = append(nss, ns)