mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
fix: use fs
This commit is contained in:
parent
8f38cf94d8
commit
dfef790884
@ -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">
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user