From f96bf655573b7eb2d86df041fcfab09d484e5e2f Mon Sep 17 00:00:00 2001 From: metacubex Date: Sat, 14 Jan 2023 02:23:30 +0800 Subject: [PATCH] chore: Refine process code --- component/process/process.go | 10 +-------- component/process/process_darwin.go | 16 +++++-------- component/process/process_freebsd_amd64.go | 16 +++++-------- component/process/process_linux.go | 10 ++++----- component/process/process_other.go | 4 ++-- component/process/process_windows.go | 10 ++++----- constant/metadata.go | 15 +++++++------ rules/common/uid.go | 26 +++++++--------------- 8 files changed, 41 insertions(+), 66 deletions(-) diff --git a/component/process/process.go b/component/process/process.go index fe4c5d2a..76ec2c45 100644 --- a/component/process/process.go +++ b/component/process/process.go @@ -16,14 +16,6 @@ const ( UDP = "udp" ) -func FindProcessName(network string, srcIP netip.Addr, srcPort int) (*uint32, string, error) { +func FindProcessName(network string, srcIP netip.Addr, srcPort int) (uint32, string, error) { return findProcessName(network, srcIP, srcPort) } - -func FindUid(network string, srcIP netip.Addr, srcPort int) (*uint32, error) { - _, uid, err := resolveSocketByNetlink(network, srcIP, srcPort) - if err != nil { - return nil, err - } - return &uid, nil -} diff --git a/component/process/process_darwin.go b/component/process/process_darwin.go index 39e56dd2..67d2e833 100644 --- a/component/process/process_darwin.go +++ b/component/process/process_darwin.go @@ -33,11 +33,7 @@ var structSize = func() int { } }() -func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) { - return 0, 0, ErrPlatformNotSupport -} - -func findProcessName(network string, ip netip.Addr, port int) (*uint32, string, error) { +func findProcessName(network string, ip netip.Addr, port int) (uint32, string, error) { var spath string switch network { case TCP: @@ -45,14 +41,14 @@ func findProcessName(network string, ip netip.Addr, port int) (*uint32, string, case UDP: spath = "net.inet.udp.pcblist_n" default: - return nil, "", ErrInvalidNetwork + return 0, "", ErrInvalidNetwork } isIPv4 := ip.Is4() value, err := syscall.Sysctl(spath) if err != nil { - return nil, "", err + return 0, "", err } buf := []byte(value) @@ -96,7 +92,7 @@ func findProcessName(network string, ip netip.Addr, port int) (*uint32, string, // xsocket_n.so_last_pid pid := readNativeUint32(buf[so+68 : so+72]) pp, err := getExecPathFromPID(pid) - return nil, pp, err + return 0, pp, err } // udp packet connection may be not equal with srcIP @@ -106,10 +102,10 @@ func findProcessName(network string, ip netip.Addr, port int) (*uint32, string, } if network == UDP && fallbackUDPProcess != "" { - return nil, fallbackUDPProcess, nil + return 0, fallbackUDPProcess, nil } - return nil, "", ErrNotFound + return 0, "", ErrNotFound } func getExecPathFromPID(pid uint32) (string, error) { diff --git a/component/process/process_freebsd_amd64.go b/component/process/process_freebsd_amd64.go index ffbe1515..709ade3b 100644 --- a/component/process/process_freebsd_amd64.go +++ b/component/process/process_freebsd_amd64.go @@ -21,11 +21,7 @@ var ( once sync.Once ) -func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) { - return 0, 0, ErrPlatformNotSupport -} - -func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) { +func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) { once.Do(func() { if err := initSearcher(); err != nil { log.Errorln("Initialize PROCESS-NAME failed: %s", err.Error()) @@ -35,7 +31,7 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, strin }) if defaultSearcher == nil { - return nil, "", ErrPlatformNotSupport + return 0, "", ErrPlatformNotSupport } var spath string @@ -46,22 +42,22 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, strin case UDP: spath = "net.inet.udp.pcblist" default: - return nil, "", ErrInvalidNetwork + return 0, "", ErrInvalidNetwork } value, err := syscall.Sysctl(spath) if err != nil { - return nil, "", err + return 0, "", err } buf := []byte(value) pid, err := defaultSearcher.Search(buf, ip, uint16(srcPort), isTCP) if err != nil { - return nil, "", err + return 0, "", err } pp, err := getExecPathFromPID(pid) - return nil, pp, err + return 0, pp, err } func getExecPathFromPID(pid uint32) (string, error) { diff --git a/component/process/process_linux.go b/component/process/process_linux.go index 9b1a4844..f8174495 100644 --- a/component/process/process_linux.go +++ b/component/process/process_linux.go @@ -59,14 +59,14 @@ type inetDiagResponse struct { INode uint32 } -func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) { - inode, uid, err := resolveSocketByNetlink(network, ip, srcPort) +func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) { + uid, inode, err := resolveSocketByNetlink(network, ip, srcPort) if err != nil { - return nil, "", err + return 0, "", err } pp, err := resolveProcessNameByProcSearch(inode, uid) - return &uid, pp, err + return uid, pp, err } func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) { @@ -119,7 +119,7 @@ func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, response := (*inetDiagResponse)(unsafe.Pointer(&msg.Data[0])) - return response.INode, response.UID, nil + return response.UID, response.INode, nil } return 0, 0, ErrNotFound diff --git a/component/process/process_other.go b/component/process/process_other.go index 32614b26..eea6e5fd 100644 --- a/component/process/process_other.go +++ b/component/process/process_other.go @@ -4,8 +4,8 @@ package process import "net/netip" -func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) { - return nil, "", ErrPlatformNotSupport +func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) { + return 0, "", ErrPlatformNotSupport } func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) { diff --git a/component/process/process_windows.go b/component/process/process_windows.go index 7e073c8e..cce08e30 100644 --- a/component/process/process_windows.go +++ b/component/process/process_windows.go @@ -62,7 +62,7 @@ func initWin32API() error { return nil } -func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) { +func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) { once.Do(func() { err := initWin32API() if err != nil { @@ -86,22 +86,22 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, strin fn = getExUDPTable class = udpTablePid default: - return nil, "", ErrInvalidNetwork + return 0, "", ErrInvalidNetwork } buf, err := getTransportTable(fn, family, class) if err != nil { - return nil, "", err + return 0, "", err } s := newSearcher(family == windows.AF_INET, network == TCP) pid, err := s.Search(buf, ip, uint16(srcPort)) if err != nil { - return nil, "", err + return 0, "", err } pp, err := getExecPathFromPID(pid) - return nil, pp, err + return 0, pp, err } type searcher struct { diff --git a/constant/metadata.go b/constant/metadata.go index 359329f9..4605a146 100644 --- a/constant/metadata.go +++ b/constant/metadata.go @@ -128,7 +128,7 @@ type Metadata struct { InName string `json:"inboundName"` Host string `json:"host"` DNSMode DNSMode `json:"dnsMode"` - Uid *uint32 `json:"uid"` + Uid uint32 `json:"uid"` Process string `json:"process"` ProcessPath string `json:"processPath"` SpecialProxy string `json:"specialProxy"` @@ -149,13 +149,14 @@ func (m *Metadata) SourceDetail() string { return fmt.Sprintf("[%s]", ClashName) } - if m.Process != "" && m.Uid != nil { - return fmt.Sprintf("%s(%s, uid=%d)", m.SourceAddress(), m.Process, *m.Uid) - } else if m.Uid != nil { - return fmt.Sprintf("%s(uid=%d)", m.SourceAddress(), *m.Uid) - } else if m.Process != "" { + switch { + case m.Process != "" && m.Uid != 0: + return fmt.Sprintf("%s(%s, uid=%d)", m.SourceAddress(), m.Process, m.Uid) + case m.Uid != 0: + return fmt.Sprintf("%s(uid=%d)", m.SourceAddress(), m.Uid) + case m.Process != "": return fmt.Sprintf("%s(%s)", m.SourceAddress(), m.Process) - } else { + default: return fmt.Sprintf("%s", m.SourceAddress()) } } diff --git a/rules/common/uid.go b/rules/common/uid.go index 1457a3d0..ea275c28 100644 --- a/rules/common/uid.go +++ b/rules/common/uid.go @@ -3,7 +3,6 @@ package common import ( "fmt" "github.com/Dreamacro/clash/common/utils" - "github.com/Dreamacro/clash/component/process" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" "runtime" @@ -72,27 +71,14 @@ func (u *Uid) RuleType() C.RuleType { } func (u *Uid) Match(metadata *C.Metadata) (bool, string) { - srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16) - if err != nil { - return false, "" - } - var uid *uint32 - if metadata.Uid != nil { - uid = metadata.Uid - } else if uid, err = process.FindUid(metadata.NetWork.String(), metadata.SrcIP, int(srcPort)); err == nil { - metadata.Uid = uid - } else { - log.Warnln("[UID] could not get uid from %s", metadata.String()) - return false, "" - } - - if uid != nil { - for _, _uid := range u.uids { - if _uid.Contains(*uid) { + if metadata.Uid != 0 { + for _, uid := range u.uids { + if uid.Contains(metadata.Uid) { return true, u.adapter } } } + log.Warnln("[UID] could not get uid from %s", metadata.String()) return false, "" } @@ -103,3 +89,7 @@ func (u *Uid) Adapter() string { func (u *Uid) Payload() string { return u.oUid } + +func (u *Uid) ShouldFindProcess() bool { + return true +}