outboundhandler.go 1.1 KB

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