github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/common/process/searcher_android.go (about)

     1  package process
     2  
     3  import (
     4  	"context"
     5  	"net/netip"
     6  
     7  	"github.com/sagernet/sing-tun"
     8  )
     9  
    10  var _ Searcher = (*androidSearcher)(nil)
    11  
    12  type androidSearcher struct {
    13  	packageManager tun.PackageManager
    14  }
    15  
    16  func NewSearcher(config Config) (Searcher, error) {
    17  	return &androidSearcher{config.PackageManager}, nil
    18  }
    19  
    20  func (s *androidSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
    21  	_, uid, err := resolveSocketByNetlink(network, source, destination)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	if sharedPackage, loaded := s.packageManager.SharedPackageByID(uid % 100000); loaded {
    26  		return &Info{
    27  			UserId:      int32(uid),
    28  			PackageName: sharedPackage,
    29  		}, nil
    30  	}
    31  	if packageName, loaded := s.packageManager.PackageByID(uid % 100000); loaded {
    32  		return &Info{
    33  			UserId:      int32(uid),
    34  			PackageName: packageName,
    35  		}, nil
    36  	}
    37  	return &Info{UserId: int32(uid)}, nil
    38  }