Get top attack pairs (origin and target locations) of Layer 3 attacks

Get the top attacks from origin to target location. Values are a percentage out of the total layer 3 attacks (with billing country). You can optionally limit the number of attacks per origin/target location (useful if all the top attacks are from or to the same location).

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 top attack pairs (origin and target locations) of Layer 3 attacks
8
 * Get the top attacks from origin to target location. Values are a percentage out of the total layer 3 attacks (with billing country). You can optionally limit the number of attacks per origin/target location (useful if all the top attacks are from or to the same location).
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  limit: string | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  location: string | undefined,
18
  ipVersion: string | undefined,
19
  protocol: string | undefined,
20
  limitDirection: "ORIGIN" | "TARGET" | undefined,
21
  limitPerLocation: string | undefined,
22
  format: "JSON" | "CSV" | undefined
23
) {
24
  const url = new URL(
25
    `https://api.cloudflare.com/client/v4/radar/attacks/layer3/top/attacks`
26
  );
27
  for (const [k, v] of [
28
    ["limit", limit],
29
    ["name", name],
30
    ["dateRange", dateRange],
31
    ["dateStart", dateStart],
32
    ["dateEnd", dateEnd],
33
    ["location", location],
34
    ["ipVersion", ipVersion],
35
    ["protocol", protocol],
36
    ["limitDirection", limitDirection],
37
    ["limitPerLocation", limitPerLocation],
38
    ["format", format],
39
  ]) {
40
    if (v !== undefined && v !== "") {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      "X-AUTH-EMAIL": auth.email,
48
      "X-AUTH-KEY": auth.key,
49
      Authorization: "Bearer " + auth.token,
50
    },
51
    body: undefined,
52
  });
53
  if (!response.ok) {
54
    const text = await response.text();
55
    throw new Error(`${response.status} ${text}`);
56
  }
57
  return await response.json();
58
}
59