Nuova gestion errori

This commit is contained in:
La Programmatrice Verde
2025-11-21 22:51:30 +01:00
parent b8db372484
commit 30832e6cce
4 changed files with 324 additions and 182 deletions

View File

@@ -15,7 +15,7 @@ Il programma restituisce come codice di errore:
`2` se viene inserito un token non valido `2` se viene inserito un token non valido
`3` se si verifica un errore in una funzione di rete `3` se il server avvisa che è stato fornito un token non valido
## Per iniziare ad usare il programma: ## Per iniziare ad usare il programma:
1- Recarsi su https://app.netbird.io/team/users 1- Recarsi su https://app.netbird.io/team/users
@@ -26,7 +26,7 @@ Il programma restituisce come codice di errore:
4- Inserire il token nella variabile d'ambiente `BASE_TOKEN` 4- Inserire il token nella variabile d'ambiente `BASE_TOKEN`
5- Avviare il programma con `node index.js` o con Docker 5- Avviare il programma con Docker
## Docker ## Docker

134
hosts.js
View File

@@ -30,6 +30,9 @@ async function Request() {
} }
async function isConnected(hostID) { async function isConnected(hostID) {
let loopError;
let errorCount = 0;
do {
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers/${hostID}`, `https://api.netbird.io/api/peers/${hostID}`,
@@ -43,16 +46,31 @@ async function isConnected(hostID) {
Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`); Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`);
return response.data.connected; return response.data.connected;
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}` `[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
}
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
} }
async function initHostList() { async function initHostList() {
let loopError;
let errorCount = 0;
if ("HOSTNAMES" in process.env) { if ("HOSTNAMES" in process.env) {
let envHostnames = process.env.HOSTNAMES; let envHostnames = process.env.HOSTNAMES;
@@ -65,6 +83,8 @@ async function initHostList() {
envHostnames = envHostnames.split(";"); envHostnames = envHostnames.split(";");
for (const host of envHostnames) { for (const host of envHostnames) {
do {
loopError = false;
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers?name=${host}`, `https://api.netbird.io/api/peers?name=${host}`,
@@ -78,21 +98,31 @@ async function initHostList() {
for (const host of response.data) { for (const host of response.data) {
hostIDs.push(host.id); hostIDs.push(host.id);
} }
/*
response.data.forEach((host) => {
hostIDs.push(host.id);
});
*/
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
} }
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
}
} else { } else {
do {
loopError = false;
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers?name=${envHostnames}`, `https://api.netbird.io/api/peers?name=${envHostnames}`,
@@ -105,14 +135,27 @@ async function initHostList() {
); );
hostIDs.push(response.data[0].id); hostIDs.push(response.data[0].id);
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
} }
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
}
} }
} else if ("HOSTIDS" in process.env) { } else if ("HOSTIDS" in process.env) {
let envHostIDs = process.env.HOSTIDS; let envHostIDs = process.env.HOSTIDS;
@@ -122,6 +165,8 @@ async function initHostList() {
envHostIDs = envHostIDs.split(";"); envHostIDs = envHostIDs.split(";");
for (const host of envHostIDs) { for (const host of envHostIDs) {
do {
loopError = false;
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers/${host}`, `https://api.netbird.io/api/peers/${host}`,
@@ -134,15 +179,30 @@ async function initHostList() {
); );
hostIDs.push(response.data.id); hostIDs.push(response.data.id);
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
} }
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
}
} else { } else {
do {
loopError = false;
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers/${envHostIDs}`, `https://api.netbird.io/api/peers/${envHostIDs}`,
@@ -155,19 +215,34 @@ async function initHostList() {
); );
hostIDs.push(response.data.id); hostIDs.push(response.data.id);
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
} }
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
}
} }
} else { } else {
Log( Log(
`[${shared.now()}] Nessun peer specificato, verranno monitorati tutti i peer disponibili` `[${shared.now()}] Nessun peer specificato, verranno monitorati tutti i peer disponibili`
); );
do {
loopError = false;
try { try {
const response = await axios.get(`https://api.netbird.io/api/peers`, { const response = await axios.get(`https://api.netbird.io/api/peers`, {
headers: { headers: {
@@ -179,30 +254,31 @@ async function initHostList() {
for (const element of response.data) { for (const element of response.data) {
hostIDs.push(element.id); hostIDs.push(element.id);
} }
/*
response.data.forEach((element) => {
hostIDs.push(element.id);
});
*/
} catch (error) { } catch (error) {
loopError = true;
Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`); Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`);
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
} }
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
}
Log(`[${shared.now()}] ID rilevati:`); Log(`[${shared.now()}] ID rilevati:`);
for (const element of hostIDs) { for (const element of hostIDs) {
Log(`[${shared.now()}] ${element}`); Log(`[${shared.now()}] ${element}`);
} }
/*
hostIDs.forEach((element) => {
Log(`[${shared.now()}] ${element}`);
});
*/
} }
module.exports = { Monitoring }; module.exports = { Monitoring };

View File

@@ -4,6 +4,7 @@ let timezone = require("dayjs/plugin/timezone");
module.exports = { module.exports = {
token: "", token: "",
codiciNonAccettati: [401, 404],
setToken(token) { setToken(token) {
this.token = token; this.token = token;
}, },

View File

@@ -24,6 +24,9 @@ async function TokenRenew() {
} }
async function getUserID() { async function getUserID() {
let loopError;
let errorCount = 0;
do {
try { try {
const response = await axios.get( const response = await axios.get(
"https://api.netbird.io/api/users/current", "https://api.netbird.io/api/users/current",
@@ -37,14 +40,31 @@ async function getUserID() {
Log(`[${shared.now()}] UserID: ${response.data.id}`); Log(`[${shared.now()}] UserID: ${response.data.id}`);
return response.data.id; return response.data.id;
} catch (error) { } catch (error) {
loopError = true;
Log(`[${shared.now()}] Errore nel reperire l'ID dell'utente corrente.`); Log(`[${shared.now()}] Errore nel reperire l'ID dell'utente corrente.`);
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
}
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
} }
async function deleteToken(tokenID) { async function deleteToken(tokenID) {
let loopError;
let errorCount = 0;
do {
loopError = false;
try { try {
await axios.delete( await axios.delete(
`https://api.netbird.io/api/users/${userID}/tokens/${tokenID}`, `https://api.netbird.io/api/users/${userID}/tokens/${tokenID}`,
@@ -56,16 +76,32 @@ async function deleteToken(tokenID) {
); );
Log(`[${shared.now()}] Token con ID ${tokenID} cancellato con successo`); Log(`[${shared.now()}] Token con ID ${tokenID} cancellato con successo`);
} catch (error) { } catch (error) {
loopError = true;
Log( Log(
`[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.` `[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.`
); );
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
}
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
} }
async function getCurrentTokenID() { async function getCurrentTokenID() {
let loopError;
let errorCount = 0;
do {
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/users/${userID}/tokens`, `https://api.netbird.io/api/users/${userID}/tokens`,
@@ -83,14 +119,30 @@ async function getCurrentTokenID() {
Log(`[${shared.now()}] CurrentTokenID: ${token.id}`); Log(`[${shared.now()}] CurrentTokenID: ${token.id}`);
return token.id; return token.id;
} catch (error) { } catch (error) {
loopError = true;
Log(`[${shared.now()}] Errore nel reperire il token corrente.`); Log(`[${shared.now()}] Errore nel reperire il token corrente.`);
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
}
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
} }
async function getNewToken() { async function getNewToken() {
let loopError;
let errorCount = 0;
do {
try { try {
const response = await axios.post( const response = await axios.post(
`https://api.netbird.io/api/users/${userID}/tokens`, `https://api.netbird.io/api/users/${userID}/tokens`,
@@ -109,11 +161,24 @@ async function getNewToken() {
Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`); Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`);
return response.data.plain_token; return response.data.plain_token;
} catch (error) { } catch (error) {
loopError = true;
Log(`[${shared.now()}] Errore nella creazione di un nuovo token.`); Log(`[${shared.now()}] Errore nella creazione di un nuovo token.`);
Log(`[${shared.now()}] ${error}`); Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
Log(`[${shared.now()}] Debug: ${error.stack}`); Log(`[${shared.now()}] Debug: ${error}`);
for (const codice of shared.codiciNonAccettati) {
if (error.response?.status === codice) {
Log(
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
);
Log(
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
);
process.exit(3); process.exit(3);
} }
}
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
} while (loopError);
} }
module.exports = { module.exports = {