2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2019-10-28 00:02:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-11-17 16:03:47 +08:00
|
|
|
"net"
|
2022-02-04 06:11:24 +08:00
|
|
|
"strings"
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2019-10-28 00:02:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-17 22:17:27 +08:00
|
|
|
errPayload = errors.New("payload error")
|
2019-10-28 00:02:23 +08:00
|
|
|
|
|
|
|
noResolve = "no-resolve"
|
|
|
|
)
|
|
|
|
|
2022-03-13 01:21:23 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-17 23:24:07 +08:00
|
|
|
func (b *Base) ShouldResolveIP() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-28 00:02:23 +08:00
|
|
|
func HasNoResolve(params []string) bool {
|
|
|
|
for _, p := range params {
|
|
|
|
if p == noResolve {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2022-01-22 22:10:45 +08:00
|
|
|
func FindNetwork(params []string) C.NetWork {
|
2021-11-17 16:03:47 +08:00
|
|
|
for _, p := range params {
|
2022-02-23 14:01:53 +08:00
|
|
|
if strings.EqualFold(p, "tcp") {
|
2021-11-17 16:03:47 +08:00
|
|
|
return C.TCP
|
2022-02-23 14:01:53 +08:00
|
|
|
} else if strings.EqualFold(p, "udp") {
|
2021-11-17 16:03:47 +08:00
|
|
|
return C.UDP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return C.ALLNet
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:10:45 +08:00
|
|
|
func FindSourceIPs(params []string) []*net.IPNet {
|
2021-11-17 16:03:47 +08:00
|
|
|
var ips []*net.IPNet
|
|
|
|
for _, p := range params {
|
|
|
|
if p == noResolve || len(p) < 7 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, ipnet, err := net.ParseCIDR(p)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ips = append(ips, ipnet)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ips) > 0 {
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-04 06:11:24 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|