2019-10-28 00:02:23 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-08-31 21:46:04 +08:00
|
|
|
"net"
|
|
|
|
"strings"
|
2021-07-01 22:49:29 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HasNoResolve(params []string) bool {
|
|
|
|
for _, p := range params {
|
2021-08-31 21:46:04 +08:00
|
|
|
p = strings.Trim(p, " ")
|
2019-10-28 00:02:23 +08:00
|
|
|
if p == noResolve {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-07-01 22:49:29 +08:00
|
|
|
|
|
|
|
func findNetwork(params []string) C.NetWork {
|
|
|
|
for _, p := range params {
|
2021-08-31 21:46:04 +08:00
|
|
|
p = strings.Trim(p, " ")
|
2021-07-01 22:49:29 +08:00
|
|
|
if p == "tcp" {
|
|
|
|
return C.TCP
|
|
|
|
} else if p == "udp" {
|
|
|
|
return C.UDP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return C.ALLNet
|
|
|
|
}
|
2021-08-31 21:46:04 +08:00
|
|
|
|
|
|
|
func findSourceIPs(params []string) []*net.IPNet {
|
|
|
|
var ips []*net.IPNet
|
|
|
|
for _, p := range params {
|
|
|
|
p = strings.Trim(p, " ")
|
|
|
|
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
|
|
|
|
}
|