Nuova gestion errori
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
134
hosts.js
134
hosts.js
@@ -30,6 +30,9 @@ async function Request() {
|
||||
}
|
||||
|
||||
async function isConnected(hostID) {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${hostID}`,
|
||||
@@ -43,16 +46,31 @@ async function isConnected(hostID) {
|
||||
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()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
async function initHostList() {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
if ("HOSTNAMES" in process.env) {
|
||||
let envHostnames = process.env.HOSTNAMES;
|
||||
|
||||
@@ -65,6 +83,8 @@ 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}`,
|
||||
@@ -78,21 +98,31 @@ async function initHostList() {
|
||||
for (const host of response.data) {
|
||||
hostIDs.push(host.id);
|
||||
}
|
||||
/*
|
||||
response.data.forEach((host) => {
|
||||
hostIDs.push(host.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}`);
|
||||
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=${envHostnames}`,
|
||||
@@ -105,14 +135,27 @@ async function initHostList() {
|
||||
);
|
||||
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}`);
|
||||
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 if ("HOSTIDS" in process.env) {
|
||||
let envHostIDs = process.env.HOSTIDS;
|
||||
@@ -122,6 +165,8 @@ 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}`,
|
||||
@@ -134,15 +179,30 @@ 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}`);
|
||||
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/${envHostIDs}`,
|
||||
@@ -155,19 +215,34 @@ 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}`);
|
||||
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 {
|
||||
Log(
|
||||
`[${shared.now()}] Nessun peer specificato, verranno monitorati tutti i peer disponibili`
|
||||
);
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
const response = await axios.get(`https://api.netbird.io/api/peers`, {
|
||||
headers: {
|
||||
@@ -179,30 +254,31 @@ async function initHostList() {
|
||||
for (const element of response.data) {
|
||||
hostIDs.push(element.id);
|
||||
}
|
||||
|
||||
/*
|
||||
response.data.forEach((element) => {
|
||||
hostIDs.push(element.id);
|
||||
});
|
||||
*/
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
Log(`[${shared.now()}] ID rilevati:`);
|
||||
for (const element of hostIDs) {
|
||||
Log(`[${shared.now()}] ${element}`);
|
||||
}
|
||||
|
||||
/*
|
||||
hostIDs.forEach((element) => {
|
||||
Log(`[${shared.now()}] ${element}`);
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
module.exports = { Monitoring };
|
||||
|
||||
@@ -4,6 +4,7 @@ let timezone = require("dayjs/plugin/timezone");
|
||||
|
||||
module.exports = {
|
||||
token: "",
|
||||
codiciNonAccettati: [401, 404],
|
||||
setToken(token) {
|
||||
this.token = token;
|
||||
},
|
||||
|
||||
81
tokens.js
81
tokens.js
@@ -24,6 +24,9 @@ async function TokenRenew() {
|
||||
}
|
||||
|
||||
async function getUserID() {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
"https://api.netbird.io/api/users/current",
|
||||
@@ -37,14 +40,31 @@ async function getUserID() {
|
||||
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()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
async function deleteToken(tokenID) {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
await axios.delete(
|
||||
`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`);
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella cancellazione del token con ID ${tokenID}.`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
async function getCurrentTokenID() {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/users/${userID}/tokens`,
|
||||
@@ -83,14 +119,30 @@ async function getCurrentTokenID() {
|
||||
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()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
async function getNewToken() {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`https://api.netbird.io/api/users/${userID}/tokens`,
|
||||
@@ -109,12 +161,25 @@ async function getNewToken() {
|
||||
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()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TokenRenew,
|
||||
|
||||
Reference in New Issue
Block a user