inboundhandler.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app/dispatcher"
  6. v2io "github.com/v2ray/v2ray-core/common/io"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. )
  9. type InboundConnectionHandler struct {
  10. port v2net.Port
  11. address v2net.Address
  12. PacketDispatcher dispatcher.PacketDispatcher
  13. ConnInput io.Reader
  14. ConnOutput io.Writer
  15. }
  16. func (this *InboundConnectionHandler) Listen(address v2net.Address, port v2net.Port) error {
  17. this.port = port
  18. this.address = address
  19. return nil
  20. }
  21. func (this *InboundConnectionHandler) Port() v2net.Port {
  22. return this.port
  23. }
  24. func (this *InboundConnectionHandler) Close() {
  25. }
  26. func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
  27. ray := this.PacketDispatcher.DispatchToOutbound(destination)
  28. input := ray.InboundInput()
  29. output := ray.InboundOutput()
  30. readFinish := &sync.Mutex{}
  31. writeFinish := &sync.Mutex{}
  32. readFinish.Lock()
  33. writeFinish.Lock()
  34. go func() {
  35. v2reader := v2io.NewAdaptiveReader(this.ConnInput)
  36. defer v2reader.Release()
  37. v2io.Pipe(v2reader, input)
  38. input.Close()
  39. readFinish.Unlock()
  40. }()
  41. go func() {
  42. v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
  43. defer v2writer.Release()
  44. v2io.Pipe(output, v2writer)
  45. output.Release()
  46. writeFinish.Unlock()
  47. }()
  48. readFinish.Lock()
  49. writeFinish.Lock()
  50. return nil
  51. }