diff --git a/packages/health-check/src/checks/critical.js b/packages/health-check/src/checks/critical.js index afa616e1..8fa62fdf 100644 --- a/packages/health-check/src/checks/critical.js +++ b/packages/health-check/src/checks/critical.js @@ -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); diff --git a/packages/health-check/src/checks/middleware.js b/packages/health-check/src/checks/middleware.js new file mode 100644 index 00000000..645eae03 --- /dev/null +++ b/packages/health-check/src/checks/middleware.js @@ -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; + }; +}; diff --git a/packages/health-check/src/run.js b/packages/health-check/src/run.js index 26f4c643..36f4319b 100644 --- a/packages/health-check/src/run.js +++ b/packages/health-check/src/run.js @@ -1,4 +1,5 @@ const { getYesterdayISOString } = require("./utils"); +const createMiddleware = require("./checks/middleware"); require("yargs/yargs")(process.argv.slice(2)).command( "$0 ", @@ -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