inbound.go 2.7 KB

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