136 lines
4.0 KiB
TypeScript
136 lines
4.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getPublicProjectDb } from '@/lib/project-auth';
|
|
import { verifyLicenseRequestSignature } from '@/lib/request-signature';
|
|
import { buildSignedPremiumModuleManifestForGrants } from '@/lib/premium-module-manifest';
|
|
import { listAuthorizedModuleGrants } from '@/lib/module-grants';
|
|
import { generateLicenseConfig, type HardwareFingerprint } from '@/lib/types';
|
|
import {
|
|
calculateFingerprintMatchScore,
|
|
createPendingAlertOnce,
|
|
isSamePersistentInstance,
|
|
} from '@/lib/abuse-detection';
|
|
import { z } from 'zod';
|
|
|
|
const premiumModulesSchema = 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(),
|
|
});
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ projectCode: string }> }
|
|
) {
|
|
try {
|
|
const { projectCode } = await params;
|
|
const projectDbResult = await getPublicProjectDb(projectCode);
|
|
|
|
if (!projectDbResult.success) {
|
|
return projectDbResult.response;
|
|
}
|
|
|
|
const body = await request.json().catch(() => ({}));
|
|
const signatureResult = verifyLicenseRequestSignature('premium-modules', projectCode, body);
|
|
if (!signatureResult.valid) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '请求签名无效' },
|
|
{ status: signatureResult.response.status }
|
|
);
|
|
}
|
|
|
|
const parsed = premiumModulesSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '请求参数无效' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const { instanceId, fingerprint, timestamp } = parsed.data;
|
|
if (Math.abs(Date.now() - timestamp) > 5 * 60 * 1000) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '请求已过期' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const activation = await projectDbResult.prisma.activation.findFirst({
|
|
where: {
|
|
instanceId,
|
|
isActive: true,
|
|
},
|
|
include: {
|
|
license: true,
|
|
},
|
|
});
|
|
|
|
if (!activation) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '激活记录不存在' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const { license } = activation;
|
|
|
|
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 storedFingerprint = activation.fingerprint as unknown as HardwareFingerprint;
|
|
const matchScore = calculateFingerprintMatchScore(storedFingerprint, fingerprint);
|
|
if (!isSamePersistentInstance(storedFingerprint, fingerprint) && matchScore < 70) {
|
|
await createPendingAlertOnce(projectDbResult.prisma, license.id, 'premium_module_fingerprint_mismatch', {
|
|
instanceId,
|
|
matchScore,
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{ success: false, error: '硬件指纹不匹配,无法获取授权模块清单' },
|
|
{ status: 403 }
|
|
);
|
|
}
|
|
|
|
const config = generateLicenseConfig(license.licenseType, license.expiresAt);
|
|
const leaseExpiresAt = new Date(config.leaseExpiresAt || Date.now());
|
|
const grants = await listAuthorizedModuleGrants(projectDbResult.prisma, {
|
|
id: license.id,
|
|
licenseType: license.licenseType,
|
|
expiresAt: license.expiresAt,
|
|
});
|
|
const signedManifest = buildSignedPremiumModuleManifestForGrants(
|
|
license.licenseType,
|
|
leaseExpiresAt,
|
|
instanceId,
|
|
grants
|
|
);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
...signedManifest,
|
|
});
|
|
} catch (error) {
|
|
console.error('授权模块清单生成失败:', error);
|
|
return NextResponse.json(
|
|
{ success: false, error: '服务器内部错误' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|