mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-31 06:25:32 -05:00
Move mocked connection handlers to proxy/testing/mocks
This commit is contained in:
55
proxy/testing/mocks/inboundhandler.go
Normal file
55
proxy/testing/mocks/inboundhandler.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
||||
)
|
||||
|
||||
type InboundConnectionHandler struct {
|
||||
Port uint16
|
||||
Dispatcher app.PacketDispatcher
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Listen(port uint16) error {
|
||||
this.Port = port
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Communicate(packet v2net.Packet) error {
|
||||
ray := this.Dispatcher.DispatchToOutbound(packet)
|
||||
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
|
||||
readFinish := &sync.Mutex{}
|
||||
writeFinish := &sync.Mutex{}
|
||||
|
||||
readFinish.Lock()
|
||||
writeFinish.Lock()
|
||||
|
||||
go func() {
|
||||
v2net.ReaderToChan(input, this.ConnInput)
|
||||
close(input)
|
||||
readFinish.Unlock()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
v2net.ChanToWriter(this.ConnOutput, output)
|
||||
writeFinish.Unlock()
|
||||
}()
|
||||
|
||||
readFinish.Lock()
|
||||
writeFinish.Lock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Create(dispatcher app.PacketDispatcher, config interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
this.Dispatcher = dispatcher
|
||||
return this, nil
|
||||
}
|
||||
49
proxy/testing/mocks/outboundhandler.go
Normal file
49
proxy/testing/mocks/outboundhandler.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
||||
"github.com/v2ray/v2ray-core/transport/ray"
|
||||
)
|
||||
|
||||
type OutboundConnectionHandler struct {
|
||||
Destination v2net.Destination
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
|
||||
func (this *OutboundConnectionHandler) Dispatch(packet v2net.Packet, ray ray.OutboundRay) error {
|
||||
input := ray.OutboundInput()
|
||||
output := ray.OutboundOutput()
|
||||
|
||||
this.Destination = packet.Destination()
|
||||
if packet.Chunk() != nil {
|
||||
this.ConnOutput.Write(packet.Chunk().Value)
|
||||
packet.Chunk().Release()
|
||||
}
|
||||
|
||||
if packet.MoreChunks() {
|
||||
writeFinish := &sync.Mutex{}
|
||||
|
||||
writeFinish.Lock()
|
||||
|
||||
go func() {
|
||||
v2net.ChanToWriter(this.ConnOutput, input)
|
||||
writeFinish.Unlock()
|
||||
}()
|
||||
|
||||
writeFinish.Lock()
|
||||
}
|
||||
|
||||
v2net.ReaderToChan(output, this.ConnInput)
|
||||
close(output)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *OutboundConnectionHandler) Create(config interface{}) (connhandler.OutboundConnectionHandler, error) {
|
||||
return this, nil
|
||||
}
|
||||
Reference in New Issue
Block a user