platform_searcher.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package route
  2. import (
  3. "context"
  4. "net/netip"
  5. "syscall"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/process"
  8. N "github.com/sagernet/sing/common/network"
  9. )
  10. type platformSearcher struct {
  11. platform adapter.PlatformInterface
  12. }
  13. func newPlatformSearcher(platform adapter.PlatformInterface) process.Searcher {
  14. return &platformSearcher{platform: platform}
  15. }
  16. func (s *platformSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
  17. if !s.platform.UsePlatformConnectionOwnerFinder() {
  18. return nil, process.ErrNotFound
  19. }
  20. var ipProtocol int32
  21. switch N.NetworkName(network) {
  22. case N.NetworkTCP:
  23. ipProtocol = syscall.IPPROTO_TCP
  24. case N.NetworkUDP:
  25. ipProtocol = syscall.IPPROTO_UDP
  26. default:
  27. return nil, process.ErrNotFound
  28. }
  29. request := &adapter.FindConnectionOwnerRequest{
  30. IpProtocol: ipProtocol,
  31. SourceAddress: source.Addr().String(),
  32. SourcePort: int32(source.Port()),
  33. DestinationAddress: destination.Addr().String(),
  34. DestinationPort: int32(destination.Port()),
  35. }
  36. return s.platform.FindConnectionOwner(request)
  37. }