inboundhandler.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. PacketDispatcher dispatcher.PacketDispatcher
  12. ConnInput io.Reader
  13. ConnOutput io.Writer
  14. }
  15. func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
  16. this.port = port
  17. return nil
  18. }
  19. func (this *InboundConnectionHandler) Port() v2net.Port {
  20. return this.port
  21. }
  22. func (this *InboundConnectionHandler) Close() {
  23. }
  24. func (this *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
  25. ray := this.PacketDispatcher.DispatchToOutbound(destination)
  26. input := ray.InboundInput()
  27. output := ray.InboundOutput()
  28. readFinish := &sync.Mutex{}
  29. writeFinish := &sync.Mutex{}
  30. readFinish.Lock()
  31. writeFinish.Lock()
  32. go func() {
  33. v2reader := v2io.NewAdaptiveReader(this.ConnInput)
  34. defer v2reader.Release()
  35. v2io.Pipe(v2reader, input)
  36. input.Close()
  37. readFinish.Unlock()
  38. }()
  39. go func() {
  40. v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
  41. defer v2writer.Release()
  42. v2io.Pipe(output, v2writer)
  43. output.Release()
  44. writeFinish.Unlock()
  45. }()
  46. readFinish.Lock()
  47. writeFinish.Lock()
  48. return nil
  49. }