- 新增许可证服务器 (license-server/) - 新增 Agent 部署模块 (flnxi-agent/) - 添加许可证验证和激活页面 - 添加 Harbor 镜像同步脚本 - 更新素材系统支持许可证验证
136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import { signLicense, hashFingerprint, isValidLicenseKeyFormat } from '@/lib/crypto';
|
|
import { generateLicenseConfig, type HardwareFingerprint } from '@/lib/types';
|
|
import { z } from 'zod';
|
|
|
|
// 请求验证 schema
|
|
const activateSchema = z.object({
|
|
licenseKey: z.string(),
|
|
fingerprint: z.object({
|
|
machineId: z.string(),
|
|
hostname: z.string(),
|
|
macAddress: z.string(),
|
|
cpuId: z.string().optional(),
|
|
instanceId: z.string(),
|
|
}),
|
|
timestamp: z.number(),
|
|
});
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const parsed = activateSchema.safeParse(body);
|
|
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '请求参数无效' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { licenseKey, fingerprint, timestamp } = parsed.data;
|
|
|
|
// 验证时间戳(防止重放攻击,允许 5 分钟误差)
|
|
const now = Date.now();
|
|
if (Math.abs(now - timestamp) > 5 * 60 * 1000) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '请求已过期' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// 验证许可证密钥格式
|
|
if (!isValidLicenseKeyFormat(licenseKey)) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '许可证密钥格式无效' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// 查找许可证
|
|
const license = await prisma.license.findUnique({
|
|
where: { licenseKey },
|
|
});
|
|
|
|
if (!license) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '许可证密钥不存在' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// 检查许可证状态
|
|
if (license.status === 'disabled') {
|
|
return NextResponse.json(
|
|
{ success: false, error: '许可证已被禁用' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
if (license.status === 'expired' || (license.expiresAt && license.expiresAt < new Date())) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '许可证已过期' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// 检查是否已激活
|
|
const existingActivation = await prisma.activation.findFirst({
|
|
where: {
|
|
licenseId: license.id,
|
|
isActive: true,
|
|
},
|
|
});
|
|
|
|
if (existingActivation) {
|
|
// 检查是否是同一设备重新激活
|
|
const existingFingerprint = existingActivation.fingerprint as unknown as HardwareFingerprint;
|
|
if (existingFingerprint.instanceId !== fingerprint.instanceId) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '许可证已在其他设备上激活,请联系管理员重置' },
|
|
{ status: 409 }
|
|
);
|
|
}
|
|
// 同一设备,更新激活信息
|
|
await prisma.activation.update({
|
|
where: { id: existingActivation.id },
|
|
data: {
|
|
fingerprint: fingerprint as object,
|
|
lastHeartbeat: new Date(),
|
|
},
|
|
});
|
|
} else {
|
|
// 创建新激活记录
|
|
await prisma.activation.create({
|
|
data: {
|
|
licenseId: license.id,
|
|
instanceId: fingerprint.instanceId,
|
|
fingerprint: fingerprint as object,
|
|
lastHeartbeat: new Date(),
|
|
},
|
|
});
|
|
}
|
|
|
|
// 生成许可证配置
|
|
const licenseConfig = generateLicenseConfig(
|
|
license.licenseType,
|
|
license.expiresAt
|
|
);
|
|
|
|
// 签名许可证
|
|
const signedLicense = signLicense(licenseConfig);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
license: signedLicense,
|
|
});
|
|
} catch (error) {
|
|
console.error('激活失败:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: '服务器内部错误' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|