dispatcher.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package testing
  2. import (
  3. v2net "v2ray.com/core/common/net"
  4. "v2ray.com/core/proxy"
  5. "v2ray.com/core/transport/ray"
  6. )
  7. type TestPacketDispatcher struct {
  8. Destination chan v2net.Destination
  9. Handler func(destination v2net.Destination, traffic ray.OutboundRay)
  10. }
  11. func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic ray.OutboundRay)) *TestPacketDispatcher {
  12. if handler == nil {
  13. handler = func(destination v2net.Destination, traffic ray.OutboundRay) {
  14. for {
  15. payload, err := traffic.OutboundInput().Read()
  16. if err != nil {
  17. break
  18. }
  19. traffic.OutboundOutput().Write(payload.Prepend([]byte("Processed: ")))
  20. }
  21. traffic.OutboundOutput().Close()
  22. }
  23. }
  24. return &TestPacketDispatcher{
  25. Destination: make(chan v2net.Destination),
  26. Handler: handler,
  27. }
  28. }
  29. func (this *TestPacketDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
  30. traffic := ray.NewRay()
  31. this.Destination <- session.Destination
  32. go this.Handler(session.Destination, traffic)
  33. return traffic
  34. }