1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00
v2fly/app/dispatcher/testing/dispatcher.go
2016-12-09 12:08:25 +01:00

45 lines
1.1 KiB
Go

package testing
import (
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
)
type TestPacketDispatcher struct {
Destination chan v2net.Destination
Handler func(destination v2net.Destination, traffic ray.OutboundRay)
}
func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic ray.OutboundRay)) *TestPacketDispatcher {
if handler == nil {
handler = func(destination v2net.Destination, traffic ray.OutboundRay) {
for {
payload, err := traffic.OutboundInput().Read()
if err != nil {
break
}
output := buf.New()
output.Append([]byte("Processed: "))
output.Append(payload.Bytes())
payload.Release()
traffic.OutboundOutput().Write(output)
}
traffic.OutboundOutput().Close()
}
}
return &TestPacketDispatcher{
Destination: make(chan v2net.Destination),
Handler: handler,
}
}
func (v *TestPacketDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
traffic := ray.NewRay()
v.Destination <- session.Destination
go v.Handler(session.Destination, traffic)
return traffic
}