Browse Source

Prevented people from simply pointing their host names to my website and ripping off my content.

pull/4/head
jrtechs 6 years ago
parent
commit
d65187fcb5
4 changed files with 75 additions and 41 deletions
  1. +0
    -1
      README.md
  2. +8
    -0
      includes/incorrectHost.html
  3. +52
    -40
      server.js
  4. +15
    -0
      utils/utils.js

+ 0
- 1
README.md View File

@ -44,7 +44,6 @@ url varchar(100) not null,
primary key(post_id) primary key(post_id)
); );
// not used yet
create table downloads( create table downloads(
download_id mediumint unsigned not null AUTO_INCREMENT, download_id mediumint unsigned not null AUTO_INCREMENT,

+ 8
- 0
includes/incorrectHost.html View File

@ -0,0 +1,8 @@
<div class="row p-lg-0">
<h1 class="align-content-center">Incorrect Host name</h1>
<p>Someone is trying to rip off my website.</p>
<div class="align-content-center">
<img src="https://jrtechs.net/img/website/404.jpg" alt="Page not found" width="40%" />
</div>
<p>Click <a href="https://jrtechs.net">here</a> to go to the genuine site.</p>
</div>

+ 52
- 40
server.js View File

@ -11,8 +11,6 @@ const url = require('url');
var express = require("express"); var express = require("express");
const fs = require('fs');
var session = require('express-session'); var session = require('express-session');
const includes = require('./includes/includes.js'); const includes = require('./includes/includes.js');
@ -34,55 +32,69 @@ var port = 8000;
*/ */
app.use(function(request, res) 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 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 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); http.createServer(app).listen(port);

+ 15
- 0
utils/utils.js View File

@ -32,6 +32,7 @@ module.exports=
}); });
}, },
/** /**
* Method which return the contents of a file as a string * Method which return the contents of a file as a string
* @param fileName * @param fileName
@ -50,6 +51,7 @@ module.exports=
return 0; return 0;
}, },
/** /**
* *
* @param fileName * @param fileName
@ -108,6 +110,7 @@ module.exports=
}); });
}, },
/** /**
* Displays 404 error to user * Displays 404 error to user
* *
@ -117,5 +120,17 @@ module.exports=
print404: function(result) print404: function(result)
{ {
return this.include(result, "includes/404.html"); 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");
} }
}; };

Loading…
Cancel
Save