mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-02 08:46:38 +00:00
Seperate out function
This commit is contained in:
32
server/lib/geoip.ts
Normal file
32
server/lib/geoip.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import config from "./config";
|
||||||
|
import { tokenManager } from "./tokenManager";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
|
||||||
|
export async function getCountryCodeForIp(
|
||||||
|
ip: string
|
||||||
|
): Promise<string | undefined> {
|
||||||
|
try {
|
||||||
|
const response = await axios.get(
|
||||||
|
`${config.getRawConfig().managed?.endpoint}/api/v1/hybrid/geoip/${ip}`,
|
||||||
|
await tokenManager.getAuthHeader()
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.data.data.countryCode;
|
||||||
|
} catch (error) {
|
||||||
|
if (axios.isAxiosError(error)) {
|
||||||
|
logger.error("Error fetching config in verify session:", {
|
||||||
|
message: error.message,
|
||||||
|
code: error.code,
|
||||||
|
status: error.response?.status,
|
||||||
|
statusText: error.response?.statusText,
|
||||||
|
url: error.config?.url,
|
||||||
|
method: error.config?.method
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
logger.error("Error fetching config in verify session:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from "./response";
|
export * from "./response";
|
||||||
export { tokenManager, TokenManager } from "./tokenManager";
|
export { tokenManager, TokenManager } from "./tokenManager";
|
||||||
|
export * from "./geoip";
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
validateResourceSessionToken
|
validateResourceSessionToken
|
||||||
} from "@server/auth/sessions/resource";
|
} from "@server/auth/sessions/resource";
|
||||||
import { verifyResourceAccessToken } from "@server/auth/verifyResourceAccessToken";
|
import { verifyResourceAccessToken } from "@server/auth/verifyResourceAccessToken";
|
||||||
import { db } from "@server/db";
|
|
||||||
import {
|
import {
|
||||||
getResourceByDomain,
|
getResourceByDomain,
|
||||||
getUserSessionWithUser,
|
getUserSessionWithUser,
|
||||||
@@ -33,8 +32,7 @@ import createHttpError from "http-errors";
|
|||||||
import NodeCache from "node-cache";
|
import NodeCache from "node-cache";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import axios from "axios";
|
import { getCountryCodeForIp } from "@server/lib";
|
||||||
import { tokenManager } from "@server/lib";
|
|
||||||
|
|
||||||
// We'll see if this speeds anything up
|
// We'll see if this speeds anything up
|
||||||
const cache = new NodeCache({
|
const cache = new NodeCache({
|
||||||
@@ -179,7 +177,9 @@ export async function verifyResourceSession(
|
|||||||
logger.debug("Resource denied by rule");
|
logger.debug("Resource denied by rule");
|
||||||
return notAllowed(res);
|
return notAllowed(res);
|
||||||
} else if (action == "PASS") {
|
} else if (action == "PASS") {
|
||||||
logger.debug("Resource passed by rule, continuing to auth checks");
|
logger.debug(
|
||||||
|
"Resource passed by rule, continuing to auth checks"
|
||||||
|
);
|
||||||
// Continue to authentication checks below
|
// Continue to authentication checks below
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,36 +758,13 @@ async function isIpInGeoIP(ip: string, countryCode: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const geoIpCacheKey = `geoip:${ip}`;
|
const geoIpCacheKey = `geoip:${ip}`;
|
||||||
|
|
||||||
let cachedCountryCode: string | undefined = cache.get(geoIpCacheKey);
|
let cachedCountryCode: string | undefined = cache.get(geoIpCacheKey);
|
||||||
|
|
||||||
if (!cachedCountryCode) {
|
if (!cachedCountryCode) {
|
||||||
try {
|
cachedCountryCode = await getCountryCodeForIp(ip);
|
||||||
const response = await axios.get(
|
// Cache for longer since IP geolocation doesn't change frequently
|
||||||
`${config.getRawConfig().managed?.endpoint}/api/v1/hybrid/geoip/${ip}`,
|
cache.set(geoIpCacheKey, cachedCountryCode, 300); // 5 minutes
|
||||||
await tokenManager.getAuthHeader()
|
|
||||||
);
|
|
||||||
|
|
||||||
cachedCountryCode = response.data.data.countryCode;
|
|
||||||
|
|
||||||
// Cache for longer since IP geolocation doesn't change frequently
|
|
||||||
cache.set(geoIpCacheKey, cachedCountryCode, 300); // 5 minutes
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
if (axios.isAxiosError(error)) {
|
|
||||||
logger.error("Error fetching config in verify session:", {
|
|
||||||
message: error.message,
|
|
||||||
code: error.code,
|
|
||||||
status: error.response?.status,
|
|
||||||
statusText: error.response?.statusText,
|
|
||||||
url: error.config?.url,
|
|
||||||
method: error.config?.method
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
logger.error("Error fetching config in verify session:", error);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(`IP ${ip} is in country: ${cachedCountryCode}`);
|
logger.debug(`IP ${ip} is in country: ${cachedCountryCode}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user