outboundhandler.go 1.1 KB

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