Skip to content

Commit 1e34537

Browse files
authored
Merge pull request #737 from schollz:schollz/issue735
fix: croc hangs with broken dns
2 parents e85575f + 0d0effd commit 1e34537

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/models/constants.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"net"
77
"os"
88
"path"
9+
"time"
910

1011
"github.com/schollz/croc/v10/src/utils"
12+
log "github.com/schollz/logger"
1113
)
1214

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

5658
func init() {
59+
log.SetLevel("info")
60+
log.SetOutput(os.Stderr)
5761
doRemember := false
5862
for _, flag := range os.Args {
5963
if flag == "--internal-dns" {
@@ -78,6 +82,7 @@ func init() {
7882
INTERNAL_DNS = utils.Exists(fname)
7983
}
8084
}
85+
log.Trace("Using internal DNS: ", INTERNAL_DNS)
8186
var err error
8287
var addr string
8388
addr, err = lookup(DEFAULT_RELAY)
@@ -86,17 +91,20 @@ func init() {
8691
} else {
8792
DEFAULT_RELAY = ""
8893
}
94+
log.Tracef("Default ipv4 relay: %s", addr)
8995
addr, err = lookup(DEFAULT_RELAY6)
9096
if err == nil {
9197
DEFAULT_RELAY6 = net.JoinHostPort(addr, DEFAULT_PORT)
9298
} else {
9399
DEFAULT_RELAY6 = ""
94100
}
101+
log.Tracef("Default ipv6 relay: %s", addr)
95102
}
96103

97104
// Resolve a hostname to an IP address using DNS.
98105
func lookup(address string) (ipaddress string, err error) {
99106
if !INTERNAL_DNS {
107+
log.Tracef("Using local DNS to resolve %s", address)
100108
return localLookupIP(address)
101109
}
102110
type Result struct {
@@ -108,11 +116,13 @@ func lookup(address string) (ipaddress string, err error) {
108116
go func(dns string) {
109117
var r Result
110118
r.s, r.err = remoteLookupIP(address, dns)
119+
log.Tracef("Resolved %s to %s using %s", address, r.s, dns)
111120
result <- r
112121
}(dns)
113122
}
114123
for i := 0; i < len(publicDNS); i++ {
115124
ipaddress = (<-result).s
125+
log.Tracef("Resolved %s to %s", address, ipaddress)
116126
if ipaddress != "" {
117127
return
118128
}
@@ -121,9 +131,16 @@ func lookup(address string) (ipaddress string, err error) {
121131
return
122132
}
123133

124-
// localLookupIP returns a host's IP address based on the local resolver.
134+
// localLookupIP returns a host's IP address using the local DNS configuration.
125135
func localLookupIP(address string) (ipaddress string, err error) {
126-
ip, err := net.LookupHost(address)
136+
// Create a context with a 500 millisecond timeout
137+
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
138+
defer cancel()
139+
140+
r := &net.Resolver{}
141+
142+
// Use the context with timeout in the LookupHost function
143+
ip, err := r.LookupHost(ctx, address)
127144
if err != nil {
128145
return
129146
}
@@ -133,14 +150,17 @@ func localLookupIP(address string) (ipaddress string, err error) {
133150

134151
// remoteLookupIP returns a host's IP address based on a remote DNS server.
135152
func remoteLookupIP(address, dns string) (ipaddress string, err error) {
153+
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
154+
defer cancel()
155+
136156
r := &net.Resolver{
137157
PreferGo: true,
138158
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
139159
d := new(net.Dialer)
140160
return d.DialContext(ctx, network, dns+":53")
141161
},
142162
}
143-
ip, err := r.LookupHost(context.Background(), address)
163+
ip, err := r.LookupHost(ctx, address)
144164
if err != nil {
145165
return
146166
}

0 commit comments

Comments
 (0)