From 5f3f173b5e3387e43f3ee95145649d56ce0de2c6 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 2 Nov 2016 16:33:04 +0100 Subject: [PATCH] shadowsocks client factory --- common/protocol/server_spec.go | 20 +++++++++----------- proxy/shadowsocks/client.go | 26 ++++++++++++++++++++++++++ proxy/vmess/outbound/command.go | 2 +- proxy/vmess/outbound/outbound.go | 3 +-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/common/protocol/server_spec.go b/common/protocol/server_spec.go index 0fa308df9..138a566a1 100644 --- a/common/protocol/server_spec.go +++ b/common/protocol/server_spec.go @@ -45,24 +45,22 @@ func (this *TimeoutValidStrategy) Invalidate() { type ServerSpec struct { sync.RWMutex - dest v2net.Destination - users []*User - valid ValidationStrategy - newAccount NewAccountFactory + dest v2net.Destination + users []*User + valid ValidationStrategy } -func NewServerSpec(newAccount NewAccountFactory, dest v2net.Destination, valid ValidationStrategy, users ...*User) *ServerSpec { +func NewServerSpec(dest v2net.Destination, valid ValidationStrategy, users ...*User) *ServerSpec { return &ServerSpec{ - dest: dest, - users: users, - valid: valid, - newAccount: newAccount, + dest: dest, + users: users, + valid: valid, } } -func NewServerSpecFromPB(newAccount NewAccountFactory, spec ServerEndpoint) *ServerSpec { +func NewServerSpecFromPB(spec ServerEndpoint) *ServerSpec { dest := v2net.TCPDestination(spec.Address.AsAddress(), v2net.Port(spec.Port)) - return NewServerSpec(newAccount, dest, AlwaysValid(), spec.User...) + return NewServerSpec(dest, AlwaysValid(), spec.User...) } func (this *ServerSpec) Destination() v2net.Destination { diff --git a/proxy/shadowsocks/client.go b/proxy/shadowsocks/client.go index 1c3bdc352..7308d432a 100644 --- a/proxy/shadowsocks/client.go +++ b/proxy/shadowsocks/client.go @@ -4,6 +4,7 @@ import ( "errors" "sync" + "v2ray.com/core/app" "v2ray.com/core/common/alloc" v2io "v2ray.com/core/common/io" "v2ray.com/core/common/log" @@ -20,6 +21,19 @@ type Client struct { meta *proxy.OutboundHandlerMeta } +func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandlerMeta) (*Client, error) { + serverList := protocol.NewServerList() + for _, rec := range config.Server { + serverList.AddServer(protocol.NewServerSpecFromPB(*rec)) + } + client := &Client{ + serverPicker: protocol.NewRoundRobinServerPicker(serverList), + meta: meta, + } + + return client, nil +} + func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error { defer payload.Release() defer ray.OutboundInput().Release() @@ -136,3 +150,15 @@ func (this *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffe return nil } + +type ClientFactory struct{} + +func (this *ClientFactory) StreamCapability() v2net.NetworkList { + return v2net.NetworkList{ + Network: []v2net.Network{v2net.Network_TCP, v2net.Network_RawTCP}, + } +} + +func (this *ClientFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { + return NewClient(rawConfig.(*ClientConfig), space, meta) +} diff --git a/proxy/vmess/outbound/command.go b/proxy/vmess/outbound/command.go index 5b0798161..ef00fdecd 100644 --- a/proxy/vmess/outbound/command.go +++ b/proxy/vmess/outbound/command.go @@ -22,7 +22,7 @@ func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitc } dest := v2net.TCPDestination(cmd.Host, cmd.Port) until := time.Now().Add(time.Duration(cmd.ValidMin) * time.Minute) - this.serverList.AddServer(protocol.NewServerSpec(vmess.NewAccount, dest, protocol.BeforeTime(until), user)) + this.serverList.AddServer(protocol.NewServerSpec(dest, protocol.BeforeTime(until), user)) } func (this *VMessOutboundHandler) handleCommand(dest v2net.Destination, cmd protocol.ResponseCommand) { diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index b52f8e7fd..fd870325d 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -14,7 +14,6 @@ import ( "v2ray.com/core/common/retry" "v2ray.com/core/proxy" "v2ray.com/core/proxy/registry" - "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/encoding" vmessio "v2ray.com/core/proxy/vmess/io" "v2ray.com/core/transport/internet" @@ -172,7 +171,7 @@ func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy. serverList := protocol.NewServerList() for _, rec := range vOutConfig.Receiver { - serverList.AddServer(protocol.NewServerSpecFromPB(vmess.NewAccount, *rec)) + serverList.AddServer(protocol.NewServerSpecFromPB(*rec)) } handler := &VMessOutboundHandler{ serverList: serverList,