1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-05 00:47:51 -05:00
v2fly/app/dispatcher/testing/dispatcher.go

45 lines
1.1 KiB
Go
Raw Normal View History

2016-02-04 07:13:15 -05:00
package testing
import (
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
2016-02-04 07:13:15 -05:00
)
type TestPacketDispatcher struct {
2016-04-18 12:44:10 -04:00
Destination chan v2net.Destination
2016-04-25 18:13:26 -04:00
Handler func(destination v2net.Destination, traffic ray.OutboundRay)
2016-02-04 07:13:15 -05:00
}
2016-04-25 18:13:26 -04:00
func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic ray.OutboundRay)) *TestPacketDispatcher {
2016-02-04 07:27:35 -05:00
if handler == nil {
2016-04-25 18:13:26 -04:00
handler = func(destination v2net.Destination, traffic ray.OutboundRay) {
2016-04-18 12:44:10 -04:00
for {
payload, err := traffic.OutboundInput().Read()
if err != nil {
break
}
2016-12-09 06:08:25 -05:00
output := buf.New()
2016-12-08 05:21:24 -05:00
output.Append([]byte("Processed: "))
output.Append(payload.Bytes())
payload.Release()
traffic.OutboundOutput().Write(output)
2016-02-04 07:13:15 -05:00
}
2016-04-18 12:44:10 -04:00
traffic.OutboundOutput().Close()
2016-02-04 07:27:35 -05:00
}
2016-02-04 07:13:15 -05:00
}
2016-02-04 07:27:35 -05:00
return &TestPacketDispatcher{
2016-04-18 12:44:10 -04:00
Destination: make(chan v2net.Destination),
Handler: handler,
2016-02-04 07:27:35 -05:00
}
}
2016-11-27 15:39:09 -05:00
func (v *TestPacketDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay {
2016-02-04 07:27:35 -05:00
traffic := ray.NewRay()
2016-11-27 15:39:09 -05:00
v.Destination <- session.Destination
go v.Handler(session.Destination, traffic)
2016-02-04 07:13:15 -05:00
return traffic
}