From f61b801d5f81594f27bd9380d08865e8e3ea9ecf Mon Sep 17 00:00:00 2001 From: Wildan M Date: Wed, 11 Dec 2024 23:23:41 +0700 Subject: [PATCH] Check granted logic --- src/controllers/status.js | 7 ++++++- src/util.js | 10 +++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/controllers/status.js b/src/controllers/status.js index 8724e7c..bb19b04 100644 --- a/src/controllers/status.js +++ b/src/controllers/status.js @@ -1,5 +1,6 @@ import { cat, + getAuth, getRevision, getSupportVersions, getVersion, @@ -31,7 +32,11 @@ export default function () { }) }); router.get('/ip', async function (req, res, next) { - res.send(req.ip); + if (getAuth(req)) { + res.json({ ip: req.ip, granted: true }) + } else { + res.json({ ip: req.ip }) + } }); router.get('/check', async function (req, res, next) { try { diff --git a/src/util.js b/src/util.js index f03a03a..7d9a44c 100644 --- a/src/util.js +++ b/src/util.js @@ -4,6 +4,7 @@ import { lock } from 'proper-lockfile'; import fs from 'fs'; import binaries from './binaries/metadata.cjs'; import { virtualminExec } from './executor/virtualmin.js'; +import { emitWarning } from 'process'; const { javaVersionsList, javaVersionsMap, @@ -202,6 +203,10 @@ export const getRevision = () => { return revision; } +export const getAuth = (/** @type {import('express').Request} */ req) => { + return req.headers.authorization === tokenSecret && (!allowIps || allowIps[req.ip]) +} + export const checkAuth = function ( /** @type {import('express').Request} */ req, @@ -209,9 +214,8 @@ export const checkAuth = function ( res, /** @type {any} */ next) { - if (req.headers.authorization === tokenSecret) { - if (!allowIps || allowIps[req.ip]) - return next(); + if (getAuth(req)) { + return next(); } if (process.env.NODE_ENV === 'development') { return next();