From 907dec42e0bdae6bb44286328cdaa4f553bc0055 Mon Sep 17 00:00:00 2001 From: boboan Date: Thu, 12 Jul 2018 10:21:28 +0800 Subject: [PATCH] net.LookupIP may cause deadlock in windows https://github.com/golang/go/issues/24178 --- services/http/http.go | 2 +- services/socks/socks.go | 2 +- utils/functions.go | 27 ++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/services/http/http.go b/services/http/http.go index a1a14aa..73dd4b5 100644 --- a/services/http/http.go +++ b/services/http/http.go @@ -492,7 +492,7 @@ func (s *HTTP) IsDeadLoop(inLocalAddr string, host string) bool { if *s.cfg.DNSAddress != "" { outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))} } else { - outIPs, err = net.LookupIP(outDomain) + outIPs, err = utils.MyLookupIP(outDomain) } if err == nil { for _, ip := range outIPs { diff --git a/services/socks/socks.go b/services/socks/socks.go index 5020c50..901e001 100644 --- a/services/socks/socks.go +++ b/services/socks/socks.go @@ -598,7 +598,7 @@ func (s *Socks) IsDeadLoop(inLocalAddr string, host string) bool { if *s.cfg.DNSAddress != "" { outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))} } else { - outIPs, err = net.LookupIP(outDomain) + outIPs, err = utils.MyLookupIP(outDomain) } if err == nil { for _, ip := range outIPs { diff --git a/utils/functions.go b/utils/functions.go index 4d20b7c..fe9d470 100755 --- a/utils/functions.go +++ b/utils/functions.go @@ -25,6 +25,7 @@ import ( "strconv" "strings" "time" + "context" "github.com/snail007/goproxy/utils/id" @@ -497,7 +498,7 @@ func IsIternalIP(domainOrIP string, always bool) bool { } if isDomain { - outIPs, err = net.LookupIP(domainOrIP) + outIPs, err = MyLookupIP(domainOrIP) } else { outIPs = []net.IP{net.ParseIP(domainOrIP)} } @@ -632,3 +633,27 @@ func InsertProxyHeaders(head []byte, headers string) []byte { // } // return // } + + +/* +net.LookupIP may cause deadlock in windows +https://github.com/golang/go/issues/24178 +*/ + +func MyLookupIP(host string) ([]net.IP, error) { + + ctx ,cancel := context.WithTimeout(context.Background(),time.Second *time.Duration(3)) + defer func() { + cancel() + //ctx.Done() + }() + addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host) + if err != nil { + return nil, err + } + ips := make([]net.IP, len(addrs)) + for i, ia := range addrs { + ips[i] = ia.IP + } + return ips, nil +} \ No newline at end of file