outboundhandler.go 1.1 KB

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