Skip to content

Commit

Permalink
Merge pull request #737 from schollz:schollz/issue735
Browse files Browse the repository at this point in the history
fix: croc hangs with broken dns
  • Loading branch information
schollz authored Jun 13, 2024
2 parents e85575f + 0d0effd commit 1e34537
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/models/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"net"
"os"
"path"
"time"

"github.com/schollz/croc/v10/src/utils"
log "github.com/schollz/logger"
)

// TCP_BUFFER_SIZE is the maximum packet size
Expand Down Expand Up @@ -54,6 +56,8 @@ func getConfigFile(requireValidPath bool) (fname string, err error) {
}

func init() {
log.SetLevel("info")
log.SetOutput(os.Stderr)
doRemember := false
for _, flag := range os.Args {
if flag == "--internal-dns" {
Expand All @@ -78,6 +82,7 @@ func init() {
INTERNAL_DNS = utils.Exists(fname)
}
}
log.Trace("Using internal DNS: ", INTERNAL_DNS)
var err error
var addr string
addr, err = lookup(DEFAULT_RELAY)
Expand All @@ -86,17 +91,20 @@ func init() {
} else {
DEFAULT_RELAY = ""
}
log.Tracef("Default ipv4 relay: %s", addr)
addr, err = lookup(DEFAULT_RELAY6)
if err == nil {
DEFAULT_RELAY6 = net.JoinHostPort(addr, DEFAULT_PORT)
} else {
DEFAULT_RELAY6 = ""
}
log.Tracef("Default ipv6 relay: %s", addr)
}

// Resolve a hostname to an IP address using DNS.
func lookup(address string) (ipaddress string, err error) {
if !INTERNAL_DNS {
log.Tracef("Using local DNS to resolve %s", address)
return localLookupIP(address)
}
type Result struct {
Expand All @@ -108,11 +116,13 @@ func lookup(address string) (ipaddress string, err error) {
go func(dns string) {
var r Result
r.s, r.err = remoteLookupIP(address, dns)
log.Tracef("Resolved %s to %s using %s", address, r.s, dns)
result <- r
}(dns)
}
for i := 0; i < len(publicDNS); i++ {
ipaddress = (<-result).s
log.Tracef("Resolved %s to %s", address, ipaddress)
if ipaddress != "" {
return
}
Expand All @@ -121,9 +131,16 @@ func lookup(address string) (ipaddress string, err error) {
return
}

// localLookupIP returns a host's IP address based on the local resolver.
// localLookupIP returns a host's IP address using the local DNS configuration.
func localLookupIP(address string) (ipaddress string, err error) {
ip, err := net.LookupHost(address)
// Create a context with a 500 millisecond timeout
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

r := &net.Resolver{}

// Use the context with timeout in the LookupHost function
ip, err := r.LookupHost(ctx, address)
if err != nil {
return
}
Expand All @@ -133,14 +150,17 @@ func localLookupIP(address string) (ipaddress string, err error) {

// remoteLookupIP returns a host's IP address based on a remote DNS server.
func remoteLookupIP(address, dns string) (ipaddress string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
d := new(net.Dialer)
return d.DialContext(ctx, network, dns+":53")
},
}
ip, err := r.LookupHost(context.Background(), address)
ip, err := r.LookupHost(ctx, address)
if err != nil {
return
}
Expand Down

0 comments on commit 1e34537

Please sign in to comment.