searcher.go 1.0 KB

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