mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 12:42:27 +08:00
chore: Better package name handling on Android
This commit is contained in:
parent
be3d121ec6
commit
7b3c9e94e6
@ -2,19 +2,23 @@ package process
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unicode"
|
"unicode"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
"github.com/mdlayher/netlink"
|
"github.com/mdlayher/netlink"
|
||||||
|
tun "github.com/metacubex/sing-tun"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,6 +63,19 @@ type inetDiagResponse struct {
|
|||||||
INode uint32
|
INode uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MyCallback struct{}
|
||||||
|
|
||||||
|
var (
|
||||||
|
packageManager tun.PackageManager
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cb *MyCallback) OnPackagesUpdated(packageCount int, sharedCount int) {}
|
||||||
|
|
||||||
|
func (cb *MyCallback) NewError(ctx context.Context, err error) {
|
||||||
|
log.Warnln("%s", err)
|
||||||
|
}
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) {
|
func findProcessName(network string, ip netip.Addr, srcPort int) (uint32, string, error) {
|
||||||
uid, inode, err := resolveSocketByNetlink(network, ip, srcPort)
|
uid, inode, err := resolveSocketByNetlink(network, ip, srcPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -162,12 +179,7 @@ func resolveProcessNameByProcSearch(inode, uid uint32) (string, error) {
|
|||||||
}
|
}
|
||||||
if runtime.GOOS == "android" {
|
if runtime.GOOS == "android" {
|
||||||
if bytes.Equal(buffer[:n], socket) {
|
if bytes.Equal(buffer[:n], socket) {
|
||||||
cmdline, err := os.ReadFile(path.Join(processPath, "cmdline"))
|
return findPackageName(uid), nil
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return splitCmdline(cmdline), nil
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if bytes.Equal(buffer[:n], socket) {
|
if bytes.Equal(buffer[:n], socket) {
|
||||||
@ -181,17 +193,29 @@ func resolveProcessNameByProcSearch(inode, uid uint32) (string, error) {
|
|||||||
return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode)
|
return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitCmdline(cmdline []byte) string {
|
func findPackageName(uid uint32) string {
|
||||||
cmdline = bytes.Trim(cmdline, " ")
|
once.Do(func() {
|
||||||
|
callback := &MyCallback{}
|
||||||
idx := bytes.IndexFunc(cmdline, func(r rune) bool {
|
var err error
|
||||||
return unicode.IsControl(r) || unicode.IsSpace(r) || r == ':'
|
packageManager, err = tun.NewPackageManager(callback)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnln("%s", err)
|
||||||
|
}
|
||||||
|
err = packageManager.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnln("%s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if idx == -1 {
|
if sharedPackage, loaded := packageManager.SharedPackageByID(uid % 100000); loaded {
|
||||||
return filepath.Base(string(cmdline))
|
fmt.Println(loaded)
|
||||||
|
return sharedPackage
|
||||||
}
|
}
|
||||||
return filepath.Base(string(cmdline[:idx]))
|
if packageName, loaded := packageManager.PackageByID(uid % 100000); loaded {
|
||||||
|
return packageName
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPid(s string) bool {
|
func isPid(s string) bool {
|
||||||
|
Loading…
Reference in New Issue
Block a user