From 30832e6ccea452e90cf23788b9ea47b64311ccf3 Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Fri, 21 Nov 2025 22:51:30 +0100 Subject: [PATCH] Nuova gestion errori --- README.md | 4 +- hosts.js | 284 ++++++++++++++++++++++++++++++++++-------------------- shared.js | 1 + tokens.js | 217 ++++++++++++++++++++++++++--------------- 4 files changed, 324 insertions(+), 182 deletions(-) diff --git a/README.md b/README.md index dfecd1c..2dc789b 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Il programma restituisce come codice di errore: `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: 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` -5- Avviare il programma con `node index.js` o con Docker +5- Avviare il programma con Docker ## Docker diff --git a/hosts.js b/hosts.js index d6333a0..db061ff 100644 --- a/hosts.js +++ b/hosts.js @@ -30,29 +30,47 @@ async function Request() { } async function isConnected(hostID) { - try { - const response = await axios.get( - `https://api.netbird.io/api/peers/${hostID}`, - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, + let loopError; + let errorCount = 0; + do { + try { + const response = await axios.get( + `https://api.netbird.io/api/peers/${hostID}`, + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`); + return response.data.connected; + } catch (error) { + loopError = true; + Log( + `[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}` + ); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`); - return response.data.connected; - } catch (error) { - Log( - `[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}` - ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } async function initHostList() { + let loopError; + let errorCount = 0; if ("HOSTNAMES" in process.env) { let envHostnames = process.env.HOSTNAMES; @@ -65,9 +83,49 @@ async function initHostList() { envHostnames = envHostnames.split(";"); for (const host of envHostnames) { + do { + loopError = false; + try { + const response = await axios.get( + `https://api.netbird.io/api/peers?name=${host}`, + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + for (const host of response.data) { + hostIDs.push(host.id); + } + } catch (error) { + loopError = true; + Log( + `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` + ); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } + } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); + } + } else { + do { + loopError = false; try { const response = await axios.get( - `https://api.netbird.io/api/peers?name=${host}`, + `https://api.netbird.io/api/peers?name=${envHostnames}`, { headers: { Accept: "application/json", @@ -75,43 +133,28 @@ async function initHostList() { }, } ); - for (const host of response.data) { - hostIDs.push(host.id); - } - /* - response.data.forEach((host) => { - hostIDs.push(host.id); - }); - */ + hostIDs.push(response.data[0].id); } catch (error) { + loopError = true; Log( `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } - } - } else { - try { - const response = await axios.get( - `https://api.netbird.io/api/peers?name=${envHostnames}`, - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - hostIDs.push(response.data[0].id); - } catch (error) { - Log( - `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` - ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } } } else if ("HOSTIDS" in process.env) { @@ -122,9 +165,47 @@ async function initHostList() { envHostIDs = envHostIDs.split(";"); for (const host of envHostIDs) { + do { + loopError = false; + try { + const response = await axios.get( + `https://api.netbird.io/api/peers/${host}`, + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + hostIDs.push(response.data.id); + } catch (error) { + loopError = true; + Log( + `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` + ); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } + } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); + } + } else { + do { + loopError = false; try { const response = await axios.get( - `https://api.netbird.io/api/peers/${host}`, + `https://api.netbird.io/api/peers/${envHostIDs}`, { headers: { Accept: "application/json", @@ -134,75 +215,70 @@ async function initHostList() { ); hostIDs.push(response.data.id); } catch (error) { + loopError = true; Log( `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } - } - } else { - try { - const response = await axios.get( - `https://api.netbird.io/api/peers/${envHostIDs}`, - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - hostIDs.push(response.data.id); - } catch (error) { - Log( - `[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.` - ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } } } else { Log( `[${shared.now()}] Nessun peer specificato, verranno monitorati tutti i peer disponibili` ); - try { - const response = await axios.get(`https://api.netbird.io/api/peers`, { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, - }); + do { + loopError = false; + try { + const response = await axios.get(`https://api.netbird.io/api/peers`, { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + }); - for (const element of response.data) { - hostIDs.push(element.id); + for (const element of response.data) { + hostIDs.push(element.id); + } + } catch (error) { + loopError = true; + Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } + } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); } - - /* - response.data.forEach((element) => { - hostIDs.push(element.id); - }); - */ - } catch (error) { - Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + } while (loopError); } Log(`[${shared.now()}] ID rilevati:`); for (const element of hostIDs) { Log(`[${shared.now()}] ${element}`); } - - /* - hostIDs.forEach((element) => { - Log(`[${shared.now()}] ${element}`); - }); - */ } module.exports = { Monitoring }; diff --git a/shared.js b/shared.js index d804d99..ec9084b 100644 --- a/shared.js +++ b/shared.js @@ -4,6 +4,7 @@ let timezone = require("dayjs/plugin/timezone"); module.exports = { token: "", + codiciNonAccettati: [401, 404], setToken(token) { this.token = token; }, diff --git a/tokens.js b/tokens.js index a5c9f71..9f2a063 100644 --- a/tokens.js +++ b/tokens.js @@ -24,96 +24,161 @@ async function TokenRenew() { } async function getUserID() { - try { - const response = await axios.get( - "https://api.netbird.io/api/users/current", - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, + let loopError; + let errorCount = 0; + do { + try { + const response = await axios.get( + "https://api.netbird.io/api/users/current", + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + Log(`[${shared.now()}] UserID: ${response.data.id}`); + return response.data.id; + } catch (error) { + loopError = true; + Log(`[${shared.now()}] Errore nel reperire l'ID dell'utente corrente.`); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - Log(`[${shared.now()}] UserID: ${response.data.id}`); - return response.data.id; - } catch (error) { - Log(`[${shared.now()}] Errore nel reperire l'ID dell'utente corrente.`); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } async function deleteToken(tokenID) { - try { - await axios.delete( - `https://api.netbird.io/api/users/${userID}/tokens/${tokenID}`, - { - headers: { - Authorization: `Token ${shared.getToken()}`, - }, + let loopError; + let errorCount = 0; + do { + loopError = false; + try { + await axios.delete( + `https://api.netbird.io/api/users/${userID}/tokens/${tokenID}`, + { + headers: { + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + Log(`[${shared.now()}] Token con ID ${tokenID} cancellato con successo`); + } catch (error) { + loopError = true; + Log( + `[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.` + ); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - Log(`[${shared.now()}] Token con ID ${tokenID} cancellato con successo`); - } catch (error) { - Log( - `[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.` - ); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } async function getCurrentTokenID() { - try { - const response = await axios.get( - `https://api.netbird.io/api/users/${userID}/tokens`, - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - }, + let loopError; + let errorCount = 0; + do { + try { + const response = await axios.get( + `https://api.netbird.io/api/users/${userID}/tokens`, + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + }, + } + ); + + const token = response.data.find((t) => t.name === "Uptime Kuma"); + if (!token) throw new Error("Token 'Uptime Kuma' non trovato"); + + Log(`[${shared.now()}] CurrentTokenID: ${token.id}`); + return token.id; + } catch (error) { + loopError = true; + Log(`[${shared.now()}] Errore nel reperire il token corrente.`); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - - const token = response.data.find((t) => t.name === "Uptime Kuma"); - if (!token) throw new Error("Token 'Uptime Kuma' non trovato"); - - Log(`[${shared.now()}] CurrentTokenID: ${token.id}`); - return token.id; - } catch (error) { - Log(`[${shared.now()}] Errore nel reperire il token corrente.`); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } async function getNewToken() { - try { - const response = await axios.post( - `https://api.netbird.io/api/users/${userID}/tokens`, - { - name: "Uptime Kuma", - expires_in: 2, - }, - { - headers: { - Accept: "application/json", - Authorization: `Token ${shared.getToken()}`, - "Content-Type": "application/json", + let loopError; + let errorCount = 0; + do { + try { + const response = await axios.post( + `https://api.netbird.io/api/users/${userID}/tokens`, + { + name: "Uptime Kuma", + expires_in: 2, }, + { + headers: { + Accept: "application/json", + Authorization: `Token ${shared.getToken()}`, + "Content-Type": "application/json", + }, + } + ); + Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`); + return response.data.plain_token; + } catch (error) { + loopError = true; + Log(`[${shared.now()}] Errore nella creazione di un nuovo token.`); + Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`); + 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); + } } - ); - Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`); - return response.data.plain_token; - } catch (error) { - Log(`[${shared.now()}] Errore nella creazione di un nuovo token.`); - Log(`[${shared.now()}] ${error}`); - Log(`[${shared.now()}] Debug: ${error.stack}`); - process.exit(3); - } + Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`); + } + } while (loopError); } module.exports = {