outboundhandler.go 934 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) Dispatch(packet v2net.Packet, ray core.OutboundRay) error {
  13. input := ray.OutboundInput()
  14. output := ray.OutboundOutput()
  15. handler.Destination = packet.Destination()
  16. if packet.Chunk() != nil {
  17. handler.Data2Send.Write(packet.Chunk())
  18. }
  19. go func() {
  20. for {
  21. data, open := <-input
  22. if !open {
  23. break
  24. }
  25. handler.Data2Send.Write(data)
  26. }
  27. dataCopy := make([]byte, len(handler.Data2Return))
  28. copy(dataCopy, handler.Data2Return)
  29. output <- dataCopy
  30. close(output)
  31. }()
  32. return nil
  33. }
  34. func (handler *OutboundConnectionHandler) Create(point *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
  35. return handler, nil
  36. }