inbound.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Domain string
  30. Protocol string
  31. User string
  32. Outbound string
  33. // cache
  34. InboundDetour string
  35. LastInbound string
  36. OriginDestination M.Socksaddr
  37. InboundOptions option.InboundOptions
  38. DestinationAddresses []netip.Addr
  39. SourceGeoIPCode string
  40. GeoIPCode string
  41. ProcessInfo *process.Info
  42. QueryType uint16
  43. FakeIP bool
  44. // rule cache
  45. IPCIDRMatchSource bool
  46. SourceAddressMatch bool
  47. SourcePortMatch bool
  48. DestinationAddressMatch bool
  49. DestinationPortMatch bool
  50. DidMatch bool
  51. IgnoreDestinationIPCIDRMatch bool
  52. }
  53. func (c *InboundContext) ResetRuleCache() {
  54. c.IPCIDRMatchSource = false
  55. c.SourceAddressMatch = false
  56. c.SourcePortMatch = false
  57. c.DestinationAddressMatch = false
  58. c.DestinationPortMatch = false
  59. c.DidMatch = false
  60. }
  61. type inboundContextKey struct{}
  62. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  63. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  64. }
  65. func ContextFrom(ctx context.Context) *InboundContext {
  66. metadata := ctx.Value((*inboundContextKey)(nil))
  67. if metadata == nil {
  68. return nil
  69. }
  70. return metadata.(*InboundContext)
  71. }
  72. func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
  73. metadata := ContextFrom(ctx)
  74. if metadata != nil {
  75. return ctx, metadata
  76. }
  77. metadata = new(InboundContext)
  78. return WithContext(ctx, metadata), metadata
  79. }
  80. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  81. var newMetadata InboundContext
  82. if metadata := ContextFrom(ctx); metadata != nil {
  83. newMetadata = *metadata
  84. }
  85. return WithContext(ctx, &newMetadata), &newMetadata
  86. }
  87. func OverrideContext(ctx context.Context) context.Context {
  88. if metadata := ContextFrom(ctx); metadata != nil {
  89. var newMetadata InboundContext
  90. newMetadata = *metadata
  91. return WithContext(ctx, &newMetadata)
  92. }
  93. return ctx
  94. }