From b236c702fffa943d4365676b8be642aa9ba7cbc7 Mon Sep 17 00:00:00 2001 From: cubemaze Date: Tue, 30 May 2023 16:08:21 +0800 Subject: [PATCH] fix: ss2022 keyLength match --- common/builder/user.go | 76 +++++++++++++++++++++++------------------- core/user.go | 19 +---------- 2 files changed, 42 insertions(+), 53 deletions(-) diff --git a/common/builder/user.go b/common/builder/user.go index b532348..fdaa4a5 100644 --- a/common/builder/user.go +++ b/common/builder/user.go @@ -77,6 +77,47 @@ func BuildTrojanUser(tag string, userInfo *panel.UserInfo) (user *protocol.User) Account: serial.ToTypedMessage(trojanAccount), } } +func BuildSSUsers(tag string, userInfo []panel.UserInfo, cypher string, serverKey string) (users []*protocol.User) { + users = make([]*protocol.User, len(userInfo)) + for i := range userInfo { + users[i] = BuildSSUser(tag, &userInfo[i], cypher, serverKey) + } + return users +} + +func BuildSSUser(tag string, userInfo *panel.UserInfo, cypher string, serverKey string) (user *protocol.User) { + if serverKey == "" { + ssAccount := &shadowsocks.Account{ + Password: userInfo.Uuid, + CipherType: getCipherFromString(cypher), + } + return &protocol.User{ + Level: 0, + Email: tag, + Account: serial.ToTypedMessage(ssAccount), + } + } else { + var keyLength int + switch cypher { + case "2022-blake3-aes-128-gcm": + keyLength = 16 + case "2022-blake3-aes-256-gcm": + keyLength = 32 + } + ssAccount := &shadowsocks_2022.User{ + Key: base64.StdEncoding.EncodeToString([]byte(userInfo.Uuid[:keyLength])), + } + return &protocol.User{ + Level: 0, + Email: tag, + Account: serial.ToTypedMessage(ssAccount), + } + } +} + +func BuildUserTag(tag string, user *panel.UserInfo) string { + return fmt.Sprintf("%s|%s|%d", tag, user.Uuid, user.Id) +} func getCipherFromString(c string) shadowsocks.CipherType { switch strings.ToLower(c) { @@ -92,38 +133,3 @@ func getCipherFromString(c string) shadowsocks.CipherType { return shadowsocks.CipherType_UNKNOWN } } - -func BuildSSUsers(tag string, userInfo []panel.UserInfo, cypher shadowsocks.CipherType, serverKey string) (users []*protocol.User) { - users = make([]*protocol.User, len(userInfo)) - for i := range userInfo { - users[i] = BuildSSUser(tag, &userInfo[i], cypher, serverKey) - } - return users -} - -func BuildSSUser(tag string, userInfo *panel.UserInfo, cypher shadowsocks.CipherType, serverKey string) (user *protocol.User) { - if serverKey == "" { - ssAccount := &shadowsocks.Account{ - Password: userInfo.Uuid, - CipherType: cypher, - } - return &protocol.User{ - Level: 0, - Email: tag, - Account: serial.ToTypedMessage(ssAccount), - } - } else { - ssAccount := &shadowsocks_2022.User{ - Key: base64.StdEncoding.EncodeToString([]byte(userInfo.Uuid[:32])), - } - return &protocol.User{ - Level: 0, - Email: tag, - Account: serial.ToTypedMessage(ssAccount), - } - } -} - -func BuildUserTag(tag string, user *panel.UserInfo) string { - return fmt.Sprintf("%s|%s|%d", tag, user.Uuid, user.Id) -} diff --git a/core/user.go b/core/user.go index 8e1956a..e751b48 100644 --- a/core/user.go +++ b/core/user.go @@ -8,8 +8,6 @@ import ( "github.com/Yuzuki616/V2bX/conf" "github.com/xtls/xray-core/common/protocol" "github.com/xtls/xray-core/proxy" - "github.com/xtls/xray-core/proxy/shadowsocks" - "strings" ) func (c *Core) GetUserManager(tag string) (proxy.UserManager, error) { @@ -86,7 +84,7 @@ func (c *Core) AddUsers(p *AddUsersParams) (added int, err error) { case "shadowsocks": users = builder.BuildSSUsers(p.Tag, p.UserInfo, - getCipherFromString(p.NodeInfo.Cipher), + p.NodeInfo.Cipher, p.NodeInfo.ServerKey) default: return 0, fmt.Errorf("unsupported node type: %s", p.NodeInfo.NodeType) @@ -107,18 +105,3 @@ func (c *Core) AddUsers(p *AddUsersParams) (added int, err error) { } return len(users), nil } - -func getCipherFromString(c string) shadowsocks.CipherType { - switch strings.ToLower(c) { - case "aes-128-gcm", "aead_aes_128_gcm": - return shadowsocks.CipherType_AES_128_GCM - case "aes-256-gcm", "aead_aes_256_gcm": - return shadowsocks.CipherType_AES_256_GCM - case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305": - return shadowsocks.CipherType_CHACHA20_POLY1305 - case "none", "plain": - return shadowsocks.CipherType_NONE - default: - return shadowsocks.CipherType_UNKNOWN - } -}