dokodemo.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package dokodemo
  2. import (
  3. "sync"
  4. "github.com/v2ray/v2ray-core/app"
  5. "github.com/v2ray/v2ray-core/app/dispatcher"
  6. "github.com/v2ray/v2ray-core/common/alloc"
  7. v2io "github.com/v2ray/v2ray-core/common/io"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-core/proxy"
  11. "github.com/v2ray/v2ray-core/proxy/internal"
  12. "github.com/v2ray/v2ray-core/transport/internet"
  13. "github.com/v2ray/v2ray-core/transport/internet/udp"
  14. )
  15. type DokodemoDoor struct {
  16. tcpMutex sync.RWMutex
  17. udpMutex sync.RWMutex
  18. config *Config
  19. accepting bool
  20. address v2net.Address
  21. port v2net.Port
  22. packetDispatcher dispatcher.PacketDispatcher
  23. tcpListener *internet.TCPHub
  24. udpHub *udp.UDPHub
  25. udpServer *udp.UDPServer
  26. meta *proxy.InboundHandlerMeta
  27. }
  28. func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
  29. d := &DokodemoDoor{
  30. config: config,
  31. address: config.Address,
  32. port: config.Port,
  33. meta: meta,
  34. }
  35. space.InitializeApplication(func() error {
  36. if !space.HasApp(dispatcher.APP_ID) {
  37. log.Error("Dokodemo: Dispatcher is not found in the space.")
  38. return app.ErrorMissingApplication
  39. }
  40. d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
  41. return nil
  42. })
  43. return d
  44. }
  45. func (this *DokodemoDoor) Port() v2net.Port {
  46. return this.meta.Port
  47. }
  48. func (this *DokodemoDoor) Close() {
  49. this.accepting = false
  50. if this.tcpListener != nil {
  51. this.tcpMutex.Lock()
  52. this.tcpListener.Close()
  53. this.tcpListener = nil
  54. this.tcpMutex.Unlock()
  55. }
  56. if this.udpHub != nil {
  57. this.udpMutex.Lock()
  58. this.udpHub.Close()
  59. this.udpHub = nil
  60. this.udpMutex.Unlock()
  61. }
  62. }
  63. func (this *DokodemoDoor) Start() error {
  64. if this.accepting {
  65. return nil
  66. }
  67. this.accepting = true
  68. if this.config.Network.HasNetwork(v2net.TCPNetwork) {
  69. err := this.ListenTCP()
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. if this.config.Network.HasNetwork(v2net.UDPNetwork) {
  75. err := this.ListenUDP()
  76. if err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. func (this *DokodemoDoor) ListenUDP() error {
  83. this.udpServer = udp.NewUDPServer(this.packetDispatcher)
  84. udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPackets)
  85. if err != nil {
  86. log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  87. return err
  88. }
  89. this.udpMutex.Lock()
  90. this.udpHub = udpHub
  91. this.udpMutex.Unlock()
  92. return nil
  93. }
  94. func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, dest v2net.Destination) {
  95. this.udpServer.Dispatch(dest, v2net.UDPDestination(this.address, this.port), payload, this.handleUDPResponse)
  96. }
  97. func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
  98. defer payload.Release()
  99. this.udpMutex.RLock()
  100. defer this.udpMutex.RUnlock()
  101. if !this.accepting {
  102. return
  103. }
  104. this.udpHub.WriteTo(payload.Value, dest)
  105. }
  106. func (this *DokodemoDoor) ListenTCP() error {
  107. log.Info("Dokodemo: Stream settings: ", this.meta.StreamSettings)
  108. tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
  109. if err != nil {
  110. log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  111. return err
  112. }
  113. this.tcpMutex.Lock()
  114. this.tcpListener = tcpListener
  115. this.tcpMutex.Unlock()
  116. return nil
  117. }
  118. func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
  119. defer conn.Close()
  120. var dest v2net.Destination
  121. if this.config.FollowRedirect {
  122. originalDest := GetOriginalDestination(conn)
  123. if originalDest != nil {
  124. log.Info("Dokodemo: Following redirect to: ", originalDest)
  125. dest = originalDest
  126. }
  127. }
  128. if dest == nil && this.address != nil && this.port > v2net.Port(0) {
  129. dest = v2net.TCPDestination(this.address, this.port)
  130. }
  131. if dest == nil {
  132. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  133. return
  134. }
  135. log.Info("Dokodemo: Handling request to ", dest)
  136. ray := this.packetDispatcher.DispatchToOutbound(dest)
  137. defer ray.InboundOutput().Release()
  138. var inputFinish, outputFinish sync.Mutex
  139. inputFinish.Lock()
  140. outputFinish.Lock()
  141. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  142. defer reader.Release()
  143. go func() {
  144. v2reader := v2io.NewAdaptiveReader(reader)
  145. defer v2reader.Release()
  146. v2io.Pipe(v2reader, ray.InboundInput())
  147. inputFinish.Unlock()
  148. ray.InboundInput().Close()
  149. }()
  150. go func() {
  151. v2writer := v2io.NewAdaptiveWriter(conn)
  152. defer v2writer.Release()
  153. v2io.Pipe(ray.InboundOutput(), v2writer)
  154. outputFinish.Unlock()
  155. }()
  156. outputFinish.Lock()
  157. inputFinish.Lock()
  158. }
  159. type Factory struct{}
  160. func (this *Factory) StreamCapability() internet.StreamConnectionType {
  161. return internet.StreamConnectionTypeRawTCP
  162. }
  163. func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  164. return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
  165. }
  166. func init() {
  167. internal.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
  168. }