From 14829f67f05583b6fd4b42a6f55279a34ffab59b Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 27 Nov 2016 23:57:19 +0100 Subject: [PATCH] refine outbound proxy handler interface --- proxy/blackhole/blackhole.go | 4 +--- proxy/freedom/freedom.go | 6 ++---- proxy/freedom/freedom_test.go | 20 -------------------- proxy/proxy.go | 2 +- proxy/shadowsocks/client.go | 20 +++++++++++--------- proxy/testing/mocks/outboundhandler.go | 4 +--- proxy/vmess/outbound/outbound.go | 8 ++++---- 7 files changed, 20 insertions(+), 44 deletions(-) diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index b176dad00..5e9985e82 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -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{} diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 982420a2d..6f147df25 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -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{} diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 0acc3cb80..3aa2217d8 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -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) diff --git a/proxy/proxy.go b/proxy/proxy.go index d9892b89f..0b3d1974e 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -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) } diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index f6b0b728f..096f8ae0e 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -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{} diff --git a/proxy/testing/mocks/outboundhandler.go b/proxy/testing/mocks/outboundhandler.go index c02b7b0d8..2b837f9bf 100644 --- a/proxy/testing/mocks/outboundhandler.go +++ b/proxy/testing/mocks/outboundhandler.go @@ -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) { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 720e30659..890ba4a25 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -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) {