dokodemo.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package dokodemo
  2. import (
  3. "io"
  4. "net"
  5. "sync"
  6. "github.com/v2ray/v2ray-core/app"
  7. "github.com/v2ray/v2ray-core/common/alloc"
  8. "github.com/v2ray/v2ray-core/common/log"
  9. v2net "github.com/v2ray/v2ray-core/common/net"
  10. "github.com/v2ray/v2ray-core/common/retry"
  11. )
  12. type DokodemoDoor struct {
  13. tcpMutex sync.RWMutex
  14. udpMutex sync.RWMutex
  15. config *Config
  16. accepting bool
  17. address v2net.Address
  18. port v2net.Port
  19. space app.Space
  20. tcpListener *net.TCPListener
  21. udpConn *net.UDPConn
  22. }
  23. func NewDokodemoDoor(space app.Space, config *Config) *DokodemoDoor {
  24. return &DokodemoDoor{
  25. config: config,
  26. space: space,
  27. address: config.Address,
  28. port: config.Port,
  29. }
  30. }
  31. func (this *DokodemoDoor) Close() {
  32. this.accepting = false
  33. if this.tcpListener != nil {
  34. this.tcpListener.Close()
  35. this.tcpMutex.Lock()
  36. this.tcpListener = nil
  37. this.tcpMutex.Unlock()
  38. }
  39. if this.udpConn != nil {
  40. this.udpConn.Close()
  41. this.udpMutex.Lock()
  42. this.udpConn = nil
  43. this.udpMutex.Unlock()
  44. }
  45. }
  46. func (this *DokodemoDoor) Listen(port v2net.Port) error {
  47. this.accepting = true
  48. if this.config.Network.HasNetwork(v2net.TCPNetwork) {
  49. err := this.ListenTCP(port)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. if this.config.Network.HasNetwork(v2net.UDPNetwork) {
  55. err := this.ListenUDP(port)
  56. if err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
  63. udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
  64. IP: []byte{0, 0, 0, 0},
  65. Port: int(port),
  66. Zone: "",
  67. })
  68. if err != nil {
  69. log.Error("Dokodemo failed to listen on port ", port, ": ", err)
  70. return err
  71. }
  72. this.udpMutex.Lock()
  73. this.udpConn = udpConn
  74. this.udpMutex.Unlock()
  75. go this.handleUDPPackets()
  76. return nil
  77. }
  78. func (this *DokodemoDoor) handleUDPPackets() {
  79. for this.accepting {
  80. buffer := alloc.NewBuffer()
  81. this.udpMutex.RLock()
  82. if !this.accepting {
  83. this.udpMutex.RUnlock()
  84. return
  85. }
  86. nBytes, addr, err := this.udpConn.ReadFromUDP(buffer.Value)
  87. this.udpMutex.RUnlock()
  88. buffer.Slice(0, nBytes)
  89. if err != nil {
  90. buffer.Release()
  91. log.Error("Dokodemo failed to read from UDP: ", err)
  92. return
  93. }
  94. packet := v2net.NewPacket(v2net.UDPDestination(this.address, this.port), buffer, false)
  95. ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
  96. close(ray.InboundInput())
  97. for payload := range ray.InboundOutput() {
  98. this.udpMutex.RLock()
  99. if !this.accepting {
  100. this.udpMutex.RUnlock()
  101. return
  102. }
  103. this.udpConn.WriteToUDP(payload.Value, addr)
  104. this.udpMutex.RUnlock()
  105. }
  106. }
  107. }
  108. func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
  109. tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
  110. IP: []byte{0, 0, 0, 0},
  111. Port: int(port),
  112. Zone: "",
  113. })
  114. if err != nil {
  115. log.Error("Dokodemo failed to listen on port ", port, ": ", err)
  116. return err
  117. }
  118. this.tcpMutex.Lock()
  119. this.tcpListener = tcpListener
  120. this.tcpMutex.Unlock()
  121. go this.AcceptTCPConnections()
  122. return nil
  123. }
  124. func (this *DokodemoDoor) AcceptTCPConnections() {
  125. for this.accepting {
  126. retry.Timed(100, 100).On(func() error {
  127. this.tcpMutex.RLock()
  128. defer this.tcpMutex.RUnlock()
  129. if !this.accepting {
  130. return nil
  131. }
  132. connection, err := this.tcpListener.AcceptTCP()
  133. if err != nil {
  134. log.Error("Dokodemo failed to accept new connections: ", err)
  135. return err
  136. }
  137. go this.HandleTCPConnection(connection)
  138. return nil
  139. })
  140. }
  141. }
  142. func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) {
  143. defer conn.Close()
  144. packet := v2net.NewPacket(v2net.TCPDestination(this.address, this.port), nil, true)
  145. ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
  146. var inputFinish, outputFinish sync.Mutex
  147. inputFinish.Lock()
  148. outputFinish.Lock()
  149. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  150. go dumpInput(reader, ray.InboundInput(), &inputFinish)
  151. go dumpOutput(conn, ray.InboundOutput(), &outputFinish)
  152. outputFinish.Lock()
  153. }
  154. func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
  155. v2net.ReaderToChan(input, reader)
  156. finish.Unlock()
  157. close(input)
  158. }
  159. func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
  160. v2net.ChanToWriter(writer, output)
  161. finish.Unlock()
  162. }