feat: 统一响应体

This commit is contained in:
cheng zhen
2025-01-03 11:40:44 +08:00
parent f0960f21a9
commit 4ffc76a063
6 changed files with 1157 additions and 1603 deletions

View File

@@ -17,4 +17,4 @@ COPY . .
EXPOSE 3000
# 启动命令
CMD ["node", "server.js"]
CMD ["npm", "start"]

1496
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -4,8 +4,8 @@
"description": "License activation server for Cursor Pro",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
"start": "NODE_ENV=production node server.js",
"dev": "NODE_ENV=development nodemon server.js"
},
"dependencies": {
"cors": "^2.8.5",
@@ -19,5 +19,5 @@
"devDependencies": {
"nodemon": "^3.0.2"
},
"packageManager": "npm@10.8.3+sha512.d08425c8062f56d43bb8e84315864218af2492eb769e1f1ca40740f44e85bd148969382d651660363942e5909cb7ffcbef7ca0ae963ddc2c57a51243b4da8f56"
"packageManager": "pnpm@9.14.2+sha512.6e2baf77d06b9362294152c851c4f278ede37ab1eba3a55fda317a4a17b209f4dbb973fb250a77abc463a341fcb1f17f17cfa24091c4eb319cda0d9b84278387"
}

1030
server/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -2,10 +2,10 @@ require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const crypto = require('crypto');
const License = require('./models/License');
const LicenseKey = require('./models/LicenseKey');
const { formatChinaTime, getNowChinaTime, getNowChinaTimeString } = require('./utils/date');
const { encryptLicenseKey, decryptLicenseKey, generateLicenseKey, encryptResponse } = require('./utils/encryption');
const app = express();
@@ -13,59 +13,15 @@ const app = express();
app.use(cors());
app.use(express.json());
// Encryption functions
function getIV() {
// 如果需要固定 IV不推荐可以从环境变量获取
if (process.env.ENCRYPTION_IV) {
return Buffer.from(process.env.ENCRYPTION_IV, 'hex');
}
// 否则生成随机 IV更安全但需要存储
return crypto.randomBytes(16);
}
function encryptLicenseKey(text) {
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = getIV();
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
// 将 IV 附加到加密文本中,以便解密时使用
return iv.toString('hex') + ':' + encrypted;
}
function decryptLicenseKey(encrypted) {
const [ivHex, encryptedText] = encrypted.split(':');
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
function generateLicenseKey() {
const randomBytes = crypto.randomBytes(16);
const timestamp = Date.now().toString();
const combined = randomBytes.toString('hex') + timestamp;
const encrypted = encryptLicenseKey(combined);
// 由于加密后的字符串现在包含 IV我们需要使用完整的字符串
return encrypted;
}
// 在现有的加密函数下添加新的响应加密函数
function encryptResponse(data) {
// 将对象转换为 JSON 字符串
const jsonStr = JSON.stringify(data);
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = getIV();
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(jsonStr, 'utf8', 'hex');
encrypted += cipher.final('hex');
// 将 IV 附加到加密文本中
return {
encrypted_data: iv.toString('hex') + ':' + encrypted
};
}
// 添加访问日志中间件
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`[${getNowChinaTimeString()}] ${req.method} ${req.originalUrl} - ${res.statusCode} - ${duration}ms`);
});
next();
});
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, {
@@ -108,15 +64,15 @@ app.post(`/${GENERATE_PATH}`, async (req, res) => {
// 加密响应数据
const responseData = {
success: true,
license_key: licenseKey
code: 0,
data: licenseKey
};
return res.json(responseData);
} catch (error) {
console.error('生成许可证错误:', error);
return res.status(500).json(encryptResponse({
success: false,
code: -1,
message: '服务器错误'
}));
}
@@ -129,46 +85,46 @@ app.post('/activate', async (req, res) => {
// Validate input
if (!license_key || !machine_code) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证密钥和机器码是必需的'
});
}));
}
// Validate license key format
try {
decryptLicenseKey(license_key);
} catch (error) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '无效的许可证密钥'
});
}));
}
// 检查许可证是否存在于生成记录中
const licenseKeyRecord = await LicenseKey.findOne({ licenseKey: license_key });
if (!licenseKeyRecord) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '无效的许可证密钥'
});
}));
}
// 检查许可证是否已被使用
if (licenseKeyRecord.isUsed) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '此许可证密钥已被使用,不能重复激活'
});
}));
}
// 检查许可证激活状态
const existingLicense = await License.findOne({ licenseKey: license_key });
if (existingLicense) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '此许可证已被激活,不能重复使用'
});
})) ;
}
// 更新过期时间计算,使用中国时区
@@ -190,16 +146,18 @@ app.post('/activate', async (req, res) => {
await licenseKeyRecord.save();
const responseData = {
success: true,
code: 0,
message: '激活成功',
expiry_date: expiryDate
data: {
expiry_date: expiryDate
}
};
return res.json(encryptResponse(responseData));
} catch (error) {
console.error('激活错误:', error);
return res.status(500).json(encryptResponse({
success: false,
code: -1,
message: '服务器错误'
}));
}
@@ -212,62 +170,62 @@ app.post('/verify', async (req, res) => {
// Validate input
if (!license_key || !machine_code) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证密钥和机器码是必需的'
});
}));
}
// Validate license key format
try {
decryptLicenseKey(license_key);
} catch (error) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '无效的许可证密钥'
});
}));
}
// Find license
const license = await License.findOne({ licenseKey: license_key });
if (!license) {
return res.status(404).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证不存在'
});
}));
}
// Check machine code
if (license.machineCode !== machine_code) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '硬件信息不匹配'
});
}));
}
// Check if license is active
if (!license.isActive) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证已被禁用'
});
}));
}
// 使用中国时区检查过期时间
if (getNowChinaTime().isAfter(license.expiryDate)) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证已过期'
});
}));
}
// 检查使用次数
if (license.currentUsageCount >= license.maxUsageCount) {
return res.status(400).json({
success: false,
return res.status(200).json(encryptResponse({
code: -1,
message: '许可证使用次数已达到上限'
});
}));
}
// 更新使用次数
@@ -275,12 +233,14 @@ app.post('/verify', async (req, res) => {
await license.save();
const responseData = {
success: true,
message: '许可证有效',
expiry_date: formatChinaTime(license.expiryDate, 'YYYY-MM-DD'),
usage_count: {
current: license.currentUsageCount,
max: license.maxUsageCount
code: 0,
data: {
message: '许可证有效',
expiry_date: formatChinaTime(license.expiryDate, 'YYYY-MM-DD'),
usage_count: {
current: license.currentUsageCount,
max: license.maxUsageCount
}
}
};
@@ -288,7 +248,7 @@ app.post('/verify', async (req, res) => {
} catch (error) {
console.error('验证错误:', error);
return res.status(500).json(encryptResponse({
success: false,
code: -1,
message: '服务器错误'
}));
}

View File

@@ -0,0 +1,60 @@
const crypto = require('crypto');
function getIV() {
if (process.env.ENCRYPTION_IV) {
return Buffer.from(process.env.ENCRYPTION_IV, 'hex');
}
return crypto.randomBytes(16);
}
function encryptLicenseKey(text) {
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = getIV();
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted;
}
function decryptLicenseKey(encrypted) {
const [ivHex, encryptedText] = encrypted.split(':');
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = Buffer.from(ivHex, 'hex');
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
function generateLicenseKey() {
const randomBytes = crypto.randomBytes(16);
const timestamp = Date.now().toString();
const combined = randomBytes.toString('hex') + timestamp;
const encrypted = encryptLicenseKey(combined);
return encrypted;
}
function encryptResponse(data) {
// 在开发模式下直接返回原始数据
if (process.env.NODE_ENV === 'development') {
return data;
}
// 生产模式下加密数据
const jsonStr = JSON.stringify(data);
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
const iv = getIV();
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(jsonStr, 'utf8', 'hex');
encrypted += cipher.final('hex');
return {
encrypted_data: iv.toString('hex') + ':' + encrypted
};
}
module.exports = {
encryptLicenseKey,
decryptLicenseKey,
generateLicenseKey,
encryptResponse
};