export view=2 HTML table

This commit is contained in:
2026-06-22 20:22:37 +04:00
parent 73afce35bd
commit deeb54af1c
2 changed files with 41 additions and 7 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "ipwhitelist", "name": "ipwhitelist",
"version": "0.5.178", "version": "0.5.179",
"description": "IP WhiteList microservice for cloud provider", "description": "IP WhiteList microservice for cloud provider",
"main": "server.js", "main": "server.js",
"scripts": { "scripts": {
+39 -5
View File
@@ -13,8 +13,9 @@
// //
// ПАРАМЕТРЫ: // ПАРАМЕТРЫ:
// ?companyId=X — фильтр по компании (внутренний id) // ?companyId=X — фильтр по компании (внутренний id)
// ?view=1 — показать в браузере (иначе — скачивание) // ?view=1 — text/plain inline (копирование)
// ?filename=X — имя файла (по умолчанию white-list.txt) // ?view=2 — HTML таблица с выровненными IP
// ?filename=X — имя файла (скачивание, когда нет view)
// ═══════════════════════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════════════════════
const express = require('express'); const express = require('express');
@@ -29,13 +30,26 @@ function createExportRouter() {
const companyId = req.query.companyId ? parseInt(req.query.companyId) : null; const companyId = req.query.companyId ? parseInt(req.query.companyId) : null;
const cidrs = await q.getExportCIDRs(companyId); const cidrs = await q.getExportCIDRs(companyId);
const aggregated = aggregateCIDRs(cidrs); const aggregated = aggregateCIDRs(cidrs);
// ?view=2 — HTML таблица с выровненными IP
if (req.query.view === '2') {
res.set('Content-Type', 'text/html; charset=utf-8');
return res.send(renderTable(aggregated));
}
// ?view=1 — text/plain inline
if (req.query.view === '1') {
const text = aggregated.join('\n') + (aggregated.length ? '\n' : ''); const text = aggregated.join('\n') + (aggregated.length ? '\n' : '');
res.set('Content-Type', 'text/plain; charset=utf-8'); res.set('Content-Type', 'text/plain; charset=utf-8');
res.set('Content-Disposition', 'inline');
return res.send(text);
}
// Скачивание (по умолчанию)
const text = aggregated.join('\n') + (aggregated.length ? '\n' : '');
res.set('Content-Type', 'text/plain; charset=utf-8');
const fname = (req.query.filename || 'white-list.txt').replace(/[^\w\-_. ]/g, '_'); const fname = (req.query.filename || 'white-list.txt').replace(/[^\w\-_. ]/g, '_');
const disp = req.query.view === '1' ? 'inline' : 'attachment'; res.set('Content-Disposition', 'attachment; filename="' + fname + '"');
res.set('Content-Disposition', disp + '; filename="' + fname + '"');
res.send(text); res.send(text);
} catch (e) { } catch (e) {
console.error('[v2:export] Error:', e); console.error('[v2:export] Error:', e);
@@ -46,4 +60,24 @@ function createExportRouter() {
return router; return router;
} }
function padIP(cidr) {
const [ip, mask] = cidr.split('/');
const octets = ip.split('.').map(o => o.padStart(3, ' '));
return octets.join('.') + '/' + mask;
}
function renderTable(cidrs) {
const rows = cidrs.map(c => '<tr><td><code>' + padIP(c) + '</code></td></tr>').join('\n');
return '<!DOCTYPE html><html><head><meta charset="utf-8"><title>WhiteList Export</title>'
+ '<style>body{font-family:monospace;background:#fff;color:#111;padding:1rem}'
+ 'h2{font-size:1rem;color:#666;margin-bottom:.5rem}'
+ 'table{border-collapse:collapse}'
+ 'td{padding:.15rem 1rem .15rem 0}'
+ 'code{font-size:.9rem}'
+ '</style></head><body>'
+ '<h2>WhiteList — ' + cidrs.length + ' записей</h2>'
+ '<table>' + rows + '</table>'
+ '</body></html>';
}
module.exports = { createExportRouter }; module.exports = { createExportRouter };