fix: remove extra and the actual original IDNA domain name is no longer stored, for reduce memory

This commit is contained in:
Skyxim 2022-08-11 21:50:16 +08:00
parent 93ea1248e3
commit 473d0f74bd
7 changed files with 37 additions and 131 deletions

View File

@ -83,6 +83,4 @@ type Rule interface {
Payload() string
ShouldResolveIP() bool
ShouldFindProcess() bool
RuleExtra() *RuleExtra
SetRuleExtra(re *RuleExtra)
}

View File

@ -1,48 +1,9 @@
package constant
import (
"net/netip"
"strings"
"github.com/Dreamacro/clash/component/geodata/router"
)
type RuleExtra struct {
Network NetWork
SourceIPs []*netip.Prefix
ProcessNames []string
}
func (re *RuleExtra) NotMatchNetwork(network NetWork) bool {
return re.Network != ALLNet && re.Network != network
}
func (re *RuleExtra) NotMatchSourceIP(srcIP netip.Addr) bool {
if re.SourceIPs == nil {
return false
}
for _, ips := range re.SourceIPs {
if ips.Contains(srcIP) {
return false
}
}
return true
}
func (re *RuleExtra) NotMatchProcessName(processName string) bool {
if re.ProcessNames == nil {
return false
}
for _, pn := range re.ProcessNames {
if strings.EqualFold(pn, processName) {
return false
}
}
return true
}
type RuleGeoSite interface {
GetDomainMatcher() *router.DomainMatcher
}

View File

@ -2,10 +2,6 @@ package common
import (
"errors"
"net/netip"
"strings"
C "github.com/Dreamacro/clash/constant"
)
var (
@ -15,15 +11,6 @@ var (
)
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 {
@ -42,47 +29,3 @@ func HasNoResolve(params []string) bool {
}
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
}

View File

@ -9,9 +9,9 @@ import (
type Domain struct {
*Base
domain string
rawDomain string
adapter string
domain string
adapter string
isIDNA bool
}
func (d *Domain) RuleType() C.RuleType {
@ -30,16 +30,20 @@ func (d *Domain) Adapter() string {
}
func (d *Domain) Payload() string {
return d.rawDomain
domain := d.domain
if d.isIDNA {
domain, _ = idna.ToUnicode(domain)
}
return domain
}
func NewDomain(domain string, adapter string) *Domain {
actualDomain, _ := idna.ToASCII(domain)
return &Domain{
Base: &Base{},
domain: strings.ToLower(actualDomain),
adapter: adapter,
rawDomain: domain,
Base: &Base{},
domain: strings.ToLower(actualDomain),
adapter: adapter,
isIDNA: actualDomain != domain,
}
}

View File

@ -9,9 +9,9 @@ import (
type DomainKeyword struct {
*Base
keyword string
adapter string
rawKeyword string
keyword string
adapter string
isIDNA bool
}
func (dk *DomainKeyword) RuleType() C.RuleType {
@ -31,16 +31,20 @@ func (dk *DomainKeyword) Adapter() string {
}
func (dk *DomainKeyword) Payload() string {
return dk.rawKeyword
keyword := dk.keyword
if dk.isIDNA {
keyword, _ = idna.ToUnicode(keyword)
}
return keyword
}
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
actualDomainKeyword, _ := idna.ToASCII(keyword)
return &DomainKeyword{
Base: &Base{},
keyword: strings.ToLower(actualDomainKeyword),
adapter: adapter,
rawKeyword: keyword,
Base: &Base{},
keyword: strings.ToLower(actualDomainKeyword),
adapter: adapter,
isIDNA: keyword != actualDomainKeyword,
}
}

View File

@ -9,9 +9,9 @@ import (
type DomainSuffix struct {
*Base
suffix string
adapter string
rawSuffix string
suffix string
adapter string
isIDNA bool
}
func (ds *DomainSuffix) RuleType() C.RuleType {
@ -31,16 +31,20 @@ func (ds *DomainSuffix) Adapter() string {
}
func (ds *DomainSuffix) Payload() string {
return ds.rawSuffix
suffix := ds.suffix
if ds.isIDNA {
suffix, _ = idna.ToUnicode(suffix)
}
return suffix
}
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
actualDomainKeyword, _ := idna.ToASCII(suffix)
actualDomainSuffix, _ := idna.ToASCII(suffix)
return &DomainSuffix{
Base: &Base{},
suffix: strings.ToLower(actualDomainKeyword),
adapter: adapter,
rawSuffix: suffix,
Base: &Base{},
suffix: strings.ToLower(actualDomainSuffix),
adapter: adapter,
isIDNA: suffix != actualDomainSuffix,
}
}

View File

@ -65,13 +65,5 @@ func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, pars
return nil, parseErr
}
ruleExtra := &C.RuleExtra{
Network: RC.FindNetwork(params),
SourceIPs: RC.FindSourceIPs(params),
ProcessNames: RC.FindProcessName(params),
}
parsed.SetRuleExtra(ruleExtra)
return
}