mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 12:42:27 +08:00
Chore: clean code
This commit is contained in:
parent
637707e58f
commit
65a289e16f
@ -2,9 +2,6 @@ package process
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -12,8 +9,6 @@ var (
|
|||||||
ErrInvalidNetwork = errors.New("invalid network")
|
ErrInvalidNetwork = errors.New("invalid network")
|
||||||
ErrPlatformNotSupport = errors.New("not support on this platform")
|
ErrPlatformNotSupport = errors.New("not support on this platform")
|
||||||
ErrNotFound = errors.New("process not found")
|
ErrNotFound = errors.New("process not found")
|
||||||
|
|
||||||
enableFindProcess = true
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -21,10 +16,6 @@ const (
|
|||||||
UDP = "udp"
|
UDP = "udp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func EnableFindProcess(e bool) {
|
|
||||||
enableFindProcess = e
|
|
||||||
}
|
|
||||||
|
|
||||||
func FindProcessName(network string, srcIP netip.Addr, srcPort int) (int32, string, error) {
|
func FindProcessName(network string, srcIP netip.Addr, srcPort int) (int32, string, error) {
|
||||||
return findProcessName(network, srcIP, srcPort)
|
return findProcessName(network, srcIP, srcPort)
|
||||||
}
|
}
|
||||||
@ -36,51 +27,3 @@ func FindUid(network string, srcIP netip.Addr, srcPort int) (int32, error) {
|
|||||||
}
|
}
|
||||||
return uid, nil
|
return uid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShouldFindProcess(metadata *C.Metadata) bool {
|
|
||||||
if !enableFindProcess ||
|
|
||||||
metadata.Process != "" ||
|
|
||||||
metadata.ProcessPath != "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, ip := range localIPs {
|
|
||||||
if ip == metadata.SrcIP {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func AppendLocalIPs(ip ...netip.Addr) {
|
|
||||||
localIPs = append(ip, localIPs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getLocalIPs() []netip.Addr {
|
|
||||||
ips := []netip.Addr{netip.IPv4Unspecified(), netip.IPv6Unspecified()}
|
|
||||||
|
|
||||||
netInterfaces, err := net.Interfaces()
|
|
||||||
if err != nil {
|
|
||||||
ips = append(ips, netip.AddrFrom4([4]byte{127, 0, 0, 1}), nnip.IpToAddr(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, nnip.IpToAddr(ipNet.IP))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ips
|
|
||||||
}
|
|
||||||
|
|
||||||
var localIPs []netip.Addr
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
localIPs = getLocalIPs()
|
|
||||||
}
|
|
||||||
|
@ -127,9 +127,9 @@ func (m *Metadata) SourceDetail() string {
|
|||||||
return fmt.Sprintf("[%s]", ClashName)
|
return fmt.Sprintf("[%s]", ClashName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Process != "" && m.Uid != nil {
|
if m.Process != "" && *m.Uid != -1 {
|
||||||
return fmt.Sprintf("%s(%s, uid=%d)", m.SourceAddress(), m.Process, *m.Uid)
|
return fmt.Sprintf("%s(%s, uid=%d)", m.SourceAddress(), m.Process, *m.Uid)
|
||||||
} else if m.Uid != nil {
|
} else if *m.Uid != -1 {
|
||||||
return fmt.Sprintf("%s(uid=%d)", m.SourceAddress(), *m.Uid)
|
return fmt.Sprintf("%s(uid=%d)", m.SourceAddress(), *m.Uid)
|
||||||
} else if m.Process != "" {
|
} else if m.Process != "" {
|
||||||
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
|
return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process)
|
||||||
|
@ -373,14 +373,16 @@ func shouldResolveIP(rule C.Rule, metadata *C.Metadata) bool {
|
|||||||
func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
configMux.RLock()
|
configMux.RLock()
|
||||||
defer configMux.RUnlock()
|
defer configMux.RUnlock()
|
||||||
var resolved bool
|
var (
|
||||||
|
resolved bool
|
||||||
|
processFound bool
|
||||||
|
)
|
||||||
|
|
||||||
if node := resolver.DefaultHosts.Search(metadata.Host); node != nil {
|
if node := resolver.DefaultHosts.Search(metadata.Host); node != nil {
|
||||||
metadata.DstIP = node.Data
|
metadata.DstIP = node.Data
|
||||||
resolved = true
|
resolved = true
|
||||||
}
|
}
|
||||||
|
|
||||||
foundProcess := false
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if !resolved && shouldResolveIP(rule, metadata) {
|
if !resolved && shouldResolveIP(rule, metadata) {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(metadata.Host)
|
||||||
@ -393,7 +395,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
|||||||
resolved = true
|
resolved = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if !foundProcess && (alwaysFindProcess || rule.ShouldFindProcess()) {
|
if !processFound && (alwaysFindProcess || rule.ShouldFindProcess()) {
|
||||||
srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
|
srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
|
||||||
uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(srcPort))
|
uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(srcPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -402,7 +404,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
|||||||
metadata.Process = filepath.Base(path)
|
metadata.Process = filepath.Base(path)
|
||||||
metadata.ProcessPath = path
|
metadata.ProcessPath = path
|
||||||
metadata.Uid = &uid
|
metadata.Uid = &uid
|
||||||
foundProcess = true
|
processFound = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user