feat: 统一响应体
This commit is contained in:
@@ -17,4 +17,4 @@ COPY . .
|
|||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
||||||
# 启动命令
|
# 启动命令
|
||||||
CMD ["node", "server.js"]
|
CMD ["npm", "start"]
|
||||||
1496
server/package-lock.json
generated
1496
server/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,8 +4,8 @@
|
|||||||
"description": "License activation server for Cursor Pro",
|
"description": "License activation server for Cursor Pro",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "NODE_ENV=production node server.js",
|
||||||
"dev": "nodemon server.js"
|
"dev": "NODE_ENV=development nodemon server.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
@@ -19,5 +19,5 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^3.0.2"
|
"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
1030
server/pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
166
server/server.js
166
server/server.js
@@ -2,10 +2,10 @@ require('dotenv').config();
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const crypto = require('crypto');
|
|
||||||
const License = require('./models/License');
|
const License = require('./models/License');
|
||||||
const LicenseKey = require('./models/LicenseKey');
|
const LicenseKey = require('./models/LicenseKey');
|
||||||
const { formatChinaTime, getNowChinaTime, getNowChinaTimeString } = require('./utils/date');
|
const { formatChinaTime, getNowChinaTime, getNowChinaTimeString } = require('./utils/date');
|
||||||
|
const { encryptLicenseKey, decryptLicenseKey, generateLicenseKey, encryptResponse } = require('./utils/encryption');
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@@ -13,59 +13,15 @@ const app = express();
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
// Encryption functions
|
// 添加访问日志中间件
|
||||||
function getIV() {
|
app.use((req, res, next) => {
|
||||||
// 如果需要固定 IV(不推荐),可以从环境变量获取
|
const start = Date.now();
|
||||||
if (process.env.ENCRYPTION_IV) {
|
res.on('finish', () => {
|
||||||
return Buffer.from(process.env.ENCRYPTION_IV, 'hex');
|
const duration = Date.now() - start;
|
||||||
}
|
console.log(`[${getNowChinaTimeString()}] ${req.method} ${req.originalUrl} - ${res.statusCode} - ${duration}ms`);
|
||||||
// 否则生成随机 IV(更安全,但需要存储)
|
});
|
||||||
return crypto.randomBytes(16);
|
next();
|
||||||
}
|
});
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to MongoDB
|
// Connect to MongoDB
|
||||||
mongoose.connect(process.env.MONGODB_URI, {
|
mongoose.connect(process.env.MONGODB_URI, {
|
||||||
@@ -108,15 +64,15 @@ app.post(`/${GENERATE_PATH}`, async (req, res) => {
|
|||||||
|
|
||||||
// 加密响应数据
|
// 加密响应数据
|
||||||
const responseData = {
|
const responseData = {
|
||||||
success: true,
|
code: 0,
|
||||||
license_key: licenseKey
|
data: licenseKey
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json(responseData);
|
return res.json(responseData);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('生成许可证错误:', error);
|
console.error('生成许可证错误:', error);
|
||||||
return res.status(500).json(encryptResponse({
|
return res.status(500).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '服务器错误'
|
message: '服务器错误'
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -129,46 +85,46 @@ app.post('/activate', async (req, res) => {
|
|||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
if (!license_key || !machine_code) {
|
if (!license_key || !machine_code) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证密钥和机器码是必需的'
|
message: '许可证密钥和机器码是必需的'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate license key format
|
// Validate license key format
|
||||||
try {
|
try {
|
||||||
decryptLicenseKey(license_key);
|
decryptLicenseKey(license_key);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '无效的许可证密钥'
|
message: '无效的许可证密钥'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查许可证是否存在于生成记录中
|
// 检查许可证是否存在于生成记录中
|
||||||
const licenseKeyRecord = await LicenseKey.findOne({ licenseKey: license_key });
|
const licenseKeyRecord = await LicenseKey.findOne({ licenseKey: license_key });
|
||||||
if (!licenseKeyRecord) {
|
if (!licenseKeyRecord) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '无效的许可证密钥'
|
message: '无效的许可证密钥'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查许可证是否已被使用
|
// 检查许可证是否已被使用
|
||||||
if (licenseKeyRecord.isUsed) {
|
if (licenseKeyRecord.isUsed) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '此许可证密钥已被使用,不能重复激活'
|
message: '此许可证密钥已被使用,不能重复激活'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查许可证激活状态
|
// 检查许可证激活状态
|
||||||
const existingLicense = await License.findOne({ licenseKey: license_key });
|
const existingLicense = await License.findOne({ licenseKey: license_key });
|
||||||
if (existingLicense) {
|
if (existingLicense) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '此许可证已被激活,不能重复使用'
|
message: '此许可证已被激活,不能重复使用'
|
||||||
});
|
})) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新过期时间计算,使用中国时区
|
// 更新过期时间计算,使用中国时区
|
||||||
@@ -190,16 +146,18 @@ app.post('/activate', async (req, res) => {
|
|||||||
await licenseKeyRecord.save();
|
await licenseKeyRecord.save();
|
||||||
|
|
||||||
const responseData = {
|
const responseData = {
|
||||||
success: true,
|
code: 0,
|
||||||
message: '激活成功',
|
message: '激活成功',
|
||||||
expiry_date: expiryDate
|
data: {
|
||||||
|
expiry_date: expiryDate
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return res.json(encryptResponse(responseData));
|
return res.json(encryptResponse(responseData));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('激活错误:', error);
|
console.error('激活错误:', error);
|
||||||
return res.status(500).json(encryptResponse({
|
return res.status(500).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '服务器错误'
|
message: '服务器错误'
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
@@ -212,62 +170,62 @@ app.post('/verify', async (req, res) => {
|
|||||||
|
|
||||||
// Validate input
|
// Validate input
|
||||||
if (!license_key || !machine_code) {
|
if (!license_key || !machine_code) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证密钥和机器码是必需的'
|
message: '许可证密钥和机器码是必需的'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate license key format
|
// Validate license key format
|
||||||
try {
|
try {
|
||||||
decryptLicenseKey(license_key);
|
decryptLicenseKey(license_key);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '无效的许可证密钥'
|
message: '无效的许可证密钥'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find license
|
// Find license
|
||||||
const license = await License.findOne({ licenseKey: license_key });
|
const license = await License.findOne({ licenseKey: license_key });
|
||||||
|
|
||||||
if (!license) {
|
if (!license) {
|
||||||
return res.status(404).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证不存在'
|
message: '许可证不存在'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check machine code
|
// Check machine code
|
||||||
if (license.machineCode !== machine_code) {
|
if (license.machineCode !== machine_code) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '硬件信息不匹配'
|
message: '硬件信息不匹配'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if license is active
|
// Check if license is active
|
||||||
if (!license.isActive) {
|
if (!license.isActive) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证已被禁用'
|
message: '许可证已被禁用'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用中国时区检查过期时间
|
// 使用中国时区检查过期时间
|
||||||
if (getNowChinaTime().isAfter(license.expiryDate)) {
|
if (getNowChinaTime().isAfter(license.expiryDate)) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证已过期'
|
message: '许可证已过期'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查使用次数
|
// 检查使用次数
|
||||||
if (license.currentUsageCount >= license.maxUsageCount) {
|
if (license.currentUsageCount >= license.maxUsageCount) {
|
||||||
return res.status(400).json({
|
return res.status(200).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '许可证使用次数已达到上限'
|
message: '许可证使用次数已达到上限'
|
||||||
});
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新使用次数
|
// 更新使用次数
|
||||||
@@ -275,12 +233,14 @@ app.post('/verify', async (req, res) => {
|
|||||||
await license.save();
|
await license.save();
|
||||||
|
|
||||||
const responseData = {
|
const responseData = {
|
||||||
success: true,
|
code: 0,
|
||||||
message: '许可证有效',
|
data: {
|
||||||
expiry_date: formatChinaTime(license.expiryDate, 'YYYY-MM-DD'),
|
message: '许可证有效',
|
||||||
usage_count: {
|
expiry_date: formatChinaTime(license.expiryDate, 'YYYY-MM-DD'),
|
||||||
current: license.currentUsageCount,
|
usage_count: {
|
||||||
max: license.maxUsageCount
|
current: license.currentUsageCount,
|
||||||
|
max: license.maxUsageCount
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -288,7 +248,7 @@ app.post('/verify', async (req, res) => {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('验证错误:', error);
|
console.error('验证错误:', error);
|
||||||
return res.status(500).json(encryptResponse({
|
return res.status(500).json(encryptResponse({
|
||||||
success: false,
|
code: -1,
|
||||||
message: '服务器错误'
|
message: '服务器错误'
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
60
server/utils/encryption.js
Normal file
60
server/utils/encryption.js
Normal 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
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user