dokodemo.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.ErrMissingApplication
  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.meta, 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(
  96. &proxy.SessionInfo{Source: dest, Destination: v2net.UDPDestination(this.address, this.port)}, payload, this.handleUDPResponse)
  97. }
  98. func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
  99. defer payload.Release()
  100. this.udpMutex.RLock()
  101. defer this.udpMutex.RUnlock()
  102. if !this.accepting {
  103. return
  104. }
  105. this.udpHub.WriteTo(payload.Value, dest)
  106. }
  107. func (this *DokodemoDoor) ListenTCP() error {
  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(this.meta, &proxy.SessionInfo{
  137. Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
  138. Destination: dest,
  139. })
  140. defer ray.InboundOutput().Release()
  141. var inputFinish, outputFinish sync.Mutex
  142. inputFinish.Lock()
  143. outputFinish.Lock()
  144. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  145. defer reader.Release()
  146. go func() {
  147. v2reader := v2io.NewAdaptiveReader(reader)
  148. defer v2reader.Release()
  149. v2io.Pipe(v2reader, ray.InboundInput())
  150. inputFinish.Unlock()
  151. ray.InboundInput().Close()
  152. }()
  153. go func() {
  154. v2writer := v2io.NewAdaptiveWriter(conn)
  155. defer v2writer.Release()
  156. v2io.Pipe(ray.InboundOutput(), v2writer)
  157. outputFinish.Unlock()
  158. }()
  159. outputFinish.Lock()
  160. inputFinish.Lock()
  161. }
  162. type Factory struct{}
  163. func (this *Factory) StreamCapability() internet.StreamConnectionType {
  164. return internet.StreamConnectionTypeRawTCP
  165. }
  166. func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  167. return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
  168. }
  169. func init() {
  170. internal.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
  171. }