mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
refine outbound proxy handler interface
This commit is contained in:
parent
2a37c62e07
commit
14829f67f0
@ -25,15 +25,13 @@ func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMe
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
func (v *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
||||||
payload.Release()
|
payload.Release()
|
||||||
|
|
||||||
v.response.WriteTo(ray.OutboundOutput())
|
v.response.WriteTo(ray.OutboundOutput())
|
||||||
ray.OutboundOutput().Close()
|
ray.OutboundOutput().Close()
|
||||||
|
|
||||||
ray.OutboundInput().Release()
|
ray.OutboundInput().Release()
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Factory struct{}
|
type Factory struct{}
|
||||||
|
@ -67,7 +67,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti
|
|||||||
return newDest
|
return newDest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
||||||
log.Info("Freedom: Opening connection to ", destination)
|
log.Info("Freedom: Opening connection to ", destination)
|
||||||
|
|
||||||
defer payload.Release()
|
defer payload.Release()
|
||||||
@ -88,7 +88,7 @@ func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *all
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
|
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
@ -127,8 +127,6 @@ func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *all
|
|||||||
}
|
}
|
||||||
v2reader.Release()
|
v2reader.Release()
|
||||||
ray.OutboundOutput().Close()
|
ray.OutboundOutput().Close()
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type FreedomFactory struct{}
|
type FreedomFactory struct{}
|
||||||
|
@ -59,26 +59,6 @@ func TestSinglePacket(t *testing.T) {
|
|||||||
tcpServer.Close()
|
tcpServer.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnreachableDestination(t *testing.T) {
|
|
||||||
assert := assert.On(t)
|
|
||||||
|
|
||||||
freedom := NewFreedomConnection(
|
|
||||||
&Config{},
|
|
||||||
app.NewSpace(),
|
|
||||||
&proxy.OutboundHandlerMeta{
|
|
||||||
Address: v2net.AnyIP,
|
|
||||||
StreamSettings: &internet.StreamConfig{
|
|
||||||
Network: v2net.Network_RawTCP,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
traffic := ray.NewRay()
|
|
||||||
data2Send := "Data to be sent to remote"
|
|
||||||
payload := alloc.NewLocalBuffer(2048).Clear().Append([]byte(data2Send))
|
|
||||||
|
|
||||||
err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)
|
|
||||||
assert.Error(err).IsNotNil()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIPResolution(t *testing.T) {
|
func TestIPResolution(t *testing.T) {
|
||||||
assert := assert.On(t)
|
assert := assert.On(t)
|
||||||
|
|
||||||
|
@ -58,5 +58,5 @@ type InboundHandler interface {
|
|||||||
// An OutboundHandler handles outbound network connection for V2Ray.
|
// An OutboundHandler handles outbound network connection for V2Ray.
|
||||||
type OutboundHandler interface {
|
type OutboundHandler interface {
|
||||||
// Dispatch sends one or more Packets to its destination.
|
// Dispatch sends one or more Packets to its destination.
|
||||||
Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error
|
Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package shadowsocks
|
package shadowsocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"sync"
|
"sync"
|
||||||
"v2ray.com/core/app"
|
"v2ray.com/core/app"
|
||||||
"v2ray.com/core/common/alloc"
|
"v2ray.com/core/common/alloc"
|
||||||
@ -33,7 +32,7 @@ func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandle
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
||||||
defer payload.Release()
|
defer payload.Release()
|
||||||
defer ray.OutboundInput().Release()
|
defer ray.OutboundInput().Release()
|
||||||
defer ray.OutboundOutput().Close()
|
defer ray.OutboundOutput().Close()
|
||||||
@ -56,7 +55,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Shadowsocks|Client: Failed to find an available destination:" + err.Error())
|
log.Warning("Shadowsocks|Client: Failed to find an available destination:", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
|
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
|
||||||
|
|
||||||
@ -76,7 +76,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
|||||||
user := server.PickUser()
|
user := server.PickUser()
|
||||||
rawAccount, err := user.GetTypedAccount()
|
rawAccount, err := user.GetTypedAccount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Shadowsocks|Client: Failed to get a valid user account: " + err.Error())
|
log.Warning("Shadowsocks|Client: Failed to get a valid user account: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
account := rawAccount.(*ShadowsocksAccount)
|
account := rawAccount.(*ShadowsocksAccount)
|
||||||
request.User = user
|
request.User = user
|
||||||
@ -93,11 +94,13 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
|||||||
defer bodyWriter.Release()
|
defer bodyWriter.Release()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Shadowsock|Client: Failed to write request: " + err.Error())
|
log.Info("Shadowsock|Client: Failed to write request: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bodyWriter.Write(payload); err != nil {
|
if err := bodyWriter.Write(payload); err != nil {
|
||||||
return errors.New("Shadowsocks|Client: Failed to write payload: " + err.Error())
|
log.Info("Shadowsocks|Client: Failed to write payload: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var responseMutex sync.Mutex
|
var responseMutex sync.Mutex
|
||||||
@ -149,7 +152,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
|||||||
}
|
}
|
||||||
if !payload.IsEmpty() {
|
if !payload.IsEmpty() {
|
||||||
if err := writer.Write(payload); err != nil {
|
if err := writer.Write(payload); err != nil {
|
||||||
return errors.New("Shadowsocks|Client: Failed to write payload: " + err.Error())
|
log.Info("Shadowsocks|Client: Failed to write payload: ", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := v2io.PipeUntilEOF(ray.OutboundInput(), writer); err != nil {
|
if err := v2io.PipeUntilEOF(ray.OutboundInput(), writer); err != nil {
|
||||||
@ -158,8 +162,6 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
|||||||
|
|
||||||
responseMutex.Lock()
|
responseMutex.Lock()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientFactory struct{}
|
type ClientFactory struct{}
|
||||||
|
@ -18,7 +18,7 @@ type OutboundConnectionHandler struct {
|
|||||||
ConnOutput io.Writer
|
ConnOutput io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
||||||
input := ray.OutboundInput()
|
input := ray.OutboundInput()
|
||||||
output := ray.OutboundOutput()
|
output := ray.OutboundOutput()
|
||||||
|
|
||||||
@ -48,8 +48,6 @@ func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payl
|
|||||||
|
|
||||||
v2io.Pipe(v2reader, output)
|
v2io.Pipe(v2reader, output)
|
||||||
output.Close()
|
output.Close()
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
|
func (v *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
|
||||||
|
@ -25,7 +25,7 @@ type VMessOutboundHandler struct {
|
|||||||
meta *proxy.OutboundHandlerMeta
|
meta *proxy.OutboundHandlerMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) {
|
||||||
defer ray.OutboundInput().Release()
|
defer ray.OutboundInput().Release()
|
||||||
defer ray.OutboundOutput().Close()
|
defer ray.OutboundOutput().Close()
|
||||||
|
|
||||||
@ -43,8 +43,8 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("VMess|Outbound: Failed to find an available destination:", err)
|
log.Warning("VMess|Outbound: Failed to find an available destination:", err)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
|
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc
|
|||||||
|
|
||||||
requestFinish.Lock()
|
requestFinish.Lock()
|
||||||
responseFinish.Lock()
|
responseFinish.Lock()
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
|
func (v *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
|
||||||
|
Loading…
Reference in New Issue
Block a user