mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -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
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
v.response.WriteTo(ray.OutboundOutput())
|
||||
ray.OutboundOutput().Close()
|
||||
|
||||
ray.OutboundInput().Release()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Factory struct{}
|
||||
|
@ -67,7 +67,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti
|
||||
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)
|
||||
|
||||
defer payload.Release()
|
||||
@ -88,7 +88,7 @@ func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *all
|
||||
})
|
||||
if err != nil {
|
||||
log.Warning("Freedom: Failed to open connection to ", destination, ": ", err)
|
||||
return err
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
@ -127,8 +127,6 @@ func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *all
|
||||
}
|
||||
v2reader.Release()
|
||||
ray.OutboundOutput().Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type FreedomFactory struct{}
|
||||
|
@ -59,26 +59,6 @@ func TestSinglePacket(t *testing.T) {
|
||||
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) {
|
||||
assert := assert.On(t)
|
||||
|
||||
|
@ -58,5 +58,5 @@ type InboundHandler interface {
|
||||
// An OutboundHandler handles outbound network connection for V2Ray.
|
||||
type OutboundHandler interface {
|
||||
// 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
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"v2ray.com/core/app"
|
||||
"v2ray.com/core/common/alloc"
|
||||
@ -33,7 +32,7 @@ func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandle
|
||||
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 ray.OutboundInput().Release()
|
||||
defer ray.OutboundOutput().Close()
|
||||
@ -56,7 +55,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
||||
return 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())
|
||||
|
||||
@ -76,7 +76,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
||||
user := server.PickUser()
|
||||
rawAccount, err := user.GetTypedAccount()
|
||||
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)
|
||||
request.User = user
|
||||
@ -93,11 +94,13 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
||||
defer bodyWriter.Release()
|
||||
|
||||
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 {
|
||||
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
|
||||
@ -149,7 +152,8 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
||||
}
|
||||
if !payload.IsEmpty() {
|
||||
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 {
|
||||
@ -158,8 +162,6 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
|
||||
|
||||
responseMutex.Lock()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type ClientFactory struct{}
|
||||
|
@ -18,7 +18,7 @@ type OutboundConnectionHandler struct {
|
||||
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()
|
||||
output := ray.OutboundOutput()
|
||||
|
||||
@ -48,8 +48,6 @@ func (v *OutboundConnectionHandler) Dispatch(destination v2net.Destination, payl
|
||||
|
||||
v2io.Pipe(v2reader, output)
|
||||
output.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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.OutboundOutput().Close()
|
||||
|
||||
@ -43,8 +43,8 @@ func (v *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("VMess|Outbound: Failed to find an available destination:", err)
|
||||
return err
|
||||
log.Warning("VMess|Outbound: Failed to find an available destination:", err)
|
||||
return
|
||||
}
|
||||
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()
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user