0

Get Layer 7 Attacks By Mitigation Product Time Series

by
Published Nov 16, 2023

Percentage distribution of attacks by mitigation product used over time.

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 Layer 7 Attacks By Mitigation Product Time Series
8
 * Percentage distribution of attacks by mitigation product used over time.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  aggInterval: "15m" | "1h" | "1d" | "1w" | undefined,
13
  name: string | undefined,
14
  dateRange: string | undefined,
15
  dateStart: string | undefined,
16
  dateEnd: string | undefined,
17
  asn: string | undefined,
18
  location: string | undefined,
19
  ipVersion: string | undefined,
20
  httpVersion: string | undefined,
21
  httpMethod: string | undefined,
22
  normalization: "PERCENTAGE" | "MIN0_MAX" | undefined,
23
  format: "JSON" | "CSV" | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/radar/attacks/layer7/timeseries_groups/mitigation_product`
27
  );
28
  for (const [k, v] of [
29
    ["aggInterval", aggInterval],
30
    ["name", name],
31
    ["dateRange", dateRange],
32
    ["dateStart", dateStart],
33
    ["dateEnd", dateEnd],
34
    ["asn", asn],
35
    ["location", location],
36
    ["ipVersion", ipVersion],
37
    ["httpVersion", httpVersion],
38
    ["httpMethod", httpMethod],
39
    ["normalization", normalization],
40
    ["format", format],
41
  ]) {
42
    if (v !== undefined && v !== "") {
43
      url.searchParams.append(k, v);
44
    }
45
  }
46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      "X-AUTH-EMAIL": auth.email,
50
      "X-AUTH-KEY": auth.key,
51
      Authorization: "Bearer " + auth.token,
52
    },
53
    body: undefined,
54
  });
55
  if (!response.ok) {
56
    const text = await response.text();
57
    throw new Error(`${response.status} ${text}`);
58
  }
59
  return await response.json();
60
}
61