From 4ebf40f5820f4ce27a3522f5641d1d24689bdae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 11 Apr 2023 14:39:57 +0800 Subject: [PATCH] Fix find process user --- common/process/searcher.go | 14 +++++++++++++- common/process/searcher_with_name.go | 25 ------------------------- common/process/searcher_without_name.go | 12 ------------ 3 files changed, 13 insertions(+), 38 deletions(-) delete mode 100644 common/process/searcher_with_name.go delete mode 100644 common/process/searcher_without_name.go diff --git a/common/process/searcher.go b/common/process/searcher.go index d5a86543..cee81068 100644 --- a/common/process/searcher.go +++ b/common/process/searcher.go @@ -3,10 +3,12 @@ package process import ( "context" "net/netip" + "os/user" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-tun" E "github.com/sagernet/sing/common/exceptions" + F "github.com/sagernet/sing/common/format" ) type Searcher interface { @@ -28,5 +30,15 @@ type Info struct { } func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) { - return findProcessInfo(searcher, ctx, network, source, destination) + info, err := searcher.FindProcessInfo(ctx, network, source, destination) + if err != nil { + return nil, err + } + if info.UserId != -1 { + osUser, _ := user.LookupId(F.ToString(info.UserId)) + if osUser != nil { + info.User = osUser.Username + } + } + return info, nil } diff --git a/common/process/searcher_with_name.go b/common/process/searcher_with_name.go deleted file mode 100644 index c7ac3c40..00000000 --- a/common/process/searcher_with_name.go +++ /dev/null @@ -1,25 +0,0 @@ -//go:build linux && !android - -package process - -import ( - "context" - "net/netip" - "os/user" - - F "github.com/sagernet/sing/common/format" -) - -func findProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) { - info, err := searcher.FindProcessInfo(ctx, network, source, destination) - if err != nil { - return nil, err - } - if info.UserId != -1 { - osUser, _ := user.LookupId(F.ToString(info.UserId)) - if osUser != nil { - info.User = osUser.Username - } - } - return info, nil -} diff --git a/common/process/searcher_without_name.go b/common/process/searcher_without_name.go deleted file mode 100644 index ffc56a7b..00000000 --- a/common/process/searcher_without_name.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build !linux || android - -package process - -import ( - "context" - "net/netip" -) - -func findProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) { - return searcher.FindProcessInfo(ctx, network, source, destination) -}