inbound.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package adapter
  2. import (
  3. "context"
  4. "net"
  5. "net/netip"
  6. "time"
  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. "github.com/miekg/dns"
  12. )
  13. type Inbound interface {
  14. Lifecycle
  15. Type() string
  16. Tag() string
  17. }
  18. type TCPInjectableInbound interface {
  19. Inbound
  20. ConnectionHandlerEx
  21. }
  22. type UDPInjectableInbound interface {
  23. Inbound
  24. PacketConnectionHandlerEx
  25. }
  26. type InboundRegistry interface {
  27. option.InboundOptionsRegistry
  28. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) (Inbound, error)
  29. }
  30. type InboundManager interface {
  31. Lifecycle
  32. Inbounds() []Inbound
  33. Get(tag string) (Inbound, bool)
  34. Remove(tag string) error
  35. Create(ctx context.Context, router Router, logger log.ContextLogger, tag string, inboundType string, options any) error
  36. }
  37. type InboundContext struct {
  38. Inbound string
  39. InboundType string
  40. IPVersion uint8
  41. Network string
  42. Source M.Socksaddr
  43. Destination M.Socksaddr
  44. User string
  45. Outbound string
  46. // sniffer
  47. Protocol string
  48. Domain string
  49. Client string
  50. SniffContext any
  51. SnifferNames []string
  52. SniffError error
  53. // cache
  54. // Deprecated: implement in rule action
  55. InboundDetour string
  56. LastInbound string
  57. OriginDestination M.Socksaddr
  58. RouteOriginalDestination M.Socksaddr
  59. UDPDisableDomainUnmapping bool
  60. UDPConnect bool
  61. UDPTimeout time.Duration
  62. TLSFragment bool
  63. TLSFragmentFallbackDelay time.Duration
  64. TLSRecordFragment bool
  65. NetworkStrategy *C.NetworkStrategy
  66. NetworkType []C.InterfaceType
  67. FallbackNetworkType []C.InterfaceType
  68. FallbackDelay time.Duration
  69. DestinationAddresses []netip.Addr
  70. DNSResponse *dns.Msg
  71. DestinationAddressMatchFromResponse bool
  72. SourceGeoIPCode string
  73. GeoIPCode string
  74. ProcessInfo *ConnectionOwner
  75. SourceMACAddress net.HardwareAddr
  76. SourceHostname string
  77. QueryType uint16
  78. FakeIP bool
  79. // rule cache
  80. IPCIDRMatchSource bool
  81. IPCIDRAcceptEmpty bool
  82. SourceAddressMatch bool
  83. SourcePortMatch bool
  84. DestinationAddressMatch bool
  85. DestinationPortMatch bool
  86. DidMatch bool
  87. IgnoreDestinationIPCIDRMatch bool
  88. }
  89. func (c *InboundContext) ResetRuleCache() {
  90. c.IPCIDRMatchSource = false
  91. c.IPCIDRAcceptEmpty = false
  92. c.ResetRuleMatchCache()
  93. }
  94. func (c *InboundContext) ResetRuleMatchCache() {
  95. c.SourceAddressMatch = false
  96. c.SourcePortMatch = false
  97. c.DestinationAddressMatch = false
  98. c.DestinationPortMatch = false
  99. c.DidMatch = false
  100. }
  101. func (c *InboundContext) DNSResponseAddressesForMatch() []netip.Addr {
  102. return DNSResponseAddresses(c.DNSResponse)
  103. }
  104. func DNSResponseAddresses(response *dns.Msg) []netip.Addr {
  105. if response == nil || response.Rcode != dns.RcodeSuccess {
  106. return nil
  107. }
  108. addresses := make([]netip.Addr, 0, len(response.Answer))
  109. for _, rawRecord := range response.Answer {
  110. switch record := rawRecord.(type) {
  111. case *dns.A:
  112. addr := M.AddrFromIP(record.A)
  113. if addr.IsValid() {
  114. addresses = append(addresses, addr)
  115. }
  116. case *dns.AAAA:
  117. addr := M.AddrFromIP(record.AAAA)
  118. if addr.IsValid() {
  119. addresses = append(addresses, addr)
  120. }
  121. case *dns.HTTPS:
  122. for _, value := range record.SVCB.Value {
  123. switch hint := value.(type) {
  124. case *dns.SVCBIPv4Hint:
  125. for _, ip := range hint.Hint {
  126. addr := M.AddrFromIP(ip).Unmap()
  127. if addr.IsValid() {
  128. addresses = append(addresses, addr)
  129. }
  130. }
  131. case *dns.SVCBIPv6Hint:
  132. for _, ip := range hint.Hint {
  133. addr := M.AddrFromIP(ip)
  134. if addr.IsValid() {
  135. addresses = append(addresses, addr)
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return addresses
  143. }
  144. type inboundContextKey struct{}
  145. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  146. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  147. }
  148. func ContextFrom(ctx context.Context) *InboundContext {
  149. metadata := ctx.Value((*inboundContextKey)(nil))
  150. if metadata == nil {
  151. return nil
  152. }
  153. return metadata.(*InboundContext)
  154. }
  155. func ExtendContext(ctx context.Context) (context.Context, *InboundContext) {
  156. var newMetadata InboundContext
  157. if metadata := ContextFrom(ctx); metadata != nil {
  158. newMetadata = *metadata
  159. }
  160. return WithContext(ctx, &newMetadata), &newMetadata
  161. }
  162. func OverrideContext(ctx context.Context) context.Context {
  163. if metadata := ContextFrom(ctx); metadata != nil {
  164. newMetadata := *metadata
  165. return WithContext(ctx, &newMetadata)
  166. }
  167. return ctx
  168. }