dokodemo.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package dokodemo
  2. import (
  3. "sync"
  4. "errors"
  5. "v2ray.com/core/app"
  6. "v2ray.com/core/app/dispatcher"
  7. "v2ray.com/core/common/alloc"
  8. v2io "v2ray.com/core/common/io"
  9. "v2ray.com/core/common/loader"
  10. "v2ray.com/core/common/log"
  11. v2net "v2ray.com/core/common/net"
  12. "v2ray.com/core/proxy"
  13. "v2ray.com/core/proxy/registry"
  14. "v2ray.com/core/transport/internet"
  15. "v2ray.com/core/transport/internet/udp"
  16. )
  17. type DokodemoDoor struct {
  18. tcpMutex sync.RWMutex
  19. udpMutex sync.RWMutex
  20. config *Config
  21. accepting bool
  22. address v2net.Address
  23. port v2net.Port
  24. packetDispatcher dispatcher.PacketDispatcher
  25. tcpListener *internet.TCPHub
  26. udpHub *udp.UDPHub
  27. udpServer *udp.UDPServer
  28. meta *proxy.InboundHandlerMeta
  29. }
  30. func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
  31. d := &DokodemoDoor{
  32. config: config,
  33. address: config.GetPredefinedAddress(),
  34. port: v2net.Port(config.Port),
  35. meta: meta,
  36. }
  37. space.InitializeApplication(func() error {
  38. if !space.HasApp(dispatcher.APP_ID) {
  39. return errors.New("Dokodemo: Dispatcher is not found in the space.")
  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.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. Concurrency: 2,
  90. })
  91. if err != nil {
  92. log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  93. return err
  94. }
  95. this.udpMutex.Lock()
  96. this.udpHub = udpHub
  97. this.udpMutex.Unlock()
  98. return nil
  99. }
  100. func (this *DokodemoDoor) handleUDPPackets(payload *alloc.Buffer, session *proxy.SessionInfo) {
  101. if session.Destination.Network == v2net.Network_Unknown && this.address != nil && this.port > 0 {
  102. session.Destination = v2net.UDPDestination(this.address, this.port)
  103. }
  104. if session.Destination.Network == v2net.Network_Unknown {
  105. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  106. return
  107. }
  108. session.Inbound = this.meta
  109. this.udpServer.Dispatch(session, payload, this.handleUDPResponse)
  110. }
  111. func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *alloc.Buffer) {
  112. defer payload.Release()
  113. this.udpMutex.RLock()
  114. defer this.udpMutex.RUnlock()
  115. if !this.accepting {
  116. return
  117. }
  118. this.udpHub.WriteTo(payload.Value, dest)
  119. }
  120. func (this *DokodemoDoor) ListenTCP() error {
  121. tcpListener, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, this.meta.StreamSettings)
  122. if err != nil {
  123. log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
  124. return err
  125. }
  126. this.tcpMutex.Lock()
  127. this.tcpListener = tcpListener
  128. this.tcpMutex.Unlock()
  129. return nil
  130. }
  131. func (this *DokodemoDoor) HandleTCPConnection(conn internet.Connection) {
  132. defer conn.Close()
  133. var dest v2net.Destination
  134. if this.config.FollowRedirect {
  135. originalDest := GetOriginalDestination(conn)
  136. if originalDest.Network != v2net.Network_Unknown {
  137. log.Info("Dokodemo: Following redirect to: ", originalDest)
  138. dest = originalDest
  139. }
  140. }
  141. if dest.Network == v2net.Network_Unknown && this.address != nil && this.port > v2net.Port(0) {
  142. dest = v2net.TCPDestination(this.address, this.port)
  143. }
  144. if dest.Network == v2net.Network_Unknown {
  145. log.Info("Dokodemo: Unknown destination, stop forwarding...")
  146. return
  147. }
  148. log.Info("Dokodemo: Handling request to ", dest)
  149. ray := this.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{
  150. Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
  151. Destination: dest,
  152. Inbound: this.meta,
  153. })
  154. defer ray.InboundOutput().Release()
  155. var wg sync.WaitGroup
  156. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  157. defer reader.Release()
  158. wg.Add(1)
  159. go func() {
  160. v2reader := v2io.NewAdaptiveReader(reader)
  161. defer v2reader.Release()
  162. if err := v2io.PipeUntilEOF(v2reader, ray.InboundInput()); err != nil {
  163. log.Info("Dokodemo: Failed to transport all TCP request: ", err)
  164. }
  165. wg.Done()
  166. ray.InboundInput().Close()
  167. }()
  168. wg.Add(1)
  169. go func() {
  170. v2writer := v2io.NewAdaptiveWriter(conn)
  171. defer v2writer.Release()
  172. if err := v2io.PipeUntilEOF(ray.InboundOutput(), v2writer); err != nil {
  173. log.Info("Dokodemo: Failed to transport all TCP response: ", err)
  174. }
  175. wg.Done()
  176. }()
  177. wg.Wait()
  178. }
  179. type Factory struct{}
  180. func (this *Factory) StreamCapability() v2net.NetworkList {
  181. return v2net.NetworkList{
  182. Network: []v2net.Network{v2net.Network_RawTCP},
  183. }
  184. }
  185. func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  186. return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
  187. }
  188. func init() {
  189. registry.MustRegisterInboundHandlerCreator(loader.GetType(new(Config)), new(Factory))
  190. }