@@ -6,8 +6,10 @@ import (
6
6
"net"
7
7
"os"
8
8
"path"
9
+ "time"
9
10
10
11
"github.com/schollz/croc/v10/src/utils"
12
+ log "github.com/schollz/logger"
11
13
)
12
14
13
15
// TCP_BUFFER_SIZE is the maximum packet size
@@ -54,6 +56,8 @@ func getConfigFile(requireValidPath bool) (fname string, err error) {
54
56
}
55
57
56
58
func init () {
59
+ log .SetLevel ("info" )
60
+ log .SetOutput (os .Stderr )
57
61
doRemember := false
58
62
for _ , flag := range os .Args {
59
63
if flag == "--internal-dns" {
@@ -78,6 +82,7 @@ func init() {
78
82
INTERNAL_DNS = utils .Exists (fname )
79
83
}
80
84
}
85
+ log .Trace ("Using internal DNS: " , INTERNAL_DNS )
81
86
var err error
82
87
var addr string
83
88
addr , err = lookup (DEFAULT_RELAY )
@@ -86,17 +91,20 @@ func init() {
86
91
} else {
87
92
DEFAULT_RELAY = ""
88
93
}
94
+ log .Tracef ("Default ipv4 relay: %s" , addr )
89
95
addr , err = lookup (DEFAULT_RELAY6 )
90
96
if err == nil {
91
97
DEFAULT_RELAY6 = net .JoinHostPort (addr , DEFAULT_PORT )
92
98
} else {
93
99
DEFAULT_RELAY6 = ""
94
100
}
101
+ log .Tracef ("Default ipv6 relay: %s" , addr )
95
102
}
96
103
97
104
// Resolve a hostname to an IP address using DNS.
98
105
func lookup (address string ) (ipaddress string , err error ) {
99
106
if ! INTERNAL_DNS {
107
+ log .Tracef ("Using local DNS to resolve %s" , address )
100
108
return localLookupIP (address )
101
109
}
102
110
type Result struct {
@@ -108,11 +116,13 @@ func lookup(address string) (ipaddress string, err error) {
108
116
go func (dns string ) {
109
117
var r Result
110
118
r .s , r .err = remoteLookupIP (address , dns )
119
+ log .Tracef ("Resolved %s to %s using %s" , address , r .s , dns )
111
120
result <- r
112
121
}(dns )
113
122
}
114
123
for i := 0 ; i < len (publicDNS ); i ++ {
115
124
ipaddress = (<- result ).s
125
+ log .Tracef ("Resolved %s to %s" , address , ipaddress )
116
126
if ipaddress != "" {
117
127
return
118
128
}
@@ -121,9 +131,16 @@ func lookup(address string) (ipaddress string, err error) {
121
131
return
122
132
}
123
133
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 .
125
135
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 )
127
144
if err != nil {
128
145
return
129
146
}
@@ -133,14 +150,17 @@ func localLookupIP(address string) (ipaddress string, err error) {
133
150
134
151
// remoteLookupIP returns a host's IP address based on a remote DNS server.
135
152
func remoteLookupIP (address , dns string ) (ipaddress string , err error ) {
153
+ ctx , cancel := context .WithTimeout (context .Background (), 500 * time .Millisecond )
154
+ defer cancel ()
155
+
136
156
r := & net.Resolver {
137
157
PreferGo : true ,
138
158
Dial : func (ctx context.Context , network , _ string ) (net.Conn , error ) {
139
159
d := new (net.Dialer )
140
160
return d .DialContext (ctx , network , dns + ":53" )
141
161
},
142
162
}
143
- ip , err := r .LookupHost (context . Background () , address )
163
+ ip , err := r .LookupHost (ctx , address )
144
164
if err != nil {
145
165
return
146
166
}
0 commit comments