inbound.go 3.4 KB

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