searcher_linux.go 870 B

123456789101112131415161718192021222324252627282930313233343536
  1. //go:build linux && !android
  2. package process
  3. import (
  4. "context"
  5. "net/netip"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/log"
  8. )
  9. var _ Searcher = (*linuxSearcher)(nil)
  10. type linuxSearcher struct {
  11. logger log.ContextLogger
  12. }
  13. func NewSearcher(config Config) (Searcher, error) {
  14. return &linuxSearcher{config.Logger}, nil
  15. }
  16. func (s *linuxSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
  17. inode, uid, err := resolveSocketByNetlink(network, source, destination)
  18. if err != nil {
  19. return nil, err
  20. }
  21. processPath, err := resolveProcessNameByProcSearch(inode, uid)
  22. if err != nil {
  23. s.logger.DebugContext(ctx, "find process path: ", err)
  24. }
  25. return &adapter.ConnectionOwner{
  26. UserId: int32(uid),
  27. ProcessPath: processPath,
  28. }, nil
  29. }