outboundhandler.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. v2io.Pipe(input, v2io.NewAdaptiveWriter(this.ConnOutput))
  29. writeFinish.Unlock()
  30. input.Release()
  31. }()
  32. writeFinish.Lock()
  33. }
  34. v2io.Pipe(v2io.NewAdaptiveReader(this.ConnInput), output)
  35. output.Close()
  36. return nil
  37. }
  38. func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
  39. return this, nil
  40. }