inbound.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) (Inbound, error)
  26. }
  27. type InboundManager interface {
  28. NewService
  29. Inbounds() []Inbound
  30. Get(tag string) (Inbound, bool)
  31. Remove(tag string) error
  32. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) error
  33. }
  34. type InboundContext struct {
  35. Inbound string
  36. InboundType string
  37. IPVersion uint8
  38. Network string
  39. Source M.Socksaddr
  40. Destination M.Socksaddr
  41. User string
  42. Outbound string
  43. // sniffer
  44. Protocol string
  45. Domain string
  46. Client string
  47. SniffContext any
  48. // cache
  49. // Deprecated: implement in rule action
  50. InboundDetour string
  51. LastInbound string
  52. OriginDestination M.Socksaddr
  53. // Deprecated
  54. InboundOptions option.InboundOptions
  55. UDPDisableDomainUnmapping bool
  56. UDPConnect bool
  57. DNSServer string
  58. DestinationAddresses []netip.Addr
  59. SourceGeoIPCode string
  60. GeoIPCode string
  61. ProcessInfo *process.Info
  62. QueryType uint16
  63. FakeIP bool
  64. // rule cache
  65. IPCIDRMatchSource bool
  66. IPCIDRAcceptEmpty bool
  67. SourceAddressMatch bool
  68. SourcePortMatch bool
  69. DestinationAddressMatch bool
  70. DestinationPortMatch bool
  71. DidMatch bool
  72. IgnoreDestinationIPCIDRMatch bool
  73. }
  74. func (c *InboundContext) ResetRuleCache() {
  75. c.IPCIDRMatchSource = false
  76. c.IPCIDRAcceptEmpty = false
  77. c.SourceAddressMatch = false
  78. c.SourcePortMatch = false
  79. c.DestinationAddressMatch = false
  80. c.DestinationPortMatch = false
  81. c.DidMatch = false
  82. }
  83. type inboundContextKey struct{}
  84. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  85. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  86. }
  87. func ContextFrom(ctx context.Context) *InboundContext {
  88. metadata := ctx.Value((*inboundContextKey)(nil))
  89. if metadata == nil {
  90. return nil
  91. }
  92. return metadata.(*InboundContext)
  93. }
  94. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  95. var newMetadata InboundContext
  96. if metadata := ContextFrom(ctx); metadata != nil {
  97. newMetadata = *metadata
  98. }
  99. return WithContext(ctx, &newMetadata), &newMetadata
  100. }
  101. func OverrideContext(ctx context.Context) context.Context {
  102. if metadata := ContextFrom(ctx); metadata != nil {
  103. var newMetadata InboundContext
  104. newMetadata = *metadata
  105. return WithContext(ctx, &newMetadata)
  106. }
  107. return ctx
  108. }