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(),
}),
],
sourcemap: true,
sourcemap: "external",
target: "node22",
})
.then(() => {

View File

@@ -753,6 +753,10 @@ export function isPathAllowed(pattern: string, path: string): boolean {
}
async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
if (countryCode == "ALL") {
return true;
}
const geoIpCacheKey = `geoip:${ip}`;
let cachedCountryCode: string | undefined = cache.get(geoIpCacheKey);
@@ -760,17 +764,15 @@ async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
if (!cachedCountryCode) {
try {
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()
);
cachedCountryCode = response.data.data.country;
cachedCountryCode = response.data.data.countryCode;
// Cache for longer since IP geolocation doesn't change frequently
cache.set(geoIpCacheKey, cachedCountryCode, 300); // 5 minutes
logger.debug(`IP ${ip} is in country:`, cachedCountryCode);
} catch (error) {
if (axios.isAxiosError(error)) {
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();
}