Feature: add mode script
This commit is contained in:
@ -2,7 +2,7 @@ package memconservative
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
@ -54,7 +54,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) {
|
||||
case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
|
||||
errInvalidGeodataFile, errInvalidGeodataVarintLength:
|
||||
log.Warnln("failed to decode geoip file: %s%s", filename, ", fallback to the original ReadFile method")
|
||||
geoipBytes, err = ioutil.ReadFile(asset)
|
||||
geoipBytes, err = os.ReadFile(asset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -119,7 +119,7 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error)
|
||||
case errFailedToReadBytes, errFailedToReadExpectedLenBytes,
|
||||
errInvalidGeodataFile, errInvalidGeodataVarintLength:
|
||||
log.Warnln("failed to decode geoip file: %s%s", filename, ", fallback to the original ReadFile method")
|
||||
geositeBytes, err = ioutil.ReadFile(asset)
|
||||
geositeBytes, err = os.ReadFile(asset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1,120 +1,43 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
|
||||
"inet.af/netaddr"
|
||||
)
|
||||
|
||||
// CIDRList is an alias of []*CIDR to provide sort.Interface.
|
||||
type CIDRList []*CIDR
|
||||
|
||||
// Len implements sort.Interface.
|
||||
func (l *CIDRList) Len() int {
|
||||
return len(*l)
|
||||
}
|
||||
|
||||
// Less implements sort.Interface.
|
||||
func (l *CIDRList) Less(i int, j int) bool {
|
||||
ci := (*l)[i]
|
||||
cj := (*l)[j]
|
||||
|
||||
if len(ci.Ip) < len(cj.Ip) {
|
||||
return true
|
||||
}
|
||||
|
||||
if len(ci.Ip) > len(cj.Ip) {
|
||||
return false
|
||||
}
|
||||
|
||||
for k := 0; k < len(ci.Ip); k++ {
|
||||
if ci.Ip[k] < cj.Ip[k] {
|
||||
return true
|
||||
}
|
||||
|
||||
if ci.Ip[k] > cj.Ip[k] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return ci.Prefix < cj.Prefix
|
||||
}
|
||||
|
||||
// Swap implements sort.Interface.
|
||||
func (l *CIDRList) Swap(i int, j int) {
|
||||
(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
|
||||
}
|
||||
|
||||
type ipv6 struct {
|
||||
a uint64
|
||||
b uint64
|
||||
}
|
||||
|
||||
type GeoIPMatcher struct {
|
||||
countryCode string
|
||||
reverseMatch bool
|
||||
ip4 []uint32
|
||||
prefix4 []uint8
|
||||
ip6 []ipv6
|
||||
prefix6 []uint8
|
||||
}
|
||||
|
||||
func normalize4(ip uint32, prefix uint8) uint32 {
|
||||
return (ip >> (32 - prefix)) << (32 - prefix)
|
||||
}
|
||||
|
||||
func normalize6(ip ipv6, prefix uint8) ipv6 {
|
||||
if prefix <= 64 {
|
||||
ip.a = (ip.a >> (64 - prefix)) << (64 - prefix)
|
||||
ip.b = 0
|
||||
} else {
|
||||
ip.b = (ip.b >> (128 - prefix)) << (128 - prefix)
|
||||
}
|
||||
return ip
|
||||
ip4 *netaddr.IPSet
|
||||
ip6 *netaddr.IPSet
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) Init(cidrs []*CIDR) error {
|
||||
ip4Count := 0
|
||||
ip6Count := 0
|
||||
|
||||
var builder4, builder6 netaddr.IPSetBuilder
|
||||
for _, cidr := range cidrs {
|
||||
ip := cidr.Ip
|
||||
switch len(ip) {
|
||||
case 4:
|
||||
ip4Count++
|
||||
case 16:
|
||||
ip6Count++
|
||||
default:
|
||||
return fmt.Errorf("unexpect ip length: %d", len(ip))
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
cidrList := CIDRList(cidrs)
|
||||
sort.Sort(&cidrList)
|
||||
|
||||
m.ip4 = make([]uint32, 0, ip4Count)
|
||||
m.prefix4 = make([]uint8, 0, ip4Count)
|
||||
m.ip6 = make([]ipv6, 0, ip6Count)
|
||||
m.prefix6 = make([]uint8, 0, ip6Count)
|
||||
|
||||
for _, cidr := range cidrs {
|
||||
ip := cidr.Ip
|
||||
prefix := uint8(cidr.Prefix)
|
||||
switch len(ip) {
|
||||
case 4:
|
||||
m.ip4 = append(m.ip4, normalize4(binary.BigEndian.Uint32(ip), prefix))
|
||||
m.prefix4 = append(m.prefix4, prefix)
|
||||
case 16:
|
||||
ip6 := ipv6{
|
||||
a: binary.BigEndian.Uint64(ip[0:8]),
|
||||
b: binary.BigEndian.Uint64(ip[8:16]),
|
||||
}
|
||||
ip6 = normalize6(ip6, prefix)
|
||||
|
||||
m.ip6 = append(m.ip6, ip6)
|
||||
m.prefix6 = append(m.prefix6, prefix)
|
||||
}
|
||||
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
|
||||
@ -124,91 +47,35 @@ func (m *GeoIPMatcher) SetReverseMatch(isReverseMatch bool) {
|
||||
m.reverseMatch = isReverseMatch
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) match4(ip uint32) bool {
|
||||
if len(m.ip4) == 0 {
|
||||
func (m *GeoIPMatcher) match4(ip net.IP) bool {
|
||||
nip, ok := netaddr.FromStdIP(ip)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if ip < m.ip4[0] {
|
||||
return false
|
||||
}
|
||||
|
||||
size := uint32(len(m.ip4))
|
||||
l := uint32(0)
|
||||
r := size
|
||||
for l < r {
|
||||
x := ((l + r) >> 1)
|
||||
if ip < m.ip4[x] {
|
||||
r = x
|
||||
continue
|
||||
}
|
||||
|
||||
nip := normalize4(ip, m.prefix4[x])
|
||||
if nip == m.ip4[x] {
|
||||
return true
|
||||
}
|
||||
|
||||
l = x + 1
|
||||
}
|
||||
|
||||
return l > 0 && normalize4(ip, m.prefix4[l-1]) == m.ip4[l-1]
|
||||
return m.ip4.Contains(nip)
|
||||
}
|
||||
|
||||
func less6(a ipv6, b ipv6) bool {
|
||||
return a.a < b.a || (a.a == b.a && a.b < b.b)
|
||||
}
|
||||
|
||||
func (m *GeoIPMatcher) match6(ip ipv6) bool {
|
||||
if len(m.ip6) == 0 {
|
||||
func (m *GeoIPMatcher) match6(ip net.IP) bool {
|
||||
nip, ok := netaddr.FromStdIP(ip)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if less6(ip, m.ip6[0]) {
|
||||
return false
|
||||
}
|
||||
|
||||
size := uint32(len(m.ip6))
|
||||
l := uint32(0)
|
||||
r := size
|
||||
for l < r {
|
||||
x := (l + r) / 2
|
||||
if less6(ip, m.ip6[x]) {
|
||||
r = x
|
||||
continue
|
||||
}
|
||||
|
||||
if normalize6(ip, m.prefix6[x]) == m.ip6[x] {
|
||||
return true
|
||||
}
|
||||
|
||||
l = x + 1
|
||||
}
|
||||
|
||||
return l > 0 && normalize6(ip, m.prefix6[l-1]) == m.ip6[l-1]
|
||||
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 4:
|
||||
if m.reverseMatch {
|
||||
return !m.match4(binary.BigEndian.Uint32(ip))
|
||||
}
|
||||
return m.match4(binary.BigEndian.Uint32(ip))
|
||||
case 16:
|
||||
if m.reverseMatch {
|
||||
return !m.match6(ipv6{
|
||||
a: binary.BigEndian.Uint64(ip[0:8]),
|
||||
b: binary.BigEndian.Uint64(ip[8:16]),
|
||||
})
|
||||
}
|
||||
return m.match6(ipv6{
|
||||
a: binary.BigEndian.Uint64(ip[0:8]),
|
||||
b: binary.BigEndian.Uint64(ip[8:16]),
|
||||
})
|
||||
default:
|
||||
return false
|
||||
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.
|
||||
@ -219,7 +86,7 @@ type GeoIPMatcherContainer struct {
|
||||
// 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 len(geoip.CountryCode) > 0 {
|
||||
if geoip.CountryCode != "" {
|
||||
for _, m := range c.matchers {
|
||||
if m.countryCode == geoip.CountryCode && m.reverseMatch == geoip.ReverseMatch {
|
||||
return m, nil
|
||||
@ -234,7 +101,7 @@ func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
|
||||
if err := m.Init(geoip.Cidr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(geoip.CountryCode) > 0 {
|
||||
if geoip.CountryCode != "" {
|
||||
c.matchers = append(c.matchers, m)
|
||||
}
|
||||
return m, nil
|
||||
|
@ -2,7 +2,7 @@ package standard
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@ -19,7 +19,7 @@ func ReadFile(path string) ([]byte, error) {
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
return ioutil.ReadAll(reader)
|
||||
return io.ReadAll(reader)
|
||||
}
|
||||
|
||||
func ReadAsset(file string) ([]byte, error) {
|
||||
|
@ -1,22 +1,21 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/component/mmdb"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"github.com/Dreamacro/clash/rule/geodata"
|
||||
"github.com/Dreamacro/clash/rule/geodata/router"
|
||||
_ "github.com/Dreamacro/clash/rule/geodata/standard"
|
||||
//"github.com/Dreamacro/clash/rule/geodata"
|
||||
//"github.com/Dreamacro/clash/rule/geodata/router"
|
||||
//_ "github.com/Dreamacro/clash/rule/geodata/standard"
|
||||
)
|
||||
|
||||
type GEOIP struct {
|
||||
country string
|
||||
adapter string
|
||||
noResolveIP bool
|
||||
ruleExtra *C.RuleExtra
|
||||
geoIPMatcher *router.GeoIPMatcher
|
||||
country string
|
||||
adapter string
|
||||
noResolveIP bool
|
||||
ruleExtra *C.RuleExtra
|
||||
//geoIPMatcher *router.GeoIPMatcher
|
||||
}
|
||||
|
||||
func (g *GEOIP) RuleType() C.RuleType {
|
||||
@ -29,10 +28,11 @@ func (g *GEOIP) Match(metadata *C.Metadata) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if strings.EqualFold(g.country, "LAN") {
|
||||
if strings.EqualFold(g.country, "LAN") || C.TunBroadcastAddr.Equal(ip) {
|
||||
return ip.IsPrivate()
|
||||
}
|
||||
return g.geoIPMatcher.Match(ip)
|
||||
record, _ := mmdb.Instance().Country(ip)
|
||||
return strings.EqualFold(record.Country.IsoCode, g.country)
|
||||
}
|
||||
|
||||
func (g *GEOIP) Adapter() string {
|
||||
@ -51,39 +51,43 @@ func (g *GEOIP) RuleExtra() *C.RuleExtra {
|
||||
return g.ruleExtra
|
||||
}
|
||||
|
||||
func (g *GEOIP) GetCountry() string {
|
||||
return g.country
|
||||
}
|
||||
|
||||
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("[GeoIP] %s", err.Error())
|
||||
}
|
||||
|
||||
records, err := geoLoader.LoadGeoIP(strings.ReplaceAll(country, "!", ""))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[GeoIP] %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("[GeoIP] %s", err.Error())
|
||||
}
|
||||
|
||||
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, len(records))
|
||||
//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,
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
noResolveIP: noResolveIP,
|
||||
ruleExtra: ruleExtra,
|
||||
//geoIPMatcher: geoIPMatcher,
|
||||
}
|
||||
|
||||
return geoip, nil
|
||||
|
@ -52,12 +52,12 @@ func NewGEOSITE(country string, adapter string, ruleExtra *C.RuleExtra) (*GEOSIT
|
||||
//geoLoaderName := "memconservative"
|
||||
geoLoader, err := geodata.GetGeoDataLoader(geoLoaderName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[GeoSite] %s", err.Error())
|
||||
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
||||
}
|
||||
|
||||
domains, err := geoLoader.LoadGeoSite(country)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[GeoSite] %s", err.Error())
|
||||
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
||||
}
|
||||
|
||||
//linear: linear algorithm
|
||||
@ -66,7 +66,7 @@ func NewGEOSITE(country string, adapter string, ruleExtra *C.RuleExtra) (*GEOSIT
|
||||
//mph:minimal perfect hash algorithm
|
||||
matcher, err := router.NewMphMatcherGroup(domains)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[GeoSite] %s", err.Error())
|
||||
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
||||
}
|
||||
|
||||
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, len(domains))
|
||||
|
@ -40,6 +40,8 @@ func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
||||
parsed, parseErr = NewPort(payload, target, false, ruleExtra)
|
||||
case "PROCESS-NAME":
|
||||
parsed, parseErr = NewProcess(payload, target, ruleExtra)
|
||||
case "SCRIPT":
|
||||
parsed, parseErr = NewScript(payload, target)
|
||||
case "MATCH":
|
||||
parsed = NewMatch(target, ruleExtra)
|
||||
default:
|
||||
|
72
rule/script.go
Normal file
72
rule/script.go
Normal file
@ -0,0 +1,72 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
S "github.com/Dreamacro/clash/component/script"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Script struct {
|
||||
shortcut string
|
||||
adapter string
|
||||
shortcutFunction *S.PyObject
|
||||
}
|
||||
|
||||
func (s *Script) RuleType() C.RuleType {
|
||||
return C.Script
|
||||
}
|
||||
|
||||
func (s *Script) Match(metadata *C.Metadata) bool {
|
||||
rs, err := S.CallPyShortcut(s.shortcutFunction, metadata)
|
||||
if err != nil {
|
||||
log.Errorln("[Script] match rule error: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return rs
|
||||
}
|
||||
|
||||
func (s *Script) Adapter() string {
|
||||
return s.adapter
|
||||
}
|
||||
|
||||
func (s *Script) Payload() string {
|
||||
return s.shortcut
|
||||
}
|
||||
|
||||
func (s *Script) ShouldResolveIP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Script) RuleExtra() *C.RuleExtra {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewScript(shortcut string, adapter string) (*Script, error) {
|
||||
shortcut = strings.ToLower(shortcut)
|
||||
if !S.Py_IsInitialized() {
|
||||
return nil, fmt.Errorf("load script shortcut [%s] failure, can't find any shortcuts in the config file", shortcut)
|
||||
}
|
||||
|
||||
shortcutFunction, err := S.LoadShortcutFunction(shortcut)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't find script shortcut [%s] in the config file", shortcut)
|
||||
}
|
||||
|
||||
obj := &Script{
|
||||
shortcut: shortcut,
|
||||
adapter: adapter,
|
||||
shortcutFunction: shortcutFunction,
|
||||
}
|
||||
|
||||
runtime.SetFinalizer(obj, func(s *Script) {
|
||||
s.shortcutFunction.Clear()
|
||||
})
|
||||
|
||||
log.Infoln("Start initial script shortcut rule %s => %s", shortcut, adapter)
|
||||
|
||||
return obj, nil
|
||||
}
|
Reference in New Issue
Block a user