Skip to content

Commit

Permalink
Better logging and stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecDusheck committed Sep 26, 2018
1 parent 8b34966 commit 372f260
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
16 changes: 11 additions & 5 deletions bin/www.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
console.log("Loading CoreAPI (github.com/ephs/coreAPI)...");

/**
* Module dependencies.
Expand All @@ -8,17 +9,21 @@ const fs = require('fs');
const app = require('../server.js');
const http = require('http');
const https = require('https');
const privateKey = fs.readFileSync('./ssl/testing_localhost.key', 'utf8');
const certificate = fs.readFileSync('./ssl/testing_localhost.crt', 'utf8');

const config = require('../server/config/config');

const privateKey = fs.readFileSync(config.key, 'utf8');
const certificate = fs.readFileSync(config.cert, 'utf8');

console.log("Checking for SSL certificate key and cert in: " + config.key + ", " + config.cert);
const credentials = {key: privateKey, cert: certificate};

/**
* Get port from environment and store in Express.
* Get port from environment and store.
*/

let port = normalizePort(process.env.PORT || '8080');
let sslPort = normalizePort(process.env.PORT || '8443');
let port = normalizePort(process.env.PORT || config.port);
let sslPort = normalizePort(process.env.PORT || config.sslPort);

/**
* Create HTTP server, for redirection purposes.
Expand All @@ -38,6 +43,7 @@ server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
console.log("HTTP server listening on port " + port);
console.log("NON-SSL will redirect to SSL.");

/**
* Create HTTPS server.
Expand Down
8 changes: 4 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ const port = process.env.PORT || '8443';
//Create logger
let logger = function(req, res, next){
if(req.connection.remoteAddress === "::1") //Local host = ::1
console.log("Localhost --> (" + req.method + ") " + req.url);
console.log("[LOGGER] Localhost --> (" + req.method + ") " + req.url);
else
console.log(req.connection.remoteAddress + " --> (" + req.method + ") " + req.url);
console.log("[LOGGER] " + req.connection.remoteAddress + " --> (" + req.method + ") " + req.url);
next();
};
//Use logger
app.all('*', logger);
console.log("Running logger.");
console.log("[LOGGER] Running logger.");

//Rate limit (a little generous because all students will have the same IP).
const limiter = rateLimit({
Expand Down Expand Up @@ -62,7 +62,7 @@ app.get('/', function(req, res) {
res.status(200);
res.json({
"error": "false",
"message": "Hello there!"
"message": "OK"
});
});

Expand Down
4 changes: 4 additions & 0 deletions server/config/EXAMPLE_CONFIG.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ let config = {};
config.secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"; //CHANGE THIS!!!! CHANGE THIS!!!!
config.coreURL = "https://edenprairie_students.na.rapidbiz.com/php/scl/edenprairie/index.php";
config.maxRequests = 2000; //Max requests per 5min.
config.key = "etc/ssl/testing_localhost.key"; //CHANGE THIS!!!! CHANGE THIS!!!!
config.cert = "etc/ssl/testing_localhost.crt"; //CHANGE THIS!!!! CHANGE THIS!!!!
config.sslPort = 8443;
config.port = 8080;
module.exports = config;
2 changes: 2 additions & 0 deletions server/controllers/authManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ module.exports.login = function (req, res) {

ephsCoreBridge.login(function (err, sessID) {
if (err) {
console.log("[LOGGER] Invalid login request from " + req.connection.remoteAddress + ".");
res.json({
"error": "true",
"error_code": "invalid_login"
});
} else {
console.log("[LOGGER] Valid login request from " + req.connection.remoteAddress + ". Logging in as user " + req.body.username + ".");
//Yay our login is good, lets encode the sessionID in a token and send it back.
let token = jwt.sign({sessID: sessID, username: htmlEntities(req.body.username)}, config.secret, {expiresIn: "23min"}); //Why 23mins? Well, (by default) our little phpsessid will expire by then.
res.json({
Expand Down

0 comments on commit 372f260

Please sign in to comment.