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