dispatcher.go 1.1 KB

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