searcher.go 1009 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. }
  15. var ErrNotFound = E.New("process not found")
  16. type Config struct {
  17. Logger log.ContextLogger
  18. PackageManager tun.PackageManager
  19. }
  20. func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
  21. info, err := searcher.FindProcessInfo(ctx, network, source, destination)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if info.UserId != -1 {
  26. osUser, _ := user.LookupId(F.ToString(info.UserId))
  27. if osUser != nil {
  28. info.UserName = osUser.Username
  29. }
  30. }
  31. return info, nil
  32. }