mirror of
https://github.com/dec0dOS/zero-ui.git
synced 2025-08-19 21:03:56 -07:00
feat: first version actually spawning ZeroNSd
This commit is contained in:
parent
60a307f562
commit
5ce0bd1009
2 changed files with 79 additions and 0 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
64
backend/utils/zns.js
Normal file
64
backend/utils/zns.js
Normal file
|
@ -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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue