0

Get Top Locations by DNS queries

by
Published Nov 16, 2023

Get top locations by DNS queries made to Cloudflare's public DNS resolver.

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
8
 * Get top locations by DNS queries made to Cloudflare's public DNS resolver.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  name: string | undefined,
14
  domain: 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/dns/top/locations`
24
  );
25
  for (const [k, v] of [
26
    ["limit", limit],
27
    ["name", name],
28
    ["domain", domain],
29
    ["dateRange", dateRange],
30
    ["dateStart", dateStart],
31
    ["dateEnd", dateEnd],
32
    ["asn", asn],
33
    ["location", location],
34
    ["format", format],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      "X-AUTH-EMAIL": auth.email,
44
      "X-AUTH-KEY": auth.key,
45
      Authorization: "Bearer " + auth.token,
46
    },
47
    body: undefined,
48
  });
49
  if (!response.ok) {
50
    const text = await response.text();
51
    throw new Error(`${response.status} ${text}`);
52
  }
53
  return await response.json();
54
}
55