From 0af5aa32d719ed8d45b8aa7beafcd0c7f2fba4e8 Mon Sep 17 00:00:00 2001 From: philc Date: Sat, 2 Nov 2024 12:07:13 +0100 Subject: [PATCH] setup fix --- apxtri/apxtri.js | 31 +++++++++++++++++++++++++++++-- apxtri/setup/setup.sh | 3 ++- package.json | 1 + 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/apxtri/apxtri.js b/apxtri/apxtri.js index f036979..7d15ad1 100755 --- a/apxtri/apxtri.js +++ b/apxtri/apxtri.js @@ -36,6 +36,32 @@ apxtri.main = async () => { }; apxtri.setup = async () => { + 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}`); + } + } + } + return network + }; console.log("Warning, this is a first install"); const initadminapi = fs.readJsonSync( "../adminapi/apxtri/setup/initadminapi.json" @@ -157,7 +183,8 @@ apxtri.setup = async () => { ); // add hosts entry for local access // this command is ran by the setup.sh - // sudo sed -i '/127.0.0.1 adminapx.adminapi/c\127.0.0.1 adminapx.adminapi' /etc/hosts + // 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 { exec } = require("child_process"); exec(initadminapi.nginx.restart, (error, stdout, stderr) => { if (error) { @@ -165,7 +192,7 @@ apxtri.setup = async () => { process.exit(0); } else { console.log( - `\x1b[42m##############################################################################\x1b[0m\n\x1b[42mWellcome into apxtri, you can now 'yarn dev' for dev or 'yarn startapx' for prod \n'Access to your town here \x1b[0m\x1b[32mhttp://adminapx.adminapî\x1b[0m \x1b[42m \n\x1b[0m\n\x1b[42m###########################################################################################\x1b[0m` + `\x1b[42m#############################################################################################\x1b[0m\n\x1b[42mWellcome into this fresh apxtri install, this run as "yarn dev" to set up your machine with a public domain name.\n If this install is to be use accross internet or local network : add on yopur local machine in /etc/hosts lines:\n ${Object.values(ips).map(ip => `${ip} adminapx.adminapi`).join('\n')} \n then open in your local browser \x1b[0m\x1b[32mhttp://adminapx.adminapi\x1b[0m \x1b[42m \n In local dev continue with 'yarn dev'\nTo run as a production run 'yarn startapx' or 'yarn restartapx' this will use pm2 as production monitoring\n\x1b[0m\n\x1b[42m###########################################################################################\x1b[0m` ); } }); diff --git a/apxtri/setup/setup.sh b/apxtri/setup/setup.sh index 981a98d..5f00c98 100755 --- a/apxtri/setup/setup.sh +++ b/apxtri/setup/setup.sh @@ -59,7 +59,8 @@ else mkdir -p "${tribe}/logs" mkdir -p "${tribe}/logs/nginx" mkdir -p "${tribe}/backups" - sudo sed -i '/127.0.0.1 adminapx.adminapi/c\127.0.0.1 adminapx.adminapi\n' /etc/hosts + # add 127.0.0.1 local host + grep -q '^127.0.0.1 adminapx.adminapi' /etc/hosts || echo '127.0.0.1 adminapx.adminapi' | sudo tee -a /etc/hosts > /dev/null cd "$tribe" yarn install cd ../adminapi diff --git a/package.json b/package.json index 6bf860f..d6bdfc3 100755 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ }, "description": "apxtri Decentralized Autonomous Organization", "main": "./apxtri/apxtri.js", + "type":"module", "repository": { "url": "https://gitea.ndda.fr/apxtri/adminapi.git", "type": "git"