2016-10-25 03:49:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const Log = require('../../logger.js').log;
|
|
|
|
const ServerModule = require('../../server_module.js').ServerModule;
|
|
|
|
const Config = require('../../config.js').get;
|
|
|
|
const { Errors } = require('../../enig_error.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
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');
|
2022-12-29 07:56:10 +00:00
|
|
|
const findSeries = require('async/findSeries');
|
2016-10-25 03:49:45 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const ModuleInfo = (exports.moduleInfo = {
|
|
|
|
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);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.method) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.method = this.method.toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.pathRegExp = new RegExp(this.path);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
Log.debug({ route: route }, 'Invalid regular expression for route path');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
return (
|
2022-06-05 20:04:25 +00:00
|
|
|
(this.pathRegExp instanceof RegExp &&
|
|
|
|
-1 !==
|
|
|
|
[
|
|
|
|
'GET',
|
|
|
|
'HEAD',
|
|
|
|
'POST',
|
|
|
|
'PUT',
|
|
|
|
'DELETE',
|
|
|
|
'CONNECT',
|
|
|
|
'OPTIONS',
|
|
|
|
'TRACE',
|
|
|
|
].indexOf(this.method)) ||
|
2018-06-23 03:26:46 +00:00
|
|
|
!_.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
|
|
|
|
2022-06-05 20:04:25 +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();
|
|
|
|
|
2022-06-05 20:04:25 +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 = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(config.contentServers.web.overrideUrlPrefix)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return `${config.contentServers.web.overrideUrlPrefix}${pathAndQuery}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let schema;
|
|
|
|
let port;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (config.contentServers.web.https.enabled) {
|
|
|
|
schema = 'https://';
|
|
|
|
port =
|
|
|
|
443 === config.contentServers.web.https.port
|
|
|
|
? ''
|
|
|
|
: `:${config.contentServers.web.https.port}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
schema = 'http://';
|
|
|
|
port =
|
|
|
|
80 === config.contentServers.web.http.port
|
|
|
|
? ''
|
|
|
|
: `:${config.contentServers.web.http.port}`;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${schema}${config.contentServers.web.domain}${port}${pathAndQuery}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEnabled() {
|
|
|
|
return this.enableHttp || this.enableHttps;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:19:26 +00:00
|
|
|
createServer(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.enableHttp) {
|
|
|
|
this.httpServer = http.createServer((req, resp) =>
|
|
|
|
this.routeRequest(req, resp)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.enableHttps) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const options = {
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign(options, config.contentServers.web.https.options || {});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.httpsServer = https.createServer(options, (req, resp) =>
|
|
|
|
this.routeRequest(req, resp)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
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();
|
2022-06-05 20:04:25 +00:00
|
|
|
forEachSeries(
|
|
|
|
['http', 'https'],
|
|
|
|
(service, nextService) => {
|
|
|
|
const name = `${service}Server`;
|
|
|
|
if (this[name]) {
|
|
|
|
const port = parseInt(config.contentServers.web[service].port);
|
|
|
|
if (isNaN(port)) {
|
|
|
|
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}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this[name].listen(
|
|
|
|
port,
|
|
|
|
config.contentServers.web[service].address,
|
|
|
|
err => {
|
|
|
|
return nextService(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return nextService(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addRoute(route) {
|
|
|
|
route = new Route(route);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!route.isValid()) {
|
|
|
|
Log.warn(
|
|
|
|
{ route: route },
|
|
|
|
'Cannot add route: missing or invalid required members'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const routeKey = route.getRouteKey();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (routeKey in this.routes) {
|
|
|
|
Log.warn(
|
|
|
|
{ route: route, routeKey: routeKey },
|
|
|
|
'Cannot add route: duplicate method/path combination exists'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.routes[routeKey] = route;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
routeRequest(req, resp) {
|
2022-12-29 07:56:10 +00:00
|
|
|
let route = _.find(this.routes, r => r.matchesRequest(req));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
if (route) {
|
|
|
|
return route.handler(req, resp);
|
|
|
|
} else {
|
|
|
|
this.tryStaticRoute(req, resp, wasHandled => {
|
|
|
|
if (!wasHandled) {
|
|
|
|
this.tryRouteIndex(req, resp, wasHandled => {
|
|
|
|
if (!wasHandled) {
|
|
|
|
return this.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
respondWithError(resp, code, bodyText, title) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const customErrorPage = paths.join(
|
|
|
|
Config().contentServers.web.staticRoot,
|
|
|
|
`${code}.html`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
fs.readFile(customErrorPage, 'utf8', (err, data) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
resp.writeHead(code, { 'Content-Type': 'text/html' });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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>
|
2022-06-05 20:04:25 +00:00
|
|
|
</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');
|
|
|
|
}
|
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
tryRouteIndex(req, resp, cb) {
|
|
|
|
const tryFiles = Config().contentServers.web.tryFiles || [
|
|
|
|
'index.html',
|
|
|
|
'index.htm',
|
|
|
|
];
|
|
|
|
|
|
|
|
findSeries(
|
|
|
|
tryFiles,
|
|
|
|
(tryFile, nextTryFile) => {
|
|
|
|
const fileName = paths.join(
|
|
|
|
req.url.substr(req.url.lastIndexOf('/', 1)),
|
|
|
|
tryFile
|
|
|
|
);
|
|
|
|
const filePath = this.resolveStaticPath(fileName);
|
|
|
|
|
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if (err || !stats.isFile()) {
|
|
|
|
return nextTryFile(null, false);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
const headers = {
|
|
|
|
'Content-Type':
|
|
|
|
mimeTypes.contentType(paths.basename(filePath)) ||
|
|
|
|
mimeTypes.contentType('.bin'),
|
|
|
|
'Content-Length': stats.size,
|
|
|
|
};
|
|
|
|
|
|
|
|
const readStream = fs.createReadStream(filePath);
|
|
|
|
resp.writeHead(200, headers);
|
|
|
|
readStream.pipe(resp);
|
|
|
|
|
|
|
|
return nextTryFile(null, true);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(_, wasHandled) => {
|
|
|
|
return cb(wasHandled);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 07:56:10 +00:00
|
|
|
tryStaticRoute(req, resp, cb) {
|
|
|
|
const fileName = req.url.substr(req.url.lastIndexOf('/', 1));
|
|
|
|
const filePath = this.resolveStaticPath(fileName);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-11-27 19:50:57 +00:00
|
|
|
if (!filePath) {
|
2022-12-29 07:56:10 +00:00
|
|
|
return cb(false);
|
2020-11-27 19:50:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.stat(filePath, (err, stats) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err || !stats.isFile()) {
|
2022-12-29 07:56:10 +00:00
|
|
|
return cb(false);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2022-06-05 20:04:25 +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);
|
2022-12-29 07:56:10 +00:00
|
|
|
readStream.pipe(resp);
|
|
|
|
|
|
|
|
return cb(true);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:50:57 +00:00
|
|
|
resolveStaticPath(requestPath) {
|
|
|
|
const staticRoot = _.get(Config(), 'contentServers.web.staticRoot');
|
|
|
|
const path = paths.resolve(staticRoot, `.${requestPath}`);
|
|
|
|
if (path.startsWith(staticRoot)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
routeTemplateFilePage(templatePath, preprocessCallback, resp) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
fs.readFile(templatePath, 'utf8', (err, templateData) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.fileNotFound(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
preprocessCallback(templateData, (err, finalPage, contentType) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err || !finalPage) {
|
|
|
|
return self.respondWithError(
|
|
|
|
resp,
|
|
|
|
500,
|
|
|
|
'Internal Server Error.',
|
|
|
|
'Internal Server Error'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const headers = {
|
2022-06-05 20:04:25 +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
|
|
|
};
|