0

Get Top Locations by DNS Queries IP version

by
Published Nov 16, 2023

Get the top locations by DNS queries IP version to AS112.

Script cloudflare Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 403 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Top Locations by DNS Queries IP version
8
 * Get the top locations by DNS queries IP version to AS112.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  ip_version: "IPv4" | "IPv6",
13
  limit: string | undefined,
14
  name: string | undefined,
15
  dateRange: string | undefined,
16
  dateStart: string | undefined,
17
  dateEnd: string | undefined,
18
  asn: string | undefined,
19
  location: string | undefined,
20
  format: "JSON" | "CSV" | undefined
21
) {
22
  const url = new URL(
23
    `https://api.cloudflare.com/client/v4/radar/as112/top/locations/ip_version/${ip_version}`
24
  );
25
  for (const [k, v] of [
26
    ["limit", limit],
27
    ["name", name],
28
    ["dateRange", dateRange],
29
    ["dateStart", dateStart],
30
    ["dateEnd", dateEnd],
31
    ["asn", asn],
32
    ["location", location],
33
    ["format", format],
34
  ]) {
35
    if (v !== undefined && v !== "") {
36
      url.searchParams.append(k, v);
37
    }
38
  }
39
  const response = await fetch(url, {
40
    method: "GET",
41
    headers: {
42
      "X-AUTH-EMAIL": auth.email,
43
      "X-AUTH-KEY": auth.key,
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: undefined,
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54