Skip to content

Commit

Permalink
Check granted logic
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Dec 11, 2024
1 parent 31cb241 commit f61b801
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/controllers/status.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
cat,
getAuth,
getRevision,
getSupportVersions,
getVersion,
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -202,16 +203,19 @@ 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,
/** @type {import('express').Response} */
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();
Expand Down

0 comments on commit f61b801

Please sign in to comment.