2019-01-25 03:07:42 +00:00
|
|
|
var http = require('http');
|
2019-01-26 12:48:38 +00:00
|
|
|
var winston = require("winston");
|
2019-01-27 14:06:39 +00:00
|
|
|
var fs = require("fs");
|
|
|
|
var sqlite3 = require('sqlite3').verbose();
|
|
|
|
var db = new sqlite3.Database("./proxy.db");
|
2019-01-27 14:17:08 +00:00
|
|
|
const url = require("url");
|
2019-01-26 12:48:38 +00:00
|
|
|
|
2019-01-26 13:14:42 +00:00
|
|
|
// Rudimentary logger.
|
|
|
|
// TODO - Make this filename configurable.
|
2019-01-26 12:48:38 +00:00
|
|
|
const logger = winston.createLogger({
|
|
|
|
transports: [
|
|
|
|
new winston.transports.File({filename: 'proxy.log'})
|
|
|
|
]
|
|
|
|
});
|
2019-01-25 03:07:42 +00:00
|
|
|
|
2019-01-26 18:12:04 +00:00
|
|
|
/**
|
|
|
|
* Connection handler for proxy, forward request and then pipe response
|
|
|
|
* back to the client.
|
|
|
|
*
|
|
|
|
* @param {IncomingMessage} request The request from the client
|
|
|
|
* @param {OutgoingMessage} response The response to write back to the client
|
|
|
|
*/
|
|
|
|
const processRequest = (request, response) => {
|
|
|
|
let body = [];
|
|
|
|
// Use closure for request.
|
|
|
|
let req = request;
|
|
|
|
let res = response;
|
|
|
|
|
|
|
|
// Set up a data handler for the socket connection.
|
|
|
|
request.on("data", (chunk) => {
|
|
|
|
body.push(chunk);
|
|
|
|
})
|
|
|
|
|
|
|
|
// Set up an end handler for the socket connection.
|
|
|
|
request.on("end", () => {
|
|
|
|
body = Buffer.concat(body).toString();
|
|
|
|
logger.info({message: "Got a message!"});
|
|
|
|
logger.info({message: "Requesting: " + req.url});
|
|
|
|
|
2019-01-27 14:17:08 +00:00
|
|
|
const hostUrl = new url.URL(req.url);
|
2019-01-27 16:44:00 +00:00
|
|
|
visitHost(hostUrl.hostname);
|
2019-01-26 18:12:04 +00:00
|
|
|
// Complicated line of JS to forward request, and pipe it to the
|
|
|
|
// response object.
|
|
|
|
req.pipe(http.request(req.url, (resp) => {resp.pipe(res)}));
|
|
|
|
});
|
|
|
|
}
|
2019-01-25 00:54:48 +00:00
|
|
|
|
2019-01-26 12:48:38 +00:00
|
|
|
function startServer() {
|
2019-01-26 13:14:42 +00:00
|
|
|
// Start the server, set up data and end handlers.
|
2019-01-26 18:12:04 +00:00
|
|
|
var server = http.createServer(processRequest);
|
2019-01-26 12:48:38 +00:00
|
|
|
|
2019-01-26 13:14:42 +00:00
|
|
|
// Start the server
|
2019-01-26 12:48:38 +00:00
|
|
|
server.listen(8124, () => {
|
|
|
|
console.log("Server bound.");
|
|
|
|
});
|
2019-01-25 01:43:49 +00:00
|
|
|
|
2019-01-26 12:48:38 +00:00
|
|
|
return server;
|
|
|
|
}
|
2019-01-25 03:13:48 +00:00
|
|
|
|
2019-01-27 14:17:08 +00:00
|
|
|
/** TODO - MAKE THESE FUNCTIONS THEIR OWN MODULE. VVV
|
|
|
|
* FIXME - need to close the database connection
|
|
|
|
* on exit.
|
|
|
|
*/
|
|
|
|
|
2019-01-27 14:06:39 +00:00
|
|
|
/**
|
|
|
|
* Initialize our database.
|
|
|
|
*/
|
|
|
|
function initializeDatabase() {
|
|
|
|
db.serialize(() => {
|
2019-01-27 16:44:00 +00:00
|
|
|
db.run("create table hosts (hostname TEXT, visitcount Int)", (err, row) => {
|
2019-01-27 14:06:39 +00:00
|
|
|
if (err !== undefined || err !== null) {
|
|
|
|
console.log("Error creating database, it likely exists already.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-01-27 14:17:08 +00:00
|
|
|
// db.close();
|
2019-01-27 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the visit count for a host logged by the proxy
|
|
|
|
* @param {string} hostname Host logged by proxy
|
|
|
|
*/
|
|
|
|
function updateVisitCount(hostname) {
|
|
|
|
db.serialize(() => {
|
2019-01-27 16:44:00 +00:00
|
|
|
db.run("update hosts set visitcount = visitcount + 1 where hostname = ?", [hostname]);
|
2019-01-27 14:06:39 +00:00
|
|
|
});
|
2019-01-27 14:17:08 +00:00
|
|
|
// db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function addHostToTable(hostname) {
|
|
|
|
db.serialize(() => {
|
2019-01-27 16:44:00 +00:00
|
|
|
db.run("insert into hosts (hostname, visitcount) values (?, 0)", [hostname])
|
2019-01-27 14:17:08 +00:00
|
|
|
})
|
|
|
|
// db.close();
|
2019-01-27 14:06:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 16:44:00 +00:00
|
|
|
function visitHost(hostname) {
|
|
|
|
return db.serialize(() => {
|
|
|
|
db.get("select * from hosts where hostname = ?", [hostname], (err, row) => {
|
|
|
|
if (row === undefined) {
|
|
|
|
addHostToTable(hostname);
|
|
|
|
} else {
|
|
|
|
updateVisitCount(hostname);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-27 14:06:39 +00:00
|
|
|
initializeDatabase();
|
2019-01-26 12:48:38 +00:00
|
|
|
startServer();
|