inbound.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. HTTPRequest *http.Request
  53. ClientHello *tls.ClientHelloInfo
  54. // cache
  55. // Deprecated: implement in rule action
  56. InboundDetour string
  57. LastInbound string
  58. OriginDestination M.Socksaddr
  59. RouteOriginalDestination M.Socksaddr
  60. // Deprecated: to be removed
  61. //nolint:staticcheck
  62. InboundOptions option.InboundOptions
  63. UDPDisableDomainUnmapping bool
  64. UDPConnect bool
  65. UDPTimeout time.Duration
  66. TLSFragment bool
  67. TLSFragmentFallbackDelay time.Duration
  68. MITM *option.MITMRouteOptions
  69. NetworkStrategy *C.NetworkStrategy
  70. NetworkType []C.InterfaceType
  71. FallbackNetworkType []C.InterfaceType
  72. FallbackDelay time.Duration
  73. DestinationAddresses []netip.Addr
  74. SourceGeoIPCode string
  75. GeoIPCode string
  76. ProcessInfo *process.Info
  77. QueryType uint16
  78. FakeIP bool
  79. // rule cache
  80. IPCIDRMatchSource bool
  81. IPCIDRAcceptEmpty bool
  82. SourceAddressMatch bool
  83. SourcePortMatch bool
  84. DestinationAddressMatch bool
  85. DestinationPortMatch bool
  86. DidMatch bool
  87. IgnoreDestinationIPCIDRMatch bool
  88. }
  89. func (c *InboundContext) ResetRuleCache() {
  90. c.IPCIDRMatchSource = false
  91. c.IPCIDRAcceptEmpty = false
  92. c.SourceAddressMatch = false
  93. c.SourcePortMatch = false
  94. c.DestinationAddressMatch = false
  95. c.DestinationPortMatch = false
  96. c.DidMatch = false
  97. }
  98. type inboundContextKey struct{}
  99. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  100. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  101. }
  102. func ContextFrom(ctx context.Context) *InboundContext {
  103. metadata := ctx.Value((*inboundContextKey)(nil))
  104. if metadata == nil {
  105. return nil
  106. }
  107. return metadata.(*InboundContext)
  108. }
  109. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  110. var newMetadata InboundContext
  111. if metadata := ContextFrom(ctx); metadata != nil {
  112. newMetadata = *metadata
  113. }
  114. return WithContext(ctx, &newMetadata), &newMetadata
  115. }
  116. func OverrideContext(ctx context.Context) context.Context {
  117. if metadata := ContextFrom(ctx); metadata != nil {
  118. var newMetadata InboundContext
  119. newMetadata = *metadata
  120. return WithContext(ctx, &newMetadata)
  121. }
  122. return ctx
  123. }