feat: 许可证

This commit is contained in:
cheng zhen
2024-12-30 22:42:20 +08:00
parent 72a37b9c2b
commit 407d397fda
4 changed files with 40 additions and 2 deletions

View File

@@ -1,3 +1,4 @@
MONGODB_URI=mongodb://mongo_3y6JyM:mongo_iNySJ5@119.8.35.41:27017/license-manager?authSource=admin&retryWrites=true&w=majority
PORT=3000
ENCRYPTION_KEY=f1e2d3c4b5a6978899aabbccddeeff00112233445566778899aabbccddeeff00
MAX_USAGE_COUNT=10

2
server/README.md Normal file
View File

@@ -0,0 +1,2 @@
docker build --platform linux/amd64 -t ccz2/cursor-auth-server:latest .
docker push ccz2/cursor-auth-server:latest

View File

@@ -22,6 +22,15 @@ const licenseSchema = new mongoose.Schema({
isActive: {
type: Boolean,
default: true
},
maxUsageCount: {
type: Number,
required: true,
default: process.env.MAX_USAGE_COUNT || 10 // 默认允许使用100次
},
currentUsageCount: {
type: Number,
default: 0
}
});

View File

@@ -76,7 +76,7 @@ mongoose.connection.on('disconnected', () => {
});
// 定义一个复杂的路径,可以放在环境变量中
const GENERATE_PATH = process.env.GENERATE_PATH || crypto.randomBytes(16).toString('hex');
const GENERATE_PATH = process.env.GENERATE_PATH || 'xx-zz-yy-dd';
// 在应用启动时输出生成路径(仅在控制台显示一次)
console.log('License generation path:', GENERATE_PATH);
@@ -156,6 +156,16 @@ app.post('/activate', async (req, res) => {
// 创建新的许可证并标记许可证密钥为已使用
const expiryDate = moment().add(1, 'month').toDate();
await License.create([{
licenseKey: license_key,
machineCode: machine_code,
activationDate: activation_date || new Date(),
expiryDate: expiryDate,
isActive: true,
maxUsageCount: process.env.MAX_USAGE_COUNT || 10, // 如果未指定默认为100次
currentUsageCount: 0
}]);
// 更新许可证密钥状态为已使用
@@ -233,10 +243,26 @@ app.post('/verify', async (req, res) => {
});
}
// 检查使用次数
if (license.currentUsageCount >= license.maxUsageCount) {
return res.status(400).json({
success: false,
message: '许可证使用次数已达到上限'
});
}
// 更新使用次数
license.currentUsageCount += 1;
await license.save();
return res.json({
success: true,
message: '许可证有效',
expiry_date: license.expiryDate.toISOString().split('T')[0]
expiry_date: license.expiryDate.toISOString().split('T')[0],
usage_count: {
current: license.currentUsageCount,
max: license.maxUsageCount
}
});
} catch (error) {
console.error('验证错误:', error);