add lookupIP in route controll
This commit is contained in:
parent
6e46ac03c7
commit
61a2d6a6ba
6442
.config/CN_IP.txt
Normal file
6442
.config/CN_IP.txt
Normal file
File diff suppressed because it is too large
Load Diff
33
bypass.go
33
bypass.go
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -172,16 +173,48 @@ func (bp *Bypass) Contains(addr string) bool {
|
||||
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
|
||||
for _, matcher := range bp.matchers {
|
||||
if matcher == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
return !bp.reversed && matched ||
|
||||
bp.reversed && !matched
|
||||
}
|
||||
|
@ -4,10 +4,26 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
"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 {
|
||||
patterns []string
|
||||
reversed bool
|
||||
@ -155,6 +171,11 @@ var bypassContainTests = []struct {
|
||||
{[]string{".example.com:*"}, false, "example.com:80", false},
|
||||
{[]string{".example.com:*"}, false, "www.example.com:8080", false},
|
||||
{[]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) {
|
||||
|
Loading…
Reference in New Issue
Block a user