From 074dfbb78ccf649991efa366df87214ba35fcfec Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 3 Apr 2018 17:51:01 +0200 Subject: [PATCH] add network list to shadowsocks server --- common/net/network.go | 9 ++++ proxy/shadowsocks/config.pb.go | 76 ++++++++++++++++----------- proxy/shadowsocks/config.proto | 6 ++- proxy/shadowsocks/server.go | 9 ++-- testing/scenarios/shadowsocks_test.go | 13 +++-- 5 files changed, 74 insertions(+), 39 deletions(-) diff --git a/common/net/network.go b/common/net/network.go index f232de0e..33d53bc7 100644 --- a/common/net/network.go +++ b/common/net/network.go @@ -46,6 +46,15 @@ func (n Network) URLPrefix() string { } } +func HasNetwork(list []Network, network Network) bool { + for _, value := range list { + if string(value) == string(network) { + return true + } + } + return false +} + // HasNetwork returns true if the given network is in v NetworkList. func (l NetworkList) HasNetwork(network Network) bool { for _, value := range l.Network { diff --git a/proxy/shadowsocks/config.pb.go b/proxy/shadowsocks/config.pb.go index bd7cd3b6..c345cf22 100644 --- a/proxy/shadowsocks/config.pb.go +++ b/proxy/shadowsocks/config.pb.go @@ -3,6 +3,7 @@ package shadowsocks import proto "github.com/golang/protobuf/proto" import fmt "fmt" import math "math" +import v2ray_core_common_net "v2ray.com/core/common/net" import v2ray_core_common_protocol "v2ray.com/core/common/protocol" import v2ray_core_common_protocol1 "v2ray.com/core/common/protocol" @@ -116,8 +117,11 @@ func (m *Account) GetOta() Account_OneTimeAuth { } type ServerConfig struct { + // UdpEnabled specified whether or not to enable UDP for Shadowsocks. + // Deprecated. Use 'network' field. UdpEnabled bool `protobuf:"varint,1,opt,name=udp_enabled,json=udpEnabled" json:"udp_enabled,omitempty"` User *v2ray_core_common_protocol.User `protobuf:"bytes,2,opt,name=user" json:"user,omitempty"` + Network []v2ray_core_common_net.Network `protobuf:"varint,3,rep,packed,name=network,enum=v2ray.core.common.net.Network" json:"network,omitempty"` } func (m *ServerConfig) Reset() { *m = ServerConfig{} } @@ -139,6 +143,13 @@ func (m *ServerConfig) GetUser() *v2ray_core_common_protocol.User { return nil } +func (m *ServerConfig) GetNetwork() []v2ray_core_common_net.Network { + if m != nil { + return m.Network + } + return nil +} + type ClientConfig struct { Server []*v2ray_core_common_protocol1.ServerEndpoint `protobuf:"bytes,1,rep,name=server" json:"server,omitempty"` } @@ -166,35 +177,38 @@ func init() { func init() { proto.RegisterFile("v2ray.com/core/proxy/shadowsocks/config.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 477 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x91, 0x51, 0x8f, 0x93, 0x40, - 0x14, 0x85, 0x97, 0xb6, 0xb6, 0xf5, 0x52, 0x95, 0x9d, 0xc4, 0xa4, 0x69, 0x36, 0xb1, 0xe9, 0x53, - 0xdd, 0xc4, 0xa1, 0x65, 0x5d, 0xe3, 0x2b, 0x45, 0xd6, 0xdd, 0xa8, 0xb4, 0xa1, 0x5d, 0x8d, 0xbe, - 0x10, 0x76, 0x18, 0x2d, 0xb1, 0x30, 0x93, 0x19, 0xd8, 0x95, 0x5f, 0xe3, 0xbb, 0xff, 0xcc, 0x7f, - 0x61, 0x18, 0xda, 0x2e, 0xf1, 0xa1, 0xfb, 0xc6, 0xbd, 0x9c, 0x73, 0x38, 0xf7, 0x03, 0x5e, 0xdd, - 0x5a, 0x22, 0x2c, 0x30, 0x61, 0x89, 0x49, 0x98, 0xa0, 0x26, 0x17, 0xec, 0x57, 0x61, 0xca, 0x75, - 0x18, 0xb1, 0x3b, 0xc9, 0xc8, 0x4f, 0x69, 0x12, 0x96, 0x7e, 0x8f, 0x7f, 0x60, 0x2e, 0x58, 0xc6, - 0xd0, 0xc9, 0x4e, 0x2e, 0x28, 0x56, 0x52, 0x5c, 0x93, 0x0e, 0x5e, 0xfe, 0x17, 0x46, 0x58, 0x92, - 0xb0, 0xd4, 0x54, 0x56, 0xc2, 0x36, 0x66, 0x2e, 0xa9, 0xa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83, 0x76, 0xc5, - 0xbd, 0xaf, 0x0d, 0x9b, 0x63, 0xdd, 0x3a, 0x3d, 0x94, 0x53, 0x15, 0x74, 0xd3, 0x88, 0xb3, 0x38, - 0xcd, 0xfc, 0xad, 0xf3, 0xf4, 0xb7, 0x06, 0x70, 0x8f, 0xb3, 0x3c, 0xeb, 0xda, 0xfb, 0xe0, 0xcd, - 0xbf, 0x78, 0xc6, 0x11, 0x7a, 0x06, 0xba, 0xed, 0x2e, 0x83, 0xa9, 0xf5, 0x36, 0x70, 0x2e, 0x66, - 0x86, 0xb6, 0x5b, 0x58, 0xe7, 0x6f, 0xd4, 0xa2, 0x51, 0x32, 0x71, 0x2e, 0x6d, 0xe7, 0xd2, 0xb6, - 0x26, 0x46, 0x13, 0x1d, 0xc3, 0x93, 0xdd, 0x14, 0x5c, 0xb9, 0xab, 0x0b, 0xa3, 0x55, 0x8f, 0x78, - 0xef, 0x7c, 0x32, 0x1e, 0xd5, 0x23, 0xca, 0x45, 0x1b, 0x3d, 0x87, 0xe3, 0xbd, 0x69, 0x31, 0xff, - 0xf8, 0x75, 0x7a, 0x36, 0x39, 0x37, 0x3a, 0x25, 0x77, 0x6f, 0xee, 0xb9, 0x46, 0x77, 0xb6, 0x80, - 0x21, 0x61, 0xc9, 0xc1, 0xbf, 0xb9, 0xd0, 0xbe, 0xe9, 0xb5, 0xf1, 0x4f, 0xe3, 0xe4, 0xb3, 0xe5, - 0x87, 0x05, 0x76, 0x4a, 0xf5, 0x42, 0xa9, 0x97, 0xf7, 0xaf, 0x6f, 0xda, 0x0a, 0xca, 0xd9, 0xbf, - 0x00, 0x00, 0x00, 0xff, 0xff, 0x02, 0xd5, 0x9f, 0x8c, 0x4d, 0x03, 0x00, 0x00, + // 522 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x92, 0xc1, 0x6e, 0xd3, 0x4e, + 0x10, 0xc6, 0xbb, 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x9e, 0xfc, 0x40, 0x00, 0xf7, 0xd7, 0x28, 0x52, 0xb9, 0x76, 0xdf, 0xb9, 0xe3, 0x4f, 0xae, 0xb6, + 0x87, 0x1f, 0x83, 0x6a, 0x8f, 0xa6, 0xfe, 0xc0, 0x3a, 0xf7, 0x9d, 0x8b, 0xa1, 0x86, 0x36, 0x0d, + 0xeb, 0xec, 0xb5, 0x6c, 0x34, 0x8a, 0x48, 0x9d, 0x4b, 0xdb, 0xb9, 0xb4, 0xad, 0xbe, 0xa6, 0xe0, + 0x23, 0x78, 0xb4, 0xa9, 0xfc, 0xab, 0xd1, 0xec, 0x42, 0xdb, 0xaf, 0x5b, 0xbc, 0x75, 0x3e, 0x68, + 0xff, 0xd5, 0x2d, 0x8a, 0xc6, 0x01, 0x7e, 0x02, 0x47, 0x5b, 0xd1, 0x64, 0xfc, 0xfe, 0xf3, 0xe0, + 0xb4, 0x7f, 0xa6, 0x35, 0x8b, 0xb3, 0xb9, 0x63, 0x77, 0xa4, 0xb5, 0x86, 0x13, 0x30, 0x28, 0x5f, + 0xee, 0xfc, 0x18, 0x26, 0xe8, 0x8b, 0x5a, 0x2b, 0x7f, 0x35, 0x8e, 0x3f, 0x5a, 0x5e, 0xb0, 0x26, + 0x4e, 0x41, 0x4f, 0x24, 0x3d, 0xbd, 0x7f, 0xbe, 0x39, 0x90, 0xa1, 0x9c, 0xfe, 0x09, 0x00, 0x00, + 0xff, 0xff, 0xdc, 0x7e, 0x6b, 0x61, 0xb5, 0x03, 0x00, 0x00, } diff --git a/proxy/shadowsocks/config.proto b/proxy/shadowsocks/config.proto index a9a9fb5a..9e77a7ef 100644 --- a/proxy/shadowsocks/config.proto +++ b/proxy/shadowsocks/config.proto @@ -6,6 +6,7 @@ option go_package = "shadowsocks"; option java_package = "com.v2ray.core.proxy.shadowsocks"; option java_multiple_files = true; +import "v2ray.com/core/common/net/network.proto"; import "v2ray.com/core/common/protocol/user.proto"; import "v2ray.com/core/common/protocol/server_spec.proto"; @@ -33,8 +34,11 @@ enum CipherType { } message ServerConfig { - bool udp_enabled = 1; + // UdpEnabled specified whether or not to enable UDP for Shadowsocks. + // Deprecated. Use 'network' field. + bool udp_enabled = 1 [deprecated = true]; v2ray.core.common.protocol.User user = 2; + repeated v2ray.core.common.net.Network network = 3; } message ClientConfig { diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 5b75b9e0..a58d1972 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -17,7 +17,7 @@ import ( ) type Server struct { - config *ServerConfig + config ServerConfig user *protocol.User account *MemoryAccount v *core.Instance @@ -36,7 +36,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { account := rawAccount.(*MemoryAccount) s := &Server{ - config: config, + config: *config, user: config.GetUser(), account: account, v: core.MustFromContext(ctx), @@ -47,7 +47,10 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) { func (s *Server) Network() net.NetworkList { list := net.NetworkList{ - Network: []net.Network{net.Network_TCP}, + Network: s.config.Network, + } + if len(list.Network) == 0 { + list.Network = append(list.Network, net.Network_TCP) } if s.config.UdpEnabled { list.Network = append(list.Network, net.Network_UDP) diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 6a803157..0db921b7 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -60,6 +60,7 @@ func TestShadowsocksAES256TCP(t *testing.T) { Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -174,11 +175,11 @@ func TestShadowsocksAES128UDP(t *testing.T) { Listen: net.NewIPOrDomain(net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ - UdpEnabled: true, User: &protocol.User{ Account: account, Level: 1, }, + Network: []net.Network{net.Network_UDP}, }), }, }, @@ -297,6 +298,7 @@ func TestShadowsocksChacha20TCP(t *testing.T) { Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -414,6 +416,7 @@ func TestShadowsocksAES256GCMTCP(t *testing.T) { Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -527,11 +530,11 @@ func TestShadowsocksAES128GCMUDP(t *testing.T) { Listen: net.NewIPOrDomain(net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ - UdpEnabled: true, User: &protocol.User{ Account: account, Level: 1, }, + Network: []net.Network{net.Network_UDP}, }), }, }, @@ -645,11 +648,11 @@ func TestShadowsocksAES128GCMUDPMux(t *testing.T) { Listen: net.NewIPOrDomain(net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ - UdpEnabled: false, User: &protocol.User{ Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -773,6 +776,7 @@ func TestShadowsocksAES256GCMConformance(t *testing.T) { Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, }, @@ -847,11 +851,11 @@ func TestShadowsocksChacha20Poly1305UDPConformance(t *testing.T) { Listen: net.NewIPOrDomain(net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&shadowsocks.ServerConfig{ - UdpEnabled: true, User: &protocol.User{ Account: account, Level: 1, }, + Network: []net.Network{net.Network_UDP}, }), }, }, @@ -934,6 +938,7 @@ func TestShadowsocksChacha20Conformance(t *testing.T) { Account: account, Level: 1, }, + Network: []net.Network{net.Network_TCP}, }), }, },