From b67314796f2e791a4311da21cd6ac2712ed3b797 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sun, 7 Aug 2022 19:18:23 -0400 Subject: [PATCH] Add shadowsocks 2022 relay config --- infra/conf/shadowsocks.go | 99 +++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/infra/conf/shadowsocks.go b/infra/conf/shadowsocks.go index 2b5f2a8a..853010c3 100644 --- a/infra/conf/shadowsocks.go +++ b/infra/conf/shadowsocks.go @@ -32,10 +32,12 @@ func cipherFromString(c string) shadowsocks.CipherType { } type ShadowsocksUserConfig struct { - Cipher string `json:"method"` - Password string `json:"password"` - Level byte `json:"level"` - Email string `json:"email"` + Cipher string `json:"method"` + Password string `json:"password"` + Level byte `json:"level"` + Email string `json:"email"` + Address *Address `json:"address"` + Port uint16 `json:"port"` } type ShadowsocksServerConfig struct { @@ -50,38 +52,7 @@ type ShadowsocksServerConfig struct { func (v *ShadowsocksServerConfig) Build() (proto.Message, error) { if C.Contains(shadowaead_2022.List, v.Cipher) { - if len(v.Users) > 0 { - if v.Cipher == "" { - return nil, newError("shadowsocks 2022 (multi-user): missing server method") - } - if !strings.Contains(v.Cipher, "aes") { - return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported") - } - - config := new(shadowsocks_2022.MultiUserServerConfig) - config.Method = v.Cipher - - config.Key = v.Password - config.Network = v.NetworkList.Build() - - for _, user := range v.Users { - if user.Cipher != "" { - return nil, newError("shadowsocks 2022 (multi-user): users must have empty method") - } - config.Users = append(config.Users, &shadowsocks_2022.User{ - Key: user.Password, - Email: user.Email, - }) - } - return config, nil - } - - config := new(shadowsocks_2022.ServerConfig) - config.Method = v.Cipher - config.Key = v.Password - config.Network = v.NetworkList.Build() - config.Email = v.Email - return config, nil + return buildShadowsocks2022(v) } config := new(shadowsocks.ServerConfig) @@ -129,6 +100,62 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) { return config, nil } +func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) { + if len(v.Users) == 0 { + config := new(shadowsocks_2022.ServerConfig) + config.Method = v.Cipher + config.Key = v.Password + config.Network = v.NetworkList.Build() + config.Email = v.Email + return config, nil + } + + if v.Cipher == "" { + return nil, newError("shadowsocks 2022 (multi-user): missing server method") + } + if !strings.Contains(v.Cipher, "aes") { + return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported") + } + + if v.Users[0].Address == nil { + config := new(shadowsocks_2022.MultiUserServerConfig) + config.Method = v.Cipher + config.Key = v.Password + config.Network = v.NetworkList.Build() + + for _, user := range v.Users { + if user.Cipher != "" { + return nil, newError("shadowsocks 2022 (multi-user): users must have empty method") + } + config.Users = append(config.Users, &shadowsocks_2022.User{ + Key: user.Password, + Email: user.Email, + }) + } + return config, nil + } + + config := new(shadowsocks_2022.RelayServerConfig) + config.Method = v.Cipher + config.Key = v.Password + config.Network = v.NetworkList.Build() + for _, user := range v.Users { + if user.Cipher != "" { + return nil, newError("shadowsocks 2022 (relay): users must have empty method") + } + if user.Address == nil { + return nil, newError("shadowsocks 2022 (relay): all users must have relay address") + } + config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{ + Key: user.Password, + Email: user.Email, + Address: user.Address.Build(), + Port: uint32(user.Port), + }) + } + return config, nil +} + type ShadowsocksServerTarget struct { Address *Address `json:"address"` Port uint16 `json:"port"`