Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added sqlite3 integration for managing client data using database tools #1

Merged
merged 2 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ bower_components

# Temp files
*.swp

#Db Files
url.db
81 changes: 65 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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');
});
Expand All @@ -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"
Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this sanitize data, or is this vulnerable to an SQL injection?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryGhost/node-sqlite3#57 so I think yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and by yes I mean yes it sanitizes :p lol

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();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}