1
0
mirror of https://github.com/schollz/croc.git synced 2024-11-24 16:23:47 +03:00

Merge pull request #383 from oxzi/dns-prefer-local

Prefer local DNS resolver over public DNS servers
Fixes #301
This commit is contained in:
Zack 2021-04-26 10:45:07 -07:00 committed by GitHub
commit 45cb545a82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package models
import ( import (
"context" "context"
"fmt"
"net" "net"
"time" "time"
) )
@ -17,15 +18,31 @@ var (
DEFAULT_PASSPHRASE = "pass123" DEFAULT_PASSPHRASE = "pass123"
) )
// lookupTimeout for DNS requests
const lookupTimeout = time.Second
// publicDns are servers to be queried if a local lookup fails
var publicDns = []string{
"1.0.0.1", // Cloudflare
"1.1.1.1", // Cloudflare
"8.8.4.4", // Google
"8.8.8.8", // Google
"8.26.56.26", // Comodo
"208.67.220.220", // Cisco OpenDNS
"208.67.222.222", // Cisco OpenDNS
"[2001:4860:4860::8844]", // Google
"[2001:4860:4860::8888]", // Google
}
func init() { func init() {
var err error var err error
DEFAULT_RELAY, err = lookupIPs(DEFAULT_RELAY) DEFAULT_RELAY, err = lookup(DEFAULT_RELAY)
if err == nil { if err == nil {
DEFAULT_RELAY += ":" + DEFAULT_PORT DEFAULT_RELAY += ":" + DEFAULT_PORT
} else { } else {
DEFAULT_RELAY = "" DEFAULT_RELAY = ""
} }
DEFAULT_RELAY6, err = lookupIPs(DEFAULT_RELAY6) DEFAULT_RELAY6, err = lookup(DEFAULT_RELAY6)
if err == nil { if err == nil {
DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT DEFAULT_RELAY6 = "[" + DEFAULT_RELAY6 + "]:" + DEFAULT_PORT
} else { } else {
@ -33,37 +50,56 @@ func init() {
} }
} }
func lookupIPs(address string) (ipaddress string, err error) { // lookup an IP address.
var publicDns = []string{"1.1.1.1", "8.8.8.8", "8.8.4.4", "1.0.0.1", "8.26.56.26", "208.67.222.222", "208.67.220.220"} //
result := make(chan string, len(publicDns)+1) // Priority is given to local queries, and the system falls back to a list of
go func() { // public DNS servers.
ips, _ := net.LookupIP(address) func lookup(address string) (ipaddress string, err error) {
for _, ip := range ips { ipaddress, err = localLookupIP(address)
result <- ip.String() if err == nil {
} return
result <- "" }
}() err = nil
result := make(chan string, len(publicDns))
for _, dns := range publicDns { for _, dns := range publicDns {
go func(dns string) { go func(dns string) {
s, _ := lookupIP(address, dns) s, _ := remoteLookupIP(address, dns)
result <- s result <- s
}(dns) }(dns)
} }
for i := 0; i < len(publicDns); i++ {
ipaddress = <-result for s := range result {
if ipaddress != "" { if s != "" {
ipaddress = s
return return
} }
} }
err = fmt.Errorf("failed to lookup %s at any DNS server", address)
return return
} }
func lookupIP(address, dns string) (ipaddress string, err error) { // localLookupIP returns a host's IP address based on the local resolver.
func localLookupIP(address string) (ipaddress string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), lookupTimeout)
defer cancel()
ip, err := net.DefaultResolver.LookupHost(ctx, address)
if err != nil {
return
}
ipaddress = ip[0]
return
}
// remoteLookupIP returns a host's IP address based on a remote DNS server.
func remoteLookupIP(address, dns string) (ipaddress string, err error) {
r := &net.Resolver{ r := &net.Resolver{
PreferGo: true, PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{ d := net.Dialer{
Timeout: time.Millisecond * time.Duration(1000), Timeout: lookupTimeout,
} }
return d.DialContext(ctx, "udp", dns+":53") return d.DialContext(ctx, "udp", dns+":53")
}, },