nehonix-uri-processor
Version:
A powerful URI processor for encoding, decoding, and analyzing URI data securely.
90 lines • 2.79 kB
JavaScript
// NLM => Nehonix Local Memory
/**
* In-memory database adapter (default)
*/
export class NLM {
constructor() {
this.suspiciousIPs = new Map();
this.blockedIPs = new Set();
this.securityEvents = [];
}
async trackSuspiciousIP(ip, details) {
const now = Date.now();
const record = this.suspiciousIPs.get(ip);
if (record) {
// Update existing record
record.count += 1;
record.lastSeen = now;
record.details = { ...record.details, ...details };
}
else {
// Create new record
this.suspiciousIPs.set(ip, { count: 1, lastSeen: now, details });
}
// Cleanup old records every 100 entries
if (this.suspiciousIPs.size % 100 === 0) {
this.cleanupSuspiciousIPs();
}
}
async getSuspiciousIPs() {
return Array.from(this.suspiciousIPs.entries()).map(([ip, data]) => ({
ip,
count: data.count,
lastSeen: data.lastSeen,
details: data.details,
}));
}
async blockIP(ip, reason) {
this.blockedIPs.add(ip);
// Add a security event
await this.saveSecurityEvent({
timestamp: Date.now(),
type: "block",
ip,
details: { reason },
});
return true;
}
async isIPBlocked(ip) {
return this.blockedIPs.has(ip);
}
async saveSecurityEvent(event) {
this.securityEvents.push(event);
// Keep only the last 10,000 events
if (this.securityEvents.length > 10000) {
this.securityEvents = this.securityEvents.slice(-10000);
}
}
async getSecurityEvents(options) {
const startTime = options.startDate.getTime();
const endTime = options.endDate.getTime();
return this.securityEvents.filter((event) => event.timestamp >= startTime && event.timestamp <= endTime);
}
cleanupSuspiciousIPs() {
const now = Date.now();
const expirationTime = 24 * 60 * 60 * 1000; // 24 hours
for (const [ip, record] of this.suspiciousIPs.entries()) {
if (now - record.lastSeen > expirationTime) {
this.suspiciousIPs.delete(ip);
}
}
}
}
// Default in-memory database
export const defaultDatabase = new NLM();
// Current active database adapter
export let activeDatabase = defaultDatabase;
/**
* Set a custom database adapter
*/
export function setDatabaseAdapter(adapter) {
activeDatabase = adapter;
}
/**
* Get the current database adapter
*/
export function getDatabaseAdapter() {
return activeDatabase;
}
export { NLM as InMemoryDatabaseAdapter };
//# sourceMappingURL=NEHONIX.LocalMemory.js.map