mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
improve android process searcher
(cherry picked from commit 7a7412b8f497e39fc6dc5e35518e96089ad1205a)
This commit is contained in:
parent
dd8eb2ce11
commit
72266f8e2d
@ -18,10 +18,21 @@ func NewSearcher(config Config) (Searcher, error) {
|
||||
}
|
||||
|
||||
func (s *androidSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
||||
_, uid, err := resolveSocketByNetlink(network, source, destination)
|
||||
inode, uid, err := resolveSocketByNetlink(network, source, destination)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if processPath, _ := resolveProcessNameByProcSearch(inode, uid); processPath != "" {
|
||||
info := Info{
|
||||
UserId: int32(uid),
|
||||
}
|
||||
if _, loaded := s.packageManager.IDByPackage(processPath); loaded {
|
||||
info.PackageName = processPath
|
||||
} else {
|
||||
info.ProcessPath = processPath
|
||||
}
|
||||
return &info, nil
|
||||
}
|
||||
if sharedPackage, loaded := s.packageManager.SharedPackageByID(uid % 100000); loaded {
|
||||
return &Info{
|
||||
UserId: int32(uid),
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unicode"
|
||||
@ -186,15 +188,41 @@ func resolveProcessNameByProcSearch(inode, uid uint32) (string, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
if bytes.Equal(buffer[:n], socket) {
|
||||
return os.Readlink(path.Join(processPath, "exe"))
|
||||
if !bytes.Equal(buffer[:n], socket) {
|
||||
continue
|
||||
}
|
||||
|
||||
exe, err := os.Readlink(path.Join(processPath, "exe"))
|
||||
|
||||
if runtime.GOOS != "android" || !strings.HasPrefix(exe, "/system/bin/app_process") {
|
||||
return exe, err
|
||||
}
|
||||
|
||||
cmdline, err := os.ReadFile(path.Join(processPath, "cmdline"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return splitCmdline(cmdline), nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode)
|
||||
}
|
||||
|
||||
func splitCmdline(cmdline []byte) string {
|
||||
cmdline = bytes.Trim(cmdline, " ")
|
||||
|
||||
idx := bytes.IndexFunc(cmdline, func(r rune) bool {
|
||||
return unicode.IsControl(r) || unicode.IsSpace(r) || r == ':'
|
||||
})
|
||||
|
||||
if idx == -1 {
|
||||
return filepath.Base(string(cmdline))
|
||||
}
|
||||
return filepath.Base(string(cmdline[:idx]))
|
||||
}
|
||||
|
||||
func isPid(s string) bool {
|
||||
return strings.IndexFunc(s, func(r rune) bool {
|
||||
return !unicode.IsDigit(r)
|
||||
|
Loading…
x
Reference in New Issue
Block a user