searcher.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. ProcessPath string
  21. PackageName string
  22. User string
  23. UserId int32
  24. }
  25. func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
  26. info, err := searcher.FindProcessInfo(ctx, network, source, destination)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if info.UserId != -1 {
  31. osUser, _ := user.LookupId(F.ToString(info.UserId))
  32. if osUser != nil {
  33. info.User = osUser.Username
  34. }
  35. }
  36. return info, nil
  37. }