inboundhandler.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  8. )
  9. type InboundConnectionHandler struct {
  10. Port v2net.Port
  11. Dispatcher app.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) Communicate(packet v2net.Packet) error {
  20. ray := this.Dispatcher.DispatchToOutbound(packet)
  21. input := ray.InboundInput()
  22. output := ray.InboundOutput()
  23. readFinish := &sync.Mutex{}
  24. writeFinish := &sync.Mutex{}
  25. readFinish.Lock()
  26. writeFinish.Lock()
  27. go func() {
  28. v2net.ReaderToChan(input, this.ConnInput)
  29. close(input)
  30. readFinish.Unlock()
  31. }()
  32. go func() {
  33. v2net.ChanToWriter(this.ConnOutput, output)
  34. writeFinish.Unlock()
  35. }()
  36. readFinish.Lock()
  37. writeFinish.Lock()
  38. return nil
  39. }
  40. func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) {
  41. this.Dispatcher = dispatcher
  42. return this, nil
  43. }