- 新增许可证服务器 (license-server/) - 新增 Agent 部署模块 (flnxi-agent/) - 添加许可证验证和激活页面 - 添加 Harbor 镜像同步脚本 - 更新素材系统支持许可证验证
151 lines
3.6 KiB
TypeScript
151 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import { signLicense, hashFingerprint } from '@/lib/crypto';
|
|
import { generateLicenseConfig, type HardwareFingerprint } from '@/lib/types';
|
|
import { z } from 'zod';
|
|
|
|
// 请求验证 schema
|
|
const verifySchema = z.object({
|
|
instanceId: z.string(),
|
|
fingerprint: z.object({
|
|
machineId: z.string(),
|
|
hostname: z.string(),
|
|
macAddress: z.string(),
|
|
cpuId: z.string().optional(),
|
|
instanceId: z.string(),
|
|
}),
|
|
timestamp: z.number(),
|
|
});
|
|
|
|
// 计算指纹匹配分数
|
|
function calculateFingerprintMatchScore(
|
|
stored: HardwareFingerprint,
|
|
current: HardwareFingerprint
|
|
): number {
|
|
let score = 0;
|
|
let total = 0;
|
|
|
|
// machineId 权重 40%
|
|
if (stored.machineId === current.machineId) {
|
|
score += 40;
|
|
}
|
|
total += 40;
|
|
|
|
// hostname 权重 20%
|
|
if (stored.hostname === current.hostname) {
|
|
score += 20;
|
|
}
|
|
total += 20;
|
|
|
|
// macAddress 权重 30%
|
|
if (stored.macAddress === current.macAddress) {
|
|
score += 30;
|
|
}
|
|
total += 30;
|
|
|
|
// instanceId 权重 10%
|
|
if (stored.instanceId === current.instanceId) {
|
|
score += 10;
|
|
}
|
|
total += 10;
|
|
|
|
return (score / total) * 100;
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const parsed = verifySchema.safeParse(body);
|
|
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '请求参数无效' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { instanceId, fingerprint, timestamp } = parsed.data;
|
|
|
|
// 验证时间戳
|
|
const now = Date.now();
|
|
if (Math.abs(now - timestamp) > 5 * 60 * 1000) {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '请求已过期' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// 查找激活记录
|
|
const activation = await prisma.activation.findFirst({
|
|
where: {
|
|
instanceId,
|
|
isActive: true,
|
|
},
|
|
include: {
|
|
license: true,
|
|
},
|
|
});
|
|
|
|
if (!activation) {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '未找到激活记录' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const { license } = activation;
|
|
|
|
// 检查许可证状态
|
|
if (license.status === 'disabled') {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '许可证已被禁用' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
if (license.status === 'expired' || (license.expiresAt && license.expiresAt < new Date())) {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '许可证已过期' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// 验证硬件指纹匹配度
|
|
const storedFingerprint = activation.fingerprint as unknown as HardwareFingerprint;
|
|
const matchScore = calculateFingerprintMatchScore(storedFingerprint, fingerprint);
|
|
|
|
if (matchScore < 60) {
|
|
return NextResponse.json(
|
|
{ valid: false, error: '硬件指纹不匹配,请重新激活' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
// 更新最后验证时间
|
|
await prisma.activation.update({
|
|
where: { id: activation.id },
|
|
data: { lastHeartbeat: new Date() },
|
|
});
|
|
|
|
// 生成许可证配置
|
|
const licenseConfig = generateLicenseConfig(
|
|
license.licenseType,
|
|
license.expiresAt
|
|
);
|
|
|
|
// 签名许可证
|
|
const signedLicense = signLicense(licenseConfig);
|
|
|
|
return NextResponse.json({
|
|
valid: true,
|
|
license: signedLicense,
|
|
});
|
|
} catch (error) {
|
|
console.error('验证失败:', error);
|
|
return NextResponse.json(
|
|
{ valid: false, error: '服务器内部错误' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|