mihomo/component/process/process.go

74 lines
1.3 KiB
Go
Raw Normal View History

package process
import (
"errors"
"net"
2022-04-09 17:54:01 +08:00
"runtime"
2022-03-13 01:21:23 +08:00
C "github.com/Dreamacro/clash/constant"
)
var (
ErrInvalidNetwork = errors.New("invalid network")
ErrPlatformNotSupport = errors.New("not support on this platform")
ErrNotFound = errors.New("process not found")
)
const (
TCP = "tcp"
UDP = "udp"
)
func FindProcessName(network string, srcIP net.IP, srcPort int) (string, error) {
return findProcessName(network, srcIP, srcPort)
}
2022-03-13 01:21:23 +08:00
func ShouldFindProcess(metadata *C.Metadata) bool {
2022-04-09 17:54:01 +08:00
if runtime.GOOS == "android" {
return false
}
2022-04-20 01:31:33 +08:00
if metadata.Process != "" || metadata.ProcessPath != "" {
2022-03-13 01:21:23 +08:00
return false
}
for _, ip := range localIPs {
if ip.Equal(metadata.SrcIP) {
return true
}
}
return false
}
func AppendLocalIPs(ip ...net.IP) {
localIPs = append(ip, localIPs...)
}
2022-03-13 01:21:23 +08:00
func getLocalIPs() []net.IP {
ips := []net.IP{net.IPv4zero, net.IPv6zero}
2022-03-13 01:21:23 +08:00
netInterfaces, err := net.Interfaces()
if err != nil {
ips = append(ips, net.IPv4(127, 0, 0, 1), net.IPv6loopback)
return ips
}
for i := 0; i < len(netInterfaces); i++ {
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
adds, _ := netInterfaces[i].Addrs()
for _, address := range adds {
if ipNet, ok := address.(*net.IPNet); ok {
ips = append(ips, ipNet.IP)
}
}
}
}
return ips
}
var localIPs []net.IP
func init() {
localIPs = getLocalIPs()
}