inbound.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package adapter
  2. import (
  3. "context"
  4. "net/netip"
  5. "time"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/log"
  8. "github.com/sagernet/sing-box/option"
  9. M "github.com/sagernet/sing/common/metadata"
  10. )
  11. type Inbound interface {
  12. Lifecycle
  13. Type() string
  14. Tag() string
  15. }
  16. type TCPInjectableInbound interface {
  17. Inbound
  18. ConnectionHandlerEx
  19. }
  20. type UDPInjectableInbound interface {
  21. Inbound
  22. PacketConnectionHandlerEx
  23. }
  24. type InboundRegistry interface {
  25. option.InboundOptionsRegistry
  26. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) (Inbound, error)
  27. }
  28. type InboundManager interface {
  29. Lifecycle
  30. Inbounds() []Inbound
  31. Get(tag string) (Inbound, bool)
  32. Remove(tag string) error
  33. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) error
  34. }
  35. type InboundContext struct {
  36. Inbound string
  37. InboundType string
  38. IPVersion uint8
  39. Network string
  40. Source M.Socksaddr
  41. Destination M.Socksaddr
  42. User string
  43. Outbound string
  44. // sniffer
  45. Protocol string
  46. Domain string
  47. Client string
  48. SniffContext any
  49. SnifferNames []string
  50. SniffError error
  51. // cache
  52. // Deprecated: implement in rule action
  53. InboundDetour string
  54. LastInbound string
  55. OriginDestination M.Socksaddr
  56. RouteOriginalDestination M.Socksaddr
  57. UDPDisableDomainUnmapping bool
  58. UDPConnect bool
  59. UDPTimeout time.Duration
  60. TLSFragment bool
  61. TLSFragmentFallbackDelay time.Duration
  62. TLSRecordFragment bool
  63. NetworkStrategy *C.NetworkStrategy
  64. NetworkType []C.InterfaceType
  65. FallbackNetworkType []C.InterfaceType
  66. FallbackDelay time.Duration
  67. DestinationAddresses []netip.Addr
  68. SourceGeoIPCode string
  69. GeoIPCode string
  70. ProcessInfo *ConnectionOwner
  71. QueryType uint16
  72. FakeIP bool
  73. // rule cache
  74. IPCIDRMatchSource bool
  75. IPCIDRAcceptEmpty bool
  76. SourceAddressMatch bool
  77. SourcePortMatch bool
  78. DestinationAddressMatch bool
  79. DestinationPortMatch bool
  80. DidMatch bool
  81. IgnoreDestinationIPCIDRMatch bool
  82. }
  83. func (c *InboundContext) ResetRuleCache() {
  84. c.IPCIDRMatchSource = false
  85. c.IPCIDRAcceptEmpty = false
  86. c.ResetRuleMatchCache()
  87. }
  88. func (c *InboundContext) ResetRuleMatchCache() {
  89. c.SourceAddressMatch = false
  90. c.SourcePortMatch = false
  91. c.DestinationAddressMatch = false
  92. c.DestinationPortMatch = false
  93. c.DidMatch = false
  94. }
  95. type inboundContextKey struct{}
  96. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  97. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  98. }
  99. func ContextFrom(ctx context.Context) *InboundContext {
  100. metadata := ctx.Value((*inboundContextKey)(nil))
  101. if metadata == nil {
  102. return nil
  103. }
  104. return metadata.(*InboundContext)
  105. }
  106. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  107. var newMetadata InboundContext
  108. if metadata := ContextFrom(ctx); metadata != nil {
  109. newMetadata = *metadata
  110. }
  111. return WithContext(ctx, &newMetadata), &newMetadata
  112. }
  113. func OverrideContext(ctx context.Context) context.Context {
  114. if metadata := ContextFrom(ctx); metadata != nil {
  115. newMetadata := *metadata
  116. return WithContext(ctx, &newMetadata)
  117. }
  118. return ctx
  119. }