searcher.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package process
  2. import (
  3. "context"
  4. "net/netip"
  5. "os/user"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-tun"
  8. E "github.com/sagernet/sing/common/exceptions"
  9. F "github.com/sagernet/sing/common/format"
  10. )
  11. type Searcher interface {
  12. FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error)
  13. }
  14. var ErrNotFound = E.New("process not found")
  15. type Config struct {
  16. Logger log.ContextLogger
  17. PackageManager tun.PackageManager
  18. }
  19. type Info struct {
  20. ProcessID uint32
  21. ProcessPath string
  22. PackageName string
  23. User string
  24. UserId int32
  25. }
  26. func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
  27. info, err := searcher.FindProcessInfo(ctx, network, source, destination)
  28. if err != nil {
  29. return nil, err
  30. }
  31. if info.UserId != -1 {
  32. osUser, _ := user.LookupId(F.ToString(info.UserId))
  33. if osUser != nil {
  34. info.User = osUser.Username
  35. }
  36. }
  37. return info, nil
  38. }