From 5f577071eee549e7f9ad6ee6d83c14050af7f846 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Wed, 13 Feb 2019 16:10:57 -0500 Subject: [PATCH] Adding blocked_hosts table, ensure only new hosts are inserted. --- lib/db.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/db.js b/lib/db.js index 0967c52..e237340 100644 --- a/lib/db.js +++ b/lib/db.js @@ -39,7 +39,29 @@ exports.initializeDatabase = function() { if (err !== null) throw err - console.log(JSON.parse(data)) + // console.log(JSON.parse(data)) + let blockedData = JSON.parse(data) + db.run("create table if not exists blocked_hosts (hostname TEXT)", (err, row) => { + if (err !== null) { + logErrorAndExit("Error creating table blocked_hosts: " + err) + } + }) + + db.get("select count(*) from blocked_hosts", (err, row) => { + if (err !== null) { + logErrorAndExit("Error getting count of blocked_hosts") + } + // if (blockedData.hosts.length row['count(*)']) + for (let host of blockedData.hosts) { + db.get("select hostname from blocked_hosts where hostname = ?", [host], (err, row) => { + if (err !== null) { + db.run("insert into blocked_hosts (hostname) values (?)", [hostname]); + } + // TODO -- run a diff for whatever has been deleted from the config file. + }) + } + }) + }) } catch(err) { console.log(err)