sing-box/common/process/searcher_android.go

39 lines
956 B
Go
Raw Normal View History

package process
import (
"context"
"net/netip"
2022-08-18 23:29:47 +00:00
"github.com/sagernet/sing-tun"
)
var _ Searcher = (*androidSearcher)(nil)
type androidSearcher struct {
packageManager tun.PackageManager
}
func NewSearcher(config Config) (Searcher, error) {
return &androidSearcher{config.PackageManager}, nil
}
2022-08-20 02:38:12 +00:00
func (s *androidSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
socket, err := resolveSocketByNetlink(network, source, destination)
if err != nil {
return nil, err
}
2022-08-20 02:38:12 +00:00
if sharedPackage, loaded := s.packageManager.SharedPackageByID(socket.UID); loaded {
return &Info{
2022-08-20 02:38:12 +00:00
UserId: int32(socket.UID),
PackageName: sharedPackage,
}, nil
}
2022-08-20 02:38:12 +00:00
if packageName, loaded := s.packageManager.PackageByID(socket.UID); loaded {
return &Info{
2022-08-20 02:38:12 +00:00
UserId: int32(socket.UID),
PackageName: packageName,
}, nil
}
2022-08-20 02:38:12 +00:00
return &Info{UserId: int32(socket.UID)}, nil
}