From 1a21984c27618af7a9ee0879a7e636e2f82686f5 Mon Sep 17 00:00:00 2001 From: Chang lue Tsen Date: Sat, 31 May 2025 10:46:28 -0400 Subject: [PATCH] feat(shadowsocks): SIP022 AEAD-2022 Ciphers --- .../logic/admin/server/createNodeLogic.go | 22 ++++++++++++++++++- .../logic/admin/server/updateNodeLogic.go | 20 +++++++++++++++++ pkg/tool/cipher_test.go | 3 ++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/internal/logic/admin/server/createNodeLogic.go b/internal/logic/admin/server/createNodeLogic.go index 430602f..5108274 100644 --- a/internal/logic/admin/server/createNodeLogic.go +++ b/internal/logic/admin/server/createNodeLogic.go @@ -56,7 +56,7 @@ func (l *CreateNodeLogic) CreateNode(req *types.CreateNodeRequest) error { serverInfo.RelayNode = string(nodeRelay) if req.Protocol == "vless" { var cfg types.Vless - if err := json.Unmarshal(config, &cfg); err != nil { + if err = json.Unmarshal(config, &cfg); err != nil { return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) } if cfg.Security == "reality" && cfg.SecurityConfig.RealityPublicKey == "" { @@ -76,6 +76,26 @@ func (l *CreateNodeLogic) CreateNode(req *types.CreateNodeRequest) error { } config, _ = json.Marshal(cfg) serverInfo.Config = string(config) + } else if req.Protocol == "shadowsocks" { + var cfg types.Shadowsocks + if err = json.Unmarshal(config, &cfg); err != nil { + l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if strings.Contains(cfg.Method, "2022") { + var length int + switch cfg.Method { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(cfg.ServerKey) != length { + cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) + } + } + config, _ = json.Marshal(cfg) + serverInfo.Config = string(config) } err = l.svcCtx.ServerModel.Insert(l.ctx, &serverInfo) diff --git a/internal/logic/admin/server/updateNodeLogic.go b/internal/logic/admin/server/updateNodeLogic.go index 582c206..13fa176 100644 --- a/internal/logic/admin/server/updateNodeLogic.go +++ b/internal/logic/admin/server/updateNodeLogic.go @@ -86,6 +86,26 @@ func (l *UpdateNodeLogic) UpdateNode(req *types.UpdateNodeRequest) error { } config, _ = json.Marshal(cfg) nodeInfo.Config = string(config) + } else if req.Protocol == "shadowsocks" { + var cfg types.Shadowsocks + if err = json.Unmarshal(config, &cfg); err != nil { + l.Errorf("[CreateNode] Unmarshal Shadowsocks Config Error: %v", err.Error()) + return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "json.Unmarshal error: %v", err.Error()) + } + if strings.Contains(cfg.Method, "2022") { + var length int + switch cfg.Method { + case "2022-blake3-aes-128-gcm": + length = 16 + default: + length = 32 + } + if len(cfg.ServerKey) != length { + cfg.ServerKey = tool.GenerateCipher(cfg.ServerKey, length) + } + } + config, _ = json.Marshal(cfg) + nodeInfo.Config = string(config) } err = l.svcCtx.ServerModel.Update(l.ctx, nodeInfo) if err != nil { diff --git a/pkg/tool/cipher_test.go b/pkg/tool/cipher_test.go index fb0f592..4bfc07e 100644 --- a/pkg/tool/cipher_test.go +++ b/pkg/tool/cipher_test.go @@ -5,6 +5,7 @@ import ( ) func TestGenerateCipher(t *testing.T) { - pwd := GenerateCipher("serverKey", 128) + pwd := GenerateCipher("", 16) t.Logf("pwd: %s", pwd) + t.Logf("pwd length: %d", len(pwd)) }