Chore: script built
This commit is contained in:
@ -2,7 +2,6 @@ package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/rule/geodata/strmatcher"
|
||||
@ -69,44 +68,3 @@ func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
|
||||
func (m *DomainMatcher) ApplyDomain(domain string) bool {
|
||||
return len(m.matchers.Match(strings.ToLower(domain))) > 0
|
||||
}
|
||||
|
||||
type MultiGeoIPMatcher struct {
|
||||
matchers []*GeoIPMatcher
|
||||
}
|
||||
|
||||
func NewMultiGeoIPMatcher(geoips []*GeoIP) (*MultiGeoIPMatcher, error) {
|
||||
var matchers []*GeoIPMatcher
|
||||
for _, geoip := range geoips {
|
||||
matcher, err := globalGeoIPContainer.Add(geoip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
matchers = append(matchers, matcher)
|
||||
}
|
||||
|
||||
matcher := &MultiGeoIPMatcher{
|
||||
matchers: matchers,
|
||||
}
|
||||
|
||||
return matcher, nil
|
||||
}
|
||||
|
||||
func (m *MultiGeoIPMatcher) ApplyIp(ip net.IP) bool {
|
||||
|
||||
for _, matcher := range m.matchers {
|
||||
if matcher.Match(ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func NewGeoIPMatcher(geoip *GeoIP) (*GeoIPMatcher, error) {
|
||||
matcher, err := globalGeoIPContainer.Add(geoip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return matcher, nil
|
||||
}
|
||||
|
@ -1,110 +0,0 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"inet.af/netaddr"
|
||||
)
|
||||
|
||||
type GeoIPMatcher struct {
|
||||
countryCode string
|
||||
reverseMatch bool
|
||||
ip4 *netaddr.IPSet
|
||||
ip6 *netaddr.IPSet
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) Init(cidrs []*CIDR) error {
|
||||
var builder4, builder6 netaddr.IPSetBuilder
|
||||
for _, cidr := range cidrs {
|
||||
netaddrIP, ok := netaddr.FromStdIP(net.IP(cidr.GetIp()))
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid IP address %v", cidr)
|
||||
}
|
||||
ipPrefix := netaddr.IPPrefixFrom(netaddrIP, uint8(cidr.GetPrefix()))
|
||||
switch {
|
||||
case netaddrIP.Is4():
|
||||
builder4.AddPrefix(ipPrefix)
|
||||
case netaddrIP.Is6():
|
||||
builder6.AddPrefix(ipPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
m.ip4, err = builder4.IPSet()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.ip6, err = builder6.IPSet()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
|
||||
m.reverseMatch = isReverseMatch
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) match4(ip net.IP) bool {
|
||||
nip, ok := netaddr.FromStdIP(ip)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return m.ip4.Contains(nip)
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) match6(ip net.IP) bool {
|
||||
nip, ok := netaddr.FromStdIP(ip)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return m.ip6.Contains(nip)
|
||||
}
|
||||
|
||||
// Match returns true if the given ip is included by the GeoIP.
|
||||
func (m *GeoIPMatcher) Match(ip net.IP) bool {
|
||||
isMatched := false
|
||||
switch len(ip) {
|
||||
case net.IPv4len:
|
||||
isMatched = m.match4(ip)
|
||||
case net.IPv6len:
|
||||
isMatched = m.match6(ip)
|
||||
}
|
||||
if m.reverseMatch {
|
||||
return !isMatched
|
||||
}
|
||||
return isMatched
|
||||
}
|
||||
|
||||
// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
|
||||
type GeoIPMatcherContainer struct {
|
||||
matchers []*GeoIPMatcher
|
||||
}
|
||||
|
||||
// Add adds a new GeoIP set into the container.
|
||||
// If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
|
||||
func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
|
||||
if geoip.CountryCode != "" {
|
||||
for _, m := range c.matchers {
|
||||
if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m := &GeoIPMatcher{
|
||||
countryCode: geoip.CountryCode,
|
||||
reverseMatch: geoip.ReverseMatch,
|
||||
}
|
||||
if err := m.Init(geoip.Cidr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if geoip.CountryCode != "" {
|
||||
c.matchers = append(c.matchers, m)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
var globalGeoIPContainer GeoIPMatcherContainer
|
@ -5,9 +5,6 @@ import (
|
||||
|
||||
"github.com/Dreamacro/clash/component/mmdb"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
//"github.com/Dreamacro/clash/rule/geodata"
|
||||
//"github.com/Dreamacro/clash/rule/geodata/router"
|
||||
//_ "github.com/Dreamacro/clash/rule/geodata/standard"
|
||||
)
|
||||
|
||||
type GEOIP struct {
|
||||
@ -15,7 +12,6 @@ type GEOIP struct {
|
||||
adapter string
|
||||
noResolveIP bool
|
||||
ruleExtra *C.RuleExtra
|
||||
//geoIPMatcher *router.GeoIPMatcher
|
||||
}
|
||||
|
||||
func (g *GEOIP) RuleType() C.RuleType {
|
||||
@ -56,38 +52,11 @@ func (g *GEOIP) GetCountry() string {
|
||||
}
|
||||
|
||||
func NewGEOIP(country string, adapter string, noResolveIP bool, ruleExtra *C.RuleExtra) (*GEOIP, error) {
|
||||
//geoLoaderName := "standard"
|
||||
////geoLoaderName := "memconservative"
|
||||
//geoLoader, err := geodata.GetGeoDataLoader(geoLoaderName)
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("load GeoIP data error, %s", err.Error())
|
||||
//}
|
||||
//
|
||||
//records, err := geoLoader.LoadGeoIP(strings.ReplaceAll(country, "!", ""))
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("load GeoIP data error, %s", err.Error())
|
||||
//}
|
||||
//
|
||||
//geoIP := &router.GeoIP{
|
||||
// CountryCode: country,
|
||||
// Cidr: records,
|
||||
// ReverseMatch: strings.Contains(country, "!"),
|
||||
//}
|
||||
//
|
||||
//geoIPMatcher, err := router.NewGeoIPMatcher(geoIP)
|
||||
//
|
||||
//if err != nil {
|
||||
// return nil, fmt.Errorf("load GeoIP data error, %s", err.Error())
|
||||
//}
|
||||
//
|
||||
//log.Infoln("Start initial GeoIP rule %s => %s, records: %d, reverse match: %v", country, adapter, len(records), geoIP.ReverseMatch)
|
||||
|
||||
geoip := &GEOIP{
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
noResolveIP: noResolveIP,
|
||||
ruleExtra: ruleExtra,
|
||||
//geoIPMatcher: geoIPMatcher,
|
||||
}
|
||||
|
||||
return geoip, nil
|
||||
|
Reference in New Issue
Block a user