chore: 调整目录与包名一致
This commit is contained in:
88
rules/common/base.go
Normal file
88
rules/common/base.go
Normal file
@ -0,0 +1,88 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
var (
|
||||
errPayload = errors.New("payload error")
|
||||
initFlag bool
|
||||
noResolve = "no-resolve"
|
||||
)
|
||||
|
||||
type Base struct {
|
||||
ruleExtra *C.RuleExtra
|
||||
}
|
||||
|
||||
func (b *Base) RuleExtra() *C.RuleExtra {
|
||||
return b.ruleExtra
|
||||
}
|
||||
|
||||
func (b *Base) SetRuleExtra(re *C.RuleExtra) {
|
||||
b.ruleExtra = re
|
||||
}
|
||||
|
||||
func (b *Base) ShouldFindProcess() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (b *Base) ShouldResolveIP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func HasNoResolve(params []string) bool {
|
||||
for _, p := range params {
|
||||
if p == noResolve {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func FindNetwork(params []string) C.NetWork {
|
||||
for _, p := range params {
|
||||
if strings.EqualFold(p, "tcp") {
|
||||
return C.TCP
|
||||
} else if strings.EqualFold(p, "udp") {
|
||||
return C.UDP
|
||||
}
|
||||
}
|
||||
return C.ALLNet
|
||||
}
|
||||
|
||||
func FindSourceIPs(params []string) []*netip.Prefix {
|
||||
var ips []*netip.Prefix
|
||||
for _, p := range params {
|
||||
if p == noResolve || len(p) < 7 {
|
||||
continue
|
||||
}
|
||||
ipnet, err := netip.ParsePrefix(p)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ips = append(ips, &ipnet)
|
||||
}
|
||||
|
||||
if len(ips) > 0 {
|
||||
return ips
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FindProcessName(params []string) []string {
|
||||
var processNames []string
|
||||
for _, p := range params {
|
||||
if strings.HasPrefix(p, "P:") {
|
||||
processNames = append(processNames, strings.TrimPrefix(p, "P:"))
|
||||
}
|
||||
}
|
||||
|
||||
if len(processNames) > 0 {
|
||||
return processNames
|
||||
}
|
||||
return nil
|
||||
}
|
42
rules/common/domain.go
Normal file
42
rules/common/domain.go
Normal file
@ -0,0 +1,42 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type Domain struct {
|
||||
*Base
|
||||
domain string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func (d *Domain) RuleType() C.RuleType {
|
||||
return C.Domain
|
||||
}
|
||||
|
||||
func (d *Domain) Match(metadata *C.Metadata) bool {
|
||||
if metadata.AddrType != C.AtypDomainName {
|
||||
return false
|
||||
}
|
||||
return metadata.Host == d.domain
|
||||
}
|
||||
|
||||
func (d *Domain) Adapter() string {
|
||||
return d.adapter
|
||||
}
|
||||
|
||||
func (d *Domain) Payload() string {
|
||||
return d.domain
|
||||
}
|
||||
|
||||
func NewDomain(domain string, adapter string) *Domain {
|
||||
return &Domain{
|
||||
Base: &Base{},
|
||||
domain: strings.ToLower(domain),
|
||||
adapter: adapter,
|
||||
}
|
||||
}
|
||||
|
||||
var _ C.Rule = (*Domain)(nil)
|
43
rules/common/domain_keyword.go
Normal file
43
rules/common/domain_keyword.go
Normal file
@ -0,0 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type DomainKeyword struct {
|
||||
*Base
|
||||
keyword string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func (dk *DomainKeyword) RuleType() C.RuleType {
|
||||
return C.DomainKeyword
|
||||
}
|
||||
|
||||
func (dk *DomainKeyword) Match(metadata *C.Metadata) bool {
|
||||
if metadata.AddrType != C.AtypDomainName {
|
||||
return false
|
||||
}
|
||||
domain := metadata.Host
|
||||
return strings.Contains(domain, dk.keyword)
|
||||
}
|
||||
|
||||
func (dk *DomainKeyword) Adapter() string {
|
||||
return dk.adapter
|
||||
}
|
||||
|
||||
func (dk *DomainKeyword) Payload() string {
|
||||
return dk.keyword
|
||||
}
|
||||
|
||||
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
|
||||
return &DomainKeyword{
|
||||
Base: &Base{},
|
||||
keyword: strings.ToLower(keyword),
|
||||
adapter: adapter,
|
||||
}
|
||||
}
|
||||
|
||||
var _ C.Rule = (*DomainKeyword)(nil)
|
43
rules/common/domain_suffix.go
Normal file
43
rules/common/domain_suffix.go
Normal file
@ -0,0 +1,43 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type DomainSuffix struct {
|
||||
*Base
|
||||
suffix string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func (ds *DomainSuffix) RuleType() C.RuleType {
|
||||
return C.DomainSuffix
|
||||
}
|
||||
|
||||
func (ds *DomainSuffix) Match(metadata *C.Metadata) bool {
|
||||
if metadata.AddrType != C.AtypDomainName {
|
||||
return false
|
||||
}
|
||||
domain := metadata.Host
|
||||
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
|
||||
}
|
||||
|
||||
func (ds *DomainSuffix) Adapter() string {
|
||||
return ds.adapter
|
||||
}
|
||||
|
||||
func (ds *DomainSuffix) Payload() string {
|
||||
return ds.suffix
|
||||
}
|
||||
|
||||
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
|
||||
return &DomainSuffix{
|
||||
Base: &Base{},
|
||||
suffix: strings.ToLower(suffix),
|
||||
adapter: adapter,
|
||||
}
|
||||
}
|
||||
|
||||
var _ C.Rule = (*DomainSuffix)(nil)
|
35
rules/common/final.go
Normal file
35
rules/common/final.go
Normal file
@ -0,0 +1,35 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type Match struct {
|
||||
*Base
|
||||
adapter string
|
||||
}
|
||||
|
||||
func (f *Match) RuleType() C.RuleType {
|
||||
return C.MATCH
|
||||
}
|
||||
|
||||
func (f *Match) Match(metadata *C.Metadata) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (f *Match) Adapter() string {
|
||||
return f.adapter
|
||||
}
|
||||
|
||||
func (f *Match) Payload() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func NewMatch(adapter string) *Match {
|
||||
return &Match{
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
}
|
||||
}
|
||||
|
||||
var _ C.Rule = (*Match)(nil)
|
101
rules/common/geoip.go
Normal file
101
rules/common/geoip.go
Normal file
@ -0,0 +1,101 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/component/geodata"
|
||||
"github.com/Dreamacro/clash/component/geodata/router"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/component/mmdb"
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type GEOIP struct {
|
||||
*Base
|
||||
country string
|
||||
adapter string
|
||||
noResolveIP bool
|
||||
geoIPMatcher *router.GeoIPMatcher
|
||||
recodeSize int
|
||||
}
|
||||
|
||||
func (g *GEOIP) RuleType() C.RuleType {
|
||||
return C.GEOIP
|
||||
}
|
||||
|
||||
func (g *GEOIP) Match(metadata *C.Metadata) bool {
|
||||
ip := metadata.DstIP
|
||||
if !ip.IsValid() {
|
||||
return false
|
||||
}
|
||||
|
||||
if strings.EqualFold(g.country, "LAN") {
|
||||
return ip.IsPrivate() ||
|
||||
ip.IsUnspecified() ||
|
||||
ip.IsLoopback() ||
|
||||
ip.IsMulticast() ||
|
||||
ip.IsLinkLocalUnicast() ||
|
||||
resolver.IsFakeBroadcastIP(ip)
|
||||
}
|
||||
if !C.GeodataMode {
|
||||
record, _ := mmdb.Instance().Country(ip.AsSlice())
|
||||
return strings.EqualFold(record.Country.IsoCode, g.country)
|
||||
}
|
||||
return g.geoIPMatcher.Match(ip.AsSlice())
|
||||
}
|
||||
|
||||
func (g *GEOIP) Adapter() string {
|
||||
return g.adapter
|
||||
}
|
||||
|
||||
func (g *GEOIP) Payload() string {
|
||||
return g.country
|
||||
}
|
||||
|
||||
func (g *GEOIP) ShouldResolveIP() bool {
|
||||
return !g.noResolveIP
|
||||
}
|
||||
|
||||
func (g *GEOIP) GetCountry() string {
|
||||
return g.country
|
||||
}
|
||||
|
||||
func (g *GEOIP) GetIPMatcher() *router.GeoIPMatcher {
|
||||
return g.geoIPMatcher
|
||||
}
|
||||
|
||||
func (g *GEOIP) GetRecodeSize() int {
|
||||
return g.recodeSize
|
||||
}
|
||||
|
||||
func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error) {
|
||||
if !C.GeodataMode {
|
||||
geoip := &GEOIP{
|
||||
Base: &Base{},
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
noResolveIP: noResolveIP,
|
||||
}
|
||||
return geoip, nil
|
||||
}
|
||||
|
||||
geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("[GeoIP] %s", err.Error())
|
||||
}
|
||||
|
||||
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
|
||||
geoip := &GEOIP{
|
||||
Base: &Base{},
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
noResolveIP: noResolveIP,
|
||||
geoIPMatcher: geoIPMatcher,
|
||||
recodeSize: size,
|
||||
}
|
||||
return geoip, nil
|
||||
}
|
||||
|
||||
var _ C.Rule = (*GEOIP)(nil)
|
81
rules/common/geosite.go
Normal file
81
rules/common/geosite.go
Normal file
@ -0,0 +1,81 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/component/geodata"
|
||||
_ "github.com/Dreamacro/clash/component/geodata/memconservative"
|
||||
"github.com/Dreamacro/clash/component/geodata/router"
|
||||
_ "github.com/Dreamacro/clash/component/geodata/standard"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
//go:linkname initGeoSite github.com/Dreamacro/clash/config.initGeoSite
|
||||
func initGeoSite() error
|
||||
|
||||
type GEOSITE struct {
|
||||
*Base
|
||||
country string
|
||||
adapter string
|
||||
matcher *router.DomainMatcher
|
||||
recodeSize int
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) RuleType() C.RuleType {
|
||||
return C.GEOSITE
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) Match(metadata *C.Metadata) bool {
|
||||
if metadata.AddrType != C.AtypDomainName {
|
||||
return false
|
||||
}
|
||||
|
||||
domain := metadata.Host
|
||||
return gs.matcher.ApplyDomain(domain)
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) Adapter() string {
|
||||
return gs.adapter
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) Payload() string {
|
||||
return gs.country
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) GetDomainMatcher() *router.DomainMatcher {
|
||||
return gs.matcher
|
||||
}
|
||||
|
||||
func (gs *GEOSITE) GetRecodeSize() int {
|
||||
return gs.recodeSize
|
||||
}
|
||||
|
||||
func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
|
||||
if !initFlag {
|
||||
if err := initGeoSite(); err != nil {
|
||||
log.Errorln("can't initial GeoSite: %s", err)
|
||||
return nil, err
|
||||
}
|
||||
initFlag = true
|
||||
}
|
||||
|
||||
matcher, size, err := geodata.LoadGeoSiteMatcher(country)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
||||
}
|
||||
|
||||
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, size)
|
||||
|
||||
geoSite := &GEOSITE{
|
||||
Base: &Base{},
|
||||
country: country,
|
||||
adapter: adapter,
|
||||
matcher: matcher,
|
||||
recodeSize: size,
|
||||
}
|
||||
|
||||
return geoSite, nil
|
||||
}
|
||||
|
||||
var _ C.Rule = (*GEOSITE)(nil)
|
74
rules/common/in_type.go
Normal file
74
rules/common/in_type.go
Normal file
@ -0,0 +1,74 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type InType struct {
|
||||
*Base
|
||||
types []C.Type
|
||||
adapter string
|
||||
payload string
|
||||
}
|
||||
|
||||
func (u *InType) Match(metadata *C.Metadata) bool {
|
||||
for _, tp := range u.types {
|
||||
if metadata.Type == tp {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *InType) RuleType() C.RuleType {
|
||||
return C.INTYPE
|
||||
}
|
||||
|
||||
func (u *InType) Adapter() string {
|
||||
return u.adapter
|
||||
}
|
||||
|
||||
func (u *InType) Payload() string {
|
||||
return u.payload
|
||||
}
|
||||
|
||||
func NewInType(iTypes, adapter string) (*InType, error) {
|
||||
types := strings.Split(iTypes, "/")
|
||||
if len(types) == 0 {
|
||||
return nil, fmt.Errorf("in type could be empty")
|
||||
}
|
||||
|
||||
tps, err := parseInTypes(types)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &InType{
|
||||
Base: &Base{},
|
||||
types: tps,
|
||||
adapter: adapter,
|
||||
payload: strings.ToUpper(iTypes),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseInTypes(tps []string) (res []C.Type, err error) {
|
||||
for _, tp := range tps {
|
||||
utp := strings.ToUpper(tp)
|
||||
var r *C.Type
|
||||
if utp == "SOCKS" {
|
||||
r, _ = C.ParseType("SOCKS4")
|
||||
res = append(res, *r)
|
||||
r, _ = C.ParseType("SOCKS5")
|
||||
res = append(res, *r)
|
||||
} else {
|
||||
r, err = C.ParseType(utp)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = append(res, *r)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
77
rules/common/ipcidr.go
Normal file
77
rules/common/ipcidr.go
Normal file
@ -0,0 +1,77 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type IPCIDROption func(*IPCIDR)
|
||||
|
||||
func WithIPCIDRSourceIP(b bool) IPCIDROption {
|
||||
return func(i *IPCIDR) {
|
||||
i.isSourceIP = b
|
||||
}
|
||||
}
|
||||
|
||||
func WithIPCIDRNoResolve(noResolve bool) IPCIDROption {
|
||||
return func(i *IPCIDR) {
|
||||
i.noResolveIP = noResolve
|
||||
}
|
||||
}
|
||||
|
||||
type IPCIDR struct {
|
||||
*Base
|
||||
ipnet *netip.Prefix
|
||||
adapter string
|
||||
isSourceIP bool
|
||||
noResolveIP bool
|
||||
}
|
||||
|
||||
func (i *IPCIDR) RuleType() C.RuleType {
|
||||
if i.isSourceIP {
|
||||
return C.SrcIPCIDR
|
||||
}
|
||||
return C.IPCIDR
|
||||
}
|
||||
|
||||
func (i *IPCIDR) Match(metadata *C.Metadata) bool {
|
||||
ip := metadata.DstIP
|
||||
if i.isSourceIP {
|
||||
ip = metadata.SrcIP
|
||||
}
|
||||
return ip.IsValid() && i.ipnet.Contains(ip)
|
||||
}
|
||||
|
||||
func (i *IPCIDR) Adapter() string {
|
||||
return i.adapter
|
||||
}
|
||||
|
||||
func (i *IPCIDR) Payload() string {
|
||||
return i.ipnet.String()
|
||||
}
|
||||
|
||||
func (i *IPCIDR) ShouldResolveIP() bool {
|
||||
return !i.noResolveIP
|
||||
}
|
||||
|
||||
func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error) {
|
||||
ipnet, err := netip.ParsePrefix(s)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
ipcidr := &IPCIDR{
|
||||
Base: &Base{},
|
||||
ipnet: &ipnet,
|
||||
adapter: adapter,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(ipcidr)
|
||||
}
|
||||
|
||||
return ipcidr, nil
|
||||
}
|
||||
|
||||
var _ C.Rule = (*IPCIDR)(nil)
|
79
rules/common/ipsuffix.go
Normal file
79
rules/common/ipsuffix.go
Normal file
@ -0,0 +1,79 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
type IPSuffix struct {
|
||||
*Base
|
||||
ipBytes []byte
|
||||
bits int
|
||||
payload string
|
||||
adapter string
|
||||
isSourceIP bool
|
||||
noResolveIP bool
|
||||
}
|
||||
|
||||
func (is *IPSuffix) RuleType() C.RuleType {
|
||||
if is.isSourceIP {
|
||||
return C.SrcIPSuffix
|
||||
}
|
||||
return C.IPSuffix
|
||||
}
|
||||
|
||||
func (is *IPSuffix) Match(metadata *C.Metadata) bool {
|
||||
ip := metadata.DstIP
|
||||
if is.isSourceIP {
|
||||
ip = metadata.SrcIP
|
||||
}
|
||||
|
||||
mIPBytes := ip.AsSlice()
|
||||
if len(is.ipBytes) != len(mIPBytes) {
|
||||
return false
|
||||
}
|
||||
|
||||
size := len(mIPBytes)
|
||||
bits := is.bits
|
||||
|
||||
for i := bits / 8; i > 0; i-- {
|
||||
if is.ipBytes[size-i] != mIPBytes[size-i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if (is.ipBytes[size-bits/8-1] << (8 - bits%8)) != (mIPBytes[size-bits/8-1] << (8 - bits%8)) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (is *IPSuffix) Adapter() string {
|
||||
return is.adapter
|
||||
}
|
||||
|
||||
func (is *IPSuffix) Payload() string {
|
||||
return is.payload
|
||||
}
|
||||
|
||||
func (is *IPSuffix) ShouldResolveIP() bool {
|
||||
return !is.noResolveIP
|
||||
}
|
||||
|
||||
func NewIPSuffix(payload, adapter string, isSrc, noResolveIP bool) (*IPSuffix, error) {
|
||||
ipnet, err := netip.ParsePrefix(payload)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
return &IPSuffix{
|
||||
Base: &Base{},
|
||||
payload: payload,
|
||||
ipBytes: ipnet.Addr().AsSlice(),
|
||||
bits: ipnet.Bits(),
|
||||
adapter: adapter,
|
||||
isSourceIP: isSrc,
|
||||
noResolveIP: noResolveIP,
|
||||
}, nil
|
||||
}
|
49
rules/common/network_type.go
Normal file
49
rules/common/network_type.go
Normal file
@ -0,0 +1,49 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NetworkType struct {
|
||||
*Base
|
||||
network C.NetWork
|
||||
adapter string
|
||||
}
|
||||
|
||||
func NewNetworkType(network, adapter string) (*NetworkType, error) {
|
||||
ntType := NetworkType{
|
||||
Base: &Base{},
|
||||
}
|
||||
|
||||
ntType.adapter = adapter
|
||||
switch strings.ToUpper(network) {
|
||||
case "TCP":
|
||||
ntType.network = C.TCP
|
||||
break
|
||||
case "UDP":
|
||||
ntType.network = C.UDP
|
||||
break
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported network type, only TCP/UDP")
|
||||
}
|
||||
|
||||
return &ntType, nil
|
||||
}
|
||||
|
||||
func (n *NetworkType) RuleType() C.RuleType {
|
||||
return C.Network
|
||||
}
|
||||
|
||||
func (n *NetworkType) Match(metadata *C.Metadata) bool {
|
||||
return n.network == metadata.NetWork
|
||||
}
|
||||
|
||||
func (n *NetworkType) Adapter() string {
|
||||
return n.adapter
|
||||
}
|
||||
|
||||
func (n *NetworkType) Payload() string {
|
||||
return n.network.String()
|
||||
}
|
103
rules/common/port.go
Normal file
103
rules/common/port.go
Normal file
@ -0,0 +1,103 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type Port struct {
|
||||
*Base
|
||||
adapter string
|
||||
port string
|
||||
isSource bool
|
||||
portList []utils.Range[uint16]
|
||||
}
|
||||
|
||||
func (p *Port) RuleType() C.RuleType {
|
||||
if p.isSource {
|
||||
return C.SrcPort
|
||||
}
|
||||
return C.DstPort
|
||||
}
|
||||
|
||||
func (p *Port) Match(metadata *C.Metadata) bool {
|
||||
if p.isSource {
|
||||
return p.matchPortReal(metadata.SrcPort)
|
||||
}
|
||||
return p.matchPortReal(metadata.DstPort)
|
||||
}
|
||||
|
||||
func (p *Port) Adapter() string {
|
||||
return p.adapter
|
||||
}
|
||||
|
||||
func (p *Port) Payload() string {
|
||||
return p.port
|
||||
}
|
||||
|
||||
func (p *Port) matchPortReal(portRef string) bool {
|
||||
port, _ := strconv.Atoi(portRef)
|
||||
|
||||
for _, pr := range p.portList {
|
||||
if pr.Contains(uint16(port)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func NewPort(port string, adapter string, isSource bool) (*Port, error) {
|
||||
ports := strings.Split(port, "/")
|
||||
if len(ports) > 28 {
|
||||
return nil, fmt.Errorf("%s, too many ports to use, maximum support 28 ports", errPayload.Error())
|
||||
}
|
||||
|
||||
var portRange []utils.Range[uint16]
|
||||
for _, p := range ports {
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
subPorts := strings.Split(p, "-")
|
||||
subPortsLen := len(subPorts)
|
||||
if subPortsLen > 2 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
portStart, err := strconv.ParseUint(strings.Trim(subPorts[0], "[ ]"), 10, 16)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
switch subPortsLen {
|
||||
case 1:
|
||||
portRange = append(portRange, *utils.NewRange(uint16(portStart), uint16(portStart)))
|
||||
case 2:
|
||||
portEnd, err := strconv.ParseUint(strings.Trim(subPorts[1], "[ ]"), 10, 16)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
portRange = append(portRange, *utils.NewRange(uint16(portStart), uint16(portEnd)))
|
||||
}
|
||||
}
|
||||
|
||||
if len(portRange) == 0 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
return &Port{
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
port: port,
|
||||
isSource: isSource,
|
||||
portList: portRange,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var _ C.Rule = (*Port)(nil)
|
42
rules/common/process.go
Normal file
42
rules/common/process.go
Normal file
@ -0,0 +1,42 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type Process struct {
|
||||
*Base
|
||||
adapter string
|
||||
process string
|
||||
nameOnly bool
|
||||
}
|
||||
|
||||
func (ps *Process) RuleType() C.RuleType {
|
||||
return C.Process
|
||||
}
|
||||
|
||||
func (ps *Process) Match(metadata *C.Metadata) bool {
|
||||
if ps.nameOnly {
|
||||
return strings.EqualFold(metadata.Process, ps.process)
|
||||
}
|
||||
return strings.EqualFold(metadata.ProcessPath, ps.process)
|
||||
}
|
||||
|
||||
func (ps *Process) Adapter() string {
|
||||
return ps.adapter
|
||||
}
|
||||
|
||||
func (ps *Process) Payload() string {
|
||||
return ps.process
|
||||
}
|
||||
|
||||
func NewProcess(process string, adapter string, nameOnly bool) (*Process, error) {
|
||||
return &Process{
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
process: process,
|
||||
nameOnly: nameOnly,
|
||||
}, nil
|
||||
}
|
103
rules/common/uid.go
Normal file
103
rules/common/uid.go
Normal file
@ -0,0 +1,103 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/common/utils"
|
||||
"github.com/Dreamacro/clash/component/process"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Uid struct {
|
||||
*Base
|
||||
uids []utils.Range[int32]
|
||||
oUid string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func NewUid(oUid, adapter string) (*Uid, error) {
|
||||
//if len(_uids) > 28 {
|
||||
// return nil, fmt.Errorf("%s, too many uid to use, maximum support 28 uid", errPayload.Error())
|
||||
//}
|
||||
if !(runtime.GOOS == "linux" || runtime.GOOS == "android") {
|
||||
return nil, fmt.Errorf("uid rule not support this platform")
|
||||
}
|
||||
|
||||
var uidRange []utils.Range[int32]
|
||||
for _, u := range strings.Split(oUid, "/") {
|
||||
if u == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
subUids := strings.Split(u, "-")
|
||||
subUidsLen := len(subUids)
|
||||
if subUidsLen > 2 {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
uidStart, err := strconv.ParseUint(strings.Trim(subUids[0], "[ ]"), 10, 32)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
switch subUidsLen {
|
||||
case 1:
|
||||
uidRange = append(uidRange, *utils.NewRange(int32(uidStart), int32(uidStart)))
|
||||
case 2:
|
||||
uidEnd, err := strconv.ParseUint(strings.Trim(subUids[1], "[ ]"), 10, 32)
|
||||
if err != nil {
|
||||
return nil, errPayload
|
||||
}
|
||||
|
||||
uidRange = append(uidRange, *utils.NewRange(int32(uidStart), int32(uidEnd)))
|
||||
}
|
||||
}
|
||||
|
||||
if len(uidRange) == 0 {
|
||||
return nil, errPayload
|
||||
}
|
||||
return &Uid{
|
||||
Base: &Base{},
|
||||
adapter: adapter,
|
||||
oUid: oUid,
|
||||
uids: uidRange,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (u *Uid) RuleType() C.RuleType {
|
||||
return C.Uid
|
||||
}
|
||||
|
||||
func (u *Uid) Match(metadata *C.Metadata) bool {
|
||||
srcPort, err := strconv.Atoi(metadata.SrcPort)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
var uid int32
|
||||
if metadata.Uid != nil {
|
||||
uid = *metadata.Uid
|
||||
} else if uid, err = process.FindUid(metadata.NetWork.String(), metadata.SrcIP, srcPort); err == nil {
|
||||
metadata.Uid = &uid
|
||||
} else {
|
||||
log.Warnln("[UID] could not get uid from %s", metadata.String())
|
||||
return false
|
||||
}
|
||||
|
||||
for _, _uid := range u.uids {
|
||||
if _uid.Contains(uid) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *Uid) Adapter() string {
|
||||
return u.adapter
|
||||
}
|
||||
|
||||
func (u *Uid) Payload() string {
|
||||
return u.oUid
|
||||
}
|
67
rules/logic/and.go
Normal file
67
rules/logic/and.go
Normal file
@ -0,0 +1,67 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/rules/common"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AND struct {
|
||||
*common.Base
|
||||
rules []C.Rule
|
||||
payload string
|
||||
adapter string
|
||||
needIP bool
|
||||
}
|
||||
|
||||
func (A *AND) ShouldFindProcess() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func NewAND(payload string, adapter string) (*AND, error) {
|
||||
and := &AND{Base: &common.Base{}, payload: payload, adapter: adapter}
|
||||
rules, err := parseRuleByPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
and.rules = rules
|
||||
payloads := make([]string, 0, len(rules))
|
||||
for _, rule := range rules {
|
||||
payloads = append(payloads, fmt.Sprintf("(%s,%s)", rule.RuleType().String(), rule.Payload()))
|
||||
if rule.ShouldResolveIP() {
|
||||
and.needIP = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
and.payload = fmt.Sprintf("(%s)", strings.Join(payloads, " && "))
|
||||
return and, nil
|
||||
}
|
||||
|
||||
func (A *AND) RuleType() C.RuleType {
|
||||
return C.AND
|
||||
}
|
||||
|
||||
func (A *AND) Match(metadata *C.Metadata) bool {
|
||||
for _, rule := range A.rules {
|
||||
if !rule.Match(metadata) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (A *AND) Adapter() string {
|
||||
return A.adapter
|
||||
}
|
||||
|
||||
func (A *AND) Payload() string {
|
||||
return A.payload
|
||||
}
|
||||
|
||||
func (A *AND) ShouldResolveIP() bool {
|
||||
return A.needIP
|
||||
}
|
131
rules/logic/common.go
Normal file
131
rules/logic/common.go
Normal file
@ -0,0 +1,131 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/common/collections"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"regexp"
|
||||
"strings"
|
||||
_ "unsafe"
|
||||
)
|
||||
|
||||
//go:linkname parseRule github.com/Dreamacro/clash/rules.ParseRule
|
||||
func parseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)
|
||||
|
||||
func parseRuleByPayload(payload string) ([]C.Rule, error) {
|
||||
regex, err := regexp.Compile("\\(.*\\)")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if regex.MatchString(payload) {
|
||||
subAllRanges, err := format(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rules := make([]C.Rule, 0, len(subAllRanges))
|
||||
|
||||
subRanges := findSubRuleRange(payload, subAllRanges)
|
||||
for _, subRange := range subRanges {
|
||||
subPayload := payload[subRange.start+1 : subRange.end]
|
||||
|
||||
rule, err := payloadToRule(subPayload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("payload format error")
|
||||
}
|
||||
|
||||
func containRange(r Range, preStart, preEnd int) bool {
|
||||
return preStart < r.start && preEnd > r.end
|
||||
}
|
||||
|
||||
func payloadToRule(subPayload string) (C.Rule, error) {
|
||||
splitStr := strings.SplitN(subPayload, ",", 2)
|
||||
if len(splitStr) < 2 {
|
||||
return nil, fmt.Errorf("[%s] format is error", subPayload)
|
||||
}
|
||||
|
||||
tp := splitStr[0]
|
||||
payload := splitStr[1]
|
||||
if tp == "NOT" || tp == "OR" || tp == "AND" {
|
||||
return parseRule(tp, payload, "", nil)
|
||||
}
|
||||
param := strings.Split(payload, ",")
|
||||
return parseRule(tp, param[0], "", param[1:])
|
||||
}
|
||||
|
||||
type Range struct {
|
||||
start int
|
||||
end int
|
||||
index int
|
||||
}
|
||||
|
||||
func format(payload string) ([]Range, error) {
|
||||
stack := collections.NewStack()
|
||||
num := 0
|
||||
subRanges := make([]Range, 0)
|
||||
for i, c := range payload {
|
||||
if c == '(' {
|
||||
sr := Range{
|
||||
start: i,
|
||||
index: num,
|
||||
}
|
||||
|
||||
num++
|
||||
stack.Push(sr)
|
||||
} else if c == ')' {
|
||||
if stack.Len() == 0 {
|
||||
return nil, fmt.Errorf("missing '('")
|
||||
}
|
||||
|
||||
sr := stack.Pop().(Range)
|
||||
sr.end = i
|
||||
subRanges = append(subRanges, sr)
|
||||
}
|
||||
}
|
||||
|
||||
if stack.Len() != 0 {
|
||||
return nil, fmt.Errorf("format error is missing )")
|
||||
}
|
||||
|
||||
sortResult := make([]Range, len(subRanges))
|
||||
for _, sr := range subRanges {
|
||||
sortResult[sr.index] = sr
|
||||
}
|
||||
|
||||
return sortResult, nil
|
||||
}
|
||||
|
||||
func findSubRuleRange(payload string, ruleRanges []Range) []Range {
|
||||
payloadLen := len(payload)
|
||||
subRuleRange := make([]Range, 0)
|
||||
for _, rr := range ruleRanges {
|
||||
if rr.start == 0 && rr.end == payloadLen-1 {
|
||||
// 最大范围跳过
|
||||
continue
|
||||
}
|
||||
|
||||
containInSub := false
|
||||
for _, r := range subRuleRange {
|
||||
if containRange(rr, r.start, r.end) {
|
||||
// The subRuleRange contains a range of rr, which is the next level node of the tree
|
||||
containInSub = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !containInSub {
|
||||
subRuleRange = append(subRuleRange, rr)
|
||||
}
|
||||
}
|
||||
|
||||
return subRuleRange
|
||||
}
|
49
rules/logic/logic_test.go
Normal file
49
rules/logic/logic_test.go
Normal file
@ -0,0 +1,49 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"github.com/Dreamacro/clash/constant"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAND(t *testing.T) {
|
||||
and, err := NewAND("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "DIRECT", and.adapter)
|
||||
assert.Equal(t, false, and.ShouldResolveIP())
|
||||
assert.Equal(t, true, and.Match(&constant.Metadata{
|
||||
Host: "baidu.com",
|
||||
AddrType: constant.AtypDomainName,
|
||||
NetWork: constant.TCP,
|
||||
DstPort: "20000",
|
||||
}))
|
||||
|
||||
and, err = NewAND("(DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT")
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
and, err = NewAND("((AND,(DOMAIN,baidu.com),(NETWORK,TCP)),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT")
|
||||
assert.Equal(t, nil, err)
|
||||
}
|
||||
|
||||
func TestNOT(t *testing.T) {
|
||||
not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, false, not.Match(&constant.Metadata{
|
||||
DstPort: "6100",
|
||||
}))
|
||||
|
||||
_, err = NewNOT("((DST-PORT,5600-6666),(DOMAIN,baidu.com))", "DIRECT")
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
_, err = NewNOT("(())", "DIRECT")
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestOR(t *testing.T) {
|
||||
or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT")
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, true, or.Match(&constant.Metadata{
|
||||
NetWork: constant.TCP,
|
||||
}))
|
||||
assert.Equal(t, false, or.ShouldResolveIP())
|
||||
}
|
55
rules/logic/not.go
Normal file
55
rules/logic/not.go
Normal file
@ -0,0 +1,55 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/rules/common"
|
||||
)
|
||||
|
||||
type NOT struct {
|
||||
*common.Base
|
||||
rule C.Rule
|
||||
payload string
|
||||
adapter string
|
||||
}
|
||||
|
||||
func (not *NOT) ShouldFindProcess() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func NewNOT(payload string, adapter string) (*NOT, error) {
|
||||
not := &NOT{Base: &common.Base{}, adapter: adapter}
|
||||
rule, err := parseRuleByPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(rule) != 1 {
|
||||
return nil, fmt.Errorf("not rule must contain one rule")
|
||||
}
|
||||
|
||||
not.rule = rule[0]
|
||||
not.payload = fmt.Sprintf("(!(%s,%s))", rule[0].RuleType(), rule[0].Payload())
|
||||
|
||||
return not, nil
|
||||
}
|
||||
|
||||
func (not *NOT) RuleType() C.RuleType {
|
||||
return C.NOT
|
||||
}
|
||||
|
||||
func (not *NOT) Match(metadata *C.Metadata) bool {
|
||||
return not.rule == nil || !not.rule.Match(metadata)
|
||||
}
|
||||
|
||||
func (not *NOT) Adapter() string {
|
||||
return not.adapter
|
||||
}
|
||||
|
||||
func (not *NOT) Payload() string {
|
||||
return not.payload
|
||||
}
|
||||
|
||||
func (not *NOT) ShouldResolveIP() bool {
|
||||
return not.rule != nil && not.rule.ShouldResolveIP()
|
||||
}
|
67
rules/logic/or.go
Normal file
67
rules/logic/or.go
Normal file
@ -0,0 +1,67 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/rules/common"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type OR struct {
|
||||
*common.Base
|
||||
rules []C.Rule
|
||||
payload string
|
||||
adapter string
|
||||
needIP bool
|
||||
}
|
||||
|
||||
func (or *OR) ShouldFindProcess() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (or *OR) RuleType() C.RuleType {
|
||||
return C.OR
|
||||
}
|
||||
|
||||
func (or *OR) Match(metadata *C.Metadata) bool {
|
||||
for _, rule := range or.rules {
|
||||
if rule.Match(metadata) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (or *OR) Adapter() string {
|
||||
return or.adapter
|
||||
}
|
||||
|
||||
func (or *OR) Payload() string {
|
||||
return or.payload
|
||||
}
|
||||
|
||||
func (or *OR) ShouldResolveIP() bool {
|
||||
return or.needIP
|
||||
}
|
||||
|
||||
func NewOR(payload string, adapter string) (*OR, error) {
|
||||
or := &OR{Base: &common.Base{}, payload: payload, adapter: adapter}
|
||||
rules, err := parseRuleByPayload(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
or.rules = rules
|
||||
payloads := make([]string, 0, len(rules))
|
||||
for _, rule := range rules {
|
||||
payloads = append(payloads, fmt.Sprintf("(%s,%s)", rule.RuleType(), rule.Payload()))
|
||||
if rule.ShouldResolveIP() {
|
||||
or.needIP = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
or.payload = fmt.Sprintf("(%s)", strings.Join(payloads, " || "))
|
||||
return or, nil
|
||||
}
|
42
rules/parser.go
Normal file
42
rules/parser.go
Normal file
@ -0,0 +1,42 @@
|
||||
package rules
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
RC "github.com/Dreamacro/clash/rules/common"
|
||||
"github.com/Dreamacro/clash/rules/logic"
|
||||
RP "github.com/Dreamacro/clash/rules/provider"
|
||||
"github.com/Dreamacro/clash/rules/ruleparser"
|
||||
)
|
||||
|
||||
func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
||||
switch tp {
|
||||
case "AND":
|
||||
parsed, parseErr = logic.NewAND(payload, target)
|
||||
case "OR":
|
||||
parsed, parseErr = logic.NewOR(payload, target)
|
||||
case "NOT":
|
||||
parsed, parseErr = logic.NewNOT(payload, target)
|
||||
case "RULE-SET":
|
||||
noResolve := RC.HasNoResolve(params)
|
||||
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
|
||||
case "MATCH":
|
||||
parsed = RC.NewMatch(target)
|
||||
parseErr = nil
|
||||
default:
|
||||
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
|
||||
}
|
||||
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
|
||||
ruleExtra := &C.RuleExtra{
|
||||
Network: RC.FindNetwork(params),
|
||||
SourceIPs: RC.FindSourceIPs(params),
|
||||
ProcessNames: RC.FindProcessName(params),
|
||||
}
|
||||
|
||||
parsed.SetRuleExtra(ruleExtra)
|
||||
|
||||
return
|
||||
}
|
51
rules/provider/classical_strategy.go
Normal file
51
rules/provider/classical_strategy.go
Normal file
@ -0,0 +1,51 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type classicalStrategy struct {
|
||||
rules []C.Rule
|
||||
count int
|
||||
shouldResolveIP bool
|
||||
}
|
||||
|
||||
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
|
||||
for _, rule := range c.rules {
|
||||
if rule.Match(metadata) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *classicalStrategy) Count() int {
|
||||
return c.count
|
||||
}
|
||||
|
||||
func (c *classicalStrategy) ShouldResolveIP() bool {
|
||||
return c.shouldResolveIP
|
||||
}
|
||||
|
||||
func (c *classicalStrategy) OnUpdate(rules []string) {
|
||||
for _, rawRule := range rules {
|
||||
ruleType, rule, params := ruleParse(rawRule)
|
||||
r, err := parseRule(ruleType, rule, "", params)
|
||||
if err != nil {
|
||||
log.Warnln("parse rule error:[%s]", err.Error())
|
||||
} else {
|
||||
if !c.shouldResolveIP {
|
||||
c.shouldResolveIP = r.ShouldResolveIP()
|
||||
}
|
||||
|
||||
c.rules = append(c.rules, r)
|
||||
c.count++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewClassicalStrategy() *classicalStrategy {
|
||||
return &classicalStrategy{rules: []C.Rule{}}
|
||||
}
|
58
rules/provider/domain_strategy.go
Normal file
58
rules/provider/domain_strategy.go
Normal file
@ -0,0 +1,58 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type domainStrategy struct {
|
||||
count int
|
||||
domainRules *trie.DomainTrie[bool]
|
||||
}
|
||||
|
||||
func (d *domainStrategy) Match(metadata *C.Metadata) bool {
|
||||
return d.domainRules != nil && d.domainRules.Search(metadata.Host) != nil
|
||||
}
|
||||
|
||||
func (d *domainStrategy) Count() int {
|
||||
return d.count
|
||||
}
|
||||
|
||||
func (d *domainStrategy) ShouldResolveIP() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *domainStrategy) OnUpdate(rules []string) {
|
||||
domainTrie := trie.New[bool]()
|
||||
count := 0
|
||||
for _, rule := range rules {
|
||||
err := domainTrie.Insert(rule, true)
|
||||
if err != nil {
|
||||
log.Warnln("invalid domain:[%s]", rule)
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
d.domainRules = domainTrie
|
||||
d.count = count
|
||||
}
|
||||
|
||||
func ruleParse(ruleRaw string) (string, string, []string) {
|
||||
item := strings.Split(ruleRaw, ",")
|
||||
if len(item) == 1 {
|
||||
return "", item[0], nil
|
||||
} else if len(item) == 2 {
|
||||
return item[0], item[1], nil
|
||||
} else if len(item) > 2 {
|
||||
return item[0], item[1], item[2:]
|
||||
}
|
||||
|
||||
return "", "", nil
|
||||
}
|
||||
|
||||
func NewDomainStrategy() *domainStrategy {
|
||||
return &domainStrategy{}
|
||||
}
|
206
rules/provider/fetcher.go
Normal file
206
rules/provider/fetcher.go
Normal file
@ -0,0 +1,206 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
P "github.com/Dreamacro/clash/constant/provider"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
fileMode os.FileMode = 0666
|
||||
dirMode os.FileMode = 0755
|
||||
)
|
||||
|
||||
type parser = func([]byte) (interface{}, error)
|
||||
|
||||
type fetcher struct {
|
||||
name string
|
||||
vehicle P.Vehicle
|
||||
updatedAt *time.Time
|
||||
ticker *time.Ticker
|
||||
done chan struct{}
|
||||
hash [16]byte
|
||||
parser parser
|
||||
onUpdate func(interface{}) error
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
func (f *fetcher) Name() string {
|
||||
return f.name
|
||||
}
|
||||
|
||||
func (f *fetcher) VehicleType() P.VehicleType {
|
||||
return f.vehicle.Type()
|
||||
}
|
||||
|
||||
func (f *fetcher) Initial() (interface{}, error) {
|
||||
var (
|
||||
buf []byte
|
||||
hasLocal bool
|
||||
err error
|
||||
)
|
||||
|
||||
defer func() {
|
||||
if f.ticker != nil {
|
||||
go f.pullLoop()
|
||||
}
|
||||
}()
|
||||
|
||||
if stat, fErr := os.Stat(f.vehicle.Path()); fErr == nil {
|
||||
buf, err = ioutil.ReadFile(f.vehicle.Path())
|
||||
modTime := stat.ModTime()
|
||||
f.updatedAt = &modTime
|
||||
hasLocal = true
|
||||
if f.interval != 0 && modTime.Add(f.interval).Before(time.Now()) {
|
||||
defer func() {
|
||||
log.Infoln("[Provider] %s's rules not updated for a long time, force refresh", f.Name())
|
||||
go f.update()
|
||||
}()
|
||||
}
|
||||
} else {
|
||||
buf, err = f.vehicle.Read()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rules, err := f.parser(buf)
|
||||
if err != nil {
|
||||
if !hasLocal {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf, err = f.vehicle.Read()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rules, err = f.parser(buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hasLocal = false
|
||||
}
|
||||
|
||||
if f.vehicle.Type() != P.File && !hasLocal {
|
||||
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
f.hash = md5.Sum(buf)
|
||||
|
||||
return rules, nil
|
||||
}
|
||||
|
||||
func (f *fetcher) Update() (interface{}, bool, error) {
|
||||
buf, err := f.vehicle.Read()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
hash := md5.Sum(buf)
|
||||
if bytes.Equal(f.hash[:], hash[:]) {
|
||||
f.updatedAt = &now
|
||||
os.Chtimes(f.vehicle.Path(), now, now)
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
rules, err := f.parser(buf)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if f.vehicle.Type() != P.File {
|
||||
if err := safeWrite(f.vehicle.Path(), buf); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
}
|
||||
|
||||
f.updatedAt = &now
|
||||
f.hash = hash
|
||||
|
||||
return rules, false, nil
|
||||
}
|
||||
|
||||
func (f *fetcher) Destroy() error {
|
||||
if f.ticker != nil {
|
||||
f.done <- struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newFetcher(name string, interval time.Duration, vehicle P.Vehicle, parser parser, onUpdate func(interface{}) error) *fetcher {
|
||||
var ticker *time.Ticker
|
||||
if interval != 0 {
|
||||
ticker = time.NewTicker(interval)
|
||||
}
|
||||
|
||||
return &fetcher{
|
||||
name: name,
|
||||
ticker: ticker,
|
||||
vehicle: vehicle,
|
||||
parser: parser,
|
||||
done: make(chan struct{}, 1),
|
||||
onUpdate: onUpdate,
|
||||
interval: interval,
|
||||
}
|
||||
}
|
||||
|
||||
func safeWrite(path string, buf []byte) error {
|
||||
dir := filepath.Dir(path)
|
||||
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(dir, dirMode); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return ioutil.WriteFile(path, buf, fileMode)
|
||||
}
|
||||
|
||||
func (f *fetcher) pullLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-f.ticker.C:
|
||||
same, err := f.update()
|
||||
if same || err != nil {
|
||||
continue
|
||||
}
|
||||
case <-f.done:
|
||||
f.ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fetcher) update() (same bool, err error) {
|
||||
elm, same, err := f.Update()
|
||||
if err != nil {
|
||||
log.Warnln("[Provider] %s pull error: %s", f.Name(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if same {
|
||||
log.Debugln("[Provider] %s's rules doesn't change", f.Name())
|
||||
return
|
||||
}
|
||||
|
||||
log.Infoln("[Provider] %s's rules update", f.Name())
|
||||
if f.onUpdate != nil {
|
||||
err := f.onUpdate(elm)
|
||||
if err != nil {
|
||||
log.Infoln("[Provider] %s update failed", f.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
46
rules/provider/ipcidr_strategy.go
Normal file
46
rules/provider/ipcidr_strategy.go
Normal file
@ -0,0 +1,46 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type ipcidrStrategy struct {
|
||||
count int
|
||||
shouldResolveIP bool
|
||||
trie *trie.IpCidrTrie
|
||||
}
|
||||
|
||||
func (i *ipcidrStrategy) Match(metadata *C.Metadata) bool {
|
||||
return i.trie != nil && i.trie.IsContain(metadata.DstIP.AsSlice())
|
||||
}
|
||||
|
||||
func (i *ipcidrStrategy) Count() int {
|
||||
return i.count
|
||||
}
|
||||
|
||||
func (i *ipcidrStrategy) ShouldResolveIP() bool {
|
||||
return i.shouldResolveIP
|
||||
}
|
||||
|
||||
func (i *ipcidrStrategy) OnUpdate(rules []string) {
|
||||
ipCidrTrie := trie.NewIpCidrTrie()
|
||||
count := 0
|
||||
for _, rule := range rules {
|
||||
err := ipCidrTrie.AddIpCidrForString(rule)
|
||||
if err != nil {
|
||||
log.Warnln("invalid Ipcidr:[%s]", rule)
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
||||
i.trie = ipCidrTrie
|
||||
i.count = count
|
||||
i.shouldResolveIP = i.count > 0
|
||||
}
|
||||
|
||||
func NewIPCidrStrategy() *ipcidrStrategy {
|
||||
return &ipcidrStrategy{}
|
||||
}
|
67
rules/provider/parse.go
Normal file
67
rules/provider/parse.go
Normal file
@ -0,0 +1,67 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/adapter/provider"
|
||||
"github.com/Dreamacro/clash/common/structure"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
P "github.com/Dreamacro/clash/constant/provider"
|
||||
RC "github.com/Dreamacro/clash/rules/common"
|
||||
"github.com/Dreamacro/clash/rules/ruleparser"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ruleProviderSchema struct {
|
||||
Type string `provider:"type"`
|
||||
Behavior string `provider:"behavior"`
|
||||
Path string `provider:"path"`
|
||||
URL string `provider:"url,omitempty"`
|
||||
Interval int `provider:"interval,omitempty"`
|
||||
}
|
||||
|
||||
func ParseRuleProvider(name string, mapping map[string]interface{}) (P.RuleProvider, error) {
|
||||
schema := &ruleProviderSchema{}
|
||||
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
||||
if err := decoder.Decode(mapping, schema); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var behavior P.RuleType
|
||||
|
||||
switch schema.Behavior {
|
||||
case "domain":
|
||||
behavior = P.Domain
|
||||
case "ipcidr":
|
||||
behavior = P.IPCIDR
|
||||
case "classical":
|
||||
behavior = P.Classical
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported behavior type: %s", schema.Behavior)
|
||||
}
|
||||
|
||||
path := C.Path.Resolve(schema.Path)
|
||||
var vehicle P.Vehicle
|
||||
switch schema.Type {
|
||||
case "file":
|
||||
vehicle = provider.NewFileVehicle(path)
|
||||
case "http":
|
||||
vehicle = provider.NewHTTPVehicle(schema.URL, path)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported vehicle type: %s", schema.Type)
|
||||
}
|
||||
|
||||
return NewRuleSetProvider(name, behavior, time.Duration(uint(schema.Interval))*time.Second, vehicle), nil
|
||||
}
|
||||
|
||||
func parseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
||||
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
|
||||
|
||||
if parseErr != nil {
|
||||
return nil, parseErr
|
||||
}
|
||||
ruleExtra := &C.RuleExtra{
|
||||
Network: RC.FindNetwork(params),
|
||||
SourceIPs: RC.FindSourceIPs(params),
|
||||
}
|
||||
parsed.SetRuleExtra(ruleExtra)
|
||||
return parsed, parseErr
|
||||
}
|
150
rules/provider/provider.go
Normal file
150
rules/provider/provider.go
Normal file
@ -0,0 +1,150 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
P "github.com/Dreamacro/clash/constant/provider"
|
||||
"gopkg.in/yaml.v2"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ruleProviders = map[string]P.RuleProvider{}
|
||||
)
|
||||
|
||||
type ruleSetProvider struct {
|
||||
*fetcher
|
||||
behavior P.RuleType
|
||||
strategy ruleStrategy
|
||||
}
|
||||
|
||||
type RuleSetProvider struct {
|
||||
*ruleSetProvider
|
||||
}
|
||||
|
||||
type RulePayload struct {
|
||||
/**
|
||||
key: Domain or IP Cidr
|
||||
value: Rule type or is empty
|
||||
*/
|
||||
Rules []string `yaml:"payload"`
|
||||
Rules2 []string `yaml:"rules"`
|
||||
}
|
||||
|
||||
type ruleStrategy interface {
|
||||
Match(metadata *C.Metadata) bool
|
||||
Count() int
|
||||
ShouldResolveIP() bool
|
||||
OnUpdate(rules []string)
|
||||
}
|
||||
|
||||
func RuleProviders() map[string]P.RuleProvider {
|
||||
return ruleProviders
|
||||
}
|
||||
|
||||
func SetRuleProvider(ruleProvider P.RuleProvider) {
|
||||
if ruleProvider != nil {
|
||||
ruleProviders[(ruleProvider).Name()] = ruleProvider
|
||||
}
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Type() P.ProviderType {
|
||||
return P.Rule
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Initial() error {
|
||||
elm, err := rp.fetcher.Initial()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return rp.fetcher.onUpdate(elm)
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Update() error {
|
||||
elm, same, err := rp.fetcher.Update()
|
||||
if err == nil && !same {
|
||||
return rp.fetcher.onUpdate(elm)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Behavior() P.RuleType {
|
||||
return rp.behavior
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) Match(metadata *C.Metadata) bool {
|
||||
return rp.strategy != nil && rp.strategy.Match(metadata)
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) ShouldResolveIP() bool {
|
||||
return rp.strategy.ShouldResolveIP()
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) AsRule(adaptor string) C.Rule {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (rp *ruleSetProvider) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(
|
||||
map[string]interface{}{
|
||||
"behavior": rp.behavior.String(),
|
||||
"name": rp.Name(),
|
||||
"ruleCount": rp.strategy.Count(),
|
||||
"type": rp.Type().String(),
|
||||
"updatedAt": rp.updatedAt,
|
||||
"vehicleType": rp.VehicleType().String(),
|
||||
})
|
||||
}
|
||||
|
||||
func NewRuleSetProvider(name string, behavior P.RuleType, interval time.Duration, vehicle P.Vehicle) P.RuleProvider {
|
||||
rp := &ruleSetProvider{
|
||||
behavior: behavior,
|
||||
}
|
||||
|
||||
onUpdate := func(elm interface{}) error {
|
||||
rulesRaw := elm.([]string)
|
||||
rp.strategy.OnUpdate(rulesRaw)
|
||||
return nil
|
||||
}
|
||||
|
||||
fetcher := newFetcher(name, interval, vehicle, rulesParse, onUpdate)
|
||||
rp.fetcher = fetcher
|
||||
rp.strategy = newStrategy(behavior)
|
||||
|
||||
wrapper := &RuleSetProvider{
|
||||
rp,
|
||||
}
|
||||
|
||||
final := func(provider *RuleSetProvider) { rp.fetcher.Destroy() }
|
||||
runtime.SetFinalizer(wrapper, final)
|
||||
return wrapper
|
||||
}
|
||||
|
||||
func newStrategy(behavior P.RuleType) ruleStrategy {
|
||||
switch behavior {
|
||||
case P.Domain:
|
||||
strategy := NewDomainStrategy()
|
||||
return strategy
|
||||
case P.IPCIDR:
|
||||
strategy := NewIPCidrStrategy()
|
||||
return strategy
|
||||
case P.Classical:
|
||||
strategy := NewClassicalStrategy()
|
||||
return strategy
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func rulesParse(buf []byte) (interface{}, error) {
|
||||
rulePayload := RulePayload{}
|
||||
err := yaml.Unmarshal(buf, &rulePayload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return append(rulePayload.Rules, rulePayload.Rules2...), nil
|
||||
}
|
62
rules/provider/rule_set.go
Normal file
62
rules/provider/rule_set.go
Normal file
@ -0,0 +1,62 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
P "github.com/Dreamacro/clash/constant/provider"
|
||||
"github.com/Dreamacro/clash/rules/common"
|
||||
)
|
||||
|
||||
type RuleSet struct {
|
||||
*common.Base
|
||||
ruleProviderName string
|
||||
adapter string
|
||||
ruleProvider P.RuleProvider
|
||||
noResolveIP bool
|
||||
}
|
||||
|
||||
func (rs *RuleSet) ShouldFindProcess() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (rs *RuleSet) RuleType() C.RuleType {
|
||||
return C.RuleSet
|
||||
}
|
||||
|
||||
func (rs *RuleSet) Match(metadata *C.Metadata) bool {
|
||||
return rs.getProviders().Match(metadata)
|
||||
}
|
||||
|
||||
func (rs *RuleSet) Adapter() string {
|
||||
return rs.adapter
|
||||
}
|
||||
|
||||
func (rs *RuleSet) Payload() string {
|
||||
return rs.getProviders().Name()
|
||||
}
|
||||
|
||||
func (rs *RuleSet) ShouldResolveIP() bool {
|
||||
return !rs.noResolveIP && rs.getProviders().ShouldResolveIP()
|
||||
}
|
||||
func (rs *RuleSet) getProviders() P.RuleProvider {
|
||||
if rs.ruleProvider == nil {
|
||||
rp := RuleProviders()[rs.ruleProviderName]
|
||||
rs.ruleProvider = rp
|
||||
}
|
||||
|
||||
return rs.ruleProvider
|
||||
}
|
||||
|
||||
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
|
||||
rp, ok := RuleProviders()[ruleProviderName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
|
||||
}
|
||||
return &RuleSet{
|
||||
Base: &common.Base{},
|
||||
ruleProviderName: ruleProviderName,
|
||||
adapter: adapter,
|
||||
ruleProvider: rp,
|
||||
noResolveIP: noResolveIP,
|
||||
}, nil
|
||||
}
|
50
rules/ruleparser/ruleparser.go
Normal file
50
rules/ruleparser/ruleparser.go
Normal file
@ -0,0 +1,50 @@
|
||||
package ruleparser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
RC "github.com/Dreamacro/clash/rules/common"
|
||||
)
|
||||
|
||||
func ParseSameRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
||||
switch tp {
|
||||
case "DOMAIN":
|
||||
parsed = RC.NewDomain(payload, target)
|
||||
case "DOMAIN-SUFFIX":
|
||||
parsed = RC.NewDomainSuffix(payload, target)
|
||||
case "DOMAIN-KEYWORD":
|
||||
parsed = RC.NewDomainKeyword(payload, target)
|
||||
case "GEOSITE":
|
||||
parsed, parseErr = RC.NewGEOSITE(payload, target)
|
||||
case "GEOIP":
|
||||
noResolve := RC.HasNoResolve(params)
|
||||
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve)
|
||||
case "IP-CIDR", "IP-CIDR6":
|
||||
noResolve := RC.HasNoResolve(params)
|
||||
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
|
||||
case "SRC-IP-CIDR":
|
||||
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
||||
case "IP-SUFFIX":
|
||||
noResolve := RC.HasNoResolve(params)
|
||||
parsed, parseErr = RC.NewIPSuffix(payload, target, false, noResolve)
|
||||
case "SRC-IP-SUFFIX":
|
||||
parsed, parseErr = RC.NewIPSuffix(payload, target, true, true)
|
||||
case "SRC-PORT":
|
||||
parsed, parseErr = RC.NewPort(payload, target, true)
|
||||
case "DST-PORT":
|
||||
parsed, parseErr = RC.NewPort(payload, target, false)
|
||||
case "PROCESS-NAME":
|
||||
parsed, parseErr = RC.NewProcess(payload, target, true)
|
||||
case "PROCESS-PATH":
|
||||
parsed, parseErr = RC.NewProcess(payload, target, false)
|
||||
case "NETWORK":
|
||||
parsed, parseErr = RC.NewNetworkType(payload, target)
|
||||
case "UID":
|
||||
parsed, parseErr = RC.NewUid(payload, target)
|
||||
case "IN-TYPE":
|
||||
parsed, parseErr = RC.NewInType(payload, target)
|
||||
default:
|
||||
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user