add ip check to all checks

This commit is contained in:
Karol Wypchlo 2021-06-22 14:03:07 +02:00
parent 1f8a553fb0
commit 59a22db5f0
3 changed files with 36 additions and 4 deletions

View File

@ -67,7 +67,7 @@ async function accountWebsiteCheck(done) {
// directServerApiAccessCheck returns the basic server api check on direct server address
async function directServerApiAccessCheck(done) {
if (!process.env.SKYNET_SERVER_API) {
return done({ up: false, info: { message: "SKYNET_SERVER_API env variable not configured" } });
return done({ up: false, errors: [{ message: "SKYNET_SERVER_API env variable not configured" }] });
}
const [portalAccessCheck, serverAccessCheck] = await Promise.all([
@ -77,13 +77,14 @@ async function directServerApiAccessCheck(done) {
if (portalAccessCheck.ip !== serverAccessCheck.ip) {
serverAccessCheck.up = false;
serverAccessCheck.info = {
serverAccessCheck.errors = serverAccessCheck.errors ?? [];
serverAccessCheck.errors.push({
message: "Access ip mismatch between portal and server access",
response: {
portal: { name: process.env.SKYNET_PORTAL_API, ip: portalAccessCheck.ip },
server: { name: process.env.SKYNET_SERVER_API, ip: serverAccessCheck.ip },
},
};
});
}
return done(serverAccessCheck);

View File

@ -0,0 +1,29 @@
const got = require("got");
const getCurrentAddress = async () => {
try {
const { body } = await got("http://whatismyip.akamai.com");
if (body) return body;
throw new Error("whatismyip.akamai.com responded with empty body");
} catch (error) {
console.log(error.message);
return "-- error fetching ip address from whatismyip.akamai.com --";
}
};
module.exports = async function middleware() {
const ip = await getCurrentAddress();
return (check) => {
if (check.ip && check.ip !== ip) {
check.up = false;
check.errors = check.errors ?? [];
check.errors.push({
message: "Response ip was different than current server ip - possibly there was an error with routing request",
data: { response: check.ip, server: ip },
});
}
return check;
};
};

View File

@ -1,4 +1,5 @@
const { getYesterdayISOString } = require("./utils");
const createMiddleware = require("./checks/middleware");
require("yargs/yargs")(process.argv.slice(2)).command(
"$0 <type>",
@ -27,10 +28,11 @@ require("yargs/yargs")(process.argv.slice(2)).command(
const db = require("../src/db");
const checks = require(`../src/checks/${type}`);
const middleware = await createMiddleware();
const entry = {
date: new Date().toISOString(),
checks: await Promise.all(checks.map((check) => new Promise(check))),
checks: (await Promise.all(checks.map((check) => new Promise(check)))).map(middleware),
};
db.read() // read before writing to make sure no external changes are overwritten