dokodemo.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, udp.ListenOption{Callback: 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, session *proxy.SessionInfo) {
  95. if session.Destination == nil && this.address != nil && this.port > 0 {
  96. session.Destination = v2net.UDPDestination(this.address, this.port)
  97. }
  98. if session.Destination == nil {
  99. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  100. return
  101. }
  102. this.udpServer.Dispatch(session, payload, this.handleUDPResponse)
  103. }
  104. func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
  105. defer payload.Release()
  106. this.udpMutex.RLock()
  107. defer this.udpMutex.RUnlock()
  108. if !this.accepting {
  109. return
  110. }
  111. this.udpHub.WriteTo(payload.Value, dest)
  112. }
  113. func (this *DokodemoDoor) ListenTCP() error {
  114. tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
  115. if err != nil {
  116. log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  117. return err
  118. }
  119. this.tcpMutex.Lock()
  120. this.tcpListener = tcpListener
  121. this.tcpMutex.Unlock()
  122. return nil
  123. }
  124. func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
  125. defer conn.Close()
  126. var dest v2net.Destination
  127. if this.config.FollowRedirect {
  128. originalDest := GetOriginalDestination(conn)
  129. if originalDest != nil {
  130. log.Info("Dokodemo: Following redirect to: ", originalDest)
  131. dest = originalDest
  132. }
  133. }
  134. if dest == nil && this.address != nil && this.port > v2net.Port(0) {
  135. dest = v2net.TCPDestination(this.address, this.port)
  136. }
  137. if dest == nil {
  138. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  139. return
  140. }
  141. log.Info("Dokodemo: Handling request to ", dest)
  142. ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
  143. Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
  144. Destination: dest,
  145. })
  146. defer ray.InboundOutput().Release()
  147. var inputFinish, outputFinish sync.Mutex
  148. inputFinish.Lock()
  149. outputFinish.Lock()
  150. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  151. defer reader.Release()
  152. go func() {
  153. v2reader := v2io.NewAdaptiveReader(reader)
  154. defer v2reader.Release()
  155. v2io.Pipe(v2reader, ray.InboundInput())
  156. inputFinish.Unlock()
  157. ray.InboundInput().Close()
  158. }()
  159. go func() {
  160. v2writer := v2io.NewAdaptiveWriter(conn)
  161. defer v2writer.Release()
  162. v2io.Pipe(ray.InboundOutput(), v2writer)
  163. outputFinish.Unlock()
  164. }()
  165. outputFinish.Lock()
  166. inputFinish.Lock()
  167. }
  168. type Factory struct{}
  169. func (this *Factory) StreamCapability() internet.StreamConnectionType {
  170. return internet.StreamConnectionTypeRawTCP
  171. }
  172. func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  173. return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
  174. }
  175. func init() {
  176. internal.MustRegisterInboundHandlerCreator("dokodemo-door", new(Factory))
  177. }