580 lines
16 KiB
JavaScript
580 lines
16 KiB
JavaScript
const { spawn } = require('child_process');
|
|
const crypto = require('crypto');
|
|
const fs = require('fs');
|
|
const http = require('http');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
|
|
const PORT = Number(process.env.PORT || 3000);
|
|
const TOKEN_SECRET = process.env.AUTH_TOKEN_SECRET || 'change-this-secret-before-production';
|
|
const TOKEN_DURATION_SECONDS = 60 * 60 * 8;
|
|
const BOT_SIBS_API_URL = process.env.BOT_SIBS_API_URL || 'http://127.0.0.1:8000';
|
|
const BOT_SIBS_TIMEOUT_MS = Number(process.env.BOT_SIBS_TIMEOUT_MS || 240000);
|
|
const PYTHON_BIN = process.env.PYTHON_BIN || 'python';
|
|
const CONVERTERS_DIR = process.env.CONVERTERS_DIR || path.join(__dirname, 'Metacase');
|
|
const CONVERTER_TIMEOUT_MS = Number(process.env.CONVERTER_TIMEOUT_MS || 600000);
|
|
const CONVERTER_MAX_UPLOAD_BYTES = Number(process.env.CONVERTER_MAX_UPLOAD_BYTES || 25 * 1024 * 1024);
|
|
const USERS_FILE = path.join(__dirname, 'server', 'users.json');
|
|
const DIST_DIR = path.join(__dirname, 'dist', 'form_metacase', 'browser');
|
|
const API_ONLY = process.argv.includes('--api-only');
|
|
|
|
const accessControlHeaders = {
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Bank-Key, X-File-Name',
|
|
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
'Access-Control-Allow-Origin': '*'
|
|
};
|
|
|
|
const mimeTypes = {
|
|
'.css': 'text/css',
|
|
'.html': 'text/html',
|
|
'.ico': 'image/x-icon',
|
|
'.jpg': 'image/jpeg',
|
|
'.js': 'text/javascript',
|
|
'.json': 'application/json',
|
|
'.map': 'application/json',
|
|
'.mp3': 'audio/mpeg',
|
|
'.mp4': 'video/mp4',
|
|
'.pdf': 'application/pdf',
|
|
'.png': 'image/png',
|
|
'.svg': 'image/svg+xml',
|
|
'.txt': 'text/plain',
|
|
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
};
|
|
|
|
const extractConverters = {
|
|
abanca: {
|
|
script: 'ABANCA.py',
|
|
outputName: 'ABanca-resultado.xlsx'
|
|
},
|
|
bic: {
|
|
script: 'BIC.py',
|
|
outputName: 'BIC-resultado.xlsx'
|
|
}
|
|
};
|
|
|
|
function readUsers() {
|
|
return JSON.parse(fs.readFileSync(USERS_FILE, 'utf8')).users;
|
|
}
|
|
|
|
function hashPassword(password, salt) {
|
|
return crypto.pbkdf2Sync(password, salt, 120000, 32, 'sha256').toString('hex');
|
|
}
|
|
|
|
function safeEqual(left, right) {
|
|
const leftBuffer = Buffer.from(left, 'hex');
|
|
const rightBuffer = Buffer.from(right, 'hex');
|
|
|
|
return leftBuffer.length === rightBuffer.length && crypto.timingSafeEqual(leftBuffer, rightBuffer);
|
|
}
|
|
|
|
function base64UrlEncode(value) {
|
|
return Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
}
|
|
|
|
function base64UrlDecode(value) {
|
|
return JSON.parse(Buffer.from(value, 'base64url').toString('utf8'));
|
|
}
|
|
|
|
function signToken(payload) {
|
|
const encodedPayload = base64UrlEncode(payload);
|
|
const signature = crypto.createHmac('sha256', TOKEN_SECRET).update(encodedPayload).digest('base64url');
|
|
|
|
return `${encodedPayload}.${signature}`;
|
|
}
|
|
|
|
function verifyToken(token) {
|
|
const [encodedPayload, signature] = String(token || '').split('.');
|
|
|
|
if (!encodedPayload || !signature) {
|
|
return null;
|
|
}
|
|
|
|
const expectedSignature = crypto
|
|
.createHmac('sha256', TOKEN_SECRET)
|
|
.update(encodedPayload)
|
|
.digest('base64url');
|
|
|
|
const signatureBuffer = Buffer.from(signature);
|
|
const expectedSignatureBuffer = Buffer.from(expectedSignature);
|
|
|
|
if (
|
|
signatureBuffer.length !== expectedSignatureBuffer.length ||
|
|
!crypto.timingSafeEqual(signatureBuffer, expectedSignatureBuffer)
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const payload = base64UrlDecode(encodedPayload);
|
|
|
|
if (!payload.expiresAt || Date.now() >= payload.expiresAt) {
|
|
return null;
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
function sendJson(response, statusCode, body) {
|
|
response.writeHead(statusCode, {
|
|
...accessControlHeaders,
|
|
'Content-Type': 'application/json'
|
|
});
|
|
response.end(JSON.stringify(body));
|
|
}
|
|
|
|
function sendBinary(response, statusCode, buffer, contentType, fileName) {
|
|
response.writeHead(statusCode, {
|
|
...accessControlHeaders,
|
|
'Content-Disposition': buildContentDisposition(fileName),
|
|
'Content-Length': buffer.length,
|
|
'Content-Type': contentType
|
|
});
|
|
response.end(buffer);
|
|
}
|
|
|
|
function readJsonBody(request, maxLength = 10000) {
|
|
return new Promise((resolve, reject) => {
|
|
let body = '';
|
|
|
|
request.on('data', (chunk) => {
|
|
body += chunk;
|
|
|
|
if (body.length > maxLength) {
|
|
request.destroy();
|
|
reject(new Error('Payload too large'));
|
|
}
|
|
});
|
|
|
|
request.on('end', () => {
|
|
try {
|
|
resolve(body ? JSON.parse(body) : {});
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function readBinaryBody(request, maxLength = CONVERTER_MAX_UPLOAD_BYTES) {
|
|
return new Promise((resolve, reject) => {
|
|
const chunks = [];
|
|
let totalLength = 0;
|
|
let settled = false;
|
|
|
|
request.on('data', (chunk) => {
|
|
totalLength += chunk.length;
|
|
|
|
if (totalLength > maxLength) {
|
|
settled = true;
|
|
request.destroy();
|
|
reject(new Error('Payload too large'));
|
|
return;
|
|
}
|
|
|
|
chunks.push(chunk);
|
|
});
|
|
|
|
request.on('end', () => {
|
|
if (!settled) {
|
|
settled = true;
|
|
resolve(Buffer.concat(chunks));
|
|
}
|
|
});
|
|
|
|
request.on('error', (error) => {
|
|
if (!settled) {
|
|
settled = true;
|
|
reject(error);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function sanitizeFileSegment(value, fallback = 'resultado') {
|
|
const sanitized = String(value || fallback)
|
|
.replace(/[^a-zA-Z0-9._-]+/g, '-')
|
|
.replace(/-+/g, '-')
|
|
.replace(/^-|-$/g, '');
|
|
|
|
return sanitized || fallback;
|
|
}
|
|
|
|
function buildContentDisposition(fileName) {
|
|
const baseName = path.basename(fileName);
|
|
const asciiName = sanitizeFileSegment(baseName, 'resultado.xlsx');
|
|
return `attachment; filename="${asciiName}"; filename*=UTF-8''${encodeURIComponent(baseName)}`;
|
|
}
|
|
|
|
function cleanupTemporaryDirectory(targetPath) {
|
|
if (!targetPath) {
|
|
return;
|
|
}
|
|
|
|
fs.rmSync(targetPath, { recursive: true, force: true });
|
|
}
|
|
|
|
function runPythonConverter(scriptPath, inputPath, outputPath) {
|
|
return new Promise((resolve, reject) => {
|
|
const child = spawn(PYTHON_BIN, [scriptPath, '--input', inputPath, '--output', outputPath], {
|
|
cwd: path.dirname(scriptPath),
|
|
stdio: ['ignore', 'pipe', 'pipe']
|
|
});
|
|
|
|
let stdout = '';
|
|
let stderr = '';
|
|
let settled = false;
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
if (!settled) {
|
|
settled = true;
|
|
child.kill();
|
|
reject(new Error(`O conversor excedeu o limite de ${CONVERTER_TIMEOUT_MS} ms.`));
|
|
}
|
|
}, CONVERTER_TIMEOUT_MS);
|
|
|
|
child.stdout.on('data', (chunk) => {
|
|
const text = chunk.toString();
|
|
stdout += text;
|
|
console.log('[PYTHON stdout]', text.trim());
|
|
});
|
|
|
|
child.stderr.on('data', (chunk) => {
|
|
const text = chunk.toString();
|
|
stderr += text;
|
|
console.error('[PYTHON stderr]', text.trim());
|
|
});
|
|
|
|
child.on('error', (error) => {
|
|
if (!settled) {
|
|
settled = true;
|
|
clearTimeout(timeoutId);
|
|
reject(error);
|
|
}
|
|
});
|
|
|
|
child.on('close', (code) => {
|
|
console.log(`[PYTHON] Processo terminou com código ${code}`);
|
|
if (!settled) {
|
|
settled = true;
|
|
clearTimeout(timeoutId);
|
|
|
|
if (code === 0) {
|
|
resolve({ stdout, stderr });
|
|
return;
|
|
}
|
|
|
|
reject(new Error(stderr.trim() || stdout.trim() || `O conversor terminou com codigo ${code}.`));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
async function forwardBotRequest(endpoint, body) {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), BOT_SIBS_TIMEOUT_MS);
|
|
|
|
try {
|
|
const response = await fetch(`${BOT_SIBS_API_URL}${endpoint}`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(body),
|
|
signal: controller.signal
|
|
});
|
|
|
|
const rawText = await response.text();
|
|
|
|
try {
|
|
return {
|
|
status: response.status,
|
|
body: rawText ? JSON.parse(rawText) : {}
|
|
};
|
|
} catch {
|
|
return {
|
|
status: response.status,
|
|
body: {
|
|
detail: rawText || 'Resposta invalida do backend do bot.'
|
|
}
|
|
};
|
|
}
|
|
} catch (error) {
|
|
if (error && error.name === 'AbortError') {
|
|
throw new Error('Timeout ao contactar o backend do bot.');
|
|
}
|
|
|
|
throw error;
|
|
} finally {
|
|
clearTimeout(timeoutId);
|
|
}
|
|
}
|
|
|
|
async function handleExtractConverter(request, response) {
|
|
console.log('[CONVERSOR] Pedido de conversão recebido.');
|
|
|
|
const bankKey = String(request.headers['x-bank-key'] || '').trim().toLowerCase();
|
|
const converter = extractConverters[bankKey];
|
|
|
|
console.log(`[CONVERSOR] Banco recebido: ${bankKey}`);
|
|
|
|
if (!converter) {
|
|
console.error(`[CONVERSOR] ERRO: não existe conversor para "${bankKey}".`);
|
|
sendJson(response, 404, { message: 'Conversor nao encontrado para o banco indicado.' });
|
|
return true;
|
|
}
|
|
|
|
const rawFileName = String(request.headers['x-file-name'] || 'extrato.pdf');
|
|
let originalFileName = 'extrato.pdf';
|
|
|
|
try {
|
|
originalFileName = decodeURIComponent(rawFileName);
|
|
} catch {
|
|
originalFileName = rawFileName;
|
|
}
|
|
|
|
console.log(`[CONVERSOR] Ficheiro recebido: ${originalFileName}`);
|
|
|
|
if (!originalFileName.toLowerCase().endsWith('.pdf')) {
|
|
console.error('[CONVERSOR] ERRO: ficheiro não é PDF.');
|
|
sendJson(response, 400, { message: 'O ficheiro enviado tem de ser um PDF.' });
|
|
return true;
|
|
}
|
|
|
|
const scriptPath = path.join(CONVERTERS_DIR, converter.script);
|
|
|
|
console.log(`[CONVERSOR] Script Python: ${scriptPath}`);
|
|
|
|
if (!fs.existsSync(scriptPath)) {
|
|
console.error(`[CONVERSOR] ERRO: script não encontrado: ${scriptPath}`);
|
|
sendJson(response, 500, { message: 'O script do conversor nao foi encontrado no servidor.' });
|
|
return true;
|
|
}
|
|
|
|
let temporaryDirectory = '';
|
|
|
|
try {
|
|
console.log('[CONVERSOR] A receber conteúdo do PDF...');
|
|
|
|
const inputBuffer = await readBinaryBody(request);
|
|
|
|
console.log(`[CONVERSOR] PDF recebido: ${inputBuffer.length} bytes`);
|
|
|
|
if (!inputBuffer.length) {
|
|
console.error('[CONVERSOR] ERRO: PDF vazio.');
|
|
sendJson(response, 400, { message: 'Nenhum ficheiro foi enviado.' });
|
|
return true;
|
|
}
|
|
|
|
temporaryDirectory = fs.mkdtempSync(
|
|
path.join(os.tmpdir(), 'metacase-converter-')
|
|
);
|
|
|
|
console.log(`[CONVERSOR] Pasta temporária: ${temporaryDirectory}`);
|
|
|
|
const safePdfName = sanitizeFileSegment(
|
|
path.basename(originalFileName),
|
|
'extrato.pdf'
|
|
);
|
|
|
|
const inputPath = path.join(
|
|
temporaryDirectory,
|
|
safePdfName.toLowerCase().endsWith('.pdf')
|
|
? safePdfName
|
|
: `${safePdfName}.pdf`
|
|
);
|
|
|
|
const outputPath = path.join(
|
|
temporaryDirectory,
|
|
converter.outputName
|
|
);
|
|
|
|
fs.writeFileSync(inputPath, inputBuffer);
|
|
|
|
console.log(`[CONVERSOR] PDF guardado em: ${inputPath}`);
|
|
console.log('[CONVERSOR] A executar Python...');
|
|
|
|
await runPythonConverter(
|
|
scriptPath,
|
|
inputPath,
|
|
outputPath
|
|
);
|
|
|
|
console.log('[CONVERSOR] Python terminou.');
|
|
|
|
if (!fs.existsSync(outputPath)) {
|
|
console.error('[CONVERSOR] ERRO: Python terminou mas não criou XLSX.');
|
|
throw new Error('O conversor terminou sem gerar o ficheiro Excel.');
|
|
}
|
|
|
|
const outputBuffer = fs.readFileSync(outputPath);
|
|
|
|
console.log(
|
|
`[CONVERSOR] Excel criado: ${converter.outputName} (${outputBuffer.length} bytes)`
|
|
);
|
|
|
|
sendBinary(
|
|
response,
|
|
200,
|
|
outputBuffer,
|
|
mimeTypes['.xlsx'],
|
|
converter.outputName
|
|
);
|
|
|
|
console.log('[CONVERSOR] Conversão concluída com sucesso.');
|
|
|
|
} catch (error) {
|
|
const message =
|
|
error instanceof Error
|
|
? error.message
|
|
: 'Erro desconhecido.';
|
|
|
|
console.error('[CONVERSOR] ERRO:', message);
|
|
|
|
sendJson(response, 500, {
|
|
message: 'Falha ao executar o conversor.',
|
|
detail: message
|
|
});
|
|
|
|
} finally {
|
|
cleanupTemporaryDirectory(temporaryDirectory);
|
|
|
|
if (temporaryDirectory) {
|
|
console.log('[CONVERSOR] Pasta temporária eliminada.');
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
async function handleApi(request, response) {
|
|
if (request.method === 'OPTIONS') {
|
|
sendJson(response, 204, {});
|
|
return true;
|
|
}
|
|
|
|
if (request.url === '/api/login' && request.method === 'POST') {
|
|
try {
|
|
const body = await readJsonBody(request);
|
|
const email = String(body.email || '').trim().toLowerCase();
|
|
const password = String(body.password || '');
|
|
const user = readUsers().find((item) => item.email.toLowerCase() === email && item.active);
|
|
|
|
if (!user || !safeEqual(hashPassword(password, user.passwordSalt), user.passwordHash)) {
|
|
sendJson(response, 401, { message: 'Credenciais invalidas.' });
|
|
return true;
|
|
}
|
|
|
|
const expiresAt = Date.now() + TOKEN_DURATION_SECONDS * 1000;
|
|
const token = signToken({
|
|
email: user.email,
|
|
name: user.name,
|
|
expiresAt
|
|
});
|
|
|
|
sendJson(response, 200, {
|
|
token,
|
|
expiresAt,
|
|
user: {
|
|
email: user.email,
|
|
name: user.name
|
|
}
|
|
});
|
|
} catch {
|
|
sendJson(response, 400, { message: 'Pedido invalido.' });
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
if (request.url === '/api/vitoria-ai/perguntar' && request.method === 'POST') {
|
|
try {
|
|
const body = await readJsonBody(request, 200000);
|
|
const proxied = await forwardBotRequest('/perguntar', body);
|
|
sendJson(response, proxied.status, proxied.body);
|
|
} catch (error) {
|
|
sendJson(response, 502, {
|
|
message: 'Nao foi possivel contactar o backend do bot.',
|
|
detail: error instanceof Error ? error.message : 'Erro desconhecido.'
|
|
});
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
if (request.url === '/api/conversor-extratos/executar' && request.method === 'POST') {
|
|
return handleExtractConverter(request, response);
|
|
}
|
|
|
|
if (request.url === '/api/session' && request.method === 'GET') {
|
|
const authorization = request.headers.authorization || '';
|
|
const token = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
|
|
const payload = verifyToken(token);
|
|
|
|
if (!payload) {
|
|
sendJson(response, 401, { message: 'Sessao invalida.' });
|
|
return true;
|
|
}
|
|
|
|
sendJson(response, 200, {
|
|
user: {
|
|
email: payload.email,
|
|
name: payload.name
|
|
},
|
|
expiresAt: payload.expiresAt
|
|
});
|
|
return true;
|
|
}
|
|
|
|
if (request.url.startsWith('/api/')) {
|
|
sendJson(response, 404, { message: 'Endpoint nao encontrado.' });
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function serveStatic(request, response) {
|
|
const requestPath = decodeURIComponent(new URL(request.url, `http://${request.headers.host}`).pathname);
|
|
const safePath = path.normalize(requestPath).replace(/^(\.\.[/\\])+/, '');
|
|
const filePath = path.join(DIST_DIR, safePath === '/' ? 'index.html' : safePath);
|
|
|
|
fs.readFile(filePath, (error, content) => {
|
|
if (!error) {
|
|
const extension = path.extname(filePath).toLowerCase();
|
|
response.writeHead(200, { 'Content-Type': mimeTypes[extension] || 'application/octet-stream' });
|
|
response.end(content);
|
|
return;
|
|
}
|
|
|
|
fs.readFile(path.join(DIST_DIR, 'index.html'), (fallbackError, fallbackContent) => {
|
|
if (fallbackError) {
|
|
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
response.end('Build not found. Run npm run build first.');
|
|
return;
|
|
}
|
|
|
|
response.writeHead(200, { 'Content-Type': 'text/html' });
|
|
response.end(fallbackContent);
|
|
});
|
|
});
|
|
}
|
|
|
|
const server = http.createServer(async (request, response) => {
|
|
const handledByApi = await handleApi(request, response);
|
|
|
|
if (handledByApi) {
|
|
return;
|
|
}
|
|
|
|
if (API_ONLY) {
|
|
sendJson(response, 404, { message: 'API only server.' });
|
|
return;
|
|
}
|
|
|
|
serveStatic(request, response);
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
const mode = API_ONLY ? 'API' : 'app';
|
|
console.log(`Metacase ${mode} server running on http://127.0.0.1:${PORT}`);
|
|
});
|