From 115c1c69c109e385cf32d4801ab44146dee02e64 Mon Sep 17 00:00:00 2001 From: philc Date: Fri, 8 Nov 2024 10:28:12 +0100 Subject: [PATCH] setup and apxtri.js for setup --- apxtri/apxtri.js | 104 +++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/apxtri/apxtri.js b/apxtri/apxtri.js index 9477ee3..9ebed3e 100755 --- a/apxtri/apxtri.js +++ b/apxtri/apxtri.js @@ -34,13 +34,16 @@ apxtri.main = async () => { `../adminapi/objects/tribes/idx/tribes_dns.json` ); //check if new tribe was add - glob.sync("../*").filter(f => fs.lstatSync(f).isDirectory()).forEach(async (t) => { - const tribe = path.basename(t); - console.log(tribe); - if (!tribesdns[tribe]) { - await apxtri.setuptribe(tribe, conf); - } - }); + glob + .sync("../*") + .filter((f) => fs.lstatSync(f).isDirectory()) + .forEach(async (t) => { + const tribe = path.basename(t); + console.log(tribe); + if (!tribesdns[tribe]) { + await apxtri.setuptribe(tribe, conf); + } + }); } else { const initconf = fs.readJsonSync( "../adminapi/apxtri/setup/initadminapi.json" @@ -53,47 +56,33 @@ apxtri.main = async () => { fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") ); }; - -apxtri.setuptribe = async (tribe, conf) => { - /** const tribedata={ - tribeId: tribe, - dns: [`${tribe}.${conf.townId}`], - status: "unchain", - nationId: conf.nationId, - townId: conf.townId, - activelog: [] - } - // must generate nginx conf based on wwwws if no wwws then create a admin{tribe} - fs.outputJSONSync(`../adminapi/tribes/itm/${tribe}.json`,tribedata,{space:2}) - tribesdns[tribe]=tribedata - fs.outputJSONSync(`../adminapi/objects/tribes/idx/tribes_dns.json`,tribesdns,{space:2}) - //create a nginx conf to access tribe.townId */ - const getip = async () => { - /** - * Return json with public IP and network interfaces with their local IP - * {WANIP:"public IP","eth0":"localIPoneth0",...} - */ - const network = {}; - const urlgetip = `https://api.ipify.org?format=json`; - const getdata = await fetch(urlgetip); - if (getdata.ok) { - const data = await getdata.json(); - network.WANIP = data.ip; - } - const os = await import("os"); - const interfaces = os.networkInterfaces(); - for (const name in interfaces) { - for (const iface of interfaces[name]) { - // Check for IPv4 and make sure it's not an internal (loopback) address - if (iface.family === "IPv4" && !iface.internal) { - network[name] = iface.address; - //localIP = iface.address; - //console.log(`Local network IP (${name}): ${localIP}`); - } +apxtri.getip = async () => { + /** + * Return json with public IP and network interfaces with their local IP + * {WANIP:"public IP","eth0":"localIPoneth0",...} + */ + const network = {}; + const urlgetip = `https://api.ipify.org?format=json`; + const getdata = await fetch(urlgetip); + if (getdata.ok) { + const data = await getdata.json(); + network.WANIP = data.ip; + } + const os = await import("os"); + const interfaces = os.networkInterfaces(); + for (const name in interfaces) { + for (const iface of interfaces[name]) { + // Check for IPv4 and make sure it's not an internal (loopback) address + if (iface.family === "IPv4" && !iface.internal) { + network[name] = iface.address; + //localIP = iface.address; + //console.log(`Local network IP (${name}): ${localIP}`); } } - return network; - }; + } + return network; +}; +apxtri.setuptribe = async (tribe, conf) => { let inittribe; if (tribe == "adminapi") { console.log( @@ -105,7 +94,9 @@ apxtri.setuptribe = async (tribe, conf) => { const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-"); inittribe.townId = townnation[0]; inittribe.nationId = townnation[1]; - inittribe.dns.push(`admin.adminapi.${inittribe.townId}.${inittribe.nationId}`); + inittribe.dns.push( + `admin.adminapi.${inittribe.townId}.${inittribe.nationId}` + ); } catch (err) { console.log("Your town folder must be something townid-nation"); } @@ -195,7 +186,7 @@ apxtri.setuptribe = async (tribe, conf) => { // add hosts entry for local access // this command is ran by the setup.sh // grep -q '^127.0.0.1 adminapx.adminapi' /etc/hosts || echo '127.0.0.1 adminapx.adminapi' | sudo tee -a /etc/hosts > /dev/null - const ips = await getip(); + const ips = await apxtri.getip(); const nginxrestart = inittribe.nginx.restart ? inittribe.nginx.restart : fs.readJSONSync("../adminapi/apxtri/setup/initadminapi.json").nginx @@ -207,10 +198,12 @@ apxtri.setuptribe = async (tribe, conf) => { process.exit(0); } else { const etchosts = Object.values(ips) - .map((ip) => `${ip} ${inittribe.website}`) + .map((ip) => `${ip} ${inittribe.dns.join(" ")}`) .join("\n "); console.log( - `\x1b[42m###############################################################################################################\x1b[0m\n\x1b[42mWellcome into this fresh apxtri install, currently running as "$ yarn dev".\n To access and set up a public domain name, add lines in file /etc/hosts into the machine you want to use to access:\n ${etchosts} \n Then open in your local browser \x1b[0m\x1b[32m http://${inittribe.website} \x1b[0m \x1b[42m \nFor local dev continue with 'yarn dev'\nTo run as a production run 'yarn startapx' or 'yarn restartapx' then pm2 monitor your production process.\n\x1b[0m\n\x1b[42m###############################################################################################################\x1b[0m` + `\x1b[42m###############################################################################################################\x1b[0m\n\x1b[42mWellcome into this fresh apxtri install, currently running as "$ yarn dev".\n To access and set up a public domain name, add lines in file /etc/hosts into the machine you want to use to access:\n\x1b[0m\x1b[32m ${etchosts} \x1b[0m \x1b[42m\n Then open in your local browser \x1b[0m\x1b[32m http:// ${inittribe.dns.join( + " " + )} \x1b[0m \x1b[42m \nFor local dev continue with 'yarn dev'\nTo run as a production run 'yarn startapx' or 'yarn restartapx' then pm2 monitor your production process.\n\x1b[0m\n\x1b[42m###############################################################################################################\x1b[0m` ); } }); @@ -271,11 +264,10 @@ apxtri.runexpress = async (tribesdns, conf) => { app.use(express.json(conf.api.json)); app.disable("x-powered-by"); // for security app.locals.tribeids = tribeIds; - const currentmod = "Odmdb"; + const currentmod = "apxtri"; const log = conf.api.activelog ? conf.api.activelog.includes(currentmod) : false; - console.log( currentmod, " Allowed DOMs to access to this apxtri server:", @@ -329,15 +321,19 @@ apxtri.runexpress = async (tribesdns, conf) => { } //Listen event file for each tribe // @TODO à ajouter ici + const ips = await apxtri.getip(); app.listen(conf.api.port, () => { - let webaccess = `api waits request on port:${conf.api.port} for `; + let webaccess = `/api/ waits request on port:${conf.api.port} `; + let localhosts = "/etc/hosts for external machine\n" conf.dns.forEach((u) => { webaccess += `http://${u}/api/ `; + localhosts+= `${ips.WAN} ${u} \n`; }); console.log(currentmod, webaccess); }); + const localhosts= console.log( - `\x1b[42mOpen in your browser http://${conf.townId}.${conf.nationId} to manage this apXtri town. \nCheck that your local machine have in /etc/hosts \n127.0.0.1 ${conf.townId}.${conf.nationId} or WANIP ${conf.townId}.${conf.nationId} \nCheck README's project to learn more.\nTo get support \x1b[0m\x1b[32mask in discord https://discord.gg/jF7cAkZn ` + `\x1b[42m\x1b[37mOpen in your browser http(s):// ${conf.dns.join(" ")} to manage this apXtri town. \nCheck that your local machine have in /etc/hosts \n${localhost}\nCheck README's project to learn more.\nTo get support ask \x1b[0m\x1b[32m in discord https://discord.gg/jF7cAkZn ` ); console.log( "\x1b[42m\x1b[37m \n",