2015-11-01 15:15:08 -05:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app/dispatcher"
|
|
|
|
v2io "v2ray.com/core/common/io"
|
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/proxy"
|
2015-11-01 15:15:08 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type InboundConnectionHandler struct {
|
2016-06-03 18:38:22 -04:00
|
|
|
ListeningPort v2net.Port
|
|
|
|
ListeningAddress v2net.Address
|
2016-01-31 11:01:28 -05:00
|
|
|
PacketDispatcher dispatcher.PacketDispatcher
|
|
|
|
ConnInput io.Reader
|
|
|
|
ConnOutput io.Writer
|
2015-11-01 15:15:08 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundConnectionHandler) Start() error {
|
2015-11-01 15:15:08 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundConnectionHandler) Port() v2net.Port {
|
|
|
|
return v.ListeningPort
|
2016-01-19 17:41:40 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundConnectionHandler) Close() {
|
2016-01-03 17:30:37 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
|
|
|
|
ray := v.PacketDispatcher.DispatchToOutbound(&proxy.SessionInfo{
|
2016-08-14 11:08:01 -04:00
|
|
|
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
|
|
|
|
Destination: destination,
|
2016-11-13 08:33:00 -05:00
|
|
|
Inbound: &proxy.InboundHandlerMeta{
|
|
|
|
AllowPassiveConnection: false,
|
|
|
|
},
|
2016-08-14 11:08:01 -04:00
|
|
|
})
|
2015-11-01 15:15:08 -05:00
|
|
|
|
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
|
|
|
|
readFinish := &sync.Mutex{}
|
|
|
|
writeFinish := &sync.Mutex{}
|
|
|
|
|
|
|
|
readFinish.Lock()
|
|
|
|
writeFinish.Lock()
|
|
|
|
|
|
|
|
go func() {
|
2016-11-27 15:39:09 -05:00
|
|
|
v2reader := v2io.NewAdaptiveReader(v.ConnInput)
|
2016-04-18 13:01:24 -04:00
|
|
|
defer v2reader.Release()
|
|
|
|
|
|
|
|
v2io.Pipe(v2reader, input)
|
2016-04-18 12:44:10 -04:00
|
|
|
input.Close()
|
2015-11-01 15:15:08 -05:00
|
|
|
readFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2016-11-27 15:39:09 -05:00
|
|
|
v2writer := v2io.NewAdaptiveWriter(v.ConnOutput)
|
2016-04-18 13:01:24 -04:00
|
|
|
defer v2writer.Release()
|
|
|
|
|
|
|
|
v2io.Pipe(output, v2writer)
|
2016-04-18 12:44:10 -04:00
|
|
|
output.Release()
|
2015-11-01 15:15:08 -05:00
|
|
|
writeFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
readFinish.Lock()
|
|
|
|
writeFinish.Lock()
|
|
|
|
return nil
|
|
|
|
}
|