mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
Merge pull request #737 from schollz:schollz/issue735
fix: croc hangs with broken dns
This commit is contained in:
commit
1e34537bb2
1 changed files with 23 additions and 3 deletions
|
@ -6,8 +6,10 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/schollz/croc/v10/src/utils"
|
"github.com/schollz/croc/v10/src/utils"
|
||||||
|
log "github.com/schollz/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TCP_BUFFER_SIZE is the maximum packet size
|
// TCP_BUFFER_SIZE is the maximum packet size
|
||||||
|
@ -54,6 +56,8 @@ func getConfigFile(requireValidPath bool) (fname string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
log.SetLevel("info")
|
||||||
|
log.SetOutput(os.Stderr)
|
||||||
doRemember := false
|
doRemember := false
|
||||||
for _, flag := range os.Args {
|
for _, flag := range os.Args {
|
||||||
if flag == "--internal-dns" {
|
if flag == "--internal-dns" {
|
||||||
|
@ -78,6 +82,7 @@ func init() {
|
||||||
INTERNAL_DNS = utils.Exists(fname)
|
INTERNAL_DNS = utils.Exists(fname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Trace("Using internal DNS: ", INTERNAL_DNS)
|
||||||
var err error
|
var err error
|
||||||
var addr string
|
var addr string
|
||||||
addr, err = lookup(DEFAULT_RELAY)
|
addr, err = lookup(DEFAULT_RELAY)
|
||||||
|
@ -86,17 +91,20 @@ func init() {
|
||||||
} else {
|
} else {
|
||||||
DEFAULT_RELAY = ""
|
DEFAULT_RELAY = ""
|
||||||
}
|
}
|
||||||
|
log.Tracef("Default ipv4 relay: %s", addr)
|
||||||
addr, err = lookup(DEFAULT_RELAY6)
|
addr, err = lookup(DEFAULT_RELAY6)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
DEFAULT_RELAY6 = net.JoinHostPort(addr, DEFAULT_PORT)
|
DEFAULT_RELAY6 = net.JoinHostPort(addr, DEFAULT_PORT)
|
||||||
} else {
|
} else {
|
||||||
DEFAULT_RELAY6 = ""
|
DEFAULT_RELAY6 = ""
|
||||||
}
|
}
|
||||||
|
log.Tracef("Default ipv6 relay: %s", addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve a hostname to an IP address using DNS.
|
// Resolve a hostname to an IP address using DNS.
|
||||||
func lookup(address string) (ipaddress string, err error) {
|
func lookup(address string) (ipaddress string, err error) {
|
||||||
if !INTERNAL_DNS {
|
if !INTERNAL_DNS {
|
||||||
|
log.Tracef("Using local DNS to resolve %s", address)
|
||||||
return localLookupIP(address)
|
return localLookupIP(address)
|
||||||
}
|
}
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
@ -108,11 +116,13 @@ func lookup(address string) (ipaddress string, err error) {
|
||||||
go func(dns string) {
|
go func(dns string) {
|
||||||
var r Result
|
var r Result
|
||||||
r.s, r.err = remoteLookupIP(address, dns)
|
r.s, r.err = remoteLookupIP(address, dns)
|
||||||
|
log.Tracef("Resolved %s to %s using %s", address, r.s, dns)
|
||||||
result <- r
|
result <- r
|
||||||
}(dns)
|
}(dns)
|
||||||
}
|
}
|
||||||
for i := 0; i < len(publicDNS); i++ {
|
for i := 0; i < len(publicDNS); i++ {
|
||||||
ipaddress = (<-result).s
|
ipaddress = (<-result).s
|
||||||
|
log.Tracef("Resolved %s to %s", address, ipaddress)
|
||||||
if ipaddress != "" {
|
if ipaddress != "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -121,9 +131,16 @@ func lookup(address string) (ipaddress string, err error) {
|
||||||
return
|
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) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -133,6 +150,9 @@ func localLookupIP(address string) (ipaddress string, err error) {
|
||||||
|
|
||||||
// remoteLookupIP returns a host's IP address based on a remote DNS server.
|
// remoteLookupIP returns a host's IP address based on a remote DNS server.
|
||||||
func remoteLookupIP(address, dns string) (ipaddress string, err error) {
|
func remoteLookupIP(address, dns string) (ipaddress string, err error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
r := &net.Resolver{
|
r := &net.Resolver{
|
||||||
PreferGo: true,
|
PreferGo: true,
|
||||||
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||||
|
@ -140,7 +160,7 @@ func remoteLookupIP(address, dns string) (ipaddress string, err error) {
|
||||||
return d.DialContext(ctx, network, dns+":53")
|
return d.DialContext(ctx, network, dns+":53")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
ip, err := r.LookupHost(context.Background(), address)
|
ip, err := r.LookupHost(ctx, address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue