mirror of
https://github.com/fosrl/pangolin.git
synced 2025-12-14 20:16:38 +00:00
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import logger from "@server/logger";
|
|
import { maxmindLookup } from "@server/db/maxmind";
|
|
import axios from "axios";
|
|
import config from "./config";
|
|
import { tokenManager } from "./tokenManager";
|
|
|
|
export async function getCountryCodeForIp(
|
|
ip: string
|
|
): Promise<string | undefined> {
|
|
try {
|
|
if (!maxmindLookup) {
|
|
logger.warn(
|
|
"MaxMind DB path not configured, cannot perform GeoIP lookup"
|
|
);
|
|
return;
|
|
}
|
|
|
|
const result = maxmindLookup.get(ip);
|
|
|
|
if (!result || !result.country) {
|
|
return;
|
|
}
|
|
|
|
const { country } = result;
|
|
|
|
logger.debug(
|
|
`GeoIP lookup successful for IP ${ip}: ${country.iso_code}`
|
|
);
|
|
|
|
return country.iso_code;
|
|
} catch (error) {
|
|
logger.error("Error fetching config in verify session:", error);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
export async function remoteGetCountryCodeForIp(
|
|
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;
|
|
}
|