dokodemo.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "github.com/v2ray/v2ray-core/proxy/dokodemo/config/json"
  12. )
  13. type DokodemoDoor struct {
  14. config *json.DokodemoConfig
  15. accepting bool
  16. address v2net.Address
  17. dispatcher app.PacketDispatcher
  18. }
  19. func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfig) *DokodemoDoor {
  20. d := &DokodemoDoor{
  21. config: config,
  22. dispatcher: dispatcher,
  23. }
  24. ip := net.ParseIP(config.Host)
  25. if ip != nil {
  26. d.address = v2net.IPAddress(ip, uint16(config.Port))
  27. } else {
  28. d.address = v2net.DomainAddress(config.Host, uint16(config.Port))
  29. }
  30. return d
  31. }
  32. func (this *DokodemoDoor) Listen(port uint16) error {
  33. if this.config.Network.HasNetwork(v2net.TCPNetwork) {
  34. err := this.ListenTCP(port)
  35. if err != nil {
  36. return err
  37. }
  38. }
  39. return nil
  40. }
  41. func (this *DokodemoDoor) ListenTCP(port uint16) error {
  42. tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
  43. IP: []byte{0, 0, 0, 0},
  44. Port: int(port),
  45. Zone: "",
  46. })
  47. if err != nil {
  48. log.Error("Dokodemo failed to listen on port %d: %v", port, err)
  49. return err
  50. }
  51. this.accepting = true
  52. go this.AcceptTCPConnections(tcpListener)
  53. return nil
  54. }
  55. func (this *DokodemoDoor) AcceptTCPConnections(tcpListener *net.TCPListener) {
  56. for this.accepting {
  57. retry.Timed(100, 100).On(func() error {
  58. connection, err := tcpListener.AcceptTCP()
  59. if err != nil {
  60. log.Error("Dokodemo failed to accept new connections: %v", err)
  61. return err
  62. }
  63. go this.HandleTCPConnection(connection)
  64. return nil
  65. })
  66. }
  67. }
  68. func (this *DokodemoDoor) HandleTCPConnection(conn *net.TCPConn) {
  69. defer conn.Close()
  70. packet := v2net.NewPacket(v2net.NewTCPDestination(this.address), nil, true)
  71. ray := this.dispatcher.DispatchToOutbound(packet)
  72. var inputFinish, outputFinish sync.Mutex
  73. inputFinish.Lock()
  74. outputFinish.Lock()
  75. reader := v2net.NewTimeOutReader(this.config.Timeout, conn)
  76. go dumpInput(reader, ray.InboundInput(), &inputFinish)
  77. go dumpOutput(conn, ray.InboundOutput(), &outputFinish)
  78. outputFinish.Lock()
  79. }
  80. func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, finish *sync.Mutex) {
  81. v2net.ReaderToChan(input, reader)
  82. finish.Unlock()
  83. close(input)
  84. }
  85. func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, finish *sync.Mutex) {
  86. v2net.ChanToWriter(writer, output)
  87. finish.Unlock()
  88. }