Implement blocking functionality after certain count of visits, added tests
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
var sqlite3 = require('sqlite3').verbose();
|
||||
var db = new sqlite3.Database("./proxy.db")
|
||||
var db = new sqlite3.Database("./proxy.db");
|
||||
|
||||
/** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV
|
||||
* FIXME - need to close the database connection
|
||||
@@ -35,7 +35,7 @@ function updateVisitCount(hostname) {
|
||||
*/
|
||||
function addHostToTable(hostname) {
|
||||
db.serialize(() => {
|
||||
db.run("insert into hosts (hostname, visitcount) values (?, 0)", [hostname])
|
||||
db.run("insert into hosts (hostname, visitcount) values (?, 0)", [hostname]);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -43,14 +43,21 @@ function addHostToTable(hostname) {
|
||||
* Either initialize a row in the database, or update the visit count for a row.
|
||||
* @param {string} hostname Host that was visited
|
||||
*/
|
||||
exports.visitHost = function (hostname) {
|
||||
exports.visitHost = function (hostname, callbackFn) {
|
||||
return db.serialize(() => {
|
||||
db.get("select * from hosts where hostname = ?", [hostname], (err, row) => {
|
||||
if (row === undefined) {
|
||||
addHostToTable(hostname);
|
||||
callbackFn(true);
|
||||
} else {
|
||||
updateVisitCount(hostname);
|
||||
// Do the blocking, make this configurable.
|
||||
if (row.visitcount >= 2) {
|
||||
callbackFn(false);
|
||||
} else {
|
||||
updateVisitCount(hostname);
|
||||
callbackFn(true);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user