fix windows/linux get system uuid error

This commit is contained in:
musistudio
2025-08-12 21:41:42 +08:00
parent 075ec76ec1
commit cce1625534
7 changed files with 43 additions and 216 deletions

View File

@@ -1,40 +1,29 @@
import { FastifyRequest, FastifyReply } from "fastify";
import { getTempAPIKey } from "../utils/systemUUID";
export const apiKeyAuth =
(config: any) =>
async (req: FastifyRequest, reply: FastifyReply, done: () => void) => {
// Check for temp API key in query parameters or headers
let tempApiKey = null;
if (req.query && (req.query as any).tempApiKey) {
tempApiKey = (req.query as any).tempApiKey;
} else if (req.headers['x-temp-api-key']) {
tempApiKey = req.headers['x-temp-api-key'] as string;
}
// If temp API key is provided, validate it
if (tempApiKey) {
try {
const expectedTempKey = await getTempAPIKey();
// If temp key matches, grant temporary full access
if (tempApiKey === expectedTempKey) {
(req as any).accessLevel = "full";
(req as any).isTempAccess = true;
return done();
}
} catch (error) {
// If there's an error generating temp key, continue with normal auth
console.warn("Failed to verify temporary API key:", error);
}
}
// Public endpoints that don't require authentication
if (["/", "/health"].includes(req.url) || req.url.startsWith("/ui")) {
return done();
}
const apiKey = config.APIKEY;
if (!apiKey) {
// If no API key is set, enable CORS for local
const allowedOrigins = [
`http://127.0.0.1:${config.PORT || 3456}`,
`http://localhost:${config.PORT || 3456}`,
];
if (req.headers.origin && allowedOrigins.includes(req.headers.origin)) {
reply.status(403).send("CORS not allowed for this origin");
return;
} else {
reply.header('Access-Control-Allow-Origin', `http://127.0.0.1:${config.PORT || 3456}`);
reply.header('Access-Control-Allow-Origin', `http://localhost:${config.PORT || 3456}`);
}
return done();
}
const isConfigEndpoint = req.url.startsWith("/api/config");
const isRestartEndpoint = req.url === "/api/restart";
@@ -42,56 +31,47 @@ export const apiKeyAuth =
if (isConfigEndpoint || isRestartEndpoint) {
// Attach access level to request for later use
(req as any).accessLevel = "restricted";
// If no API key is set in config, allow restricted access
if (!apiKey) {
(req as any).accessLevel = "restricted";
return done();
}
// Check for temporary access via query parameter (for UI)
if ((req as any).isTempAccess) {
return done();
}
// If API key is set, check authentication
const authHeaderValue = req.headers.authorization || req.headers["x-api-key"];
const authKey: string = Array.isArray(authHeaderValue) ? authHeaderValue[0] : authHeaderValue || "";
const authHeaderValue =
req.headers.authorization || req.headers["x-api-key"];
const authKey: string = Array.isArray(authHeaderValue)
? authHeaderValue[0]
: authHeaderValue || "";
if (!authKey) {
(req as any).accessLevel = "restricted";
return done();
}
let token = "";
if (authKey.startsWith("Bearer")) {
token = authKey.split(" ")[1];
} else {
token = authKey;
}
if (token !== apiKey) {
(req as any).accessLevel = "restricted";
return done();
}
// Full access for authenticated users
(req as any).accessLevel = "full";
return done();
}
// For other non-config endpoints, use existing logic
if (!apiKey) {
return done();
}
// Check for temporary access via query parameter (for UI)
if ((req as any).isTempAccess) {
return done();
}
const authHeaderValue = req.headers.authorization || req.headers["x-api-key"];
const authKey: string = Array.isArray(authHeaderValue) ? authHeaderValue[0] : authHeaderValue || "";
const authHeaderValue =
req.headers.authorization || req.headers["x-api-key"];
const authKey: string = Array.isArray(authHeaderValue)
? authHeaderValue[0]
: authHeaderValue || "";
if (!authKey) {
reply.status(401).send("APIKEY is missing");
return;
@@ -102,7 +82,7 @@ export const apiKeyAuth =
} else {
token = authKey;
}
if (token !== apiKey) {
reply.status(401).send("Invalid API key");
return;