Geoblocking works

This commit is contained in:
Owen
2025-08-26 17:14:55 -07:00
parent 218a6642a2
commit a1802add19
2 changed files with 9 additions and 5 deletions

View File

@@ -63,7 +63,7 @@ esbuild
packagePath: getPackagePaths(), packagePath: getPackagePaths(),
}), }),
], ],
sourcemap: true, sourcemap: "external",
target: "node22", target: "node22",
}) })
.then(() => { .then(() => {

View File

@@ -753,6 +753,10 @@ export function isPathAllowed(pattern: string, path: string): boolean {
} }
async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> { async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
if (countryCode == "ALL") {
return true;
}
const geoIpCacheKey = `geoip:${ip}`; const geoIpCacheKey = `geoip:${ip}`;
let cachedCountryCode: string | undefined = cache.get(geoIpCacheKey); let cachedCountryCode: string | undefined = cache.get(geoIpCacheKey);
@@ -760,17 +764,15 @@ async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
if (!cachedCountryCode) { if (!cachedCountryCode) {
try { try {
const response = await axios.get( const response = await axios.get(
`${config.getRawConfig().managed?.endpoint}/api/v1/geoip/${ip}`, `${config.getRawConfig().managed?.endpoint}/api/v1/hybrid/geoip/${ip}`,
await tokenManager.getAuthHeader() await tokenManager.getAuthHeader()
); );
cachedCountryCode = response.data.data.country; cachedCountryCode = response.data.data.countryCode;
// Cache for longer since IP geolocation doesn't change frequently // Cache for longer since IP geolocation doesn't change frequently
cache.set(geoIpCacheKey, cachedCountryCode, 300); // 5 minutes cache.set(geoIpCacheKey, cachedCountryCode, 300); // 5 minutes
logger.debug(`IP ${ip} is in country:`, cachedCountryCode);
} catch (error) { } catch (error) {
if (axios.isAxiosError(error)) { if (axios.isAxiosError(error)) {
logger.error("Error fetching config in verify session:", { logger.error("Error fetching config in verify session:", {
@@ -788,5 +790,7 @@ async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
} }
} }
logger.debug(`IP ${ip} is in country: ${cachedCountryCode}`);
return cachedCountryCode?.toUpperCase() === countryCode.toUpperCase(); return cachedCountryCode?.toUpperCase() === countryCode.toUpperCase();
} }