1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * List Page Shield connections |
8 | * Lists all connections detected by Page Shield. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | zone_id: string, |
13 | exclude_urls: string | undefined, |
14 | urls: string | undefined, |
15 | hosts: string | undefined, |
16 | page: string | undefined, |
17 | per_page: string | undefined, |
18 | order_by: "first_seen_at" | "last_seen_at" | undefined, |
19 | direction: "asc" | "desc" | undefined, |
20 | prioritize_malicious: string | undefined, |
21 | exclude_cdn_cgi: string | undefined, |
22 | status: string | undefined, |
23 | page_url: string | undefined, |
24 | _export: "csv" | undefined |
25 | ) { |
26 | const url = new URL( |
27 | `https://api.cloudflare.com/client/v4/zones/${zone_id}/page_shield/connections` |
28 | ); |
29 | for (const [k, v] of [ |
30 | ["exclude_urls", exclude_urls], |
31 | ["urls", urls], |
32 | ["hosts", hosts], |
33 | ["page", page], |
34 | ["per_page", per_page], |
35 | ["order_by", order_by], |
36 | ["direction", direction], |
37 | ["prioritize_malicious", prioritize_malicious], |
38 | ["exclude_cdn_cgi", exclude_cdn_cgi], |
39 | ["status", status], |
40 | ["page_url", page_url], |
41 | ["export", _export], |
42 | ]) { |
43 | if (v !== undefined && v !== "") { |
44 | url.searchParams.append(k, v); |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: "GET", |
49 | headers: { |
50 | "X-AUTH-EMAIL": auth.email, |
51 | "X-AUTH-KEY": auth.key, |
52 | Authorization: "Bearer " + auth.token, |
53 | }, |
54 | body: undefined, |
55 | }); |
56 | if (!response.ok) { |
57 | const text = await response.text(); |
58 | throw new Error(`${response.status} ${text}`); |
59 | } |
60 | return await response.json(); |
61 | } |
62 |
|