outboundhandler.go 931 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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) Create(point *core.Point, config interface{}, packet v2net.Packet) (core.OutboundConnectionHandler, error) {
  31. handler.Destination = packet.Destination()
  32. if packet.Chunk() != nil {
  33. handler.Data2Send.Write(packet.Chunk())
  34. }
  35. return handler, nil
  36. }