router.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package adapter
  2. import (
  3. "context"
  4. "net"
  5. "time"
  6. "github.com/sagernet/sing-tun"
  7. N "github.com/sagernet/sing/common/network"
  8. "github.com/sagernet/sing/common/x/list"
  9. "go4.org/netipx"
  10. )
  11. type Router interface {
  12. Lifecycle
  13. ConnectionRouter
  14. PreMatch(metadata InboundContext, context tun.DirectRouteContext, timeout time.Duration, supportBypass bool) (tun.DirectRouteDestination, error)
  15. ConnectionRouterEx
  16. RuleSet(tag string) (RuleSet, bool)
  17. Rules() []Rule
  18. NeedFindProcess() bool
  19. NeedFindNeighbor() bool
  20. NeighborResolver() NeighborResolver
  21. AppendTracker(tracker ConnectionTracker)
  22. ResetNetwork()
  23. }
  24. type ConnectionTracker interface {
  25. RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule, matchOutbound Outbound) net.Conn
  26. RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule, matchOutbound Outbound) N.PacketConn
  27. }
  28. // Deprecated: Use ConnectionRouterEx instead.
  29. type ConnectionRouter interface {
  30. RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
  31. RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
  32. }
  33. type ConnectionRouterEx interface {
  34. ConnectionRouter
  35. RouteConnectionEx(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
  36. RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
  37. }
  38. type RuleSet interface {
  39. Name() string
  40. StartContext(ctx context.Context, startContext *HTTPStartContext) error
  41. PostStart() error
  42. Metadata() RuleSetMetadata
  43. ExtractIPSet() []*netipx.IPSet
  44. IncRef()
  45. DecRef()
  46. Cleanup()
  47. RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
  48. UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
  49. Close() error
  50. HeadlessRule
  51. }
  52. type RuleSetUpdateCallback func(it RuleSet)
  53. type DNSRuleSetUpdateValidator interface {
  54. ValidateRuleSetMetadataUpdate(tag string, metadata RuleSetMetadata) error
  55. }
  56. // ip_version is not a headless-rule item, so ContainsIPVersionRule is intentionally absent.
  57. type RuleSetMetadata struct {
  58. ContainsProcessRule bool
  59. ContainsWIFIRule bool
  60. ContainsIPCIDRRule bool
  61. ContainsDNSQueryTypeRule bool
  62. // ContainsNonIPCIDRRule signals that the rule-set carries at least one sub-rule
  63. // with a predicate other than destination ip_cidr / ip_set, so it can contribute
  64. // to DNS pre-response matching. A rule-set where this is false and
  65. // ContainsIPCIDRRule is true is "pure-IP" and matches nothing before a DNS
  66. // response is available.
  67. ContainsNonIPCIDRRule bool
  68. }