0

Get Top Autonomous Systems By Network Traffic

by
Published Nov 16, 2023

Get the top autonomous systems (AS) by network traffic (NetFlows) over a given time period. Visit https://en.wikipedia.org/wiki/NetFlow for more information.

Script cloudflare
  • Submitted by hugo697 Typescript (fetch-only)
    Created 535 days ago
    1
    type Cloudflare = {
    2
      token: string;
    3
      email: string;
    4
      key: string;
    5
    };
    6
    /**
    7
     * Get Top Autonomous Systems By Network Traffic
    8
     * Get the top autonomous systems (AS) by network traffic (NetFlows) over a given time period. Visit https://en.wikipedia.org/wiki/NetFlow for more information.
    9
     */
    10
    export async function main(
    11
      auth: Cloudflare,
    12
      limit: string | undefined,
    13
      name: string | undefined,
    14
      dateRange: string | undefined,
    15
      dateStart: string | undefined,
    16
      dateEnd: string | undefined,
    17
      asn: string | undefined,
    18
      location: string | undefined,
    19
      format: "JSON" | "CSV" | undefined
    20
    ) {
    21
      const url = new URL(
    22
        `https://api.cloudflare.com/client/v4/radar/netflows/top/ases`
    23
      );
    24
      for (const [k, v] of [
    25
        ["limit", limit],
    26
        ["name", name],
    27
        ["dateRange", dateRange],
    28
        ["dateStart", dateStart],
    29
        ["dateEnd", dateEnd],
    30
        ["asn", asn],
    31
        ["location", location],
    32
        ["format", format],
    33
      ]) {
    34
        if (v !== undefined && v !== "") {
    35
          url.searchParams.append(k, v);
    36
        }
    37
      }
    38
      const response = await fetch(url, {
    39
        method: "GET",
    40
        headers: {
    41
          "X-AUTH-EMAIL": auth.email,
    42
          "X-AUTH-KEY": auth.key,
    43
          Authorization: "Bearer " + auth.token,
    44
        },
    45
        body: undefined,
    46
      });
    47
      if (!response.ok) {
    48
        const text = await response.text();
    49
        throw new Error(`${response.status} ${text}`);
    50
      }
    51
      return await response.json();
    52
    }
    53