setup and apxtri.js for setup

This commit is contained in:
philc 2024-11-08 10:28:12 +01:00
parent b81e71d978
commit 115c1c69c1

View File

@ -34,13 +34,16 @@ apxtri.main = async () => {
`../adminapi/objects/tribes/idx/tribes_dns.json` `../adminapi/objects/tribes/idx/tribes_dns.json`
); );
//check if new tribe was add //check if new tribe was add
glob.sync("../*").filter(f => fs.lstatSync(f).isDirectory()).forEach(async (t) => { glob
const tribe = path.basename(t); .sync("../*")
console.log(tribe); .filter((f) => fs.lstatSync(f).isDirectory())
if (!tribesdns[tribe]) { .forEach(async (t) => {
await apxtri.setuptribe(tribe, conf); const tribe = path.basename(t);
} console.log(tribe);
}); if (!tribesdns[tribe]) {
await apxtri.setuptribe(tribe, conf);
}
});
} else { } else {
const initconf = fs.readJsonSync( const initconf = fs.readJsonSync(
"../adminapi/apxtri/setup/initadminapi.json" "../adminapi/apxtri/setup/initadminapi.json"
@ -53,47 +56,33 @@ apxtri.main = async () => {
fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json") fs.readJSONSync("../adminapi/objects/tribes/itm/adminapi.json")
); );
}; };
apxtri.getip = async () => {
apxtri.setuptribe = async (tribe, conf) => { /**
/** const tribedata={ * Return json with public IP and network interfaces with their local IP
tribeId: tribe, * {WANIP:"public IP","eth0":"localIPoneth0",...}
dns: [`${tribe}.${conf.townId}`], */
status: "unchain", const network = {};
nationId: conf.nationId, const urlgetip = `https://api.ipify.org?format=json`;
townId: conf.townId, const getdata = await fetch(urlgetip);
activelog: [] if (getdata.ok) {
} const data = await getdata.json();
// must generate nginx conf based on wwwws if no wwws then create a admin{tribe} network.WANIP = data.ip;
fs.outputJSONSync(`../adminapi/tribes/itm/${tribe}.json`,tribedata,{space:2}) }
tribesdns[tribe]=tribedata const os = await import("os");
fs.outputJSONSync(`../adminapi/objects/tribes/idx/tribes_dns.json`,tribesdns,{space:2}) const interfaces = os.networkInterfaces();
//create a nginx conf to access tribe.townId */ for (const name in interfaces) {
const getip = async () => { for (const iface of interfaces[name]) {
/** // Check for IPv4 and make sure it's not an internal (loopback) address
* Return json with public IP and network interfaces with their local IP if (iface.family === "IPv4" && !iface.internal) {
* {WANIP:"public IP","eth0":"localIPoneth0",...} network[name] = iface.address;
*/ //localIP = iface.address;
const network = {}; //console.log(`Local network IP (${name}): ${localIP}`);
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; let inittribe;
if (tribe == "adminapi") { if (tribe == "adminapi") {
console.log( console.log(
@ -105,7 +94,9 @@ apxtri.setuptribe = async (tribe, conf) => {
const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-"); const townnation = inittribe.townpath.split("/").slice(-1)[0].split("-");
inittribe.townId = townnation[0]; inittribe.townId = townnation[0];
inittribe.nationId = townnation[1]; inittribe.nationId = townnation[1];
inittribe.dns.push(`admin.adminapi.${inittribe.townId}.${inittribe.nationId}`); inittribe.dns.push(
`admin.adminapi.${inittribe.townId}.${inittribe.nationId}`
);
} catch (err) { } catch (err) {
console.log("Your town folder must be something townid-nation"); 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 // add hosts entry for local access
// this command is ran by the setup.sh // 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 // 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 const nginxrestart = inittribe.nginx.restart
? inittribe.nginx.restart ? inittribe.nginx.restart
: fs.readJSONSync("../adminapi/apxtri/setup/initadminapi.json").nginx : fs.readJSONSync("../adminapi/apxtri/setup/initadminapi.json").nginx
@ -207,10 +198,12 @@ apxtri.setuptribe = async (tribe, conf) => {
process.exit(0); process.exit(0);
} else { } else {
const etchosts = Object.values(ips) const etchosts = Object.values(ips)
.map((ip) => `${ip} ${inittribe.website}`) .map((ip) => `${ip} ${inittribe.dns.join(" ")}`)
.join("\n "); .join("\n ");
console.log( 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.use(express.json(conf.api.json));
app.disable("x-powered-by"); // for security app.disable("x-powered-by"); // for security
app.locals.tribeids = tribeIds; app.locals.tribeids = tribeIds;
const currentmod = "Odmdb"; const currentmod = "apxtri";
const log = conf.api.activelog const log = conf.api.activelog
? conf.api.activelog.includes(currentmod) ? conf.api.activelog.includes(currentmod)
: false; : false;
console.log( console.log(
currentmod, currentmod,
" Allowed DOMs to access to this apxtri server:", " Allowed DOMs to access to this apxtri server:",
@ -329,15 +321,19 @@ apxtri.runexpress = async (tribesdns, conf) => {
} }
//Listen event file for each tribe //Listen event file for each tribe
// @TODO à ajouter ici // @TODO à ajouter ici
const ips = await apxtri.getip();
app.listen(conf.api.port, () => { 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) => { conf.dns.forEach((u) => {
webaccess += `http://${u}/api/ `; webaccess += `http://${u}/api/ `;
localhosts+= `${ips.WAN} ${u} \n`;
}); });
console.log(currentmod, webaccess); console.log(currentmod, webaccess);
}); });
const localhosts=
console.log( 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( console.log(
"\x1b[42m\x1b[37m \n", "\x1b[42m\x1b[37m \n",