Determining location with restrictions disabled, cleaned up.
This commit is contained in:
+64
-20
@@ -2,6 +2,7 @@ import path from 'node:path';
|
||||
import { Reader } from '@maxmind/geoip2-node';
|
||||
import config from 'config';
|
||||
|
||||
import runtimeId from '../../utils/runtime-id.js';
|
||||
import initLogger from '../logger.js';
|
||||
|
||||
const logger = initLogger();
|
||||
@@ -11,22 +12,47 @@ export default async function initRestrictionHandler() {
|
||||
const reader = await Reader.open('assets/GeoLite2-City.mmdb');
|
||||
|
||||
function getRestriction(req) {
|
||||
if (Object.hasOwn(req.session, 'restriction') && Object.hasOwn(req.session, 'country') && req.session.restrictionIp === req.userIp) {
|
||||
return {
|
||||
restriction: req.session.restriction,
|
||||
country: req.session.country,
|
||||
};
|
||||
if (req.session.geo
|
||||
&& Object.hasOwn(req.session.geo, 'country')
|
||||
&& req.session.geo.ip === req.userIp
|
||||
&& req.session.geo.runtimeId === runtimeId) {
|
||||
const { country, subdivision } = req.session.geo;
|
||||
|
||||
if (!config.restrictions.enabled) {
|
||||
return {
|
||||
restriction: 0,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
if (Object.hasOwn(req.session.geo, 'restriction')) {
|
||||
return {
|
||||
restriction: req.session.geo.restriction,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const location = reader.city(req.userIp);
|
||||
const country = location.country.isoCode;
|
||||
const subdivision = location.subdivisions?.[0]?.isoCode;
|
||||
|
||||
if (!config.restrictions.enabled) {
|
||||
return {
|
||||
restriction: 0,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
if (regions[country]?.[subdivision]) {
|
||||
// state or province restriction
|
||||
return {
|
||||
restriction: config.restrictions.modes[regions[country][subdivision]],
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -35,23 +61,24 @@ export default async function initRestrictionHandler() {
|
||||
return {
|
||||
restriction: config.restrictions.modes[regions[country]],
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
restriction: null,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
function restrictionHandler(req, res, next) {
|
||||
if (!config.restrictions.enabled) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { restriction, country } = getRestriction(req);
|
||||
const {
|
||||
restriction,
|
||||
country,
|
||||
subdivision,
|
||||
} = getRestriction(req);
|
||||
|
||||
if (restriction === 'block' || req.path === '/sfw/') {
|
||||
res.render(path.join(import.meta.dirname, '../../assets/sfw.ejs'), {
|
||||
@@ -61,21 +88,38 @@ export default async function initRestrictionHandler() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.session.restriction !== restriction) {
|
||||
req.session.restrictionIp = req.userIp;
|
||||
req.session.restriction = restriction;
|
||||
req.session.country = country;
|
||||
if (req.session.geo?.restriction !== restriction) {
|
||||
req.session.geo = {
|
||||
ip: req.userIp,
|
||||
runtimeId,
|
||||
restriction,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
|
||||
req.restriction = restriction;
|
||||
req.country = country;
|
||||
req.geo = {
|
||||
restriction,
|
||||
country,
|
||||
subdivision,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
logger.error(`Failed Maxmind IP lookup for ${req.ip}: ${error.message}`);
|
||||
|
||||
req.session.restrictionIp = req.userIp;
|
||||
req.session.restriction = 0;
|
||||
req.session.country = null;
|
||||
req.session.geo = {
|
||||
ip: req.userIp,
|
||||
runtimeId,
|
||||
restriction: 0,
|
||||
country: null,
|
||||
subdivision: null,
|
||||
};
|
||||
|
||||
req.geo = {
|
||||
restriction: 0,
|
||||
country: null,
|
||||
subdivision: null,
|
||||
};
|
||||
}
|
||||
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user