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

284
hosts.js
View File

@@ -30,29 +30,47 @@ async function Request() {
} }
async function isConnected(hostID) { async function isConnected(hostID) {
try { let loopError;
const response = await axios.get( let errorCount = 0;
`https://api.netbird.io/api/peers/${hostID}`, do {
{ try {
headers: { const response = await axios.get(
Accept: "application/json", `https://api.netbird.io/api/peers/${hostID}`,
Authorization: `Token ${shared.getToken()}`, {
}, 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()}] Nuovo tentativo n. ${errorCount++}`);
Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`); }
return response.data.connected; } while (loopError);
} 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);
}
} }
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,9 +83,49 @@ async function initHostList() {
envHostnames = envHostnames.split(";"); envHostnames = envHostnames.split(";");
for (const host of envHostnames) { 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 { 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=${envHostnames}`,
{ {
headers: { headers: {
Accept: "application/json", Accept: "application/json",
@@ -75,43 +133,28 @@ async function initHostList() {
}, },
} }
); );
for (const host of response.data) { hostIDs.push(response.data[0].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}`);
process.exit(3); for (const codice of shared.codiciNonAccettati) {
} if (error.response?.status === codice) {
} Log(
} else { `[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
try { );
const response = await axios.get( Log(
`https://api.netbird.io/api/peers?name=${envHostnames}`, `[${shared.now()}] Per favore riavviare il programma con un token valido.`
{ );
headers: { process.exit(3);
Accept: "application/json", }
Authorization: `Token ${shared.getToken()}`,
},
} }
); Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
hostIDs.push(response.data[0].id); }
} catch (error) { } while (loopError);
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 if ("HOSTIDS" in process.env) { } else if ("HOSTIDS" in process.env) {
@@ -122,9 +165,47 @@ async function initHostList() {
envHostIDs = envHostIDs.split(";"); envHostIDs = envHostIDs.split(";");
for (const host of envHostIDs) { 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 { try {
const response = await axios.get( const response = await axios.get(
`https://api.netbird.io/api/peers/${host}`, `https://api.netbird.io/api/peers/${envHostIDs}`,
{ {
headers: { headers: {
Accept: "application/json", Accept: "application/json",
@@ -134,75 +215,70 @@ 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}`);
process.exit(3); for (const codice of shared.codiciNonAccettati) {
} if (error.response?.status === codice) {
} Log(
} else { `[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
try { );
const response = await axios.get( Log(
`https://api.netbird.io/api/peers/${envHostIDs}`, `[${shared.now()}] Per favore riavviare il programma con un token valido.`
{ );
headers: { process.exit(3);
Accept: "application/json", }
Authorization: `Token ${shared.getToken()}`,
},
} }
); Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
hostIDs.push(response.data.id); }
} catch (error) { } while (loopError);
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 { } 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`
); );
try { do {
const response = await axios.get(`https://api.netbird.io/api/peers`, { loopError = false;
headers: { try {
Accept: "application/json", const response = await axios.get(`https://api.netbird.io/api/peers`, {
Authorization: `Token ${shared.getToken()}`, headers: {
}, Accept: "application/json",
}); Authorization: `Token ${shared.getToken()}`,
},
});
for (const element of response.data) { for (const element of response.data) {
hostIDs.push(element.id); 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++}`);
} }
} while (loopError);
/*
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);
}
} }
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;
}, },

217
tokens.js
View File

@@ -24,96 +24,161 @@ async function TokenRenew() {
} }
async function getUserID() { async function getUserID() {
try { let loopError;
const response = await axios.get( let errorCount = 0;
"https://api.netbird.io/api/users/current", do {
{ try {
headers: { const response = await axios.get(
Accept: "application/json", "https://api.netbird.io/api/users/current",
Authorization: `Token ${shared.getToken()}`, {
}, 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()}] Nuovo tentativo n. ${errorCount++}`);
Log(`[${shared.now()}] UserID: ${response.data.id}`); }
return response.data.id; } while (loopError);
} 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);
}
} }
async function deleteToken(tokenID) { async function deleteToken(tokenID) {
try { let loopError;
await axios.delete( let errorCount = 0;
`https://api.netbird.io/api/users/${userID}/tokens/${tokenID}`, do {
{ loopError = false;
headers: { try {
Authorization: `Token ${shared.getToken()}`, 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()}] Nuovo tentativo n. ${errorCount++}`);
Log(`[${shared.now()}] Token con ID ${tokenID} cancellato con successo`); }
} catch (error) { } while (loopError);
Log(
`[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.`
);
Log(`[${shared.now()}] ${error}`);
Log(`[${shared.now()}] Debug: ${error.stack}`);
process.exit(3);
}
} }
async function getCurrentTokenID() { async function getCurrentTokenID() {
try { let loopError;
const response = await axios.get( let errorCount = 0;
`https://api.netbird.io/api/users/${userID}/tokens`, do {
{ try {
headers: { const response = await axios.get(
Accept: "application/json", `https://api.netbird.io/api/users/${userID}/tokens`,
Authorization: `Token ${shared.getToken()}`, {
}, 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);
}
} }
); Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
}
const token = response.data.find((t) => t.name === "Uptime Kuma"); } while (loopError);
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);
}
} }
async function getNewToken() { async function getNewToken() {
try { let loopError;
const response = await axios.post( let errorCount = 0;
`https://api.netbird.io/api/users/${userID}/tokens`, do {
{ try {
name: "Uptime Kuma", const response = await axios.post(
expires_in: 2, `https://api.netbird.io/api/users/${userID}/tokens`,
}, {
{ name: "Uptime Kuma",
headers: { expires_in: 2,
Accept: "application/json",
Authorization: `Token ${shared.getToken()}`,
"Content-Type": "application/json",
}, },
{
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 tentativo n. ${errorCount++}`);
Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`); }
return response.data.plain_token; } while (loopError);
} 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);
}
} }
module.exports = { module.exports = {