net.LookupIP may cause deadlock in windows

https://github.com/golang/go/issues/24178
This commit is contained in:
boboan
2018-07-12 10:21:28 +08:00
parent dd355b5d98
commit 907dec42e0
3 changed files with 28 additions and 3 deletions

View File

@ -492,7 +492,7 @@ func (s *HTTP) IsDeadLoop(inLocalAddr string, host string) bool {
if *s.cfg.DNSAddress != "" { if *s.cfg.DNSAddress != "" {
outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))} outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))}
} else { } else {
outIPs, err = net.LookupIP(outDomain) outIPs, err = utils.MyLookupIP(outDomain)
} }
if err == nil { if err == nil {
for _, ip := range outIPs { for _, ip := range outIPs {

View File

@ -598,7 +598,7 @@ func (s *Socks) IsDeadLoop(inLocalAddr string, host string) bool {
if *s.cfg.DNSAddress != "" { if *s.cfg.DNSAddress != "" {
outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))} outIPs = []net.IP{net.ParseIP(s.Resolve(outDomain))}
} else { } else {
outIPs, err = net.LookupIP(outDomain) outIPs, err = utils.MyLookupIP(outDomain)
} }
if err == nil { if err == nil {
for _, ip := range outIPs { for _, ip := range outIPs {

View File

@ -25,6 +25,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"context"
"github.com/snail007/goproxy/utils/id" "github.com/snail007/goproxy/utils/id"
@ -497,7 +498,7 @@ func IsIternalIP(domainOrIP string, always bool) bool {
} }
if isDomain { if isDomain {
outIPs, err = net.LookupIP(domainOrIP) outIPs, err = MyLookupIP(domainOrIP)
} else { } else {
outIPs = []net.IP{net.ParseIP(domainOrIP)} outIPs = []net.IP{net.ParseIP(domainOrIP)}
} }
@ -632,3 +633,27 @@ func InsertProxyHeaders(head []byte, headers string) []byte {
// } // }
// return // 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
}