|
|
@@ -3,105 +3,162 @@ |
|
|
|
* |
|
|
|
* Requires express >= 4 |
|
|
|
*/ |
|
|
|
|
|
|
|
var common = require ('./server/common'), |
|
|
|
authorize = require ('./server/authorization'); |
|
|
|
/* |
|
|
|
var common = require ('./server/common'), |
|
|
|
authorize = require ('./server/authorization'), |
|
|
|
dbs = require ('./server/dbs'), |
|
|
|
files = require ('./server/files'); |
|
|
|
*/ |
|
|
|
var fs = require ('fs'), |
|
|
|
http = require ('http'), |
|
|
|
https = require ('https'), |
|
|
|
express = require ('express'), |
|
|
|
session = require ('express-session'), // session management |
|
|
|
morgan = require ('morgan'), // logger |
|
|
|
const fs = common.fs, // file sync, read certificates |
|
|
|
http = common.http, // http handler |
|
|
|
https = require ('https'), // https handler |
|
|
|
express = require ('express'), // node server framework |
|
|
|
session = require ('express-session'), // session management (security) |
|
|
|
morgan = require ('morgan'), // logger |
|
|
|
//serveFavicon = require ('serve-favicon'), |
|
|
|
bodyParser = require ('body-parser'); |
|
|
|
//MongoStore = require ('connect-mongo')(session); // uss mongodb as session storage |
|
|
|
var Message = require('./message.model.js'); |
|
|
|
bodyParser = require ('body-parser'), // post request bodyparser |
|
|
|
MongoStore = require ('connect-mongo')(session), // use mongodb as session storage |
|
|
|
Message = require('./database/message.model.js'); |
|
|
|
|
|
|
|
var app = express(); |
|
|
|
|
|
|
|
var http_port=8013; |
|
|
|
https_port=8889; |
|
|
|
|
|
|
|
/* |
|
|
|
* Init |
|
|
|
*/ |
|
|
|
/*ll |
|
|
|
common .init (); |
|
|
|
authorize.init (common); |
|
|
|
dbs .init (common); |
|
|
|
files .init (common); |
|
|
|
*/ |
|
|
|
|
|
|
|
// Security |
|
|
|
app.disable ('x-powered-by'); // TODO: Disable Header information: Powerd by Express -> Information disclosure |
|
|
|
//dbs .init (common); |
|
|
|
//files .init (common); |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* Route Control |
|
|
|
* Security |
|
|
|
* |
|
|
|
* TODO: Install helmet |
|
|
|
* https://expressjs.com/de/advanced/best-practice-security.html |
|
|
|
* |
|
|
|
* (Disable Header information: Powerd by Express) |
|
|
|
* -> Information disclosure |
|
|
|
*/ |
|
|
|
|
|
|
|
// Logger |
|
|
|
app.use (morgan ('dev')); |
|
|
|
//app.use(express.logger ( { format: 'default', stream: output_stream } )); |
|
|
|
|
|
|
|
// Fastpaths |
|
|
|
//app.use (serveFavicon (__dirname + '/public/favicon.ico')); |
|
|
|
app.disable ('x-powered-by'); |
|
|
|
|
|
|
|
// Session Management |
|
|
|
app.set('trust proxy', 1) // trust first proxy, neccessary for cookie secure: true flag |
|
|
|
app.use (session({ |
|
|
|
secret: 'adluhohks', |
|
|
|
secret: 'ahhgylhuvh', // caesar(3) 2 letter surname |
|
|
|
resave: false, |
|
|
|
saveUninitialized: false, |
|
|
|
cookie: { |
|
|
|
maxAge: 30*24*3600*1000, // TODO: ttl for session as well (Store) |
|
|
|
secure: false, // true for https only |
|
|
|
secure: true, // true for https only (since our app works only with https) |
|
|
|
}, |
|
|
|
name: 'om.sid', |
|
|
|
//store: new MongoStore ({mongooseConnection: dbs.mongoose.connection, ttl: 30*24*3600}), // mongoose + connect-mongo |
|
|
|
store: new MongoStore ({mongooseConnection: common.mongoose.connection, ttl: 30*24*3600}), // mongoose + connect-mongo |
|
|
|
//store: new MemoryStore ({checkPeriod: 24*3600*1000}), // memorystore |
|
|
|
})); |
|
|
|
|
|
|
|
// Args |
|
|
|
|
|
|
|
/* |
|
|
|
* Route Control |
|
|
|
*/ |
|
|
|
|
|
|
|
// Fastpaths |
|
|
|
//app.use (serveFavicon (__dirname + '/public/favicon.ico')); |
|
|
|
|
|
|
|
|
|
|
|
// Minimal Logging |
|
|
|
//app.use (morgan ('dev')); |
|
|
|
// Advanced Logging |
|
|
|
morgan.token ('user', function (req, res) { return (req.session && req.session.user) || '-'; }); |
|
|
|
morgan.token ('userColored', function (req, res) { |
|
|
|
var color = 0; |
|
|
|
if (req.session && req.session.roles) |
|
|
|
color = req.session.roles.admin ? 31 // red |
|
|
|
: req.session.roles.user ? 34 // blue |
|
|
|
: 0; // no color |
|
|
|
return '\x1b[' + color + 'm' + ((req.session && req.session.user) || '-') + '\x1b[0m'; |
|
|
|
}); |
|
|
|
morgan.token ('statusColored', function (req, res) { |
|
|
|
var color = res.statusCode >= 500 ? 31 // red |
|
|
|
: res.statusCode >= 400 ? 33 // yellow |
|
|
|
: res.statusCode >= 300 ? 36 // cyan |
|
|
|
: res.statusCode >= 200 ? 32 // green |
|
|
|
: 0; // no color |
|
|
|
return '\x1b[' + color + 'm' + (res.headersSent ? res.statusCode : '-') + '\x1b[0m'; |
|
|
|
}); |
|
|
|
app.use (morgan (':date[iso] :statusColored :method :url :userColored :response-time ms :res[content-length]')); |
|
|
|
|
|
|
|
// BodyParser |
|
|
|
// Returns middleware that only parses json bodies. |
|
|
|
// (https://www.npmjs.com/package/body-parser#bodyparserjsonoptions) |
|
|
|
app.use (bodyParser.json()); |
|
|
|
// Returns middleware that only parses urlencoded bodies |
|
|
|
// with qs library (https://www.npmjs.com/package/qs#readme) |
|
|
|
app.use (bodyParser.urlencoded({extended: true})); |
|
|
|
|
|
|
|
// API |
|
|
|
//var api_routes = express.Router(); // express app-object routing |
|
|
|
//app.use ('/api', api_routes); |
|
|
|
|
|
|
|
app.use (function (req, res, done) { |
|
|
|
console.log(req.url); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
var api_routes = express.Router(); // express app-object routing |
|
|
|
|
|
|
|
//global.__basedir = __dirname; |
|
|
|
app.use ('/api', api_routes); |
|
|
|
|
|
|
|
// Static Files |
|
|
|
app.use(express.static(__dirname + '/public')); // Allow server access to 'public' folder |
|
|
|
// Allow server access to 'public' folder |
|
|
|
app.use(express.static(__dirname + '/public')); |
|
|
|
|
|
|
|
// Other stuff is NOT authorized unless logged in |
|
|
|
//app.use (authorize.genCheckAuthorized ('user')); |
|
|
|
|
|
|
|
//app.use(express.static('resources')); |
|
|
|
// Uploaded files |
|
|
|
//app.use ('/uploads', express.static(__dirname + '/uploads')); |
|
|
|
|
|
|
|
// Configuring the database |
|
|
|
var dbConfig = require('./mongodb.config.js'); |
|
|
|
var mongoose = require('mongoose'); |
|
|
|
//var dbConfig = require('./mongodb.config.js'); |
|
|
|
|
|
|
|
mongoose.Promise = global.Promise; |
|
|
|
common.mongoose.Promise = global.Promise; |
|
|
|
|
|
|
|
// Connecting to the database |
|
|
|
//mongoose.connect(`mongodb://${server}/${dbConfig.url}`) |
|
|
|
mongoose.connect(dbConfig.url, {useNewUrlParser: true}).then(() => { |
|
|
|
// Local db: common.config.dbLocalConn |
|
|
|
// Efi db: common.config.dbConn |
|
|
|
common.mongoose.connect (common.config.dbLocalConn, {useNewUrlParser: true}) .then( () => { |
|
|
|
console.log("Successfully connected to MongoDB."); |
|
|
|
}).catch(err => { |
|
|
|
}).catch( err => { |
|
|
|
console.log('Could not connect to MongoDB.'); |
|
|
|
process.exit(); |
|
|
|
}); |
|
|
|
|
|
|
|
// No error so far? Then it's a 404! |
|
|
|
//app.use (function (req, res, next) { next (common.genError (404, req.url)); }); |
|
|
|
//app.use (routes.errorHandler (true)); /* true: show stack traces */ |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* API |
|
|
|
*/ |
|
|
|
/* |
|
|
|
// API allowed for all |
|
|
|
api_routes.post ('/login', authorize.login); // /api/login |
|
|
|
|
|
|
|
//require('./app/routes/message.route.js')(app); |
|
|
|
// Validate all other API calls |
|
|
|
api_routes.use (authorize.genCheckAuthorized ('user')); |
|
|
|
api_routes.post ('/logout', authorize.logout); |
|
|
|
|
|
|
|
function addRoutes (r) { |
|
|
|
for (var e in r.routes) { |
|
|
|
var params = r.routes[e].params ? "/" + r.routes[e].params : ""; |
|
|
|
console.log ("Adding routes for /" + e + params + ":" + |
|
|
|
(r.routes[e].get ? " get":" ") + (r.routes[e].post ? " post":" ") + |
|
|
|
(r.routes[e].put ? " put":" ") + (r.routes[e].delete ? " delete":" ")); |
|
|
|
if (r.routes[e].get) |
|
|
|
api_routes.get ('/' + e + params, r.routes[e].get); |
|
|
|
if (r.routes[e].post) |
|
|
|
api_routes.post ('/' + e + params, r.routes[e].post); |
|
|
|
if (r.routes[e].put) |
|
|
|
api_routes.put ('/' + e + params, r.routes[e].put); |
|
|
|
if (r.routes[e].delete) |
|
|
|
api_routes.delete ('/' + e + params, r.routes[e].delete); |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
app.get ('/api/ids', function (req, res) { |
|
|
|
Message.find({},{id: true}) .exec () .then(results => { |
|
|
@@ -169,62 +226,17 @@ app.post("/api/createMsg", function(req, res){ |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
// Other stuff is NOT authorized unless logged in |
|
|
|
//app.use (authorize.genCheckAuthorized ('user')); |
|
|
|
|
|
|
|
// Uploaded files |
|
|
|
//app.use ('/uploads', expr ess.static(__dirname + '/uploads')); |
|
|
|
|
|
|
|
// Other stuff is NOT authorized unless logged in |
|
|
|
//app.use (authorize.genCheckAuthorized ('user')); |
|
|
|
|
|
|
|
// Uploaded files |
|
|
|
//app.use ('/uploads', express.static(__dirname + '/uploads')); |
|
|
|
|
|
|
|
// Errors |
|
|
|
// No error so far? Then it's a 404! |
|
|
|
//app.use (function (req, res, next) { next (common.genError (404, req.url)); }); |
|
|
|
//app.use (routes.errorHandler (true)); /* true: show stack traces */ // TODO: Error Handler |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* API |
|
|
|
*/ |
|
|
|
/* |
|
|
|
// API allowed for all |
|
|
|
api_routes.post ('/login', authorize.login); // /api/login |
|
|
|
|
|
|
|
// Validate all other API calls |
|
|
|
api_routes.use (authorize.genCheckAuthorized ('user')); |
|
|
|
api_routes.post ('/logout', authorize.logout); |
|
|
|
|
|
|
|
function addRoutes (r) { |
|
|
|
for (var e in r.routes) { |
|
|
|
var params = r.routes[e].params ? "/" + r.routes[e].params : ""; |
|
|
|
console.log ("Adding routes for /" + e + params + ":" + |
|
|
|
(r.routes[e].get ? " get":" ") + (r.routes[e].post ? " post":" ") + |
|
|
|
(r.routes[e].put ? " put":" ") + (r.routes[e].delete ? " delete":" ")); |
|
|
|
if (r.routes[e].get) |
|
|
|
api_routes.get ('/' + e + params, r.routes[e].get); |
|
|
|
if (r.routes[e].post) |
|
|
|
api_routes.post ('/' + e + params, r.routes[e].post); |
|
|
|
if (r.routes[e].put) |
|
|
|
api_routes.put ('/' + e + params, r.routes[e].put); |
|
|
|
if (r.routes[e].delete) |
|
|
|
api_routes.delete ('/' + e + params, r.routes[e].delete); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
addRoutes (dbs); |
|
|
|
addRoutes (admin); |
|
|
|
addRoutes (files); |
|
|
|
*/ |
|
|
|
|
|
|
|
/* |
|
|
|
* Servers |
|
|
|
*/ |
|
|
|
|
|
|
|
http.createServer (app) .listen (http_port, function () { |
|
|
|
console.log ("Express http server listening on port " + http_port); |
|
|
|
http.createServer (app) .listen (common.config.httpPort, function () { |
|
|
|
console.log ("Express http server listening on port " + common.config.httpPort); |
|
|
|
}); |
|
|
|
|
|
|
|
/* |
|
|
@@ -238,36 +250,36 @@ http.createServer (app) .listen (http_port, function () { |
|
|
|
* openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem |
|
|
|
* rm certrequest.csr |
|
|
|
*/ |
|
|
|
|
|
|
|
var options; |
|
|
|
try { |
|
|
|
try { |
|
|
|
// In case it's a real certificate: add CA chain cersts (TODO: use array if required) |
|
|
|
var ca = fs.readFileSync ('keys/ca_cert.pem'); |
|
|
|
} catch (e) { |
|
|
|
ca = undefined; |
|
|
|
console.log ("Note: Can't read CA bundle: "+e); |
|
|
|
} |
|
|
|
if (ca != null) { |
|
|
|
|
|
|
|
options = { |
|
|
|
key: fs.readFileSync ('keys/omkey.pem'), |
|
|
|
cert: fs.readFileSync ('keys/certificate.pem'), |
|
|
|
ca: ca |
|
|
|
}; |
|
|
|
https.createServer (options, app) .listen (https_port, function () { |
|
|
|
console.log ("Express https server listening on port " + https_port); |
|
|
|
}); |
|
|
|
if (common.config.httpsPort) { |
|
|
|
var options; |
|
|
|
try { |
|
|
|
try { |
|
|
|
// In case it's a real certificate: add CA chain cersts (TODO: use array if required) |
|
|
|
var ca = fs.readFileSync ('keys/ca_cert.pem'); |
|
|
|
} catch (e) { |
|
|
|
ca = undefined; |
|
|
|
console.log ("Note: Can't read CA bundle: "+e); |
|
|
|
} |
|
|
|
if (ca != undefined) { |
|
|
|
options = { |
|
|
|
key: fs.readFileSync ('keys/omkey.pem'), |
|
|
|
cert: fs.readFileSync ('keys/certificate.pem'), |
|
|
|
ca: ca |
|
|
|
}; |
|
|
|
https.createServer (options, app) .listen (common.config.httpsPort, function () { |
|
|
|
console.log ("Express https server listening on port " + common.config.httpsPort); |
|
|
|
}); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.log ("Note: Can't read SSL keys/certs: "+e+"\nDisabling https server"); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.log ("Note: Can't read SSL keys/certs: "+e+"\nDisabling https server"); |
|
|
|
} else { |
|
|
|
console.log("Note: https server disabled by config"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* Uncaught Exceptions |
|
|
|
*/ |
|
|
|
|
|
|
|
process.on ("uncaughtException", function (err) { |
|
|
|
console.error ("*** Uncaught Exception:"); |
|
|
|
console.error (err.stack); |