outboundhandler.go 1.2 KB

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