Get autonomous system information by IP address

Get the requested autonomous system information based on IP address. Population estimates come from APNIC (refer to https://labs.apnic.net/?p=526).

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get autonomous system information by IP address
8
 * Get the requested autonomous system information based on IP address. Population estimates come from APNIC (refer to https://labs.apnic.net/?p=526).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  ip: string | undefined,
13
  format: "JSON" | "CSV" | undefined
14
) {
15
  const url = new URL(
16
    `https://api.cloudflare.com/client/v4/radar/entities/asns/ip`
17
  );
18
  for (const [k, v] of [
19
    ["ip", ip],
20
    ["format", format],
21
  ]) {
22
    if (v !== undefined && v !== "") {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      "X-AUTH-EMAIL": auth.email,
30
      "X-AUTH-KEY": auth.key,
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41