diff --git a/proxy/shadowsocks/config.go b/proxy/shadowsocks/config.go index b6ee555fa..8718cc0bb 100644 --- a/proxy/shadowsocks/config.go +++ b/proxy/shadowsocks/config.go @@ -8,15 +8,15 @@ import ( "v2ray.com/core/common/protocol" ) -func (this *Config) GetCipher() Cipher { - switch this.Cipher { - case Config_AES_128_CFB: +func (this *Account) GetCipher() Cipher { + switch this.CipherType { + case CipherType_AES_128_CFB: return &AesCfb{KeyBytes: 16} - case Config_AES_256_CFB: + case CipherType_AES_256_CFB: return &AesCfb{KeyBytes: 32} - case Config_CHACHA20: + case CipherType_CHACHA20: return &ChaCha20{IVBytes: 8} - case Config_CHACHA20_IEFT: + case CipherType_CHACHA20_IEFT: return &ChaCha20{IVBytes: 12} } panic("Failed to create Cipher. Should not happen.") diff --git a/proxy/shadowsocks/config.pb.go b/proxy/shadowsocks/config.pb.go index 11ffdeef8..11ea541bf 100644 --- a/proxy/shadowsocks/config.pb.go +++ b/proxy/shadowsocks/config.pb.go @@ -10,7 +10,8 @@ It is generated from these files: It has these top-level messages: Account - Config + ServerConfig + ClientConfig */ package shadowsocks @@ -18,6 +19,7 @@ import proto "github.com/golang/protobuf/proto" import fmt "fmt" import math "math" import com_v2ray_core_common_protocol "v2ray.com/core/common/protocol" +import com_v2ray_core_common_protocol1 "v2ray.com/core/common/protocol" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -30,24 +32,24 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package -type Config_Cipher int32 +type CipherType int32 const ( - Config_UNKNOWN Config_Cipher = 0 - Config_AES_128_CFB Config_Cipher = 1 - Config_AES_256_CFB Config_Cipher = 2 - Config_CHACHA20 Config_Cipher = 3 - Config_CHACHA20_IEFT Config_Cipher = 4 + CipherType_UNKNOWN CipherType = 0 + CipherType_AES_128_CFB CipherType = 1 + CipherType_AES_256_CFB CipherType = 2 + CipherType_CHACHA20 CipherType = 3 + CipherType_CHACHA20_IEFT CipherType = 4 ) -var Config_Cipher_name = map[int32]string{ +var CipherType_name = map[int32]string{ 0: "UNKNOWN", 1: "AES_128_CFB", 2: "AES_256_CFB", 3: "CHACHA20", 4: "CHACHA20_IEFT", } -var Config_Cipher_value = map[string]int32{ +var CipherType_value = map[string]int32{ "UNKNOWN": 0, "AES_128_CFB": 1, "AES_256_CFB": 2, @@ -55,13 +57,14 @@ var Config_Cipher_value = map[string]int32{ "CHACHA20_IEFT": 4, } -func (x Config_Cipher) String() string { - return proto.EnumName(Config_Cipher_name, int32(x)) +func (x CipherType) String() string { + return proto.EnumName(CipherType_name, int32(x)) } -func (Config_Cipher) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{1, 0} } +func (CipherType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } type Account struct { - Password string `protobuf:"bytes,1,opt,name=password" json:"password,omitempty"` + Password string `protobuf:"bytes,1,opt,name=password" json:"password,omitempty"` + CipherType CipherType `protobuf:"varint,2,opt,name=cipher_type,json=cipherType,enum=com.v2ray.core.proxy.shadowsocks.CipherType" json:"cipher_type,omitempty"` } func (m *Account) Reset() { *m = Account{} } @@ -69,52 +72,71 @@ func (m *Account) String() string { return proto.CompactTextString(m) func (*Account) ProtoMessage() {} func (*Account) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } -type Config struct { - Cipher Config_Cipher `protobuf:"varint,1,opt,name=cipher,enum=com.v2ray.core.proxy.shadowsocks.Config_Cipher" json:"cipher,omitempty"` - UdpEnabled bool `protobuf:"varint,2,opt,name=udp_enabled,json=udpEnabled" json:"udp_enabled,omitempty"` - User *com_v2ray_core_common_protocol.User `protobuf:"bytes,3,opt,name=user" json:"user,omitempty"` +type ServerConfig struct { + UdpEnabled bool `protobuf:"varint,1,opt,name=udp_enabled,json=udpEnabled" json:"udp_enabled,omitempty"` + User *com_v2ray_core_common_protocol.User `protobuf:"bytes,2,opt,name=user" json:"user,omitempty"` } -func (m *Config) Reset() { *m = Config{} } -func (m *Config) String() string { return proto.CompactTextString(m) } -func (*Config) ProtoMessage() {} -func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } +func (m *ServerConfig) Reset() { *m = ServerConfig{} } +func (m *ServerConfig) String() string { return proto.CompactTextString(m) } +func (*ServerConfig) ProtoMessage() {} +func (*ServerConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } -func (m *Config) GetUser() *com_v2ray_core_common_protocol.User { +func (m *ServerConfig) GetUser() *com_v2ray_core_common_protocol.User { if m != nil { return m.User } return nil } +type ClientConfig struct { + Server []*com_v2ray_core_common_protocol1.ServerSpecPB `protobuf:"bytes,1,rep,name=server" json:"server,omitempty"` +} + +func (m *ClientConfig) Reset() { *m = ClientConfig{} } +func (m *ClientConfig) String() string { return proto.CompactTextString(m) } +func (*ClientConfig) ProtoMessage() {} +func (*ClientConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } + +func (m *ClientConfig) GetServer() []*com_v2ray_core_common_protocol1.ServerSpecPB { + if m != nil { + return m.Server + } + return nil +} + func init() { proto.RegisterType((*Account)(nil), "com.v2ray.core.proxy.shadowsocks.Account") - proto.RegisterType((*Config)(nil), "com.v2ray.core.proxy.shadowsocks.Config") - proto.RegisterEnum("com.v2ray.core.proxy.shadowsocks.Config_Cipher", Config_Cipher_name, Config_Cipher_value) + proto.RegisterType((*ServerConfig)(nil), "com.v2ray.core.proxy.shadowsocks.ServerConfig") + proto.RegisterType((*ClientConfig)(nil), "com.v2ray.core.proxy.shadowsocks.ClientConfig") + proto.RegisterEnum("com.v2ray.core.proxy.shadowsocks.CipherType", CipherType_name, CipherType_value) } func init() { proto.RegisterFile("v2ray.com/core/proxy/shadowsocks/config.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 308 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x8f, 0x41, 0x4f, 0xc2, 0x40, - 0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 0x67, 0x83, 0x42, 0x21, 0x3d, 0xa8, 0xfd, 0x34, - 0x6d, 0x55, 0x1d, 0xa3, 0x6b, 0xb9, 0xc7, 0xdb, 0x39, 0xc5, 0x2a, 0x5a, 0xae, 0xa2, 0x13, 0x25, - 0x64, 0xa0, 0x89, 0xce, 0x33, 0x98, 0x45, 0x18, 0xb1, 0xa0, 0x3e, 0x19, 0xdf, 0x8d, 0xef, 0x9f, - 0xc6, 0xf6, 0x0e, 0x39, 0x00, 0xcb, 0x1b, 0x3c, 0x84, 0x17, 0x6e, 0x2f, 0xf4, 0x87, 0x7d, 0xdb, - 0x28, 0x05, 0xf7, 0xea, 0x5a, 0x0b, 0x15, 0xb2, 0x07, 0x0d, 0x7f, 0xe4, 0xf9, 0x23, 0xcf, 0x3d, - 0xb7, 0xab, 0xe4, 0x10, 0x9a, 0xe5, 0x15, 0xde, 0x0e, 0x86, 0x8f, 0x76, 0xad, 0xdf, 0x7c, 0xb1, - 0x36, 0x96, 0x4d, 0x4d, 0x9d, 0x7e, 0xf9, 0x1d, 0x00, 0x00, 0xff, 0xff, 0x1d, 0xee, 0x41, 0x94, - 0xc3, 0x01, 0x00, 0x00, + // 363 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x90, 0xcf, 0x6f, 0xda, 0x30, + 0x14, 0xc7, 0x17, 0x40, 0xc0, 0x5e, 0x60, 0xcb, 0x7c, 0x42, 0x5c, 0x16, 0xa1, 0x1d, 0xd8, 0xb4, + 0x39, 0x2c, 0xd3, 0x26, 0xae, 0x21, 0x03, 0x51, 0x55, 0xa5, 0x55, 0x00, 0x55, 0xaa, 0x2a, 0x45, + 0xc1, 0x71, 0x4b, 0x54, 0x12, 0x5b, 0x76, 0x02, 0xcd, 0x7f, 0x5f, 0xd5, 0x21, 0x14, 0xd1, 0x03, + 0x37, 0xfb, 0xe9, 0x7d, 0xde, 0xf7, 0x07, 0x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0x9a, 0xa4, + 0x7b, 0xa9, 0xff, 0x50, 0x2f, 0x5a, 0xe8, 0x68, 0x66, 0xb5, 0xaf, 0xbf, 0x0f, 0x71, 0x7a, 0xab, + 0x30, 0x3a, 0xe7, 0x94, 0xdc, 0x8c, 0xbc, 0x3d, 0xfb, 0xe3, 0x1e, 0xe0, 0x2d, 0x1a, 0xd2, 0xa1, + 0xb1, 0x9c, 0x5d, 0xce, 0xae, 0x6f, 0x67, 0xc6, 0x07, 0xf4, 0x19, 0x74, 0x67, 0x3c, 0xf7, 0x7f, + 0xdb, 0x43, 0xdf, 0x9d, 0x8c, 0x0c, 0xad, 0x1c, 0xd8, 0x7f, 0xff, 0xa9, 0x41, 0x05, 0xb5, 0xa0, + 0xe9, 0x4e, 0x1d, 0x77, 0xea, 0xd8, 0x03, 0xa3, 0x8a, 0xbe, 0x40, 0xbb, 0xfc, 0xf9, 0x17, 0xe3, + 0xc9, 0xc2, 0xa8, 0x8d, 0xda, 0x77, 0xfa, 0x51, 0x89, 0xab, 0xba, 0xf2, 0xf2, 0xe7, 0x25, 0x00, + 0x00, 0xff, 0xff, 0x37, 0xac, 0x76, 0x71, 0x5a, 0x02, 0x00, 0x00, } diff --git a/proxy/shadowsocks/config.proto b/proxy/shadowsocks/config.proto index 53c6990fb..f7c273071 100644 --- a/proxy/shadowsocks/config.proto +++ b/proxy/shadowsocks/config.proto @@ -1,23 +1,29 @@ syntax = "proto3"; -import "v2ray.com/core/common/protocol/user.proto"; - package com.v2ray.core.proxy.shadowsocks; option go_package = "shadowsocks"; +import "v2ray.com/core/common/protocol/user.proto"; +import "v2ray.com/core/common/protocol/server_spec.proto"; + message Account { string password = 1; + CipherType cipher_type = 2; } -message Config { - enum Cipher { - UNKNOWN = 0; - AES_128_CFB = 1; - AES_256_CFB = 2; - CHACHA20 = 3; - CHACHA20_IEFT = 4; - } - Cipher cipher = 1; - bool udp_enabled = 2; - com.v2ray.core.common.protocol.User user = 3; +enum CipherType { + UNKNOWN = 0; + AES_128_CFB = 1; + AES_256_CFB = 2; + CHACHA20 = 3; + CHACHA20_IEFT = 4; +} + +message ServerConfig { + bool udp_enabled = 1; + com.v2ray.core.common.protocol.User user = 2; +} + +message ClientConfig { + repeated com.v2ray.core.common.protocol.ServerSpecPB server = 1; } \ No newline at end of file diff --git a/proxy/shadowsocks/config_json.go b/proxy/shadowsocks/config_json.go index 5294249fa..7dfcf8fca 100644 --- a/proxy/shadowsocks/config_json.go +++ b/proxy/shadowsocks/config_json.go @@ -15,7 +15,7 @@ import ( "github.com/golang/protobuf/ptypes" ) -func (this *Config) UnmarshalJSON(data []byte) error { +func (this *ServerConfig) UnmarshalJSON(data []byte) error { type JsonConfig struct { Cipher string `json:"method"` Password string `json:"password"` @@ -29,28 +29,30 @@ func (this *Config) UnmarshalJSON(data []byte) error { } this.UdpEnabled = jsonConfig.UDP - jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher) - switch jsonConfig.Cipher { - case "aes-256-cfb": - this.Cipher = Config_AES_256_CFB - case "aes-128-cfb": - this.Cipher = Config_AES_128_CFB - case "chacha20": - this.Cipher = Config_CHACHA20 - case "chacha20-ietf": - this.Cipher = Config_CHACHA20_IEFT - default: - log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher) - return common.ErrBadConfiguration - } if len(jsonConfig.Password) == 0 { log.Error("Shadowsocks: Password is not specified.") return common.ErrBadConfiguration } - account, err := ptypes.MarshalAny(&Account{ + account := &Account{ Password: jsonConfig.Password, - }) + } + jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher) + switch jsonConfig.Cipher { + case "aes-256-cfb": + account.CipherType = CipherType_AES_256_CFB + case "aes-128-cfb": + account.CipherType = CipherType_AES_128_CFB + case "chacha20": + account.CipherType = CipherType_CHACHA20 + case "chacha20-ietf": + account.CipherType = CipherType_CHACHA20_IEFT + default: + log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher) + return common.ErrBadConfiguration + } + + anyAccount, err := ptypes.MarshalAny(account) if err != nil { log.Error("Shadowsocks: Failed to create account: ", err) return common.ErrBadConfiguration @@ -58,12 +60,12 @@ func (this *Config) UnmarshalJSON(data []byte) error { this.User = &protocol.User{ Email: jsonConfig.Email, Level: uint32(jsonConfig.Level), - Account: account, + Account: anyAccount, } return nil } func init() { - registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) }) + registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(ServerConfig) }) } diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index df4e64814..c64f9710d 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -23,7 +23,7 @@ import ( type Server struct { packetDispatcher dispatcher.PacketDispatcher - config *Config + config *ServerConfig cipher Cipher cipherKey []byte meta *proxy.InboundHandlerMeta @@ -33,7 +33,7 @@ type Server struct { udpServer *udp.UDPServer } -func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) { +func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) { if config.GetUser() == nil { return nil, protocol.ErrUserMissing } @@ -41,7 +41,7 @@ func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) if _, err := config.GetUser().GetTypedAccount(account); err != nil { return nil, err } - cipher := config.GetCipher() + cipher := account.GetCipher() s := &Server{ config: config, meta: meta, @@ -283,7 +283,7 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta * if !space.HasApp(dispatcher.APP_ID) { return nil, common.ErrBadConfiguration } - return NewServer(rawConfig.(*Config), space, meta) + return NewServer(rawConfig.(*ServerConfig), space, meta) } func init() {