inbound.go 3.3 KB

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