dokodemo.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package dokodemo
  2. import (
  3. "sync"
  4. "v2ray.com/core/app"
  5. "v2ray.com/core/app/dispatcher"
  6. "v2ray.com/core/common/alloc"
  7. v2io "v2ray.com/core/common/io"
  8. "v2ray.com/core/common/loader"
  9. "v2ray.com/core/common/log"
  10. v2net "v2ray.com/core/common/net"
  11. "v2ray.com/core/proxy"
  12. "v2ray.com/core/proxy/registry"
  13. "v2ray.com/core/transport/internet"
  14. "v2ray.com/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.GetPredefinedAddress(),
  33. port: v2net.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.NetworkList.HasNetwork(v2net.Network_TCP) {
  70. err := this.ListenTCP()
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. if this.config.NetworkList.HasNetwork(v2net.Network_UDP) {
  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(
  86. this.meta.Address, this.meta.Port, udp.ListenOption{
  87. Callback: this.handleUDPPackets,
  88. ReceiveOriginalDest: this.config.FollowRedirect,
  89. })
  90. if err != nil {
  91. log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  92. return err
  93. }
  94. this.udpMutex.Lock()
  95. this.udpHub = udpHub
  96. this.udpMutex.Unlock()
  97. return nil
  98. }
  99. func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) {
  100. if session.Destination.Network == v2net.Network_Unknown && this.address != nil && this.port > 0 {
  101. session.Destination = v2net.UDPDestination(this.address, this.port)
  102. }
  103. if session.Destination.Network == v2net.Network_Unknown {
  104. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  105. return
  106. }
  107. this.udpServer.Dispatch(session, payload, this.handleUDPResponse)
  108. }
  109. func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
  110. defer payload.Release()
  111. this.udpMutex.RLock()
  112. defer this.udpMutex.RUnlock()
  113. if !this.accepting {
  114. return
  115. }
  116. this.udpHub.WriteTo(payload.Value, dest)
  117. }
  118. func (this *DokodemoDoor) ListenTCP() error {
  119. tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
  120. if err != nil {
  121. log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  122. return err
  123. }
  124. this.tcpMutex.Lock()
  125. this.tcpListener = tcpListener
  126. this.tcpMutex.Unlock()
  127. return nil
  128. }
  129. func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
  130. defer conn.Close()
  131. var dest v2net.Destination
  132. if this.config.FollowRedirect {
  133. originalDest := GetOriginalDestination(conn)
  134. if originalDest.Network != v2net.Network_Unknown {
  135. log.Info("Dokodemo: Following redirect to: ", originalDest)
  136. dest = originalDest
  137. }
  138. }
  139. if dest.Network == v2net.Network_Unknown && this.address != nil && this.port > v2net.Port(0) {
  140. dest = v2net.TCPDestination(this.address, this.port)
  141. }
  142. if dest.Network == v2net.Network_Unknown {
  143. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  144. return
  145. }
  146. log.Info("Dokodemo: Handling request to ", dest)
  147. ray := this.packetDispatcher.DispatchToOutbound(this.meta, &proxy.SessionInfo{
  148. Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
  149. Destination: dest,
  150. })
  151. defer ray.InboundOutput().Release()
  152. var wg sync.WaitGroup
  153. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  154. defer reader.Release()
  155. wg.Add(1)
  156. go func() {
  157. v2reader := v2io.NewAdaptiveReader(reader)
  158. defer v2reader.Release()
  159. v2io.Pipe(v2reader, ray.InboundInput())
  160. wg.Done()
  161. ray.InboundInput().Close()
  162. }()
  163. wg.Add(1)
  164. go func() {
  165. v2writer := v2io.NewAdaptiveWriter(conn)
  166. defer v2writer.Release()
  167. v2io.Pipe(ray.InboundOutput(), v2writer)
  168. wg.Done()
  169. }()
  170. wg.Wait()
  171. }
  172. type Factory struct{}
  173. func (this *Factory) StreamCapability() v2net.NetworkList {
  174. return v2net.NetworkList{
  175. Network: []v2net.Network{v2net.Network_RawTCP},
  176. }
  177. }
  178. func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  179. return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
  180. }
  181. func init() {
  182. registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
  183. }