2015-09-19 09:35:20 -04:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-09-19 09:35:20 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type OutboundConnectionHandler struct {
|
|
|
|
Data2Send *bytes.Buffer
|
|
|
|
Data2Return []byte
|
2015-09-20 12:22:29 -04:00
|
|
|
Destination v2net.Destination
|
2015-09-19 09:35:20 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 18:30:44 -04:00
|
|
|
func (handler *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray core.OutboundRay) error {
|
2015-09-19 09:35:20 -04:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
|
2015-10-06 18:30:44 -04:00
|
|
|
handler.Destination = packet.Destination()
|
|
|
|
if packet.Chunk() != nil {
|
2015-10-08 08:46:18 -04:00
|
|
|
handler.Data2Send.Write(packet.Chunk().Value)
|
2015-10-06 18:30:44 -04:00
|
|
|
}
|
|
|
|
|
2015-09-19 09:35:20 -04:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
data, open := <-input
|
|
|
|
if !open {
|
|
|
|
break
|
|
|
|
}
|
2015-10-08 08:46:18 -04:00
|
|
|
handler.Data2Send.Write(data.Value)
|
|
|
|
data.Release()
|
2015-09-19 09:35:20 -04:00
|
|
|
}
|
2015-10-08 08:46:18 -04:00
|
|
|
response := alloc.NewBuffer()
|
|
|
|
response.Clear()
|
|
|
|
response.Append(handler.Data2Return)
|
|
|
|
output <- response
|
2015-09-19 09:35:20 -04:00
|
|
|
close(output)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:30:44 -04:00
|
|
|
func (handler *OutboundConnectionHandler) Create(point *core.Point, config interface{}) (core.OutboundConnectionHandler, error) {
|
2015-09-19 09:35:20 -04:00
|
|
|
return handler, nil
|
|
|
|
}
|