outboundhandler.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package mocks
  2. import (
  3. "bytes"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/proxy"
  7. "github.com/v2ray/v2ray-core/transport/ray"
  8. )
  9. type OutboundConnectionHandler struct {
  10. Data2Send *bytes.Buffer
  11. Data2Return []byte
  12. Destination v2net.Destination
  13. }
  14. func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
  15. input := ray.OutboundInput()
  16. output := ray.OutboundOutput()
  17. handler.Destination = packet.Destination()
  18. if packet.Chunk() != nil {
  19. handler.Data2Send.Write(packet.Chunk().Value)
  20. }
  21. go func() {
  22. for {
  23. data, open := <-input
  24. if !open {
  25. break
  26. }
  27. handler.Data2Send.Write(data.Value)
  28. data.Release()
  29. }
  30. response := alloc.NewBuffer()
  31. response.Clear()
  32. response.Append(handler.Data2Return)
  33. output <- response
  34. close(output)
  35. }()
  36. return nil
  37. }
  38. func (handler *OutboundConnectionHandler) Create(config interface{}) (proxy.OutboundConnectionHandler, error) {
  39. return handler, nil
  40. }