1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 |
|
7 | * Get Managed Rules Summary |
8 | * Percentage distribution of attacks by managed rules used. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | name: string | undefined, |
13 | dateRange: string | undefined, |
14 | dateStart: string | undefined, |
15 | dateEnd: string | undefined, |
16 | asn: string | undefined, |
17 | location: string | undefined, |
18 | ipVersion: string | undefined, |
19 | httpVersion: string | undefined, |
20 | httpMethod: string | undefined, |
21 | mitigationProduct: string | undefined, |
22 | format: "JSON" | "CSV" | undefined |
23 | ) { |
24 | const url = new URL( |
25 | `https://api.cloudflare.com/client/v4/radar/attacks/layer7/summary/managed_rules` |
26 | ); |
27 | for (const [k, v] of [ |
28 | ["name", name], |
29 | ["dateRange", dateRange], |
30 | ["dateStart", dateStart], |
31 | ["dateEnd", dateEnd], |
32 | ["asn", asn], |
33 | ["location", location], |
34 | ["ipVersion", ipVersion], |
35 | ["httpVersion", httpVersion], |
36 | ["httpMethod", httpMethod], |
37 | ["mitigationProduct", mitigationProduct], |
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 |
|