Feature: custom dns ipv4/ipv6 dual stack
This commit is contained in:
32
dns/iputil.go
Normal file
32
dns/iputil.go
Normal file
@ -0,0 +1,32 @@
|
||||
package dns
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
errIPNotFound = errors.New("ip not found")
|
||||
)
|
||||
|
||||
// ResolveIP with a host, return ip
|
||||
func ResolveIP(host string) (net.IP, error) {
|
||||
if DefaultResolver != nil {
|
||||
if DefaultResolver.ipv6 {
|
||||
return DefaultResolver.ResolveIP(host)
|
||||
}
|
||||
return DefaultResolver.ResolveIPv4(host)
|
||||
}
|
||||
|
||||
ip := net.ParseIP(host)
|
||||
if ip != nil {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
ipAddr, err := net.ResolveIPAddr("ip", host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ipAddr.IP, nil
|
||||
}
|
@ -18,6 +18,11 @@ import (
|
||||
geoip2 "github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultResolver aim to resolve ip with host
|
||||
DefaultResolver *Resolver
|
||||
)
|
||||
|
||||
var (
|
||||
globalSessionCache = tls.NewLRUClientSessionCache(64)
|
||||
|
||||
@ -47,11 +52,16 @@ type Resolver struct {
|
||||
|
||||
// ResolveIP request with TypeA and TypeAAAA, priority return TypeAAAA
|
||||
func (r *Resolver) ResolveIP(host string) (ip net.IP, err error) {
|
||||
ip = net.ParseIP(host)
|
||||
if ip != nil {
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
ch := make(chan net.IP)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
ip, err := r.resolveIP(host, D.TypeA)
|
||||
if err != nil {
|
||||
close(ch)
|
||||
return
|
||||
}
|
||||
ch <- ip
|
||||
@ -65,8 +75,8 @@ func (r *Resolver) ResolveIP(host string) (ip net.IP, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
ip, closed := <-ch
|
||||
if closed {
|
||||
ip, open := <-ch
|
||||
if !open {
|
||||
return nil, errors.New("can't found ip")
|
||||
}
|
||||
|
||||
@ -275,8 +285,8 @@ func New(config Config) *Resolver {
|
||||
})
|
||||
|
||||
r := &Resolver{
|
||||
main: transform(config.Main),
|
||||
ipv6: config.IPv6,
|
||||
main: transform(config.Main),
|
||||
cache: cache.New(time.Second * 60),
|
||||
mapping: config.EnhancedMode == MAPPING,
|
||||
fakeip: config.EnhancedMode == FAKEIP,
|
||||
|
Reference in New Issue
Block a user