add lookupIP in route controll

This commit is contained in:
zhylmzr 2019-11-25 14:44:41 +08:00
parent 6e46ac03c7
commit 61a2d6a6ba
3 changed files with 6498 additions and 2 deletions

6442
.config/CN_IP.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"net/url"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -172,13 +173,45 @@ func (bp *Bypass) Contains(addr string) bool {
return false return false
} }
// lookup ip if parse ip is nil
ip := net.ParseIP(addr)
var ips []string
if ip == nil {
// avoid long time lookupIP
// invalid domain when CI
if u, err := url.ParseRequestURI(addr); err == nil {
if ipArr, err := net.LookupIP(u.Hostname()); err == nil {
for _, ip := range ipArr {
ips = append(ips, fmt.Sprintf("%v", ip))
}
}
}
} else {
ips = append(ips, fmt.Sprintf("%v", ip))
}
var matched bool var matched bool
for _, matcher := range bp.matchers { for _, matcher := range bp.matchers {
if matcher == nil { if matcher == nil {
continue continue
} }
if matcher.Match(addr) {
matched = true // only to apply cidrMatcher
if _, ok := matcher.(*cidrMatcher); ok {
for _, ip := range ips {
if matcher.Match(ip) {
matched = true
break
}
}
} else {
if matcher.Match(addr) {
matched = true
break
}
}
if matched {
break break
} }
} }

View File

@ -4,10 +4,26 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log"
"strings"
"testing" "testing"
"time" "time"
) )
func loadCNIP() []string {
bs, err := ioutil.ReadFile(".config/CN_IP.txt")
if err != nil {
log.Fatalln(err)
return nil
}
s := string(bs)
return strings.Split(s, "\n")
}
// Cinese IP
var CNIP = loadCNIP()
var bypassContainTests = []struct { var bypassContainTests = []struct {
patterns []string patterns []string
reversed bool reversed bool
@ -155,6 +171,11 @@ var bypassContainTests = []struct {
{[]string{".example.com:*"}, false, "example.com:80", false}, {[]string{".example.com:*"}, false, "example.com:80", false},
{[]string{".example.com:*"}, false, "www.example.com:8080", false}, {[]string{".example.com:*"}, false, "www.example.com:8080", false},
{[]string{".example.com:*"}, false, "http://www.example.com:80", true}, {[]string{".example.com:*"}, false, "http://www.example.com:80", true},
{CNIP, false, "http://www.baidu.com", true},
{CNIP, false, "http://www.google.com", false},
{CNIP, true, "http://www.jd.com", false},
{CNIP, true, "http://www.facebook.com", true},
} }
func TestBypassContains(t *testing.T) { func TestBypassContains(t *testing.T) {