From d65187fcb5abeda2c3c02b3f18b47b4fcb35a6d0 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 9 Jul 2018 18:44:03 -0400 Subject: [PATCH] Prevented people from simply pointing their host names to my website and ripping off my content. --- README.md | 1 - includes/incorrectHost.html | 8 ++++ server.js | 92 +++++++++++++++++++++---------------- utils/utils.js | 15 ++++++ 4 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 includes/incorrectHost.html diff --git a/README.md b/README.md index 66654ca..f40ab1b 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ url varchar(100) not null, primary key(post_id) ); -// not used yet create table downloads( download_id mediumint unsigned not null AUTO_INCREMENT, diff --git a/includes/incorrectHost.html b/includes/incorrectHost.html new file mode 100644 index 0000000..3822599 --- /dev/null +++ b/includes/incorrectHost.html @@ -0,0 +1,8 @@ +
+

Incorrect Host name

+

Someone is trying to rip off my website.

+
+ Page not found +
+

Click here to go to the genuine site.

+
\ No newline at end of file diff --git a/server.js b/server.js index 23e35a7..6a64e51 100644 --- a/server.js +++ b/server.js @@ -11,8 +11,6 @@ const url = require('url'); var express = require("express"); -const fs = require('fs'); - var session = require('express-session'); const includes = require('./includes/includes.js'); @@ -34,55 +32,69 @@ var port = 8000; */ app.use(function(request, res) { - var q = url.parse(request.url, true); - var filename = q.pathname; - //handles image requests - if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) - { - require("./img/image.js").main(res, filename); - } - else if(filename.includes("/css/") || filename.includes(".txt")) - { - includes.sendCSS(res, filename) - } - else if(filename.includes("/downloads/")) - { - require("./downloads/downloads.js").main(res, filename, request); - } - else + console.log(request.headers.host); + + if(request.headers.host.includes("localhost:" + port) || + request.headers.host.includes("jrtechs.net")) { - var file = ""; + var q = url.parse(request.url, true); + var filename = q.pathname; - if(filename === '' || filename === '/') + //handles image requests + if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png")) + { + require("./img/image.js").main(res, filename); + } + else if(filename.includes("/css/") || filename.includes(".txt")) + { + includes.sendCSS(res, filename) + } + else if(filename.includes("/downloads/")) { - file="./posts/homePage.js"; + require("./downloads/downloads.js").main(res, filename, request); } else { - var urlSplit = filename.split("/"); - - if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - file = "./posts/category.js"; - - else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page - file = "./admin/admin.js"; + var file = ""; + if(filename === '' || filename === '/') + { + file="./posts/homePage.js"; + } else - file = "./posts/posts.js"; + { + var urlSplit = filename.split("/"); + + if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page + file = "./posts/category.js"; + + else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page + file = "./admin/admin.js"; + + else + file = "./posts/posts.js"; + } + + includes.printHeader(res).then(function() + { + return require(file).main(res, filename, request); + }).then(function() + { + return includes.printFooter(res); + }).catch(function(err) + { + console.log(err); + }) } - - includes.printHeader(res).then(function() - { - return require(file).main(res, filename, request); - }).then(function() - { - return includes.printFooter(res); - }).catch(function(err) - { - console.log(err); - }) } + else + { + utils.printWrongHost(res); + res.end(); + } + + }); http.createServer(app).listen(port); diff --git a/utils/utils.js b/utils/utils.js index 7116124..d2e2e93 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -32,6 +32,7 @@ module.exports= }); }, + /** * Method which return the contents of a file as a string * @param fileName @@ -50,6 +51,7 @@ module.exports= return 0; }, + /** * * @param fileName @@ -108,6 +110,7 @@ module.exports= }); }, + /** * Displays 404 error to user * @@ -117,5 +120,17 @@ module.exports= print404: function(result) { return this.include(result, "includes/404.html"); + }, + + + /** + * Displays 404 error to user + * + * @param result + * @returns {*} + */ + printWrongHost: function(result) + { + return this.include(result, "includes/incorrectHost.html"); } }; \ No newline at end of file