mihomo/rule/common/base.go

69 lines
1.0 KiB
Go
Raw Normal View History

package common
import (
"errors"
2021-11-17 16:03:47 +08:00
"net"
"strings"
2021-11-17 16:03:47 +08:00
C "github.com/Dreamacro/clash/constant"
)
var (
errPayload = errors.New("payload error")
noResolve = "no-resolve"
)
func HasNoResolve(params []string) bool {
for _, p := range params {
if p == noResolve {
return true
}
}
return false
}
2021-11-17 16:03:47 +08:00
func FindNetwork(params []string) C.NetWork {
2021-11-17 16:03:47 +08:00
for _, p := range params {
if p == "tcp" {
return C.TCP
} else if p == "udp" {
return C.UDP
}
}
return C.ALLNet
}
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
}
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
}