add new critical checks

This commit is contained in:
Karol Wypchlo 2021-05-24 16:27:30 +02:00
parent ad7ba38e11
commit 02e0e45bc4
1 changed files with 45 additions and 26 deletions

View File

@ -1,7 +1,10 @@
const got = require("got"); const got = require("got");
const FormData = require("form-data"); const FormData = require("form-data");
const { StatusCodes } = require("http-status-codes");
const { calculateElapsedTime, getResponseContent } = require("../utils"); const { calculateElapsedTime, getResponseContent } = require("../utils");
const { SkynetClient } = require("skynet-js");
const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API);
const exampleSkylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q";
// uploadCheck returns the result of uploading a sample file // uploadCheck returns the result of uploading a sample file
async function uploadCheck(done) { async function uploadCheck(done) {
@ -25,37 +28,33 @@ async function uploadCheck(done) {
data.ip = error?.response?.ip ?? null; data.ip = error?.response?.ip ?? null;
} }
done({ done({ name: "upload_file", time: calculateElapsedTime(time), ...data });
name: "upload_file", }
time: calculateElapsedTime(time),
...data, // websiteCheck checks whether the main website is working
}); async function websiteCheck(done) {
return genericAccessCheck("website", process.env.SKYNET_PORTAL_API, done);
} }
// downloadCheck returns the result of downloading the hard coded link // downloadCheck returns the result of downloading the hard coded link
async function downloadCheck(done) { async function downloadCheck(done) {
const time = process.hrtime(); const url = await skynetClient.getSkylinkUrl(exampleSkylink);
const skylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q";
const data = { up: false };
try { return genericAccessCheck("skylink", url, done);
const response = await got(`${process.env.SKYNET_PORTAL_API}/${skylink}?nocache=true`);
data.statusCode = response.statusCode;
data.up = true;
data.ip = response.ip;
} catch (error) {
data.statusCode = error?.response?.statusCode || error.statusCode || error.status;
data.errorMessage = error.message;
data.errorResponseContent = getResponseContent(error.response);
data.ip = error?.response?.ip ?? null;
} }
done({ // skylinkSubdomainCheck returns the result of downloading the hard coded link via subdomain
name: "download_file", async function skylinkSubdomainCheck(done) {
time: calculateElapsedTime(time), const url = await skynetClient.getSkylinkUrl(exampleSkylink, { subdomain: true });
...data,
}); return genericAccessCheck("skylink_via_subdomain", done);
}
// skylinkSubdomainCheck returns the result of downloading the hard coded link via subdomain
async function handshakeSubdomainCheck(done) {
const url = await skynetClient.getHnsUrl("note-to-self", { subdomain: true });
return genericAccessCheck("hns_via_subdomain", url, done);
} }
async function accountHealthCheck(done) { async function accountHealthCheck(done) {
@ -83,7 +82,27 @@ async function accountHealthCheck(done) {
}); });
} }
const checks = [uploadCheck, downloadCheck]; async function genericAccessCheck(name, url, done) {
const time = process.hrtime();
const data = { up: false, url };
try {
const response = await got(url, { headers: { cookie: "nocache=true" } });
data.statusCode = response.statusCode;
data.up = true;
data.ip = response.ip;
} catch (error) {
data.statusCode = error?.response?.statusCode || error.statusCode || error.status;
data.errorMessage = error.message;
data.errorResponseContent = getResponseContent(error.response);
data.ip = error?.response?.ip ?? null;
}
done({ name, time: calculateElapsedTime(time), ...data });
}
const checks = [uploadCheck, websiteCheck, downloadCheck, skylinkSubdomainCheck, handshakeSubdomainCheck];
if (process.env.ACCOUNTS_ENABLED) { if (process.env.ACCOUNTS_ENABLED) {
checks.push(accountHealthCheck); checks.push(accountHealthCheck);