2024-12-30 13:39:24 +08:00
|
|
|
|
require('dotenv').config();
|
|
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const mongoose = require('mongoose');
|
|
|
|
|
|
const cors = require('cors');
|
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
|
const License = require('./models/License');
|
2024-12-30 21:45:46 +08:00
|
|
|
|
const LicenseKey = require('./models/LicenseKey');
|
2024-12-31 09:27:15 +08:00
|
|
|
|
const { formatChinaTime, getNowChinaTime, getNowChinaTimeString } = require('./utils/date');
|
2024-12-30 13:39:24 +08:00
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
// Encryption functions
|
2024-12-30 21:45:46 +08:00
|
|
|
|
function getIV() {
|
|
|
|
|
|
// 如果需要固定 IV(不推荐),可以从环境变量获取
|
|
|
|
|
|
if (process.env.ENCRYPTION_IV) {
|
|
|
|
|
|
return Buffer.from(process.env.ENCRYPTION_IV, 'hex');
|
|
|
|
|
|
}
|
|
|
|
|
|
// 否则生成随机 IV(更安全,但需要存储)
|
|
|
|
|
|
return crypto.randomBytes(16);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
function encryptLicenseKey(text) {
|
2024-12-30 21:45:46 +08:00
|
|
|
|
const key = Buffer.from(process.env.ENCRYPTION_KEY, 'hex');
|
|
|
|
|
|
const iv = getIV();
|
|
|
|
|
|
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
|
2024-12-30 13:42:18 +08:00
|
|
|
|
let encrypted = cipher.update(text, 'utf8', 'hex');
|
|
|
|
|
|
encrypted += cipher.final('hex');
|
2024-12-30 21:45:46 +08:00
|
|
|
|
// 将 IV 附加到加密文本中,以便解密时使用
|
|
|
|
|
|
return iv.toString('hex') + ':' + encrypted;
|
2024-12-30 13:42:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function decryptLicenseKey(encrypted) {
|
2024-12-30 21:45:46 +08:00
|
|
|
|
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');
|
2024-12-30 13:42:18 +08:00
|
|
|
|
decrypted += decipher.final('utf8');
|
|
|
|
|
|
return decrypted;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function generateLicenseKey() {
|
|
|
|
|
|
const randomBytes = crypto.randomBytes(16);
|
|
|
|
|
|
const timestamp = Date.now().toString();
|
|
|
|
|
|
const combined = randomBytes.toString('hex') + timestamp;
|
2024-12-30 21:45:46 +08:00
|
|
|
|
const encrypted = encryptLicenseKey(combined);
|
|
|
|
|
|
// 由于加密后的字符串现在包含 IV,我们需要使用完整的字符串
|
|
|
|
|
|
return encrypted;
|
2024-12-30 13:42:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 13:39:24 +08:00
|
|
|
|
// Connect to MongoDB
|
2024-12-30 21:45:46 +08:00
|
|
|
|
mongoose.connect(process.env.MONGODB_URI, {
|
|
|
|
|
|
useNewUrlParser: true,
|
|
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
|
|
serverSelectionTimeoutMS: 5000, // 超时时间
|
|
|
|
|
|
socketTimeoutMS: 45000, // Socket 超时
|
|
|
|
|
|
family: 4, // 强制使用 IPv4
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(() => console.log('Connected to MongoDB'))
|
|
|
|
|
|
.catch(err => {
|
|
|
|
|
|
console.error('MongoDB connection error:', err);
|
|
|
|
|
|
process.exit(1); // 如果连接失败,终止程序
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 添加连接错误处理
|
|
|
|
|
|
mongoose.connection.on('error', err => {
|
|
|
|
|
|
console.error('MongoDB connection error:', err);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
mongoose.connection.on('disconnected', () => {
|
|
|
|
|
|
console.log('MongoDB disconnected');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个复杂的路径,可以放在环境变量中
|
2024-12-30 22:42:20 +08:00
|
|
|
|
const GENERATE_PATH = process.env.GENERATE_PATH || 'xx-zz-yy-dd';
|
2024-12-30 21:45:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 在应用启动时输出生成路径(仅在控制台显示一次)
|
|
|
|
|
|
console.log('License generation path:', GENERATE_PATH);
|
2024-12-30 13:39:24 +08:00
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
// Generate license key endpoint
|
2024-12-30 21:45:46 +08:00
|
|
|
|
app.post(`/${GENERATE_PATH}`, async (req, res) => {
|
2024-12-30 13:42:18 +08:00
|
|
|
|
try {
|
|
|
|
|
|
const licenseKey = generateLicenseKey();
|
2024-12-30 21:45:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 保存生成的许可证记录
|
|
|
|
|
|
await LicenseKey.create({
|
|
|
|
|
|
licenseKey: licenseKey,
|
|
|
|
|
|
isUsed: false
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
return res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
license_key: licenseKey
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('生成许可证错误:', error);
|
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '服务器错误'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-12-30 13:39:24 +08:00
|
|
|
|
// Activation endpoint
|
|
|
|
|
|
app.post('/activate', async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { license_key, machine_code, activation_date } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Validate input
|
|
|
|
|
|
if (!license_key || !machine_code) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证密钥和机器码是必需的'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
// Validate license key format
|
|
|
|
|
|
try {
|
|
|
|
|
|
decryptLicenseKey(license_key);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '无效的许可证密钥'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 21:45:46 +08:00
|
|
|
|
// 检查许可证是否存在于生成记录中
|
|
|
|
|
|
const licenseKeyRecord = await LicenseKey.findOne({ licenseKey: license_key });
|
|
|
|
|
|
if (!licenseKeyRecord) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '无效的许可证密钥'
|
|
|
|
|
|
});
|
2024-12-30 13:39:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 21:45:46 +08:00
|
|
|
|
// 检查许可证是否已被使用
|
|
|
|
|
|
if (licenseKeyRecord.isUsed) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '此许可证密钥已被使用,不能重复激活'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-12-30 13:39:24 +08:00
|
|
|
|
|
2024-12-30 21:45:46 +08:00
|
|
|
|
// 检查许可证激活状态
|
|
|
|
|
|
const existingLicense = await License.findOne({ licenseKey: license_key });
|
2024-12-30 13:39:24 +08:00
|
|
|
|
if (existingLicense) {
|
2024-12-30 21:45:46 +08:00
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '此许可证已被激活,不能重复使用'
|
|
|
|
|
|
});
|
2024-12-30 13:39:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-31 09:27:15 +08:00
|
|
|
|
// 更新过期时间计算,使用中国时区
|
|
|
|
|
|
const expiryDate = formatChinaTime(getNowChinaTime().add(1, 'month'), 'YYYY-MM-DD');
|
2024-12-30 22:42:20 +08:00
|
|
|
|
|
|
|
|
|
|
await License.create([{
|
|
|
|
|
|
licenseKey: license_key,
|
|
|
|
|
|
machineCode: machine_code,
|
2024-12-31 09:27:15 +08:00
|
|
|
|
activationDate: activation_date ? activation_date : getNowChinaTimeString(),
|
2024-12-30 22:42:20 +08:00
|
|
|
|
expiryDate: expiryDate,
|
|
|
|
|
|
isActive: true,
|
2024-12-31 09:27:15 +08:00
|
|
|
|
maxUsageCount: process.env.MAX_USAGE_COUNT || 10,
|
2024-12-30 22:42:20 +08:00
|
|
|
|
currentUsageCount: 0
|
|
|
|
|
|
}]);
|
2024-12-30 21:45:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新许可证密钥状态为已使用
|
|
|
|
|
|
licenseKeyRecord.isUsed = true;
|
|
|
|
|
|
await licenseKeyRecord.save();
|
|
|
|
|
|
|
2024-12-30 13:39:24 +08:00
|
|
|
|
return res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '激活成功',
|
2024-12-31 09:27:15 +08:00
|
|
|
|
expiry_date: expiryDate
|
2024-12-30 13:39:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('激活错误:', error);
|
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '服务器错误'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Verification endpoint
|
|
|
|
|
|
app.post('/verify', async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { license_key, machine_code } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Validate input
|
|
|
|
|
|
if (!license_key || !machine_code) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证密钥和机器码是必需的'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 13:42:18 +08:00
|
|
|
|
// Validate license key format
|
|
|
|
|
|
try {
|
|
|
|
|
|
decryptLicenseKey(license_key);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '无效的许可证密钥'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 13:39:24 +08:00
|
|
|
|
// Find license
|
|
|
|
|
|
const license = await License.findOne({ licenseKey: license_key });
|
|
|
|
|
|
|
|
|
|
|
|
if (!license) {
|
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证不存在'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check machine code
|
|
|
|
|
|
if (license.machineCode !== machine_code) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '硬件信息不匹配'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if license is active
|
|
|
|
|
|
if (!license.isActive) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证已被禁用'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-31 09:27:15 +08:00
|
|
|
|
// 使用中国时区检查过期时间
|
|
|
|
|
|
if (getNowChinaTime().isAfter(license.expiryDate)) {
|
2024-12-30 13:39:24 +08:00
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证已过期'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-30 22:42:20 +08:00
|
|
|
|
// 检查使用次数
|
|
|
|
|
|
if (license.currentUsageCount >= license.maxUsageCount) {
|
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '许可证使用次数已达到上限'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新使用次数
|
|
|
|
|
|
license.currentUsageCount += 1;
|
|
|
|
|
|
await license.save();
|
|
|
|
|
|
|
2024-12-30 13:39:24 +08:00
|
|
|
|
return res.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
message: '许可证有效',
|
2024-12-31 09:27:15 +08:00
|
|
|
|
expiry_date: formatChinaTime(license.expiryDate, 'YYYY-MM-DD'),
|
2024-12-30 22:42:20 +08:00
|
|
|
|
usage_count: {
|
|
|
|
|
|
current: license.currentUsageCount,
|
|
|
|
|
|
max: license.maxUsageCount
|
|
|
|
|
|
}
|
2024-12-30 13:39:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('验证错误:', error);
|
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: '服务器错误'
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
|
|
app.listen(PORT, () => {
|
|
|
|
|
|
console.log(`Server is running on port ${PORT}`);
|
|
|
|
|
|
});
|