From 5ce0bd1009e3e4a3e95121449fae3c6ab78b4c66 Mon Sep 17 00:00:00 2001 From: Mauro Condarelli Date: Fri, 10 Sep 2021 18:08:17 +0200 Subject: [PATCH] feat: first version actually spawning ZeroNSd --- backend/services/network.js | 15 +++++++++ backend/utils/zns.js | 64 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 backend/utils/zns.js diff --git a/backend/services/network.js b/backend/services/network.js index da1f64a..228214a 100644 --- a/backend/services/network.js +++ b/backend/services/network.js @@ -4,6 +4,7 @@ const axios = require("axios"); const api = require("../utils/controller-api"); const db = require("../utils/db"); const constants = require("../utils/constants"); +const startStopDNS = require("../utils/zns"); async function getNetworkAdditionalData(data) { let additionalData = db @@ -98,6 +99,20 @@ async function updateNetworkAdditionalData(nwid, data) { .map("additionalConfig") .map((additionalConfig) => _.assign(additionalConfig, additionalData)) .write(); + + if (data.hasOwnProperty("dnsEnable")) { + let token = db.get("users").value()[0]; + token = token.token; + let config = db.get("networks").find({ id: nwid }).value(); + let ret = startStopDNS(token, config.id, config.additionalConfig); + if (ret) { + db.get("networks") + .filter({ id: nwid }) + .map("additionalConfig") + .map((additionalConfig) => _.assign(additionalConfig, ret)) + .write(); + } + } } } diff --git a/backend/utils/zns.js b/backend/utils/zns.js new file mode 100644 index 0000000..9982f43 --- /dev/null +++ b/backend/utils/zns.js @@ -0,0 +1,64 @@ +const { spawn } = require("child_process"); + +function pidIsRunning(pid) { + try { + process.kill(pid, 0); + return true; + } catch (e) { + return false; + } +} + +function startDNS(token, nwid, conf) { + if (conf.hasOwnProperty("pidDNS")) { + if (pidIsRunning(conf.pidDNS)) { + return null; + } + delete conf.pidDNS; + } + const opts = Array("start"); + if (conf.hasOwnProperty("dnsWildcard") && conf.dnsWildcard) { + opts.push("-w"); + } + if (conf.hasOwnProperty("dnsDomain") && !!conf.dnsDomain) { + opts.push("-d", conf.dnsDomain); + } + opts.push(nwid); + let env = process.env; + env.ZEROTIER_CENTRAL_TOKEN = token; + console.log(`*** PATH: "${env.PATH}"`); + const dns = spawn("zeronsd", opts, { + detached: true, + env: env, + }); + dns.on("spawn", () => { + console.log(`zeronsd successfully spawned [${dns.pid}](${dns.spawnargs})`); + console.log("*** should update PID ***"); + }); + dns.on("error", (error) => { + console.log(`zeronsd spawn ERROR: [${error}](${dns.spawnargs})`); + }); + conf.pidDNS = dns.pid; + return conf; +} + +function stopDNS(conf) { + if (conf.hasOwnProperty("pidDNS")) { + try { + process.kill(conf.pidDNS); + } catch (e) {} + delete conf.pidDNS; + return conf; + } + return null; +} + +module.exports = function (token, nwid, conf) { + let ret; + if (conf.dnsEnable) { + ret = startDNS(token, nwid, conf); + } else { + ret = stopDNS(conf); + } + return ret; +};