1 | type Cloudflare = { |
2 | token: string; |
3 | email: string; |
4 | key: string; |
5 | }; |
6 | |
7 | * Get Top Locations By IP Version |
8 | * Get the top locations, by HTTP traffic, of the requested IP protocol version. Values are a percentage out of the total traffic. |
9 | */ |
10 | export async function main( |
11 | auth: Cloudflare, |
12 | ip_version: "IPv4" | "IPv6", |
13 | limit: string | undefined, |
14 | name: string | undefined, |
15 | dateRange: string | undefined, |
16 | dateStart: string | undefined, |
17 | dateEnd: string | undefined, |
18 | asn: string | undefined, |
19 | location: string | undefined, |
20 | botClass: string | undefined, |
21 | deviceType: string | undefined, |
22 | httpProtocol: string | undefined, |
23 | httpVersion: string | undefined, |
24 | os: string | undefined, |
25 | tlsVersion: string | undefined, |
26 | format: "JSON" | "CSV" | undefined |
27 | ) { |
28 | const url = new URL( |
29 | `https://api.cloudflare.com/client/v4/radar/http/top/locations/ip_version/${ip_version}` |
30 | ); |
31 | for (const [k, v] of [ |
32 | ["limit", limit], |
33 | ["name", name], |
34 | ["dateRange", dateRange], |
35 | ["dateStart", dateStart], |
36 | ["dateEnd", dateEnd], |
37 | ["asn", asn], |
38 | ["location", location], |
39 | ["botClass", botClass], |
40 | ["deviceType", deviceType], |
41 | ["httpProtocol", httpProtocol], |
42 | ["httpVersion", httpVersion], |
43 | ["os", os], |
44 | ["tlsVersion", tlsVersion], |
45 | ["format", format], |
46 | ]) { |
47 | if (v !== undefined && v !== "") { |
48 | url.searchParams.append(k, v); |
49 | } |
50 | } |
51 | const response = await fetch(url, { |
52 | method: "GET", |
53 | headers: { |
54 | "X-AUTH-EMAIL": auth.email, |
55 | "X-AUTH-KEY": auth.key, |
56 | Authorization: "Bearer " + auth.token, |
57 | }, |
58 | body: undefined, |
59 | }); |
60 | if (!response.ok) { |
61 | const text = await response.text(); |
62 | throw new Error(`${response.status} ${text}`); |
63 | } |
64 | return await response.json(); |
65 | } |
66 |
|