From 6406d3216523780fcc3b63e5ed47d5a953ec761d Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 18 Feb 2017 09:56:23 -0700 Subject: [PATCH] Handle (default) case of web server being disabled in file areas/web link generation --- core/enig_error.js | 1 + core/file_area_web.js | 34 +++++++++++++++++++++++++++------- core/servers/content/web.js | 8 ++++++-- mods/file_area_list.js | 7 ++++++- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/core/enig_error.js b/core/enig_error.js index 27453227..3fcff3bc 100644 --- a/core/enig_error.js +++ b/core/enig_error.js @@ -37,4 +37,5 @@ exports.ErrorReasons = { InvalidNextMenu : 'BADNEXT', NoPreviousMenu : 'NOPREV', NoConditionMatch : 'NOCONDMATCH', + NotEnabled : 'NOTENABLED', }; \ No newline at end of file diff --git a/core/file_area_web.js b/core/file_area_web.js index 4cffa69c..b610fd06 100644 --- a/core/file_area_web.js +++ b/core/file_area_web.js @@ -8,6 +8,7 @@ const getISOTimestampString = require('./database.js').getISOTimestampString; const FileEntry = require('./file_entry.js'); const getServer = require('./listening_server.js').getServer; const Errors = require('./enig_error.js').Errors; +const ErrNotEnabled = require('./enig_error.js').ErrorReasons.NotEnabled; // deps const hashids = require('hashids'); @@ -27,6 +28,10 @@ const WEB_SERVER_PACKAGE_NAME = 'codes.l33t.enigma.web.server'; * */ +function notEnabledError() { + return Errors.General('Web server is not enabled', ErrNotEnabled); +} + class FileAreaWebAccess { constructor() { this.hashids = new hashids(Config.general.boardName); @@ -46,14 +51,17 @@ class FileAreaWebAccess { if(!self.webServer) { return callback(Errors.DoesNotExist(`Server with package name "${WEB_SERVER_PACKAGE_NAME}" does not exist`)); } - - const routeAdded = self.webServer.instance.addRoute({ - method : 'GET', - path : Config.fileBase.web.routePath, - handler : self.routeWebRequestForFile.bind(self), - }); - return callback(routeAdded ? null : Errors.General('Failed adding route')); + if(self.isEnabled()) { + const routeAdded = self.webServer.instance.addRoute({ + method : 'GET', + path : Config.fileBase.web.routePath, + handler : self.routeWebRequestForFile.bind(self), + }); + return callback(routeAdded ? null : Errors.General('Failed adding route')); + } else { + return callback(null); // not enabled, but no error + } } ], err => { @@ -66,6 +74,10 @@ class FileAreaWebAccess { return cb(null); } + isEnabled() { + return this.webServer.instance.isEnabled(); + } + load(cb) { // // Load entries, register expiration timers @@ -187,6 +199,10 @@ class FileAreaWebAccess { } getExistingTempDownloadServeItem(client, fileEntry, cb) { + if(!this.isEnabled()) { + return cb(notEnabledError()); + } + const hashId = this.getHashId(client, fileEntry); this.loadServedHashId(hashId, (err, servedItem) => { if(err) { @@ -200,6 +216,10 @@ class FileAreaWebAccess { } createAndServeTempDownload(client, fileEntry, options, cb) { + if(!this.isEnabled()) { + return cb(notEnabledError()); + } + const hashId = this.getHashId(client, fileEntry); const url = this.buildTempDownloadLink(client, fileEntry, hashId); options.expireTime = options.expireTime || moment().add(2, 'days'); diff --git a/core/servers/content/web.js b/core/servers/content/web.js index 11394659..8970aa86 100644 --- a/core/servers/content/web.js +++ b/core/servers/content/web.js @@ -53,12 +53,12 @@ exports.getModule = class WebServerModule extends ServerModule { constructor() { super(); - this.enableHttp = Config.contentServers.web.http.enabled || true; + this.enableHttp = Config.contentServers.web.http.enabled || false; this.enableHttps = Config.contentServers.web.https.enabled || false; this.routes = {}; - if(Config.contentServers.web.staticRoot) { + if(this.isEnabled() && Config.contentServers.web.staticRoot) { this.addRoute({ method : 'GET', path : '/static/.*$', @@ -67,6 +67,10 @@ exports.getModule = class WebServerModule extends ServerModule { } } + isEnabled() { + return this.enableHttp || this.enableHttps; + } + createServer() { if(this.enableHttp) { this.httpServer = http.createServer( (req, resp) => this.routeRequest(req, resp) ); diff --git a/mods/file_area_list.js b/mods/file_area_list.js index aa661a5e..88163b0e 100644 --- a/mods/file_area_list.js +++ b/mods/file_area_list.js @@ -11,6 +11,7 @@ const stringFormat = require('../core/string_format.js'); const createCleanAnsi = require('../core/string_util.js').createCleanAnsi; const FileArea = require('../core/file_base_area.js'); const Errors = require('../core/enig_error.js').Errors; +const ErrNotEnabled = require('../core/enig_error.js').ErrorReasons.NotEnabled; const ArchiveUtil = require('../core/archive_util.js'); const Config = require('../core/config.js').config; const DownloadQueue = require('../core/download_queue.js'); @@ -256,8 +257,12 @@ exports.getModule = class FileAreaList extends MenuModule { FileAreaWeb.getExistingTempDownloadServeItem(this.client, this.currentFileEntry, (err, serveItem) => { if(err) { - entryInfo.webDlLink = config.webDlLinkNeedsGenerated || 'Not yet generated'; entryInfo.webDlExpire = ''; + if(ErrNotEnabled === err.reasonCode) { + entryInfo.webDlExpire = config.webDlLinkNoWebserver || 'Web server is not enabled'; + } else { + entryInfo.webDlLink = config.webDlLinkNeedsGenerated || 'Not yet generated'; + } } else { const webDlExpireTimeFormat = config.webDlExpireTimeFormat || 'YYYY-MMM-DD @ h:mm';