github.com/yaling888/clash@v1.53.0/rule/base.go (about) 1 package rules 2 3 import ( 4 "errors" 5 "net/netip" 6 "strings" 7 8 C "github.com/yaling888/clash/constant" 9 ) 10 11 var ( 12 errPayload = errors.New("payload error") 13 14 noResolve = "no-resolve" 15 ) 16 17 type Base struct { 18 ruleExtra *C.RuleExtra 19 ruleGroup C.RuleGroup 20 } 21 22 func (b *Base) RuleExtra() *C.RuleExtra { 23 return b.ruleExtra 24 } 25 26 func (b *Base) SetRuleExtra(re *C.RuleExtra) { 27 b.ruleExtra = re 28 } 29 30 func (b *Base) SubRules() []C.Rule { 31 return nil 32 } 33 34 func (b *Base) RuleGroups() C.RuleGroup { 35 return b.ruleGroup 36 } 37 38 func (b *Base) AppendGroup(group string) { 39 b.ruleGroup = append(b.ruleGroup, group) 40 } 41 42 func (b *Base) ShouldFindProcess() bool { 43 return false 44 } 45 46 func HasNoResolve(params []string) bool { 47 for _, p := range params { 48 if p == noResolve { 49 return true 50 } 51 } 52 return false 53 } 54 55 func findNetwork(params []string) C.NetWork { 56 for _, p := range params { 57 if strings.EqualFold(p, "tcp") { 58 return C.TCP 59 } else if strings.EqualFold(p, "udp") { 60 return C.UDP 61 } 62 } 63 return C.ALLNet 64 } 65 66 func findSourceIPs(params []string) []*netip.Prefix { 67 var ips []*netip.Prefix 68 for _, p := range params { 69 if p == noResolve || len(p) < 7 { 70 continue 71 } 72 ipnet, err := netip.ParsePrefix(p) 73 if err != nil { 74 continue 75 } 76 ips = append(ips, &ipnet) 77 } 78 79 if len(ips) > 0 { 80 return ips 81 } 82 return nil 83 } 84 85 func findProcessName(params []string) []string { 86 var processNames []string 87 for _, p := range params { 88 if strings.HasPrefix(p, "P:") { 89 processNames = append(processNames, strings.TrimPrefix(p, "P:")) 90 } 91 } 92 93 if len(processNames) > 0 { 94 return processNames 95 } 96 return nil 97 }