dispatcher.go 664 B

12345678910111213141516171819202122232425262728
  1. package testing
  2. import (
  3. v2net "github.com/v2ray/v2ray-core/common/net"
  4. "github.com/v2ray/v2ray-core/transport/ray"
  5. )
  6. type TestPacketDispatcher struct {
  7. LastPacket v2net.Packet
  8. Handler func(packet v2net.Packet, traffic ray.OutboundRay)
  9. }
  10. func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
  11. traffic := ray.NewRay()
  12. this.LastPacket = packet
  13. if this.Handler == nil {
  14. go func() {
  15. for payload := range traffic.OutboundInput() {
  16. traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: "))
  17. }
  18. close(traffic.OutboundOutput())
  19. }()
  20. } else {
  21. go this.Handler(packet, traffic)
  22. }
  23. return traffic
  24. }