diff --git a/CHANGELOG.md b/CHANGELOG.md index f3428c3d..45528ab2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [2.1.3] - Unreleased ### Changed + ### Added +- Add `clientonly` script to start only the electron client for a remote server. - Add symbol and color properties of event when `CALENDAR_EVENTS` notification is broadcasted from `default/calendar` module. + ### Updated + ### Fixed - Fixed issue with incorrect allignment of analog clock when displayed in the center column of the MM diff --git a/README.md b/README.md index 8b0c66be..b7dea0fb 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,11 @@ bash -c "$(curl -sL https://raw.githubusercontent.com/MichMich/MagicMirror/maste ### Server Only In some cases, you want to start the application without an actual app window. In this case, you can start MagicMirror² in server only mode by manually running `node serveronly` or using Docker. This will start the server, after which you can open the application in your browser of choice. Detailed description below. +### Client Only +When you have a server running remotely and want to connect a standalone client to this instance, you can manually run `node clientonly --address 192.168.1.5 --port 8080`. (Specify the ip address and port number of the server) + +**Important:** Make sure that you whitelist the interface/ip in the server config where you want the client to connect to, otherwise it will not be allowed to connect to the server + #### Docker MagicMirror² in server only mode can be deployed using [Docker](https://docker.com). After a successful [Docker installation](https://docs.docker.com/engine/installation/) you just need to execute the following command in the shell: diff --git a/clientonly/index.js b/clientonly/index.js new file mode 100644 index 00000000..750a98e6 --- /dev/null +++ b/clientonly/index.js @@ -0,0 +1,97 @@ +/* jshint esversion: 6 */ + +"use strict"; + +// Use seperate scope to prevent global scope pollution +(function () { + var config = {}; + + // Helper function to get server address/hostname from either the commandline or env + function getServerAddress() { + // Helper function to get command line parameters + // Assumes that a cmdline parameter is defined with `--key [value]` + function getCommandLineParameter(key, defaultValue = undefined) { + var index = process.argv.indexOf(`--${key}`); + var value = index > -1 ? process.argv[index + 1] : undefined; + return value !== undefined ? String(value) : defaultValue; + } + + // Prefer command line arguments over environment variables + ["address", "port"].forEach((key) => { + config[key] = getCommandLineParameter(key, process.env[key.toUpperCase()]); + }) + } + + function getServerConfig(url) { + // Return new pending promise + return new Promise((resolve, reject) => { + // Select http or https module, depending on reqested url + const lib = url.startsWith("https") ? require("https") : require("http"); + const request = lib.get(url, (response) => { + var configData = ""; + + // Gather incomming data + response.on("data", function(chunk) { + configData += chunk; + }); + // Resolve promise at the end of the HTTP/HTTPS stream + response.on("end", function() { + resolve(JSON.parse(configData)); + }); + }); + + request.on("error", function(error) { + reject(new Error(`Unable to read config from server (${url} (${error.message}`)); + }); + }) + }; + + function fail(message, code = 1) { + if (message !== undefined && typeof message === "string") { + console.log(message); + } else { + console.log("Usage: 'node clientonly --address 192.168.1.10 --port 8080'"); + } + process.exit(code); + } + + getServerAddress(); + + (config.address && config.port) || fail(); + + // Only start the client if a non-local server was provided + if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) { + getServerConfig(`http://${config.address}:${config.port}/config/`) + .then(function (config) { + // Pass along the server config via an environment variable + var env = Object.create(process.env); + var options = { env: env }; + config.address = config.address; + config.port = config.port; + env.config = JSON.stringify(config); + + // Spawn electron application + const electron = require("electron"); + const child = require("child_process").spawn(electron, ["js/electron.js"], options); + + // Pipe all child process output to current stdout + child.stdout.on("data", function (buf) { + process.stdout.write(`Client: ${buf}`); + }); + + // Pipe all child process errors to current stderr + child.stderr.on("data", function (buf) { + process.stderr.write(`Client: ${buf}`); + }); + + child.on("error", function (err) { + process.stdout.write(`Client: ${err}`); + }); + }) + .catch(function (reason) { + fail(`Unable to connect to server: (${reason})`); + }); + } else { + fail(); + } +}()); \ No newline at end of file diff --git a/config/config.js.sample b/config/config.js.sample index b2eeee8a..8294e319 100644 --- a/config/config.js.sample +++ b/config/config.js.sample @@ -9,6 +9,7 @@ */ var config = { + address: "localhost", port: 8080, ipWhitelist: ["127.0.0.1", "::ffff:127.0.0.1", "::1"], // Set [] to allow all IP addresses // or add a specific IPv4 of 192.168.1.5 : diff --git a/js/defaults.js b/js/defaults.js index eada87a4..08c4d945 100644 --- a/js/defaults.js +++ b/js/defaults.js @@ -8,10 +8,12 @@ */ var port = 8080; +var address = "localhost"; if (typeof(mmPort) !== "undefined") { port = mmPort; } var defaults = { + address: address, port: port, kioskmode: false, electronOptions: {}, diff --git a/js/electron.js b/js/electron.js index 334a3593..d55f17a0 100644 --- a/js/electron.js +++ b/js/electron.js @@ -6,7 +6,7 @@ const electron = require("electron"); const core = require(__dirname + "/app.js"); // Config -var config = {}; +var config = process.env.config ? JSON.parse(process.env.config) : {}; // Module to control application life. const app = electron.app; // Module to create native browser window. @@ -47,7 +47,7 @@ function createWindow() { // and load the index.html of the app. //mainWindow.loadURL('file://' + __dirname + '../../index.html'); - mainWindow.loadURL("http://localhost:" + config.port); + mainWindow.loadURL(`http://${config.address}:${config.port}`); // Open the DevTools if run with "npm start dev" if (process.argv.includes("dev")) { @@ -96,8 +96,10 @@ app.on("activate", function() { } }); -// Start the core application. +// Start the core application if server is run on localhost // This starts all node helpers and starts the webserver. -core.start(function(c) { - config = c; -}); +if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) > -1) { + core.start(function (c) { + config = c; + }); +} \ No newline at end of file diff --git a/js/server.js b/js/server.js index 002c2031..8520e392 100644 --- a/js/server.js +++ b/js/server.js @@ -52,6 +52,10 @@ var Server = function(config, callback) { res.send(global.version); }); + app.get("/config", function(req,res) { + res.send(config); + }); + app.get("/", function(req, res) { var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), {encoding: "utf8"}); html = html.replace("#VERSION#", global.version);