0

Get Layer 7 Attacks By Managed Rules Time Series

by
Published Nov 16, 2023

Percentage distribution of attacks by managed rules 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 Managed Rules Time Series
8
 * Percentage distribution of attacks by managed rules 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
  mitigationProduct: string | undefined,
23
  normalization: "PERCENTAGE" | "MIN0_MAX" | undefined,
24
  format: "JSON" | "CSV" | undefined
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/radar/attacks/layer7/timeseries_groups/managed_rules`
28
  );
29
  for (const [k, v] of [
30
    ["aggInterval", aggInterval],
31
    ["name", name],
32
    ["dateRange", dateRange],
33
    ["dateStart", dateStart],
34
    ["dateEnd", dateEnd],
35
    ["asn", asn],
36
    ["location", location],
37
    ["ipVersion", ipVersion],
38
    ["httpVersion", httpVersion],
39
    ["httpMethod", httpMethod],
40
    ["mitigationProduct", mitigationProduct],
41
    ["normalization", normalization],
42
    ["format", format],
43
  ]) {
44
    if (v !== undefined && v !== "") {
45
      url.searchParams.append(k, v);
46
    }
47
  }
48
  const response = await fetch(url, {
49
    method: "GET",
50
    headers: {
51
      "X-AUTH-EMAIL": auth.email,
52
      "X-AUTH-KEY": auth.key,
53
      Authorization: "Bearer " + auth.token,
54
    },
55
    body: undefined,
56
  });
57
  if (!response.ok) {
58
    const text = await response.text();
59
    throw new Error(`${response.status} ${text}`);
60
  }
61
  return await response.json();
62
}
63