1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Get Mitigation Product Summary |
8 | * Percentage distribution of attacks by mitigation product 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 | format: "JSON" | "CSV" | undefined |
22 | ) { |
23 | const url = new URL( |
24 | `https://api.cloudflare.com/client/v4/radar/attacks/layer7/summary/mitigation_product` |
25 | ); |
26 | for (const [k, v] of [ |
27 | ["name", name], |
28 | ["dateRange", dateRange], |
29 | ["dateStart", dateStart], |
30 | ["dateEnd", dateEnd], |
31 | ["asn", asn], |
32 | ["location", location], |
33 | ["ipVersion", ipVersion], |
34 | ["httpVersion", httpVersion], |
35 | ["httpMethod", httpMethod], |
36 | ["format", format], |
37 | ]) { |
38 | if (v !== undefined && v !== "") { |
39 | url.searchParams.append(k, v); |
40 | } |
41 | } |
42 | const response = await fetch(url, { |
43 | method: "GET", |
44 | headers: { |
45 | "X-AUTH-EMAIL": auth.email, |
46 | "X-AUTH-KEY": auth.key, |
47 | Authorization: "Bearer " + auth.token, |
48 | }, |
49 | body: undefined, |
50 | }); |
51 | if (!response.ok) { |
52 | const text = await response.text(); |
53 | throw new Error(`${response.status} ${text}`); |
54 | } |
55 | return await response.json(); |
56 | } |
57 |
|