fix: use fs

This commit is contained in:
hamster1963 2024-12-12 00:52:19 +08:00
parent 8f38cf94d8
commit dfef790884
3 changed files with 33 additions and 30 deletions

View File

@ -17,10 +17,9 @@ export default function Page(props: { params: Promise<{ id: string }> }) {
const updateViews = async () => { const updateViews = async () => {
const ipInfo = await GetIPInfo({ server_id: params.id }); const ipInfo = await GetIPInfo({ server_id: params.id });
console.log(ipInfo); console.log(ipInfo);
} };
updateViews() updateViews();
}, []) }, []);
return ( return (
<div className="mx-auto grid w-full max-w-5xl gap-2"> <div className="mx-auto grid w-full max-w-5xl gap-2">

View File

@ -1,26 +1,30 @@
"use server"; "use server";
import maxmind, { CityResponse, AsnResponse } from 'maxmind'; import fs from "fs";
import { GetServerIP } from './serverFetch'; import { AsnResponse, CityResponse, Reader } from "maxmind";
import path from 'path';
import { GetServerIP } from "./serverFetch";
type IPInfo = { type IPInfo = {
city: CityResponse; city: CityResponse;
asn: AsnResponse; asn: AsnResponse;
};
export default async function GetIPInfo({
server_id,
}: {
server_id: string;
}): Promise<IPInfo> {
const ip = await GetServerIP({ server_id: Number(server_id) });
const cityDbBuffer = fs.readFileSync("./lib/GeoLite2-City.mmdb");
const asnDbBuffer = fs.readFileSync("./lib/GeoLite2-ASN.mmdb");
const cityLookup = new Reader<CityResponse>(cityDbBuffer);
const asnLookup = new Reader<AsnResponse>(asnDbBuffer);
return {
city: cityLookup.get(ip) as CityResponse,
asn: asnLookup.get(ip) as AsnResponse,
};
} }
export default async function GetIPInfo({ server_id }: { server_id: string }): Promise<IPInfo> {
const ip = await GetServerIP({ server_id: Number(server_id) })
// 使用 path.join 获取正确的文件路径
const cityDbPath = path.join(process.cwd(), 'lib', 'GeoLite2-City.mmdb');
const asnDbPath = path.join(process.cwd(), 'lib', 'GeoLite2-ASN.mmdb');
const cityLookup = await maxmind.open<CityResponse>(cityDbPath)
const asnLookup = await maxmind.open<AsnResponse>(asnDbPath)
return {
city: cityLookup.get(ip) as CityResponse,
asn: asnLookup.get(ip) as AsnResponse
}
}

View File

@ -130,8 +130,11 @@ export async function GetServerMonitor({ server_id }: { server_id: number }) {
} }
} }
export async function GetServerIP({
export async function GetServerIP({ server_id }: { server_id: number }): Promise<string> { server_id,
}: {
server_id: number;
}): Promise<string> {
let nezhaBaseUrl = getEnv("NezhaBaseUrl"); let nezhaBaseUrl = getEnv("NezhaBaseUrl");
if (!nezhaBaseUrl) { if (!nezhaBaseUrl) {
console.error("NezhaBaseUrl is not set"); console.error("NezhaBaseUrl is not set");
@ -176,11 +179,8 @@ export async function GetServerIP({ server_id }: { server_id: number }): Promise
console.error("GetNezhaData error:", error); console.error("GetNezhaData error:", error);
throw error; // Rethrow the error to be caught by the caller throw error; // Rethrow the error to be caught by the caller
} }
} }
export async function GetServerDetail({ server_id }: { server_id: number }) { export async function GetServerDetail({ server_id }: { server_id: number }) {
let nezhaBaseUrl = getEnv("NezhaBaseUrl"); let nezhaBaseUrl = getEnv("NezhaBaseUrl");
if (!nezhaBaseUrl) { if (!nezhaBaseUrl) {