Cloudflare/JD Cloud IP Details

Get IPs used on the Cloudflare/JD Cloud network, see https://www.cloudflare.com/ips for Cloudflare IPs or https://developers.cloudflare.com/china-network/reference/infrastructure/ for JD Cloud IPs.

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
 * Cloudflare/JD Cloud IP Details
8
 * Get IPs used on the Cloudflare/JD Cloud network, see https://www.cloudflare.com/ips for Cloudflare IPs or https://developers.cloudflare.com/china-network/reference/infrastructure/ for JD Cloud IPs.
9
 */
10
export async function main(auth: Cloudflare, networks: string | undefined) {
11
  const url = new URL(`https://api.cloudflare.com/client/v4/ips`);
12
  for (const [k, v] of [["networks", networks]]) {
13
    if (v !== undefined && v !== "") {
14
      url.searchParams.append(k, v);
15
    }
16
  }
17
  const response = await fetch(url, {
18
    method: "GET",
19
    headers: {
20
      "X-AUTH-EMAIL": auth.email,
21
      "X-AUTH-KEY": auth.key,
22
      Authorization: "Bearer " + auth.token,
23
    },
24
    body: undefined,
25
  });
26
  if (!response.ok) {
27
    const text = await response.text();
28
    throw new Error(`${response.status} ${text}`);
29
  }
30
  return await response.json();
31
}
32