dokodemo.go 4.5 KB

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