+ control: use our own DNS resolver instead of system default
This commit is contained in:
parent
1836e56e6e
commit
bebdc1b5bc
|
@ -35,8 +35,13 @@ var protocols = []string{"tls://", "https://", "tcp://", "sdns://"}
|
||||||
const versionCheckURL = "https://adguardteam.github.io/AdGuardHome/version.json"
|
const versionCheckURL = "https://adguardteam.github.io/AdGuardHome/version.json"
|
||||||
const versionCheckPeriod = time.Hour * 8
|
const versionCheckPeriod = time.Hour * 8
|
||||||
|
|
||||||
|
var transport = &http.Transport{
|
||||||
|
DialContext: customDialContext,
|
||||||
|
}
|
||||||
|
|
||||||
var client = &http.Client{
|
var client = &http.Client{
|
||||||
Timeout: time.Minute * 5,
|
Timeout: time.Minute * 5,
|
||||||
|
Transport: transport,
|
||||||
}
|
}
|
||||||
|
|
||||||
var controlLock sync.Mutex
|
var controlLock sync.Mutex
|
||||||
|
|
46
helpers.go
46
helpers.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -14,7 +15,10 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/dnsproxy/upstream"
|
||||||
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/joomcode/errorx"
|
"github.com/joomcode/errorx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -300,6 +304,48 @@ func checkPacketPortAvailable(host string, port int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect to a remote server resolving hostname using our own DNS server
|
||||||
|
func customDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
log.Tracef("network:%v addr:%v", network, addr)
|
||||||
|
|
||||||
|
host, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
dialer := &net.Dialer{
|
||||||
|
Timeout: time.Minute * 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
if net.ParseIP(host) != nil {
|
||||||
|
con, err := dialer.DialContext(ctx, network, addr)
|
||||||
|
return con, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resolverAddr := fmt.Sprintf("%s:%d", config.DNS.BindHost, config.DNS.Port)
|
||||||
|
r := upstream.NewResolver(resolverAddr, 30*time.Second)
|
||||||
|
addrs, e := r.LookupIPAddr(ctx, host)
|
||||||
|
log.Tracef("LookupIPAddr: %s: %v", host, addrs)
|
||||||
|
if e != nil {
|
||||||
|
return nil, e
|
||||||
|
}
|
||||||
|
|
||||||
|
var firstErr error
|
||||||
|
firstErr = nil
|
||||||
|
for _, a := range addrs {
|
||||||
|
addr = fmt.Sprintf("%s:%s", a.String(), port)
|
||||||
|
con, err := dialer.DialContext(ctx, network, addr)
|
||||||
|
if err != nil {
|
||||||
|
if firstErr == nil {
|
||||||
|
firstErr = err
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return con, err
|
||||||
|
}
|
||||||
|
return nil, firstErr
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------
|
// ---------------------
|
||||||
// debug logging helpers
|
// debug logging helpers
|
||||||
// ---------------------
|
// ---------------------
|
||||||
|
|
Loading…
Reference in New Issue