From 7069e6cbdaaacc0432c59c2fb5add66b269eaae1 Mon Sep 17 00:00:00 2001 From: hitanshup Date: Wed, 18 Jan 2017 21:02:03 -0500 Subject: [PATCH 1/2] added sqlite3 integration for managing client data using database tools --- .gitignore | 3 ++ app.js | 81 +++++++++++++++++++++++++++++++++++++++++----------- package.json | 3 +- 3 files changed, 70 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 611dd26..b53de30 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ bower_components # Temp files *.swp + +#Db Files +url.db diff --git a/app.js b/app.js index e7b0579..685c5d7 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,9 @@ const express = require('express'); const bodyParser = require('body-parser'); const cryptohat = require('cryptohat'); const cron = require('cron'); -const Ddos = require('ddos') +const Ddos = require('ddos'); +var sqlite3 = require('sqlite3').verbose(); +var fs = require("fs"); const app = express(); @@ -18,24 +20,49 @@ const HASH_LENGTH = 5; const CLEANSE_FREQ = '0 0 4 * * *'; const EXPIRY_TIME = 48; var maps = {}; +var file = "url.db"; +var exists = fs.existsSync(file); +var db = new sqlite3.Database(file); var cleanse = new cron.CronJob(CLEANSE_FREQ, function() { var current_date = new Date(); - for(var key in maps) { - if(maps[key]['expiry'] < current_date) { - delete maps[key]; - } - } + db.serialize(function() { + db.each("SELECT rowid AS id, expiry FROM user_info", function(err, row) { + if (new Date(row.expiry) < current_date) { + db.run("DELETE FROM user_info WHERE rowid=(?)", row.id, function(error) { + if(error){ + console.log(error); + } else { + console.log("Deletion Successful"); + } + }); + } + }); + }); }); + app.get('/*', function(req, res, next) { - if(req.url.substr(1) in maps) { - res.redirect(maps[req.url.substr(1)]['url']); - } else { - next(); - } + db.serialize(function() { + db.each("SELECT url FROM user_info WHERE shrunk = '" + req.url.substr(1) + "'", function(err, row) { + if(err) { + console.log(err); + } else { + res.redirect(row.url); + } + }, function(error, rows) { // oncomplete functions + if(error) { + console.log(error); + } else { + if( rows === 0) { // the case where the url doesn't exist in db + next(); + } + } + }); + }); }); + app.get('/', function (req, res) { res.sendfile(__dirname + '/public/index.html'); }); @@ -62,11 +89,7 @@ app.post('/add', function(req, res) { } expiry = new Date(); expiry.setHours(expiry.getHours() + EXPIRY_TIME); - maps[shrunk] = { - 'url' : url.trim, - 'expiry' : expiry, - }; - + addUrlToDb(shrunk, (url.trim()).toString(), expiry.toString()); // Return the new URL res.writeHead(200, { "Content-Type": "text/plain" @@ -77,3 +100,29 @@ app.post('/add', function(req, res) { app.listen(port, function () { console.log('z-9 listening on port ' + port + '.'); }); + +//initalizes the database i.e creates the db.url file if it doens't already exist +function initDb() { + if (!exists) { + console.log("Creating DB file."); + fs.openSync(file, "w"); + db.serialize(function() { + db.run("CREATE TABLE if not exists user_info (shrunk TEXT UNIQUE, url TEXT, expiry TEXT)"); + }); + } +} + +//adds new urls to the database +function addUrlToDb(shrunk, url, expiry) { + db.serialize(function() { + var stmt = db.prepare("INSERT INTO user_info VALUES (?,?,?)"); + stmt.run(shrunk, url, expiry); + stmt.finalize(); + db.each("SELECT rowid AS id, shrunk, url, expiry FROM user_info", function(err, row) { + console.log(row.id + ": " + row.shrunk + " " + row.url + " " + row.expiry); + }); + }); +} + +initDb(); +cleanse.start(); diff --git a/package.json b/package.json index 7a90084..998408b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "cryptohat": "^1.0.1", "ddos": "^0.1.11", "express": "^4.13.4", - "pm2": "^1.1.3" + "pm2": "^1.1.3", + "sqlite3": "^3.1.8" } } From e02948c5e72e4b3e30d7edaa6197cb8714351ae8 Mon Sep 17 00:00:00 2001 From: hitanshup Date: Wed, 18 Jan 2017 21:08:11 -0500 Subject: [PATCH 2/2] improved indentation --- app.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 685c5d7..50ddf27 100644 --- a/app.js +++ b/app.js @@ -30,7 +30,7 @@ var cleanse = new cron.CronJob(CLEANSE_FREQ, function() { db.each("SELECT rowid AS id, expiry FROM user_info", function(err, row) { if (new Date(row.expiry) < current_date) { db.run("DELETE FROM user_info WHERE rowid=(?)", row.id, function(error) { - if(error){ + if (error){ console.log(error); } else { console.log("Deletion Successful"); @@ -45,16 +45,16 @@ var cleanse = new cron.CronJob(CLEANSE_FREQ, function() { app.get('/*', function(req, res, next) { db.serialize(function() { db.each("SELECT url FROM user_info WHERE shrunk = '" + req.url.substr(1) + "'", function(err, row) { - if(err) { + if (err) { console.log(err); } else { res.redirect(row.url); } }, function(error, rows) { // oncomplete functions - if(error) { + if (error) { console.log(error); } else { - if( rows === 0) { // the case where the url doesn't exist in db + if (rows === 0) { // the case where the url doesn't exist in db next(); } }