security-gateway
Version:
A plug-and-play security gateway that detects malicious traffic and redirects it to a decoy API
95 lines (89 loc) • 3.43 kB
JavaScript
// middleware/adminDashboard.js
/**
* Middleware to render the admin dashboard
*/
const adminDashboard = (req, res, suspiciousIPs, suspiciousRequests) => {
res.send(`
<html>
<head>
<title>Security Gateway Dashboard</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
.stats { display: flex; justify-content: space-between; margin-bottom: 20px; }
.stat-card { background-color: #f5f5f5; padding: 15px; border-radius: 5px; width: 23%; }
.refresh { float: right; margin-bottom: 10px; }
</style>
<script>
function refreshPage() {
location.reload();
}
// Auto refresh every 30 seconds
setTimeout(refreshPage, 30000);
</script>
</head>
<body>
<h1>Security Gateway Dashboard</h1>
<button onclick="refreshPage()" class="refresh">Refresh Data</button>
<div class="stats">
<div class="stat-card">
<h3>Suspicious IPs</h3>
<h2>${suspiciousIPs.size}</h2>
</div>
<div class="stat-card">
<h3>Total Attacks</h3>
<h2>${suspiciousRequests.length}</h2>
</div>
<div class="stat-card">
<h3>Last Attack</h3>
<h2>${suspiciousRequests.length > 0 ? new Date(suspiciousRequests[suspiciousRequests.length - 1].timestamp).toLocaleTimeString() : 'None'}</h2>
</div>
<div class="stat-card">
<h3>Status</h3>
<h2>Active</h2>
</div>
</div>
<h2>Suspicious IPs</h2>
<table>
<tr>
<th>IP Address</th>
<th>Action</th>
</tr>
${Array.from(suspiciousIPs).map(ip => `
<tr>
<td>${ip}</td>
<td><button onclick="alert('IP would be blocked permanently')">Block Permanently</button></td>
</tr>
`).join('') || '<tr><td colspan="2">No suspicious IPs detected yet</td></tr>'}
</table>
<h2>Attack History</h2>
<table>
<tr>
<th>Timestamp</th>
<th>IP</th>
<th>Method</th>
<th>URL</th>
<th>Attack Type</th>
</tr>
${suspiciousRequests.map(req => `
<tr>
<td>${new Date(req.timestamp).toLocaleString()}</td>
<td>${req.ip}</td>
<td>${req.method}</td>
<td>${req.url}</td>
<td>${Object.entries(req.reason)
.filter(([k, v]) => v === true)
.map(([k]) => k)
.join(', ')}
</td>
</tr>
`).join('') || '<tr><td colspan="5">No attacks detected yet</td></tr>'}
</table>
</body>
</html>
`);
};
module.exports = { adminDashboard };