fix addr judge

This commit is contained in:
zhylmzr 2019-11-25 18:02:14 +08:00
parent 61a2d6a6ba
commit d177f3df69
2 changed files with 12 additions and 6 deletions

View File

@ -179,13 +179,19 @@ func (bp *Bypass) Contains(addr string) bool {
if ip == nil { if ip == nil {
// avoid long time lookupIP // avoid long time lookupIP
// invalid domain when CI // invalid domain when CI
if u, err := url.ParseRequestURI(addr); err == nil { if _, _, ok := net.ParseCIDR(addr); ok != nil {
newAddr := addr
if strings.Index(addr, "http://") == -1 && strings.Index(addr, "https://") == -1 {
newAddr = "http://" + addr
}
if u, err := url.Parse(newAddr); err == nil {
if ipArr, err := net.LookupIP(u.Hostname()); err == nil { if ipArr, err := net.LookupIP(u.Hostname()); err == nil {
for _, ip := range ipArr { for _, ip := range ipArr {
ips = append(ips, fmt.Sprintf("%v", ip)) ips = append(ips, fmt.Sprintf("%v", ip))
} }
} }
} }
}
} else { } else {
ips = append(ips, fmt.Sprintf("%v", ip)) ips = append(ips, fmt.Sprintf("%v", ip))
} }

View File

@ -174,8 +174,8 @@ var bypassContainTests = []struct {
{CNIP, false, "http://www.baidu.com", true}, {CNIP, false, "http://www.baidu.com", true},
{CNIP, false, "http://www.google.com", false}, {CNIP, false, "http://www.google.com", false},
{CNIP, true, "http://www.jd.com", false}, {CNIP, true, "www.jd.com", false},
{CNIP, true, "http://www.facebook.com", true}, {CNIP, true, "www.facebook.com", true},
} }
func TestBypassContains(t *testing.T) { func TestBypassContains(t *testing.T) {