fix: let doh/dot server follow hosts and can remotely resolve itself ip
This commit is contained in:
@ -4,12 +4,14 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
"go.uber.org/atomic"
|
||||
"net"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -149,7 +151,7 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
||||
results := make(chan dialResult)
|
||||
var primary, fallback dialResult
|
||||
|
||||
startRacer := func(ctx context.Context, network, host string, direct bool, ipv6 bool) {
|
||||
startRacer := func(ctx context.Context, network, host string, r resolver.Resolver, ipv6 bool) {
|
||||
result := dialResult{ipv6: ipv6, done: true}
|
||||
defer func() {
|
||||
select {
|
||||
@ -163,16 +165,16 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
||||
|
||||
var ip netip.Addr
|
||||
if ipv6 {
|
||||
if !direct {
|
||||
if r == nil {
|
||||
ip, result.error = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ip, result.error = resolver.ResolveIPv6(ctx, host)
|
||||
ip, result.error = resolver.ResolveIPv6WithResolver(ctx, host, r)
|
||||
}
|
||||
} else {
|
||||
if !direct {
|
||||
if r == nil {
|
||||
ip, result.error = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ip, result.error = resolver.ResolveIPv4(ctx, host)
|
||||
ip, result.error = resolver.ResolveIPv4WithResolver(ctx, host, r)
|
||||
}
|
||||
}
|
||||
if result.error != nil {
|
||||
@ -183,8 +185,8 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
||||
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
||||
}
|
||||
|
||||
go startRacer(ctx, network+"4", host, opt.direct, false)
|
||||
go startRacer(ctx, network+"6", host, opt.direct, true)
|
||||
go startRacer(ctx, network+"4", host, opt.resolver, false)
|
||||
go startRacer(ctx, network+"6", host, opt.resolver, true)
|
||||
|
||||
count := 2
|
||||
for i := 0; i < count; i++ {
|
||||
@ -230,8 +232,8 @@ func concurrentDualStackDialContext(ctx context.Context, network, address string
|
||||
}
|
||||
|
||||
var ips []netip.Addr
|
||||
if opt.direct {
|
||||
ips, err = resolver.LookupIP(ctx, host)
|
||||
if opt.resolver != nil {
|
||||
ips, err = resolver.LookupIPWithResolver(ctx, host, opt.resolver)
|
||||
} else {
|
||||
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||
}
|
||||
@ -363,16 +365,16 @@ func singleDialContext(ctx context.Context, network string, address string, opt
|
||||
var ip netip.Addr
|
||||
switch network {
|
||||
case "tcp4", "udp4":
|
||||
if !opt.direct {
|
||||
if opt.resolver == nil {
|
||||
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ip, err = resolver.ResolveIPv4(ctx, host)
|
||||
ip, err = resolver.ResolveIPv4WithResolver(ctx, host, opt.resolver)
|
||||
}
|
||||
default:
|
||||
if !opt.direct {
|
||||
if opt.resolver == nil {
|
||||
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ip, err = resolver.ResolveIPv6(ctx, host)
|
||||
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, opt.resolver)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
@ -398,10 +400,10 @@ func concurrentIPv4DialContext(ctx context.Context, network, address string, opt
|
||||
}
|
||||
|
||||
var ips []netip.Addr
|
||||
if !opt.direct {
|
||||
if opt.resolver == nil {
|
||||
ips, err = resolver.LookupIPv4ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ips, err = resolver.LookupIPv4(ctx, host)
|
||||
ips, err = resolver.LookupIPv4WithResolver(ctx, host, opt.resolver)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -418,10 +420,10 @@ func concurrentIPv6DialContext(ctx context.Context, network, address string, opt
|
||||
}
|
||||
|
||||
var ips []netip.Addr
|
||||
if !opt.direct {
|
||||
if opt.resolver == nil {
|
||||
ips, err = resolver.LookupIPv6ProxyServerHost(ctx, host)
|
||||
} else {
|
||||
ips, err = resolver.LookupIPv6(ctx, host)
|
||||
ips, err = resolver.LookupIPv6WithResolver(ctx, host, opt.resolver)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@ -1,6 +1,8 @@
|
||||
package dialer
|
||||
|
||||
import (
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
@ -14,9 +16,9 @@ type option struct {
|
||||
interfaceName string
|
||||
addrReuse bool
|
||||
routingMark int
|
||||
direct bool
|
||||
network int
|
||||
prefer int
|
||||
resolver resolver.Resolver
|
||||
}
|
||||
|
||||
type Option func(opt *option)
|
||||
@ -39,9 +41,9 @@ func WithRoutingMark(mark int) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithDirect() Option {
|
||||
func WithResolver(r resolver.Resolver) Option {
|
||||
return func(opt *option) {
|
||||
opt.direct = true
|
||||
opt.resolver = r
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user