2022-07-23 19:01:41 +08:00
|
|
|
package process
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/netip"
|
|
|
|
|
2022-08-19 07:29:47 +08:00
|
|
|
"github.com/sagernet/sing-tun"
|
2022-07-23 19:01:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Searcher = (*androidSearcher)(nil)
|
|
|
|
|
|
|
|
type androidSearcher struct {
|
2022-08-15 11:40:49 +08:00
|
|
|
packageManager tun.PackageManager
|
2022-07-23 19:01:41 +08:00
|
|
|
}
|
|
|
|
|
2022-08-15 11:40:49 +08:00
|
|
|
func NewSearcher(config Config) (Searcher, error) {
|
|
|
|
return &androidSearcher{config.PackageManager}, nil
|
2022-07-23 19:01:41 +08:00
|
|
|
}
|
|
|
|
|
2022-08-20 10:38:12 +08:00
|
|
|
func (s *androidSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
|
2022-08-26 17:25:36 +08:00
|
|
|
_, uid, err := resolveSocketByNetlink(network, source, destination)
|
2022-07-23 19:01:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-29 12:02:29 +08:00
|
|
|
if sharedPackage, loaded := s.packageManager.SharedPackageByID(uid % 100000); loaded {
|
2022-07-23 19:01:41 +08:00
|
|
|
return &Info{
|
2022-08-26 17:25:36 +08:00
|
|
|
UserId: int32(uid),
|
2022-08-15 11:40:49 +08:00
|
|
|
PackageName: sharedPackage,
|
2022-07-23 19:01:41 +08:00
|
|
|
}, nil
|
|
|
|
}
|
2022-08-29 12:02:29 +08:00
|
|
|
if packageName, loaded := s.packageManager.PackageByID(uid % 100000); loaded {
|
2022-07-23 19:01:41 +08:00
|
|
|
return &Info{
|
2022-08-26 17:25:36 +08:00
|
|
|
UserId: int32(uid),
|
2022-07-23 19:01:41 +08:00
|
|
|
PackageName: packageName,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-08-26 17:25:36 +08:00
|
|
|
return &Info{UserId: int32(uid)}, nil
|
2022-07-23 19:01:41 +08:00
|
|
|
}
|