dokodemo.go 5.1 KB

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