diff --git a/src/lib/core/config.js b/src/lib/core/config.js index 020c62ca8..122e545e4 100644 --- a/src/lib/core/config.js +++ b/src/lib/core/config.js @@ -467,13 +467,25 @@ Config.prototype.loadWebServerConfigFile = function() { "host": defaultHost, "openBrowser": true, "port": 8000, - "enableCatchAll": true + "enableCatchAll": true, + "protocol": "http" }; let configFilePath = this._getFileOrOject(this.configDir, 'webserver', 'webserver'); let webServerConfig = this._mergeConfig(configFilePath, configObject, false); + if (webServerConfig.https){ + try { + webServerConfig.certOptions = { + key: fs.readFileSync(webServerConfig.key), + cert: fs.readFileSync(webServerConfig.cert) + }; + webServerConfig.protocol = 'https'; + } catch (e) { + webServerConfig.protocol = 'http'; + } + } if (configFilePath === false) { this.webServerConfig = {enabled: false}; return; diff --git a/src/lib/modules/code_generator/code/reload-on-change.js b/src/lib/modules/code_generator/code/reload-on-change.js index 831616895..a8974d6af 100644 --- a/src/lib/modules/code_generator/code/reload-on-change.js +++ b/src/lib/modules/code_generator/code/reload-on-change.js @@ -1,4 +1,4 @@ -const ws = new WebSocket(`ws://${location.hostname}:${location.port}`); +const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${location.port}`); ws.addEventListener('message', (evt) => { if(evt.data === 'outputDone') { location.reload(true); diff --git a/src/lib/modules/webserver/index.js b/src/lib/modules/webserver/index.js index 00710f430..d023bcd37 100644 --- a/src/lib/modules/webserver/index.js +++ b/src/lib/modules/webserver/index.js @@ -40,7 +40,9 @@ class WebServer { host: this.host, port: this.port, plugins: this.plugins, - openBrowser: this.webServerConfig.openBrowser + openBrowser: this.webServerConfig.openBrowser, + protocol: this.webServerConfig.protocol, + certOptions : this.webServerConfig.certOptions }); this.listenToCommands(); @@ -54,6 +56,8 @@ class WebServer { this.port = this.webServerConfig.port; this.server.host = this.host; this.server.port = this.port; + this.server.protocol = this.webServerConfig.protocol; + this.server.certOptions = this.webServerConfig.certOptions; this.testPort(() => { this.events.request('processes:stop', 'webserver', _err => { @@ -81,7 +85,7 @@ class WebServer { this.server.port = 0; return done(); } - utils.pingEndpoint(this.host, this.port, 'http', 'http', '', (err) => { + utils.pingEndpoint(this.host, this.port, this.protocol, this.protocol, '', (err) => { if (err) { // Port is ok return done(); } @@ -96,7 +100,7 @@ class WebServer { const self = this; this.events.request("services:register", 'Webserver', function (cb) { - let url = 'http://' + canonicalHost(self.host) + ':' + self.port; + let url = this.protocol + '://' + canonicalHost(self.host) + ':' + self.port; utils.checkIsAvailable(url, function (available) { let devServer = __('Webserver') + ' (' + url + ')'; let serverStatus = (available ? 'on' : 'off'); @@ -165,7 +169,7 @@ class WebServer { openBrowser(cb) { const _cb = () => { cb(); }; return opn( - `http://${canonicalHost(this.server.hostname)}:${this.server.port}`, + `${this.protocol}://${canonicalHost(this.server.hostname)}:${this.server.port}`, {wait: false} ).then(_cb, _cb); // fail silently, e.g. in a docker container } diff --git a/src/lib/modules/webserver/server.js b/src/lib/modules/webserver/server.js index 7991eac62..2e6c4ada1 100644 --- a/src/lib/modules/webserver/server.js +++ b/src/lib/modules/webserver/server.js @@ -4,6 +4,7 @@ const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host') const expressWebSocket = require('express-ws'); const express = require('express'); const fs = require('../../core/fs'); +const https = require('https'); var cors = require('cors'); let path = require('path'); var bodyParser = require('body-parser'); @@ -24,6 +25,9 @@ class Server { this.plugins = options.plugins; this.enableCatchAll = options.enableCatchAll; + this.protocol = options.protocol || 'http'; + this.certOptions = options.certOptions; + this.events.once('outputDone', () => { this.logger.info(this._getMessage()); }); @@ -54,7 +58,9 @@ class Server { const main = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']}); this.app = express(); - const expressWs = expressWebSocket(this.app); + this.secureServer = this.protocol === 'https' ? https.createServer(self.certOptions, (req, res) => self.app.handle(req, res)) : null; + const expressWs = this.protocol === 'https' ? expressWebSocket(this.app, this.secureServer) : expressWebSocket(this.app); + // Assign Logging Function this.app.use(function(req, res, next) { if (self.logging) { @@ -136,10 +142,18 @@ class Server { self.events.request('build-placeholder', next); }, function listen(next) { - self.server = self.app.listen(self.port, self.hostname, () => { - self.port = self.server.address().port; - next(); - }); + if (self.protocol === 'https'){ + self.server = self.secureServer.listen(self.port, self.hostname, () => { + self.port = self.secureServer.address().port; + next(); + }); + } + else{ + self.server = self.app.listen(self.port, self.hostname, () => { + self.port = self.server.address().port; + next(); + }); + } }, function openBrowser(next) { if (!self.openBrowser || self.opened) { @@ -159,7 +173,7 @@ class Server { _getMessage() { return __('webserver available at') + ' ' + - ('http://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green; + (this.protocol + '://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green; } applyAPIFunction(cb, req, res) { diff --git a/src/lib/modules/webserver/templates/embark-building-placeholder.html.ejs b/src/lib/modules/webserver/templates/embark-building-placeholder.html.ejs index 4e77c6917..bf5d30bd9 100644 --- a/src/lib/modules/webserver/templates/embark-building-placeholder.html.ejs +++ b/src/lib/modules/webserver/templates/embark-building-placeholder.html.ejs @@ -26,7 +26,7 @@