0
Get Top Locations By DMARC Validations
One script reply has been approved by the moderators Verified

Get the locations by email DMARC validation.

Created by hugo697 175 days ago Viewed 5905 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 175 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Get Top Locations By DMARC Validations
8
 * Get the locations by email DMARC validation.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  dmarc: "PASS" | "NONE" | "FAIL",
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
  arc: string | undefined,
21
  dkim: string | undefined,
22
  spf: string | undefined,
23
  format: "JSON" | "CSV" | undefined
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/radar/email/security/top/locations/dmarc/${dmarc}`
27
  );
28
  for (const [k, v] of [
29
    ["limit", limit],
30
    ["name", name],
31
    ["dateRange", dateRange],
32
    ["dateStart", dateStart],
33
    ["dateEnd", dateEnd],
34
    ["asn", asn],
35
    ["location", location],
36
    ["arc", arc],
37
    ["dkim", dkim],
38
    ["spf", spf],
39
    ["format", format],
40
  ]) {
41
    if (v !== undefined && v !== "") {
42
      url.searchParams.append(k, v);
43
    }
44
  }
45
  const response = await fetch(url, {
46
    method: "GET",
47
    headers: {
48
      "X-AUTH-EMAIL": auth.email,
49
      "X-AUTH-KEY": auth.key,
50
      Authorization: "Bearer " + auth.token,
51
    },
52
    body: undefined,
53
  });
54
  if (!response.ok) {
55
    const text = await response.text();
56
    throw new Error(`${response.status} ${text}`);
57
  }
58
  return await response.json();
59
}
60