2016-10-25 03:49:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const Log = require('../../logger.js').log;
|
|
|
|
const ServerModule = require('../../server_module.js').ServerModule;
|
|
|
|
const Config = require('../../config.js').get;
|
2018-12-27 09:46:16 +00:00
|
|
|
const { Errors } = require('../../enig_error.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const http = require('http');
|
|
|
|
const https = require('https');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const mimeTypes = require('mime-types');
|
2018-12-27 09:46:16 +00:00
|
|
|
const forEachSeries = require('async/forEachSeries');
|
2016-10-25 03:49:45 +00:00
|
|
|
|
|
|
|
const ModuleInfo = exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Web',
|
|
|
|
desc : 'Web Server',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
packageName : 'codes.l33t.enigma.web.server',
|
2016-10-25 03:49:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Route {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(route) {
|
|
|
|
Object.assign(this, route);
|
|
|
|
|
|
|
|
if(this.method) {
|
|
|
|
this.method = this.method.toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.pathRegExp = new RegExp(this.path);
|
|
|
|
} catch(e) {
|
|
|
|
Log.debug( { route : route }, 'Invalid regular expression for route path' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
return (
|
|
|
|
this.pathRegExp instanceof RegExp &&
|
2018-06-23 03:26:46 +00:00
|
|
|
( -1 !== [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', ].indexOf(this.method) ) ||
|
|
|
|
!_.isFunction(this.handler)
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
matchesRequest(req) {
|
|
|
|
return req.method === this.method && this.pathRegExp.test(req.url);
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
getRouteKey() { return `${this.method}:${this.path}`; }
|
2016-10-25 03:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getModule = class WebServerModule extends ServerModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const config = Config();
|
|
|
|
this.enableHttp = config.contentServers.web.http.enabled || false;
|
|
|
|
this.enableHttps = config.contentServers.web.https.enabled || false;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.routes = {};
|
|
|
|
|
|
|
|
if(this.isEnabled() && config.contentServers.web.staticRoot) {
|
|
|
|
this.addRoute({
|
2018-06-23 03:26:46 +00:00
|
|
|
method : 'GET',
|
|
|
|
path : '/static/.*$',
|
|
|
|
handler : this.routeStaticFile.bind(this),
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buildUrl(pathAndQuery) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create a URL such as
|
|
|
|
// https://l33t.codes:44512/ + |pathAndQuery|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Prefer HTTPS over HTTP. Be explicit about the port
|
|
|
|
// only if non-standard. Allow users to override full prefix in config.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const config = Config();
|
|
|
|
if(_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
|
|
|
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema;
|
|
|
|
let port;
|
|
|
|
if(config.contentServers.web.https.enabled) {
|
2018-06-23 03:26:46 +00:00
|
|
|
schema = 'https://';
|
|
|
|
port = (443 === config.contentServers.web.https.port) ?
|
2018-06-22 05:15:04 +00:00
|
|
|
'' :
|
|
|
|
`:${config.contentServers.web.https.port}`;
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
schema = 'http://';
|
|
|
|
port = (80 === config.contentServers.web.http.port) ?
|
2018-06-22 05:15:04 +00:00
|
|
|
'' :
|
|
|
|
`:${config.contentServers.web.http.port}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
return this.enableHttp || this.enableHttps;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
createServer(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(this.enableHttp) {
|
|
|
|
this.httpServer = http.createServer( (req, resp) => this.routeRequest(req, resp) );
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = Config();
|
|
|
|
if(this.enableHttps) {
|
|
|
|
const options = {
|
2018-06-23 03:26:46 +00:00
|
|
|
cert : fs.readFileSync(config.contentServers.web.https.certPem),
|
|
|
|
key : fs.readFileSync(config.contentServers.web.https.keyPem),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// additional options
|
2018-06-22 05:15:04 +00:00
|
|
|
Object.assign(options, config.contentServers.web.https.options || {} );
|
|
|
|
|
|
|
|
this.httpsServer = https.createServer(options, (req, resp) => this.routeRequest(req, resp) );
|
|
|
|
}
|
2018-12-27 09:19:26 +00:00
|
|
|
|
|
|
|
return cb(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:46:16 +00:00
|
|
|
listen(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
2018-12-27 09:46:16 +00:00
|
|
|
forEachSeries([ 'http', 'https' ], (service, nextService) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const name = `${service}Server`;
|
|
|
|
if(this[name]) {
|
|
|
|
const port = parseInt(config.contentServers.web[service].port);
|
|
|
|
if(isNaN(port)) {
|
2018-12-27 09:46:16 +00:00
|
|
|
Log.warn( { port : config.contentServers.web[service].port, server : ModuleInfo.name }, `Invalid port (${service})` );
|
|
|
|
return nextService(Errors.Invalid(`Invalid port: ${config.contentServers.web[service].port}`));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-12-27 09:46:16 +00:00
|
|
|
|
2019-04-10 02:25:03 +00:00
|
|
|
this[name].listen(port, config.contentServers.web[service].address, err => {
|
2018-12-27 09:46:16 +00:00
|
|
|
return nextService(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return nextService(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-12-27 09:46:16 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addRoute(route) {
|
|
|
|
route = new Route(route);
|
|
|
|
|
|
|
|
if(!route.isValid()) {
|
|
|
|
Log.warn( { route : route }, 'Cannot add route: missing or invalid required members' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const routeKey = route.getRouteKey();
|
|
|
|
if(routeKey in this.routes) {
|
|
|
|
Log.warn( { route : route, routeKey : routeKey }, 'Cannot add route: duplicate method/path combination exists' );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routes[routeKey] = route;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
routeRequest(req, resp) {
|
|
|
|
const route = _.find(this.routes, r => r.matchesRequest(req) );
|
|
|
|
|
|
|
|
if(!route && '/' === req.url) {
|
|
|
|
return this.routeIndex(req, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return route ? route.handler(req, resp) : this.accessDenied(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
respondWithError(resp, code, bodyText, title) {
|
|
|
|
const customErrorPage = paths.join(Config().contentServers.web.staticRoot, `${code}.html`);
|
|
|
|
|
|
|
|
fs.readFile(customErrorPage, 'utf8', (err, data) => {
|
|
|
|
resp.writeHead(code, { 'Content-Type' : 'text/html' } );
|
|
|
|
|
|
|
|
if(err) {
|
|
|
|
return resp.end(`<!doctype html>
|
2018-06-23 03:26:46 +00:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>${title}</title>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article>
|
|
|
|
<h2>${bodyText}</h2>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>`
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.end(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
accessDenied(resp) {
|
|
|
|
return this.respondWithError(resp, 401, 'Access denied.', 'Access Denied');
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNotFound(resp) {
|
|
|
|
return this.respondWithError(resp, 404, 'File not found.', 'File Not Found');
|
|
|
|
}
|
|
|
|
|
|
|
|
routeIndex(req, resp) {
|
|
|
|
const filePath = paths.join(Config().contentServers.web.staticRoot, 'index.html');
|
|
|
|
|
|
|
|
return this.returnStaticPage(filePath, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
routeStaticFile(req, resp) {
|
|
|
|
const fileName = req.url.substr(req.url.indexOf('/', 1));
|
|
|
|
const filePath = paths.join(Config().contentServers.web.staticRoot, fileName);
|
|
|
|
|
|
|
|
return this.returnStaticPage(filePath, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
returnStaticPage(filePath, resp) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if(err || !stats.isFile()) {
|
|
|
|
return self.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2018-06-23 03:26:46 +00:00
|
|
|
'Content-Type' : mimeTypes.contentType(paths.basename(filePath)) || mimeTypes.contentType('.bin'),
|
|
|
|
'Content-Length' : stats.size,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const readStream = fs.createReadStream(filePath);
|
|
|
|
resp.writeHead(200, headers);
|
|
|
|
return readStream.pipe(resp);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
routeTemplateFilePage(templatePath, preprocessCallback, resp) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
fs.readFile(templatePath, 'utf8', (err, templateData) => {
|
|
|
|
if(err) {
|
|
|
|
return self.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
preprocessCallback(templateData, (err, finalPage, contentType) => {
|
|
|
|
if(err || !finalPage) {
|
|
|
|
return self.respondWithError(resp, 500, 'Internal Server Error.', 'Internal Server Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2018-06-23 03:26:46 +00:00
|
|
|
'Content-Type' : contentType || mimeTypes.contentType('.html'),
|
|
|
|
'Content-Length' : finalPage.length,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
resp.writeHead(200, headers);
|
|
|
|
return resp.end(finalPage);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-02-04 16:20:36 +00:00
|
|
|
};
|