List Cloudflare Tunnels

Lists and filters Cloudflare Tunnels in an account.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * List Cloudflare Tunnels
8
 * Lists and filters Cloudflare Tunnels in an account.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  name: string | undefined,
14
  is_deleted: string | undefined,
15
  existed_at: string | undefined,
16
  uuid: string | undefined,
17
  was_active_at: string | undefined,
18
  was_inactive_at: string | undefined,
19
  include_prefix: string | undefined,
20
  exclude_prefix: string | undefined,
21
  per_page: string | undefined,
22
  page: string | undefined
23
) {
24
  const url = new URL(
25
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/cfd_tunnel`
26
  );
27
  for (const [k, v] of [
28
    ["name", name],
29
    ["is_deleted", is_deleted],
30
    ["existed_at", existed_at],
31
    ["uuid", uuid],
32
    ["was_active_at", was_active_at],
33
    ["was_inactive_at", was_inactive_at],
34
    ["include_prefix", include_prefix],
35
    ["exclude_prefix", exclude_prefix],
36
    ["per_page", per_page],
37
    ["page", page],
38
  ]) {
39
    if (v !== undefined && v !== "") {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "GET",
45
    headers: {
46
      "X-AUTH-EMAIL": auth.email,
47
      "X-AUTH-KEY": auth.key,
48
      Authorization: "Bearer " + auth.token,
49
    },
50
    body: undefined,
51
  });
52
  if (!response.ok) {
53
    const text = await response.text();
54
    throw new Error(`${response.status} ${text}`);
55
  }
56
  return await response.json();
57
}
58