bot-guardian-js
Version:
A powerful bot detection and prevention library for Node.js applications
62 lines (61 loc) • 2.65 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
class DashboardApp {
constructor() {
this.updateInterval = 5000; // 5 seconds
this.initialize();
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
yield this.updateDashboard();
setInterval(() => this.updateDashboard(), this.updateInterval);
});
}
updateDashboard() {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield fetch('/data');
const data = yield response.json();
this.updateMetrics(data);
this.updateDetectionsList(data.recentDetections);
}
catch (error) {
console.error('Failed to update dashboard:', error);
}
});
}
updateMetrics(data) {
document.getElementById('totalRequests').textContent = data.totalRequests;
document.getElementById('detectedBots').textContent = data.detectedBots;
}
updateDetectionsList(detections) {
const list = document.getElementById('detectionsList');
list.innerHTML = detections
.map(detection => `
<div class="detection-item">
<div>Type: ${detection.type}</div>
<div>User Agent: ${detection.userAgent}</div>
<div>Confidence: ${detection.confidence}</div>
<div>Timestamp: ${new Date(detection.timestamp).toLocaleString()}</div>
<div class="behavior-metrics">
<h4>Behavior Metrics:</h4>
<div>Mouse Movements: ${detection.behavior.mouseMovements}</div>
<div>Keystrokes: ${detection.behavior.keystrokes}</div>
<div>Time on Page: ${detection.behavior.timeOnPage}s</div>
<div>Scrolling: ${detection.behavior.scrolling ? 'Yes' : 'No'}</div>
</div>
</div>
`)
.join('');
}
}
// Initialize dashboard
window.dashboard = new DashboardApp();