inbound.go 3.5 KB

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