20260718 Explorer 接入 VRP 检索查询

This commit is contained in:
2026-07-20 10:05:00 +08:00
parent 6d9eedd680
commit 06f4b086b4
12 changed files with 554 additions and 22 deletions

View File

@@ -21,6 +21,7 @@ import {
STATS_REASONS,
STATS_VALIDATION,
VALIDATION_SUMMARY,
VRPS,
} from "./fixtures";
export interface MockJob {
@@ -74,6 +75,63 @@ function paginate<T>(items: T[], url: URL, defaultLimit = 50): { page: T[]; next
return { page, next: nextOffset < items.length ? String(nextOffset) : null };
}
/** VRP lookup pagination with the backend's `vrp:<offset>` cursor format. */
function paginateVrp<T>(items: T[], url: URL): { page: T[]; next: string | null } {
const limit = Math.max(1, Number(url.searchParams.get("limit") ?? 50));
const raw = url.searchParams.get("cursor") ?? "";
const offset = raw.startsWith("vrp:") ? Number(raw.slice(4)) || 0 : 0;
const page = items.slice(offset, offset + limit);
const nextOffset = offset + limit;
return { page, next: nextOffset < items.length ? `vrp:${nextOffset}` : null };
}
function ipv4ToInt(addr: string): number | null {
const parts = addr.split(".").map(Number);
if (parts.length !== 4 || parts.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return null;
return ((parts[0] * 256 + parts[1]) * 256 + parts[2]) * 256 + parts[3];
}
/**
* Mock coverage semantics: VRP covers an IP when its prefix contains it;
* covers a queried prefix when the VRP prefix contains it (shorter or equal
* length) AND vrp.maxLength >= queried length. IPv6 uses a leading-hextet
* heuristic — good enough for the fixture dataset.
*/
function vrpCovers(
vrp: { prefix: string; maxLength: number },
query: { ip?: string; prefix?: string },
): boolean {
const [net, lenRaw] = vrp.prefix.split("/");
const vlen = Number(lenRaw);
if (vrp.prefix.includes(":")) {
const head = net.split("::")[0].replace(/:$/, "");
const target = query.ip ?? query.prefix?.split("/")[0] ?? "";
if (!target.includes(":")) return false;
if (query.prefix) {
const qlen = Number(query.prefix.split("/")[1]);
if (!(vlen <= qlen && vrp.maxLength >= qlen)) return false;
}
return target === net || target.startsWith(`${head}:`);
}
if (query.ip) {
if (query.ip.includes(":")) return false;
const netInt = ipv4ToInt(net);
const ipInt = ipv4ToInt(query.ip);
if (netInt === null || ipInt === null) return false;
return vlen === 0 || netInt >>> (32 - vlen) === ipInt >>> (32 - vlen);
}
if (query.prefix) {
const [qnet, qlenRaw] = query.prefix.split("/");
const qlen = Number(qlenRaw);
const netInt = ipv4ToInt(net);
const qnetInt = ipv4ToInt(qnet);
if (netInt === null || qnetInt === null) return false;
if (!(vlen <= qlen && vrp.maxLength >= qlen)) return false;
return vlen === 0 || netInt >>> (32 - vlen) === qnetInt >>> (32 - vlen);
}
return false;
}
function applyObjectFilters(items: typeof OBJECTS, url: URL): typeof OBJECTS {
let out = items;
const type = url.searchParams.get("type")?.toLowerCase();
@@ -280,6 +338,24 @@ export async function installMockApi(
return fulfillJson(route, pageItems, next);
}
if (section === "vrps" && a === "lookup") {
const ip = url.searchParams.get("ip") ?? undefined;
const prefix = url.searchParams.get("prefix") ?? undefined;
if (ip && prefix) return fulfillError(route, 400, "ip and prefix are mutually exclusive");
if (!ip && !prefix) return fulfillError(route, 400, "ip or prefix query parameter is required");
const asnRaw = url.searchParams.get("asn");
let items = VRPS.filter((v) => vrpCovers(v, { ip, prefix }));
if (asnRaw !== null) {
const asn = Number(asnRaw);
if (!Number.isInteger(asn) || asn < 0) {
return fulfillError(route, 400, `invalid ASN query parameter: ${asnRaw}`);
}
items = items.filter((v) => v.asn === asn);
}
const { page: vrpPage, next } = paginateVrp(items, url);
return fulfillJson(route, vrpPage, next);
}
if (section === "search") {
const q = (url.searchParams.get("q") ?? "").trim();
if (!q) return fulfillError(route, 400, "q query parameter is required");