Improved error handling
This commit is contained in:
111
tokens.js
111
tokens.js
@@ -7,7 +7,6 @@ let userID;
|
||||
async function TokenRenew() {
|
||||
setInterval(async () => {
|
||||
shared.eventEmitter.emit("tokenRenewalStart");
|
||||
try {
|
||||
// ottieni l'ID dell'utente associato al token
|
||||
userID = await getUserID();
|
||||
|
||||
@@ -21,69 +20,89 @@ async function TokenRenew() {
|
||||
// cancella il token precedente
|
||||
await deleteToken(tokenID);
|
||||
shared.eventEmitter.emit("tokenRenewalEnd");
|
||||
} catch (error) {
|
||||
Log(`[${shared.now()}] Errore nel rinnovo del token: ${error}`);
|
||||
}
|
||||
}, day - hour);
|
||||
}
|
||||
|
||||
async function getUserID() {
|
||||
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}`);
|
||||
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}`);
|
||||
} catch (error) {
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
process.exit(3);
|
||||
}
|
||||
return response.data.id;
|
||||
}
|
||||
|
||||
async function deleteToken(tokenID) {
|
||||
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`);
|
||||
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) {
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
async function getCurrentTokenID() {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/users/${userID}/tokens`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
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");
|
||||
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}`);
|
||||
Log(`[${shared.now()}] CurrentTokenID: ${token.id}`);
|
||||
} catch (error) {
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
process.exit(3);
|
||||
}
|
||||
return token.id;
|
||||
}
|
||||
|
||||
async function getNewToken() {
|
||||
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",
|
||||
try {
|
||||
const response = await axios.post(
|
||||
`https://api.netbird.io/api/users/${userID}/tokens`,
|
||||
{
|
||||
name: "Uptime Kuma",
|
||||
expires_in: 2,
|
||||
},
|
||||
}
|
||||
);
|
||||
Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`);
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
Log(`[${shared.now()}] Nuovo token creato: ${response.data.plain_token}`);
|
||||
} catch (error) {
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
process.exit(3);
|
||||
}
|
||||
return response.data.plain_token;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user