File size: 5,252 Bytes
6b825ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
// @ts-check
import fsPromises from 'fs/promises';
import path from 'path';
import { Request, Response } from 'express';
interface LoginClientRequestBody {
user: string;
pass: string;
}
function isLoginClientRequestBody(body: any): body is LoginClientRequestBody {
return (body && typeof body === 'object' &&
typeof body.user === 'string' && body.user.length > 0 &&
typeof body.pass === 'string' && body.pass.length > 0);
}
interface LoginRouteParams {
req: Request<any, any, unknown>;
res: Response;
}
interface ConfigData {
user: string;
pass: string;
[key: string]: any;
}
interface PanelExpressRouteModule {
method: 'post';
path: string;
install: (params: LoginRouteParams) => Promise<void>;
}
export const modules: PanelExpressRouteModule[] = [
{
method: 'post',
path: '/panel',
install: async ({ req, res }: LoginRouteParams): Promise<void> => {
const requestBody: unknown = req.body;
// DEBUG: Log the raw request body
console.log('[Panel Login] Raw request body:', JSON.stringify(requestBody));
if (!isLoginClientRequestBody(requestBody)) {
res.status(400).json({
message: 'Invalid request payload. "user" and "pass" are required and must be non-empty strings.',
status: 'error',
});
return;
}
const { user: clientUser, pass: clientPass } = requestBody;
// DEBUG: Log received client credentials (use quotes to see whitespace)
console.log(`[Panel Login] Received from client - User: "${clientUser}", Pass: "${clientPass}"`);
const relativeConfigPath = '../../config.json';
let configPath: string;
if (typeof __dirname !== 'undefined') {
configPath = path.resolve(__dirname, relativeConfigPath);
} else {
console.warn(
`[Panel Login] Warning: __dirname is not defined (standard in ES Modules). ` +
`Resolving config path "${relativeConfigPath}" relative to current working directory: ${process.cwd()}. ` +
`For robust file-relative paths in ES modules, the standard approach uses import.meta.url.`
);
configPath = path.resolve(process.cwd(), relativeConfigPath);
}
// DEBUG: Log the resolved config path
console.log(`[Panel Login] Attempting to load config from: "${configPath}"`);
try {
const configFileContent: string = await fsPromises.readFile(configPath, 'utf-8');
const config: ConfigData = JSON.parse(configFileContent);
// DEBUG: Log credentials loaded from config.json (use quotes to see whitespace)
if (config.user && config.pass) {
console.log(`[Panel Login] Loaded from config.json - User: "${config.user}", Pass: "${config.pass}"`);
} else {
console.log('[Panel Login] Loaded from config.json - "user" or "pass" field is missing or not a string.');
}
if (typeof config.user !== 'string' || typeof config.pass !== 'string') {
console.error(`[Panel Login] Invalid config.json structure at "${configPath}". "user" and "pass" must be strings.`);
res.status(500).json({
message: 'Server configuration error: Invalid config file structure.',
status: 'error',
});
return;
}
if (clientUser === config.user && clientPass === config.pass) {
console.log('[Panel Login] Credentials MATCHED!'); // DEBUG
res.status(200).json({
message: 'Login successful!',
status: 'success',
});
return;
} else {
console.log('[Panel Login] Credentials DID NOT MATCH.'); // DEBUG
console.log(`Comparison details: Client User ("${clientUser}") === Config User ("${config.user}") -> ${clientUser === config.user}`);
console.log(`Comparison details: Client Pass ("${clientPass}") === Config Pass ("${config.pass}") -> ${clientPass === config.pass}`);
res.status(401).json({
message: 'Invalid username or password.',
status: 'failed',
});
return;
}
} catch (error: unknown) {
console.error(`[Panel Login] Error during login process for config path "${configPath}":`, error instanceof Error ? error.message : String(error));
// ... (rest of your error handling) ...
if (error instanceof SyntaxError) {
res.status(500).json({
message: `Server configuration error: Malformed config file at "${configPath}". Please ensure it is valid JSON.`,
status: 'error',
});
return;
}
if (error instanceof Error && 'code' in error) {
const errnoError = error as NodeJS.ErrnoException;
if (errnoError.code === 'ENOENT') {
res.status(500).json({
message: `Server configuration error: Config file not found at "${configPath}". Please ensure the path is correct.`,
status: 'error',
});
return;
}
}
res.status(500).json({
message: 'An internal server error occurred during login.',
status: 'error',
});
return;
}
},
},
];
|