2022-07-23 11:01:41 +00:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
|
2022-08-18 23:29:47 +00:00
|
|
|
"github.com/sagernet/sing-tun"
|
2022-07-23 11:01:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Searcher = (*androidSearcher)(nil)
|
|
|
|
|
|
|
|
type androidSearcher struct {
|
2022-08-15 03:40:49 +00:00
|
|
|
packageManager tun.PackageManager
|
2022-07-23 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 03:40:49 +00:00
|
|
|
func NewSearcher(config Config) (Searcher, error) {
|
|
|
|
return &androidSearcher{config.PackageManager}, nil
|
2022-07-23 11:01:41 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2022-07-23 11:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-20 02:38:12 +00:00
|
|
|
if sharedPackage, loaded := s.packageManager.SharedPackageByID(socket.UID); loaded {
|
2022-07-23 11:01:41 +00:00
|
|
|
return &Info{
|
2022-08-20 02:38:12 +00:00
|
|
|
UserId: int32(socket.UID),
|
2022-08-15 03:40:49 +00:00
|
|
|
PackageName: sharedPackage,
|
2022-07-23 11:01:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-08-20 02:38:12 +00:00
|
|
|
if packageName, loaded := s.packageManager.PackageByID(socket.UID); loaded {
|
2022-07-23 11:01:41 +00:00
|
|
|
return &Info{
|
2022-08-20 02:38:12 +00:00
|
|
|
UserId: int32(socket.UID),
|
2022-07-23 11:01:41 +00:00
|
|
|
PackageName: packageName,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-20 02:38:12 +00:00
|
|
|
return &Info{UserId: int32(socket.UID)}, nil
|
2022-07-23 11:01:41 +00:00
|
|
|
}
|