-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from hamster1963/ip-info
Ip info
- Loading branch information
Showing
13 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"use client"; | ||
|
||
import { IPInfo } from "@/app/api/server-ip/route"; | ||
import { Loader } from "@/components/loading/Loader"; | ||
import { Card, CardContent } from "@/components/ui/card"; | ||
import { nezhaFetcher } from "@/lib/utils"; | ||
import { useTranslations } from "next-intl"; | ||
import useSWRImmutable from "swr/immutable"; | ||
|
||
export default function ServerIPInfo({ server_id }: { server_id: number }) { | ||
const t = useTranslations("IPInfo"); | ||
|
||
const { data } = useSWRImmutable<IPInfo>( | ||
`/api/server-ip?server_id=${server_id}`, | ||
nezhaFetcher, | ||
); | ||
|
||
if (!data) { | ||
return ( | ||
<div className="mb-11"> | ||
<Loader visible /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<section className="flex flex-wrap gap-2 mb-4"> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{"ASN"}</p> | ||
<div className="text-xs"> | ||
{data.asn.autonomous_system_organization} | ||
</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{t("asn_number")}</p> | ||
<div className="text-xs"> | ||
AS{data.asn.autonomous_system_number} | ||
</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground"> | ||
{t("registered_country")} | ||
</p> | ||
<div className="text-xs"> | ||
{data.city.registered_country?.names.en} | ||
</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{"ISO"}</p> | ||
<div className="text-xs">{data.city.country?.iso_code}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{t("city")}</p> | ||
<div className="text-xs">{data.city.city?.names.en}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{t("longitude")}</p> | ||
<div className="text-xs">{data.city.location?.longitude}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{t("latitude")}</p> | ||
<div className="text-xs">{data.city.location?.latitude}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground">{t("time_zone")}</p> | ||
<div className="text-xs">{data.city.location?.time_zone}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
{data.city.postal && ( | ||
<Card className="rounded-[10px] bg-transparent border-none shadow-none"> | ||
<CardContent className="px-1.5 py-1"> | ||
<section className="flex flex-col items-start gap-0.5"> | ||
<p className="text-xs text-muted-foreground"> | ||
{t("postal_code")} | ||
</p> | ||
<div className="text-xs">{data.city.postal?.code}</div> | ||
</section> | ||
</CardContent> | ||
</Card> | ||
)} | ||
</section> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { auth } from "@/auth"; | ||
import getEnv from "@/lib/env-entry"; | ||
import { GetServerIP } from "@/lib/serverFetch"; | ||
import fs from "fs"; | ||
import { AsnResponse, CityResponse, Reader } from "maxmind"; | ||
import { redirect } from "next/navigation"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import path from "path"; | ||
|
||
export const dynamic = "force-dynamic"; | ||
|
||
interface ResError extends Error { | ||
statusCode: number; | ||
message: string; | ||
} | ||
|
||
export type IPInfo = { | ||
city: CityResponse; | ||
asn: AsnResponse; | ||
}; | ||
|
||
export async function GET(req: NextRequest) { | ||
if (getEnv("SitePassword")) { | ||
const session = await auth(); | ||
if (!session) { | ||
redirect("/"); | ||
} | ||
} | ||
|
||
if (!getEnv("NEXT_PUBLIC_ShowIpInfo")) { | ||
return NextResponse.json( | ||
{ error: "NEXT_PUBLIC_ShowIpInfo is disable" }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
const { searchParams } = new URL(req.url); | ||
const server_id = searchParams.get("server_id"); | ||
|
||
if (!server_id) { | ||
return NextResponse.json( | ||
{ error: "server_id is required" }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
try { | ||
const ip = await GetServerIP({ server_id: Number(server_id) }); | ||
|
||
const cityDbPath = path.join(process.cwd(), "lib", "GeoLite2-City.mmdb"); | ||
|
||
const asnDbPath = path.join(process.cwd(), "lib", "GeoLite2-ASN.mmdb"); | ||
|
||
const cityDbBuffer = fs.readFileSync(cityDbPath); | ||
const asnDbBuffer = fs.readFileSync(asnDbPath); | ||
|
||
const cityLookup = new Reader<CityResponse>(cityDbBuffer); | ||
const asnLookup = new Reader<AsnResponse>(asnDbBuffer); | ||
|
||
const data: IPInfo = { | ||
city: cityLookup.get(ip) as CityResponse, | ||
asn: asnLookup.get(ip) as AsnResponse, | ||
}; | ||
return NextResponse.json(data, { status: 200 }); | ||
} catch (error) { | ||
const err = error as ResError; | ||
console.error("Error in GET handler:", err); | ||
const statusCode = err.statusCode || 500; | ||
const message = err.message || "Internal Server Error"; | ||
return NextResponse.json({ error: message }, { status: statusCode }); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters