inbound.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package adapter
  2. import (
  3. "context"
  4. "net/netip"
  5. "github.com/sagernet/sing-box/common/process"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-box/option"
  8. M "github.com/sagernet/sing/common/metadata"
  9. )
  10. type Inbound interface {
  11. Service
  12. Type() string
  13. Tag() string
  14. }
  15. type TCPInjectableInbound interface {
  16. Inbound
  17. ConnectionHandlerEx
  18. }
  19. type UDPInjectableInbound interface {
  20. Inbound
  21. PacketConnectionHandlerEx
  22. }
  23. type InboundRegistry interface {
  24. option.InboundOptionsRegistry
  25. CreateInbound(ctx context.Context, router Router, logger log.ContextLogger, tag string, outboundType string, options any) (Inbound, error)
  26. }
  27. type InboundContext struct {
  28. Inbound string
  29. InboundType string
  30. IPVersion uint8
  31. Network string
  32. Source M.Socksaddr
  33. Destination M.Socksaddr
  34. User string
  35. Outbound string
  36. // sniffer
  37. Protocol string
  38. Domain string
  39. Client string
  40. SniffContext any
  41. // cache
  42. // Deprecated: implement in rule action
  43. InboundDetour string
  44. LastInbound string
  45. OriginDestination M.Socksaddr
  46. // Deprecated
  47. InboundOptions option.InboundOptions
  48. UDPDisableDomainUnmapping bool
  49. DNSServer string
  50. DestinationAddresses []netip.Addr
  51. SourceGeoIPCode string
  52. GeoIPCode string
  53. ProcessInfo *process.Info
  54. QueryType uint16
  55. FakeIP bool
  56. // rule cache
  57. IPCIDRMatchSource bool
  58. IPCIDRAcceptEmpty bool
  59. SourceAddressMatch bool
  60. SourcePortMatch bool
  61. DestinationAddressMatch bool
  62. DestinationPortMatch bool
  63. DidMatch bool
  64. IgnoreDestinationIPCIDRMatch bool
  65. }
  66. func (c *InboundContext) ResetRuleCache() {
  67. c.IPCIDRMatchSource = false
  68. c.IPCIDRAcceptEmpty = false
  69. c.SourceAddressMatch = false
  70. c.SourcePortMatch = false
  71. c.DestinationAddressMatch = false
  72. c.DestinationPortMatch = false
  73. c.DidMatch = false
  74. }
  75. type inboundContextKey struct{}
  76. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  77. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  78. }
  79. func ContextFrom(ctx context.Context) *InboundContext {
  80. metadata := ctx.Value((*inboundContextKey)(nil))
  81. if metadata == nil {
  82. return nil
  83. }
  84. return metadata.(*InboundContext)
  85. }
  86. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  87. var newMetadata InboundContext
  88. if metadata := ContextFrom(ctx); metadata != nil {
  89. newMetadata = *metadata
  90. }
  91. return WithContext(ctx, &newMetadata), &newMetadata
  92. }
  93. func OverrideContext(ctx context.Context) context.Context {
  94. if metadata := ContextFrom(ctx); metadata != nil {
  95. var newMetadata InboundContext
  96. newMetadata = *metadata
  97. return WithContext(ctx, &newMetadata)
  98. }
  99. return ctx
  100. }