feat(client): 添加 Node.js 客户端及示例应用
- 新增 nodejs-client 模块,提供 ConfigClient 配置拉取、定时同步、变更监听 - 使用 Node.js 内置 crypto 模块实现 AES 解密,零外部依赖 - 新增 nodejs-client-demo Express 示例应用,端口 8083 - 更新 README.md,补充 Python 和 Node.js 客户端使用说明 - 修复 fetchAllConfigs API 响应格式判断逻辑
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
const CRYPT_PREFIX = '{Cipher}';
|
||||
|
||||
export function decryptConfig(secret, data) {
|
||||
if (!data.startsWith(CRYPT_PREFIX)) {
|
||||
return data;
|
||||
}
|
||||
const encrypted = data.slice(CRYPT_PREFIX.length);
|
||||
return decryptWithAES(secret, encrypted);
|
||||
}
|
||||
|
||||
export function decryptWithAES(key, data) {
|
||||
const md5Hash = crypto.createHash('md5').update(key).digest('hex');
|
||||
const aesKey = md5Hash.slice(16);
|
||||
const cipherText = Buffer.from(data, 'base64');
|
||||
const iv = Buffer.alloc(16, 0);
|
||||
const decipher = crypto.createDecipheriv('aes-128-cbc', aesKey, iv);
|
||||
decipher.setAutoPadding(false);
|
||||
let plainText = Buffer.concat([decipher.update(cipherText), decipher.final()]);
|
||||
const padding = plainText[plainText.length - 1];
|
||||
plainText = plainText.slice(0, plainText.length - padding);
|
||||
return plainText.toString('utf8');
|
||||
}
|
||||
Reference in New Issue
Block a user