mirror of https://github.com/v2ray/v2ray-core
parent
c693867603
commit
4c7faadfb9
@ -1,72 +0,0 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"v2ray.com/core/app/dispatcher"
|
||||
"v2ray.com/core/common/buf"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
)
|
||||
|
||||
type InboundConnectionHandler struct {
|
||||
ListeningPort v2net.Port
|
||||
ListeningAddress v2net.Address
|
||||
PacketDispatcher dispatcher.PacketDispatcher
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
|
||||
func (v *InboundConnectionHandler) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *InboundConnectionHandler) Port() v2net.Port {
|
||||
return v.ListeningPort
|
||||
}
|
||||
|
||||
func (v *InboundConnectionHandler) Close() {
|
||||
|
||||
}
|
||||
|
||||
func (v *InboundConnectionHandler) Communicate(destination v2net.Destination) error {
|
||||
ray := v.PacketDispatcher.DispatchToOutbound(&proxy.SessionInfo{
|
||||
Source: v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(0)),
|
||||
Destination: destination,
|
||||
Inbound: &proxy.InboundHandlerMeta{
|
||||
AllowPassiveConnection: false,
|
||||
},
|
||||
})
|
||||
|
||||
input := ray.InboundInput()
|
||||
output := ray.InboundOutput()
|
||||
|
||||
readFinish := &sync.Mutex{}
|
||||
writeFinish := &sync.Mutex{}
|
||||
|
||||
readFinish.Lock()
|
||||
writeFinish.Lock()
|
||||
|
||||
go func() {
|
||||
v2reader := buf.NewReader(v.ConnInput)
|
||||
defer v2reader.Release()
|
||||
|
||||
buf.Pipe(v2reader, input)
|
||||
input.Close()
|
||||
readFinish.Unlock()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
v2writer := buf.NewWriter(v.ConnOutput)
|
||||
defer v2writer.Release()
|
||||
|
||||
buf.Pipe(output, v2writer)
|
||||
output.Release()
|
||||
writeFinish.Unlock()
|
||||
}()
|
||||
|
||||
readFinish.Lock()
|
||||
writeFinish.Lock()
|
||||
return nil
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/common/buf"
|
||||
v2net "v2ray.com/core/common/net"
|
||||
"v2ray.com/core/proxy"
|
||||
"v2ray.com/core/transport/ray"
|
||||
)
|
||||
|
||||
type OutboundConnectionHandler struct {
|
||||
Destination v2net.Destination
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
|
||||
func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
|
||||
input := ray.OutboundInput()
|
||||
output := ray.OutboundOutput()
|
||||
|
||||
v.Destination = destination
|
||||
if !payload.IsEmpty() {
|
||||
v.ConnOutput.Write(payload.Bytes())
|
||||
}
|
||||
payload.Release()
|
||||
|
||||
writeFinish := &sync.Mutex{}
|
||||
|
||||
writeFinish.Lock()
|
||||
|
||||
go func() {
|
||||
v2writer := buf.NewWriter(v.ConnOutput)
|
||||
defer v2writer.Release()
|
||||
|
||||
buf.Pipe(input, v2writer)
|
||||
writeFinish.Unlock()
|
||||
input.Release()
|
||||
}()
|
||||
|
||||
writeFinish.Lock()
|
||||
|
||||
v2reader := buf.NewReader(v.ConnInput)
|
||||
defer v2reader.Release()
|
||||
|
||||
buf.Pipe(v2reader, output)
|
||||
output.Close()
|
||||
}
|
||||
|
||||
func (v *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
|
||||
return v, nil
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package testing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/proxy/registry"
|
||||
)
|
||||
|
||||
var count = 0
|
||||
|
||||
func randomString() string {
|
||||
count++
|
||||
return fmt.Sprintf("-%d", count)
|
||||
}
|
||||
|
||||
func RegisterInboundConnectionHandlerCreator(prefix string, creator registry.InboundHandlerFactory) (string, error) {
|
||||
for {
|
||||
name := prefix + randomString()
|
||||
err := registry.RegisterInboundHandlerCreator(name, creator)
|
||||
if err != common.ErrDuplicatedName {
|
||||
return name, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterOutboundConnectionHandlerCreator(prefix string, creator registry.OutboundHandlerFactory) (string, error) {
|
||||
for {
|
||||
name := prefix + randomString()
|
||||
err := registry.RegisterOutboundHandlerCreator(name, creator)
|
||||
if err != common.ErrDuplicatedName {
|
||||
return name, err
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue