outboundhandler.go 1002 B

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