Create PCAP request

Create new PCAP request for 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
 * Create PCAP request
8
 * Create new PCAP request for account.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  body:
14
    | {
15
        filter_v1?: {
16
          destination_address?: string;
17
          destination_port?: number;
18
          protocol?: number;
19
          source_address?: string;
20
          source_port?: number;
21
          [k: string]: unknown;
22
        };
23
        packet_limit: number;
24
        system: "magic-transit";
25
        time_limit: number;
26
        type: "simple" | "full";
27
        [k: string]: unknown;
28
      }
29
    | {
30
        byte_limit?: number;
31
        colo_name: string;
32
        destination_conf: string;
33
        filter_v1?: {
34
          destination_address?: string;
35
          destination_port?: number;
36
          protocol?: number;
37
          source_address?: string;
38
          source_port?: number;
39
          [k: string]: unknown;
40
        };
41
        packet_limit?: number;
42
        system: "magic-transit";
43
        time_limit: number;
44
        type: "simple" | "full";
45
        [k: string]: unknown;
46
      }
47
) {
48
  const url = new URL(
49
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/pcaps`
50
  );
51

52
  const response = await fetch(url, {
53
    method: "POST",
54
    headers: {
55
      "X-AUTH-EMAIL": auth.email,
56
      "X-AUTH-KEY": auth.key,
57
      "Content-Type": "application/json",
58
      Authorization: "Bearer " + auth.token,
59
    },
60
    body: JSON.stringify(body),
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68