outboundhandler.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package mocks
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/app"
  6. v2io "github.com/v2ray/v2ray-core/common/io"
  7. v2net "github.com/v2ray/v2ray-core/common/net"
  8. "github.com/v2ray/v2ray-core/proxy"
  9. "github.com/v2ray/v2ray-core/transport/ray"
  10. )
  11. type OutboundConnectionHandler struct {
  12. Destination v2net.Destination
  13. ConnInput io.Reader
  14. ConnOutput io.Writer
  15. }
  16. func (this *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
  17. input := ray.OutboundInput()
  18. output := ray.OutboundOutput()
  19. this.Destination = packet.Destination()
  20. if packet.Chunk() != nil {
  21. this.ConnOutput.Write(packet.Chunk().Value)
  22. packet.Chunk().Release()
  23. }
  24. if packet.MoreChunks() {
  25. writeFinish := &sync.Mutex{}
  26. writeFinish.Lock()
  27. go func() {
  28. v2writer := v2io.NewAdaptiveWriter(this.ConnOutput)
  29. defer v2writer.Release()
  30. v2io.Pipe(input, v2writer)
  31. writeFinish.Unlock()
  32. input.Release()
  33. }()
  34. writeFinish.Lock()
  35. }
  36. v2reader := v2io.NewAdaptiveReader(this.ConnInput)
  37. defer v2reader.Release()
  38. v2io.Pipe(v2reader, output)
  39. output.Close()
  40. return nil
  41. }
  42. func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  43. return this, nil
  44. }