import ipaddr from 'ipaddr.js'; export class IPRestrictionModule { private blockedIPs: Set = new Set(); private blockedCIDRs: any[] = []; constructor() {} setRules(rules: any[]) { this.blockedIPs.clear(); this.blockedCIDRs = []; rules.forEach(rule => { if (rule.type === 'IP' && rule.enabled) { if (rule.config?.ip) { this.blockedIPs.add(rule.config.ip); } if (rule.config?.cidr) { try { this.blockedCIDRs.push(ipaddr.parseCIDR(rule.config.cidr)); } catch (e) { console.error('Invalid CIDR rule', rule.config.cidr); } } } }); } isBlocked(ip: string): boolean { // Direct match if (this.blockedIPs.has(ip)) return true; // CIDR match if (this.blockedCIDRs.length > 0) { try { const parsedIP = ipaddr.parse(ip); for (const cidr of this.blockedCIDRs) { // ipaddr.js match method signature issue with TS union types // Cast to any to bypass strict union check or use specific type guard if ((parsedIP as any).match(cidr)) return true; } } catch (e) { // Invalid IP in request } } return false; } }