searcher.go 994 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package process
  2. import (
  3. "context"
  4. "net/netip"
  5. "github.com/sagernet/sing-box/log"
  6. "github.com/sagernet/sing-tun"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. )
  9. type Searcher interface {
  10. FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error)
  11. }
  12. var ErrNotFound = E.New("process not found")
  13. type Config struct {
  14. Logger log.ContextLogger
  15. PackageManager tun.PackageManager
  16. }
  17. type Info struct {
  18. ProcessPath string
  19. PackageName string
  20. User string
  21. UserId int32
  22. }
  23. func FindProcessInfo(searcher Searcher, ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*Info, error) {
  24. info, err := findProcessInfo(searcher, ctx, network, source, destination)
  25. if err != nil {
  26. if source.Addr().Is4In6() {
  27. info, err = findProcessInfo(searcher, ctx, network, netip.AddrPortFrom(netip.AddrFrom4(source.Addr().As4()), source.Port()), destination)
  28. }
  29. }
  30. return info, err
  31. }