default.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/settings"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. "github.com/sagernet/sing/common"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. M "github.com/sagernet/sing/common/metadata"
  13. N "github.com/sagernet/sing/common/network"
  14. "go.uber.org/atomic"
  15. )
  16. var _ adapter.Inbound = (*myInboundAdapter)(nil)
  17. type myInboundAdapter struct {
  18. protocol string
  19. network []string
  20. ctx context.Context
  21. router adapter.Router
  22. logger log.ContextLogger
  23. tag string
  24. listenOptions option.ListenOptions
  25. connHandler adapter.ConnectionHandler
  26. packetHandler adapter.PacketHandler
  27. oobPacketHandler adapter.OOBPacketHandler
  28. packetUpstream any
  29. // http mixed
  30. setSystemProxy bool
  31. clearSystemProxy func() error
  32. // internal
  33. tcpListener net.Listener
  34. udpConn *net.UDPConn
  35. udpAddr M.Socksaddr
  36. packetOutboundClosed chan struct{}
  37. packetOutbound chan *myInboundPacket
  38. inShutdown atomic.Bool
  39. }
  40. func (a *myInboundAdapter) Type() string {
  41. return a.protocol
  42. }
  43. func (a *myInboundAdapter) Tag() string {
  44. return a.tag
  45. }
  46. func (a *myInboundAdapter) Network() []string {
  47. return a.network
  48. }
  49. func (a *myInboundAdapter) Start() error {
  50. var err error
  51. if common.Contains(a.network, N.NetworkTCP) {
  52. _, err = a.ListenTCP()
  53. if err != nil {
  54. return err
  55. }
  56. go a.loopTCPIn()
  57. }
  58. if common.Contains(a.network, N.NetworkUDP) {
  59. _, err = a.ListenUDP()
  60. if err != nil {
  61. return err
  62. }
  63. a.packetOutboundClosed = make(chan struct{})
  64. a.packetOutbound = make(chan *myInboundPacket)
  65. if a.oobPacketHandler != nil {
  66. if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
  67. go a.loopUDPOOBIn()
  68. } else {
  69. go a.loopUDPOOBInThreadSafe()
  70. }
  71. } else {
  72. if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
  73. go a.loopUDPIn()
  74. } else {
  75. go a.loopUDPInThreadSafe()
  76. }
  77. go a.loopUDPOut()
  78. }
  79. }
  80. if a.setSystemProxy {
  81. a.clearSystemProxy, err = settings.SetSystemProxy(a.router, M.SocksaddrFromNet(a.tcpListener.Addr()).Port, a.protocol == C.TypeMixed)
  82. if err != nil {
  83. return E.Cause(err, "set system proxy")
  84. }
  85. }
  86. return nil
  87. }
  88. func (a *myInboundAdapter) Close() error {
  89. a.inShutdown.Store(true)
  90. var err error
  91. if a.clearSystemProxy != nil {
  92. err = a.clearSystemProxy()
  93. }
  94. return E.Errors(err, common.Close(
  95. a.tcpListener,
  96. common.PtrOrNil(a.udpConn),
  97. ))
  98. }
  99. func (a *myInboundAdapter) upstreamHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
  100. return adapter.NewUpstreamHandler(metadata, a.newConnection, a.streamPacketConnection, a)
  101. }
  102. func (a *myInboundAdapter) upstreamContextHandler() adapter.UpstreamHandlerAdapter {
  103. return adapter.NewUpstreamContextHandler(a.newConnection, a.newPacketConnection, a)
  104. }
  105. func (a *myInboundAdapter) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  106. a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
  107. return a.router.RouteConnection(ctx, conn, metadata)
  108. }
  109. func (a *myInboundAdapter) streamPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  110. a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  111. return a.router.RoutePacketConnection(ctx, conn, metadata)
  112. }
  113. func (a *myInboundAdapter) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  114. ctx = log.ContextWithNewID(ctx)
  115. a.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
  116. a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
  117. return a.router.RoutePacketConnection(ctx, conn, metadata)
  118. }
  119. func (a *myInboundAdapter) createMetadata(conn net.Conn, metadata adapter.InboundContext) adapter.InboundContext {
  120. metadata.Inbound = a.tag
  121. metadata.InboundType = a.protocol
  122. metadata.InboundDetour = a.listenOptions.Detour
  123. metadata.InboundOptions = a.listenOptions.InboundOptions
  124. if !metadata.Source.IsValid() {
  125. metadata.Source = M.SocksaddrFromNet(conn.RemoteAddr()).Unwrap()
  126. }
  127. if !metadata.Destination.IsValid() {
  128. metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
  129. }
  130. if tcpConn, isTCP := common.Cast[*net.TCPConn](conn); isTCP {
  131. metadata.OriginDestination = M.SocksaddrFromNet(tcpConn.LocalAddr()).Unwrap()
  132. }
  133. return metadata
  134. }
  135. func (a *myInboundAdapter) newError(err error) {
  136. a.logger.Error(err)
  137. }
  138. func (a *myInboundAdapter) NewError(ctx context.Context, err error) {
  139. NewError(a.logger, ctx, err)
  140. }
  141. func NewError(logger log.ContextLogger, ctx context.Context, err error) {
  142. common.Close(err)
  143. if E.IsClosedOrCanceled(err) {
  144. logger.DebugContext(ctx, "connection closed: ", err)
  145. return
  146. }
  147. logger.ErrorContext(ctx, err)
  148. }