2015-11-01 20:15:08 +00:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/common/alloc"
|
|
|
|
v2io "v2ray.com/core/common/io"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/ray"
|
2015-11-01 20:15:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OutboundConnectionHandler struct {
|
|
|
|
Destination v2net.Destination
|
|
|
|
ConnInput io.Reader
|
|
|
|
ConnOutput io.Writer
|
|
|
|
}
|
|
|
|
|
2016-11-27 22:57:19 +00:00
|
|
|
func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
2015-11-01 20:15:08 +00:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
v.Destination = destination
|
2016-11-19 21:40:14 +00:00
|
|
|
if !payload.IsEmpty() {
|
2016-11-27 20:39:09 +00:00
|
|
|
v.ConnOutput.Write(payload.Value)
|
2016-11-19 21:40:14 +00:00
|
|
|
}
|
2016-04-25 22:13:26 +00:00
|
|
|
payload.Release()
|
2015-11-01 20:15:08 +00:00
|
|
|
|
2016-04-25 22:13:26 +00:00
|
|
|
writeFinish := &sync.Mutex{}
|
2015-11-01 20:15:08 +00:00
|
|
|
|
2016-04-25 22:13:26 +00:00
|
|
|
writeFinish.Lock()
|
2015-11-01 20:15:08 +00:00
|
|
|
|
2016-04-25 22:13:26 +00:00
|
|
|
go func() {
|
2016-11-27 20:39:09 +00:00
|
|
|
v2writer := v2io.NewAdaptiveWriter(v.ConnOutput)
|
2016-04-25 22:13:26 +00:00
|
|
|
defer v2writer.Release()
|
2016-04-18 17:01:24 +00:00
|
|
|
|
2016-04-25 22:13:26 +00:00
|
|
|
v2io.Pipe(input, v2writer)
|
|
|
|
writeFinish.Unlock()
|
|
|
|
input.Release()
|
|
|
|
}()
|
2015-11-01 20:15:08 +00:00
|
|
|
|
2016-04-25 22:13:26 +00:00
|
|
|
writeFinish.Lock()
|
2015-11-01 20:15:08 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
v2reader := v2io.NewAdaptiveReader(v.ConnInput)
|
2016-04-18 17:01:24 +00:00
|
|
|
defer v2reader.Release()
|
|
|
|
|
|
|
|
v2io.Pipe(v2reader, output)
|
2016-04-18 16:44:10 +00:00
|
|
|
output.Close()
|
2015-11-01 20:15:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
|
|
|
|
return v, nil
|
2015-11-01 20:15:08 +00:00
|
|
|
}
|