@geekmidas/edge 10.0.0-alpha.188 → 10.0.0-alpha.190
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dns/cloudflare/index.cjs +431 -0
- package/dist/dns/cloudflare/index.cjs.map +1 -0
- package/dist/dns/cloudflare/index.d.cts +182 -0
- package/dist/dns/cloudflare/index.d.cts.map +1 -0
- package/dist/dns/cloudflare/index.d.mts +182 -0
- package/dist/dns/cloudflare/index.d.mts.map +1 -0
- package/dist/dns/cloudflare/index.mjs +419 -0
- package/dist/dns/cloudflare/index.mjs.map +1 -0
- package/dist/dns/index.cjs +2 -1
- package/dist/dns/index.cjs.map +1 -1
- package/dist/dns/index.d.cts +12 -0
- package/dist/dns/index.d.cts.map +1 -1
- package/dist/dns/index.d.mts +12 -0
- package/dist/dns/index.d.mts.map +1 -1
- package/dist/dns/index.mjs +2 -1
- package/dist/dns/index.mjs.map +1 -1
- package/dist/{index-BVE0VFku.d.cts → index-BhrEC4ux.d.cts} +37 -3
- package/dist/index-BhrEC4ux.d.cts.map +1 -0
- package/dist/{index-BVE0VFku.d.mts → index-BhrEC4ux.d.mts} +37 -3
- package/dist/index-BhrEC4ux.d.mts.map +1 -0
- package/dist/proxy/caddy.cjs +8 -0
- package/dist/proxy/caddy.cjs.map +1 -1
- package/dist/proxy/caddy.d.cts +6 -1
- package/dist/proxy/caddy.d.cts.map +1 -1
- package/dist/proxy/caddy.d.mts +6 -1
- package/dist/proxy/caddy.d.mts.map +1 -1
- package/dist/proxy/caddy.mjs +8 -1
- package/dist/proxy/caddy.mjs.map +1 -1
- package/dist/proxy/traefik.cjs +24 -6
- package/dist/proxy/traefik.cjs.map +1 -1
- package/dist/proxy/traefik.d.cts +2 -2
- package/dist/proxy/traefik.d.mts +2 -2
- package/dist/proxy/traefik.mjs +24 -7
- package/dist/proxy/traefik.mjs.map +1 -1
- package/dist/routes/index.cjs.map +1 -1
- package/dist/routes/index.d.cts +2 -2
- package/dist/routes/index.d.mts +2 -2
- package/dist/routes/index.mjs.map +1 -1
- package/package.json +12 -2
- package/dist/index-BVE0VFku.d.cts.map +0 -1
- package/dist/index-BVE0VFku.d.mts.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../../src/dns/cloudflare/api.ts","../../../src/dns/cloudflare/index.ts"],"mappings":";;;;UAuBiB;EAChB;;qBAGY;;;;;qBAMA;;qBAGA;KAED,+BAA+B;;UAG1B;EAChB;EACA;;EAEA;EACA;EACA;;EAEA;;EAEA;;;UAIgB;EAChB,MAAM;;EAEN;EACA;EACA;EACA;EACA;;;UAIgB;;EAEhB;;EAEA;EACA;;EAEA;;;qBAYY,qCAAqC;WAC5B;EAArB,YAAqB;;;qBAYT,oCAAoC;WAEtC;WACA;;WAEA;EAJV,YACU,gBACA,6BAEA;;EAAA;;;qBAYE,+BAA+B;WACtB;EAArB,YAAqB;;;qBAWT,8BAA8B;WACrB;EAArB,YAAqB;;;qBAWT,gCAAgC;WAElC;WACA;WACA;;WAEA;IAAqB;IAAc;;EAL7C,YACU,gBACA,cACA,gBAEA;;EAAA;IAAqB;IAAc;;;;qBAYjC,mCAAmC;WAErC;WACA;;WAEA;EAJV,YACU,gBACA,cAEA;;EAAA;;UAmBM;EAChB,YAAY;;EAEZ;;EAEA,SAAS,eAAe;EACxB;;qBAGY;mBAMiB;mBALZ;mBACA;mBACA;mBACA;EAEjB,YAA6B,SAAS;UAQxB;;EAiDR,OAAO,iBAAiB;;EAgBxB,WACL,gBACA,MAAM,sBACN,gBACE,QAAQ;EAqBL,aACL,gBACA,QAAQ,wBACN,QAAQ;EAWL,aACL,gBACA,YACA,QAAQ,wBACN,QAAQ;;;;;;;;;EAmBL,wBACL,gBACA;IACC;IACA;IACA;IACA;MAEC,QAAQ;EAgBL,aAAa,gBAAgB,aAAa;;;;iBChThC;;;;;;EAMhB,kBAAkB,QAAQ;;EAE1B;;EAEA,QAAQ;EACR;;;wBASe,SAAS,cAAc;qBAuB1B,8BAA8B;mBAIb;WAHpB;UACD;EAER,YAA6B,SAAS;UAExB;UAcN;;;;;EASF,WAAW,iBAAiB,QAAQ;;EAYpC,YACL,gBACA,iBAAiB,oBACf,QAAQ;;;;;;;EAqBL,cACL,gBACA,SAAS,oBACP,QAAQ;EAqCL,cACL,gBACA,SAAS,oBACP,QAAQ"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { t as EdgeError } from "../../errors-DBZfelEN.mjs";
|
|
2
|
+
import { DeleteDnsRecord, DeleteResult, DnsProvider, DnsRecord, UpsertDnsRecord, UpsertResult } from "../index.mjs";
|
|
3
|
+
//#region src/dns/cloudflare/api.d.ts
|
|
4
|
+
/** A Cloudflare API token, sent as `Authorization: Bearer <token>`. */
|
|
5
|
+
interface CloudflareCredential {
|
|
6
|
+
token: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
|
|
9
|
+
/**
|
|
10
|
+
* Cloudflare's "automatic" TTL — 300 seconds, and the only TTL a proxied
|
|
11
|
+
* record takes.
|
|
12
|
+
*/
|
|
13
|
+
export declare const CLOUDFLARE_AUTO_TTL = 1;
|
|
14
|
+
/** The record types gkm writes. Never TXT, NS, SOA, SRV or CAA. */
|
|
15
|
+
export declare const CLOUDFLARE_MANAGED_TYPES: readonly ['A', 'AAAA', 'CNAME', 'MX'];
|
|
16
|
+
type CloudflareRecordType = (typeof CLOUDFLARE_MANAGED_TYPES)[number];
|
|
17
|
+
/** One record as Cloudflare returns it. */
|
|
18
|
+
interface CloudflareRecord {
|
|
19
|
+
id: string;
|
|
20
|
+
type: string;
|
|
21
|
+
/** The full name: `api.example.com`. */
|
|
22
|
+
name: string;
|
|
23
|
+
content: string;
|
|
24
|
+
ttl: number;
|
|
25
|
+
/** Whether Cloudflare's proxy answers for it — its CDN, the orange cloud. */
|
|
26
|
+
proxied?: boolean;
|
|
27
|
+
/** An MX record's: Cloudflare keeps it apart from `content`. */
|
|
28
|
+
priority?: number;
|
|
29
|
+
}
|
|
30
|
+
/** What a write sends. */
|
|
31
|
+
interface CloudflareRecordInput {
|
|
32
|
+
type: CloudflareRecordType;
|
|
33
|
+
/** The full name: `api.example.com`. */
|
|
34
|
+
name: string;
|
|
35
|
+
content: string;
|
|
36
|
+
ttl: number;
|
|
37
|
+
proxied?: boolean;
|
|
38
|
+
priority?: number;
|
|
39
|
+
}
|
|
40
|
+
/** An origin certificate as the Origin CA returns it. */
|
|
41
|
+
interface CloudflareOriginCertificate {
|
|
42
|
+
/** Its serial number. */
|
|
43
|
+
id: string;
|
|
44
|
+
/** PEM. */
|
|
45
|
+
certificate: string;
|
|
46
|
+
hostnames: string[];
|
|
47
|
+
/** `2041-11-22 10:00:00 +0000 UTC` */
|
|
48
|
+
expires_on: string;
|
|
49
|
+
}
|
|
50
|
+
/** The token is missing, malformed, expired or revoked. */
|
|
51
|
+
export declare class CloudflareCredentialsInvalid extends EdgeError {
|
|
52
|
+
readonly detail?: string | undefined;
|
|
53
|
+
constructor(detail?: string | undefined);
|
|
54
|
+
}
|
|
55
|
+
/** The token is good, and may not do this to this zone. */
|
|
56
|
+
export declare class CloudflarePermissionMissing extends EdgeError {
|
|
57
|
+
readonly domain: string;
|
|
58
|
+
readonly detail?: string | undefined;
|
|
59
|
+
/** What the token needs for it: `Zone:Read and DNS:Edit`. */
|
|
60
|
+
readonly permission: string;
|
|
61
|
+
constructor(domain: string, detail?: string | undefined,
|
|
62
|
+
/** What the token needs for it: `Zone:Read and DNS:Edit`. */
|
|
63
|
+
permission?: string);
|
|
64
|
+
}
|
|
65
|
+
/** No zone of that name is in the token's reach. */
|
|
66
|
+
export declare class CloudflareZoneNotFound extends EdgeError {
|
|
67
|
+
readonly domain: string;
|
|
68
|
+
constructor(domain: string);
|
|
69
|
+
}
|
|
70
|
+
/** Still rate limited after waiting it out. */
|
|
71
|
+
export declare class CloudflareRateLimited extends EdgeError {
|
|
72
|
+
readonly attempts: number;
|
|
73
|
+
constructor(attempts: number);
|
|
74
|
+
}
|
|
75
|
+
/** Any other answer that is not a success. */
|
|
76
|
+
export declare class CloudflareRequestFailed extends EdgeError {
|
|
77
|
+
readonly method: string;
|
|
78
|
+
readonly path: string;
|
|
79
|
+
readonly status: number;
|
|
80
|
+
/** Cloudflare's `errors`, as it answered them. */
|
|
81
|
+
readonly failures: readonly {
|
|
82
|
+
code: number;
|
|
83
|
+
message: string;
|
|
84
|
+
}[];
|
|
85
|
+
constructor(method: string, path: string, status: number,
|
|
86
|
+
/** Cloudflare's `errors`, as it answered them. */
|
|
87
|
+
failures: readonly {
|
|
88
|
+
code: number;
|
|
89
|
+
message: string;
|
|
90
|
+
}[]);
|
|
91
|
+
}
|
|
92
|
+
/** A record type gkm does not write. */
|
|
93
|
+
export declare class CloudflareRecordNotAllowed extends EdgeError {
|
|
94
|
+
readonly domain: string;
|
|
95
|
+
readonly type: string;
|
|
96
|
+
/** The record's name, as it was asked for: `api`, `@`. */
|
|
97
|
+
readonly record: string;
|
|
98
|
+
constructor(domain: string, type: string,
|
|
99
|
+
/** The record's name, as it was asked for: `api`, `@`. */
|
|
100
|
+
record: string);
|
|
101
|
+
}
|
|
102
|
+
interface CloudflareApiOptions {
|
|
103
|
+
credential: CloudflareCredential;
|
|
104
|
+
/** The API's base URL, for tests. */
|
|
105
|
+
baseUrl?: string;
|
|
106
|
+
/** How a 429 is waited out, for tests. */
|
|
107
|
+
sleep?: (ms: number) => Promise<void>;
|
|
108
|
+
maxAttempts?: number;
|
|
109
|
+
}
|
|
110
|
+
export declare class CloudflareApi {
|
|
111
|
+
private readonly options;
|
|
112
|
+
private readonly baseUrl;
|
|
113
|
+
private readonly sleep;
|
|
114
|
+
private readonly maxAttempts;
|
|
115
|
+
private readonly zones;
|
|
116
|
+
constructor(options: CloudflareApiOptions);
|
|
117
|
+
private request;
|
|
118
|
+
/** The zone's id, found once by its name. */
|
|
119
|
+
zoneId(domain: string): Promise<string>;
|
|
120
|
+
/** Every record of exactly this name and type — of the type, without a name. */
|
|
121
|
+
getRecords(domain: string, type: CloudflareRecordType, fqdn?: string): Promise<CloudflareRecord[]>;
|
|
122
|
+
createRecord(domain: string, record: CloudflareRecordInput): Promise<CloudflareRecord>;
|
|
123
|
+
updateRecord(domain: string, id: string, record: CloudflareRecordInput): Promise<CloudflareRecord>;
|
|
124
|
+
/**
|
|
125
|
+
* An origin certificate from Cloudflare's Origin CA, for a CSR made where
|
|
126
|
+
* the key is: trusted by Cloudflare's proxy alone, for `hostnames`, valid
|
|
127
|
+
* `days` (one of 7, 30, 90, 365, 730, 1095, 5475). The token needs
|
|
128
|
+
* `Zone → SSL and Certificates → Edit` on the zone.
|
|
129
|
+
*
|
|
130
|
+
* @param domain - the zone, for what an error says
|
|
131
|
+
*/
|
|
132
|
+
createOriginCertificate(domain: string, request: {
|
|
133
|
+
csr: string;
|
|
134
|
+
hostnames: readonly string[];
|
|
135
|
+
days: number;
|
|
136
|
+
type: 'origin-rsa' | 'origin-ecc';
|
|
137
|
+
}): Promise<CloudflareOriginCertificate>;
|
|
138
|
+
deleteRecord(domain: string, id: string): Promise<void>;
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/dns/cloudflare/index.d.ts
|
|
142
|
+
export interface CloudflareProviderOptions {
|
|
143
|
+
/**
|
|
144
|
+
* Where the token comes from, asked the first time the API is called. It
|
|
145
|
+
* throws when there is none: what to do about a missing token is the
|
|
146
|
+
* caller's to say.
|
|
147
|
+
*/
|
|
148
|
+
credential: () => Promise<CloudflareCredential>;
|
|
149
|
+
/** The API's base URL, for tests. */
|
|
150
|
+
baseUrl?: string;
|
|
151
|
+
/** How a 429 is waited out, for tests. */
|
|
152
|
+
sleep?: CloudflareApiOptions['sleep'];
|
|
153
|
+
maxAttempts?: number;
|
|
154
|
+
}
|
|
155
|
+
/** `api` under `example.com` → `api.example.com`; `@` → `example.com`. */
|
|
156
|
+
export declare function fullName(name: string, domain: string): string;
|
|
157
|
+
export declare class CloudflareProvider implements DnsProvider {
|
|
158
|
+
private readonly options;
|
|
159
|
+
readonly name = "cloudflare";
|
|
160
|
+
private api;
|
|
161
|
+
constructor(options: CloudflareProviderOptions);
|
|
162
|
+
private getApi;
|
|
163
|
+
private assertManaged;
|
|
164
|
+
/**
|
|
165
|
+
* The managed records of the whole zone: A, AAAA, CNAME and MX. Prefer
|
|
166
|
+
* {@link readRecords}, which asks for exactly the names it needs.
|
|
167
|
+
*/
|
|
168
|
+
getRecords(domain: string): Promise<DnsRecord[]>;
|
|
169
|
+
/** The records of exactly these names and types, one request each. */
|
|
170
|
+
readRecords(domain: string, wanted: readonly DeleteDnsRecord[]): Promise<DnsRecord[]>;
|
|
171
|
+
/**
|
|
172
|
+
* Each name and type set to exactly its one value: read it, and write it
|
|
173
|
+
* only when its value or its proxy differ. The first record of the name
|
|
174
|
+
* is updated in place and any other of that type deleted, so the set
|
|
175
|
+
* ends up with one.
|
|
176
|
+
*/
|
|
177
|
+
upsertRecords(domain: string, records: UpsertDnsRecord[]): Promise<UpsertResult[]>;
|
|
178
|
+
deleteRecords(domain: string, records: DeleteDnsRecord[]): Promise<DeleteResult[]>;
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
181
|
+
export type { CloudflareApiOptions, CloudflareCredential, CloudflareOriginCertificate, CloudflareRecord, CloudflareRecordInput, CloudflareRecordType };
|
|
182
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../../src/dns/cloudflare/api.ts","../../../src/dns/cloudflare/index.ts"],"mappings":";;;;UAuBiB;EAChB;;qBAGY;;;;;qBAMA;;qBAGA;KAED,+BAA+B;;UAG1B;EAChB;EACA;;EAEA;EACA;EACA;;EAEA;;EAEA;;;UAIgB;EAChB,MAAM;;EAEN;EACA;EACA;EACA;EACA;;;UAIgB;;EAEhB;;EAEA;EACA;;EAEA;;;qBAYY,qCAAqC;WAC5B;EAArB,YAAqB;;;qBAYT,oCAAoC;WAEtC;WACA;;WAEA;EAJV,YACU,gBACA,6BAEA;;EAAA;;;qBAYE,+BAA+B;WACtB;EAArB,YAAqB;;;qBAWT,8BAA8B;WACrB;EAArB,YAAqB;;;qBAWT,gCAAgC;WAElC;WACA;WACA;;WAEA;IAAqB;IAAc;;EAL7C,YACU,gBACA,cACA,gBAEA;;EAAA;IAAqB;IAAc;;;;qBAYjC,mCAAmC;WAErC;WACA;;WAEA;EAJV,YACU,gBACA,cAEA;;EAAA;;UAmBM;EAChB,YAAY;;EAEZ;;EAEA,SAAS,eAAe;EACxB;;qBAGY;mBAMiB;mBALZ;mBACA;mBACA;mBACA;EAEjB,YAA6B,SAAS;UAQxB;;EAiDR,OAAO,iBAAiB;;EAgBxB,WACL,gBACA,MAAM,sBACN,gBACE,QAAQ;EAqBL,aACL,gBACA,QAAQ,wBACN,QAAQ;EAWL,aACL,gBACA,YACA,QAAQ,wBACN,QAAQ;;;;;;;;;EAmBL,wBACL,gBACA;IACC;IACA;IACA;IACA;MAEC,QAAQ;EAgBL,aAAa,gBAAgB,aAAa;;;;iBChThC;;;;;;EAMhB,kBAAkB,QAAQ;;EAE1B;;EAEA,QAAQ;EACR;;;wBASe,SAAS,cAAc;qBAuB1B,8BAA8B;mBAIb;WAHpB;UACD;EAER,YAA6B,SAAS;UAExB;UAcN;;;;;EASF,WAAW,iBAAiB,QAAQ;;EAYpC,YACL,gBACA,iBAAiB,oBACf,QAAQ;;;;;;;EAqBL,cACL,gBACA,SAAS,oBACP,QAAQ;EAqCL,cACL,gBACA,SAAS,oBACP,QAAQ"}
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { t as EdgeError } from "../../errors-CO33_TDV.mjs";
|
|
2
|
+
import { mxValue, parseMxValue } from "../index.mjs";
|
|
3
|
+
//#region src/dns/cloudflare/api.ts
|
|
4
|
+
/**
|
|
5
|
+
* Cloudflare's v4 DNS API, as gkm uses it: the records of one name and type
|
|
6
|
+
* at a time, inside a zone found by its name.
|
|
7
|
+
*
|
|
8
|
+
* - zone: `GET /zones?name={domain}` — the one call that is not a record's
|
|
9
|
+
* - read: `GET /zones/{zone}/dns_records?type={type}&name.exact={fqdn}`
|
|
10
|
+
* - create: `POST /zones/{zone}/dns_records`
|
|
11
|
+
* - update: `PATCH /zones/{zone}/dns_records/{id}`
|
|
12
|
+
* - delete: `DELETE /zones/{zone}/dns_records/{id}`
|
|
13
|
+
*
|
|
14
|
+
* Never the zone itself, its settings or the account. An API token with
|
|
15
|
+
* `Zone:Read` and `DNS:Edit` on the zone is all it needs, sent as
|
|
16
|
+
* `Authorization: Bearer <token>`.
|
|
17
|
+
*
|
|
18
|
+
* Every answer is the v4 envelope — `{ success, errors, result }` — and a
|
|
19
|
+
* failure is read from its `errors`, not from the status alone. A 429 is
|
|
20
|
+
* waited out a few times (its `Retry-After`, else a second) before the run
|
|
21
|
+
* gives up with {@link CloudflareRateLimited}.
|
|
22
|
+
*/
|
|
23
|
+
const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
|
|
24
|
+
/**
|
|
25
|
+
* Cloudflare's "automatic" TTL — 300 seconds, and the only TTL a proxied
|
|
26
|
+
* record takes.
|
|
27
|
+
*/
|
|
28
|
+
const CLOUDFLARE_AUTO_TTL = 1;
|
|
29
|
+
/** The record types gkm writes. Never TXT, NS, SOA, SRV or CAA. */
|
|
30
|
+
const CLOUDFLARE_MANAGED_TYPES = [
|
|
31
|
+
"A",
|
|
32
|
+
"AAAA",
|
|
33
|
+
"CNAME",
|
|
34
|
+
"MX"
|
|
35
|
+
];
|
|
36
|
+
/** The token is missing, malformed, expired or revoked. */
|
|
37
|
+
var CloudflareCredentialsInvalid = class extends EdgeError {
|
|
38
|
+
detail;
|
|
39
|
+
constructor(detail) {
|
|
40
|
+
super(`Cloudflare refused the API token${detail ? ` (${detail})` : ""}. Create one at dash.cloudflare.com/profile/api-tokens with Zone:Read and DNS:Edit on the zone, then set CLOUDFLARE_API_TOKEN or run \`gkm login --provider cloudflare\`.`);
|
|
41
|
+
this.detail = detail;
|
|
42
|
+
this.name = "CloudflareCredentialsInvalid";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/** The token is good, and may not do this to this zone. */
|
|
46
|
+
var CloudflarePermissionMissing = class extends EdgeError {
|
|
47
|
+
domain;
|
|
48
|
+
detail;
|
|
49
|
+
permission;
|
|
50
|
+
constructor(domain, detail, permission = DNS_PERMISSIONS) {
|
|
51
|
+
super(`The Cloudflare token was refused on ${domain}${detail ? ` (${detail})` : ""}. Give it ${permission} on ${domain} (dash.cloudflare.com/profile/api-tokens → the token → Edit).`);
|
|
52
|
+
this.domain = domain;
|
|
53
|
+
this.detail = detail;
|
|
54
|
+
this.permission = permission;
|
|
55
|
+
this.name = "CloudflarePermissionMissing";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/** No zone of that name is in the token's reach. */
|
|
59
|
+
var CloudflareZoneNotFound = class extends EdgeError {
|
|
60
|
+
domain;
|
|
61
|
+
constructor(domain) {
|
|
62
|
+
super(`Cloudflare has no zone ${domain} the token can read. Add ${domain} to the Cloudflare account and point its nameservers at Cloudflare, and make sure the token's zone resources include it.`);
|
|
63
|
+
this.domain = domain;
|
|
64
|
+
this.name = "CloudflareZoneNotFound";
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/** Still rate limited after waiting it out. */
|
|
68
|
+
var CloudflareRateLimited = class extends EdgeError {
|
|
69
|
+
attempts;
|
|
70
|
+
constructor(attempts) {
|
|
71
|
+
super(`Cloudflare kept answering 429 Too Many Requests after ${attempts} attempts. Wait a few minutes and deploy again: every record written so far is kept.`);
|
|
72
|
+
this.attempts = attempts;
|
|
73
|
+
this.name = "CloudflareRateLimited";
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
/** Any other answer that is not a success. */
|
|
77
|
+
var CloudflareRequestFailed = class extends EdgeError {
|
|
78
|
+
method;
|
|
79
|
+
path;
|
|
80
|
+
status;
|
|
81
|
+
failures;
|
|
82
|
+
constructor(method, path, status, failures) {
|
|
83
|
+
const detail = failures.length > 0 ? failures.map((e) => `${e.code}: ${e.message}`).join("; ") : `HTTP ${status}`;
|
|
84
|
+
super(`Cloudflare refused ${method} ${path}: ${detail}.`);
|
|
85
|
+
this.method = method;
|
|
86
|
+
this.path = path;
|
|
87
|
+
this.status = status;
|
|
88
|
+
this.failures = failures;
|
|
89
|
+
this.name = "CloudflareRequestFailed";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/** A record type gkm does not write. */
|
|
93
|
+
var CloudflareRecordNotAllowed = class extends EdgeError {
|
|
94
|
+
domain;
|
|
95
|
+
type;
|
|
96
|
+
record;
|
|
97
|
+
constructor(domain, type, record) {
|
|
98
|
+
super(`gkm does not manage ${type} records, so it will not touch '${record}' ${type} in ${domain}: only ${CLOUDFLARE_MANAGED_TYPES.join(", ")}.`);
|
|
99
|
+
this.domain = domain;
|
|
100
|
+
this.type = type;
|
|
101
|
+
this.record = record;
|
|
102
|
+
this.name = "CloudflareRecordNotAllowed";
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
/** What reading and writing a zone's records needs. */
|
|
106
|
+
const DNS_PERMISSIONS = "Zone:Read and DNS:Edit";
|
|
107
|
+
/** What an origin certificate needs, beside finding the zone. */
|
|
108
|
+
const ORIGIN_CA_PERMISSIONS = "Zone:Read and SSL and Certificates:Edit";
|
|
109
|
+
/** Cloudflare's authentication error codes: a bad or unusable token. */
|
|
110
|
+
const AUTH_CODES = /* @__PURE__ */ new Set([
|
|
111
|
+
6003,
|
|
112
|
+
6111,
|
|
113
|
+
9106,
|
|
114
|
+
9109,
|
|
115
|
+
1e4
|
|
116
|
+
]);
|
|
117
|
+
var CloudflareApi = class {
|
|
118
|
+
options;
|
|
119
|
+
baseUrl;
|
|
120
|
+
sleep;
|
|
121
|
+
maxAttempts;
|
|
122
|
+
zones = /* @__PURE__ */ new Map();
|
|
123
|
+
constructor(options) {
|
|
124
|
+
this.options = options;
|
|
125
|
+
this.baseUrl = (options.baseUrl ?? "https://api.cloudflare.com/client/v4").replace(/\/$/, "");
|
|
126
|
+
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
127
|
+
this.maxAttempts = options.maxAttempts ?? 4;
|
|
128
|
+
}
|
|
129
|
+
async request(domain, method, path, body, permission = DNS_PERMISSIONS) {
|
|
130
|
+
for (let attempt = 1;; attempt++) {
|
|
131
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
132
|
+
method,
|
|
133
|
+
headers: {
|
|
134
|
+
Authorization: `Bearer ${this.options.credential.token}`,
|
|
135
|
+
...body ? { "Content-Type": "application/json" } : {}
|
|
136
|
+
},
|
|
137
|
+
...body ? { body: JSON.stringify(body) } : {}
|
|
138
|
+
});
|
|
139
|
+
if (response.status === 429) {
|
|
140
|
+
if (attempt >= this.maxAttempts) throw new CloudflareRateLimited(attempt);
|
|
141
|
+
const after = Number(response.headers.get("retry-after"));
|
|
142
|
+
await this.sleep(Number.isFinite(after) && after > 0 ? after * 1e3 : 1e3);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
let envelope;
|
|
146
|
+
try {
|
|
147
|
+
envelope = await response.json();
|
|
148
|
+
} catch {
|
|
149
|
+
envelope = void 0;
|
|
150
|
+
}
|
|
151
|
+
if (response.ok && envelope?.success) return envelope;
|
|
152
|
+
const errors = envelope?.errors ?? [];
|
|
153
|
+
const detail = errors.map((e) => e.message).join("; ") || void 0;
|
|
154
|
+
if (response.status === 401 || errors.some((e) => AUTH_CODES.has(e.code))) throw new CloudflareCredentialsInvalid(detail);
|
|
155
|
+
if (response.status === 403) throw new CloudflarePermissionMissing(domain, detail, permission);
|
|
156
|
+
throw new CloudflareRequestFailed(method, path, response.status, errors);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** The zone's id, found once by its name. */
|
|
160
|
+
async zoneId(domain) {
|
|
161
|
+
const key = domain.toLowerCase();
|
|
162
|
+
const known = this.zones.get(key);
|
|
163
|
+
if (known) return known;
|
|
164
|
+
const { result } = await this.request(key, "GET", `/zones?name=${encodeURIComponent(key)}`);
|
|
165
|
+
const zone = result.find((z) => z.name.toLowerCase() === key);
|
|
166
|
+
if (!zone) throw new CloudflareZoneNotFound(key);
|
|
167
|
+
this.zones.set(key, zone.id);
|
|
168
|
+
return zone.id;
|
|
169
|
+
}
|
|
170
|
+
/** Every record of exactly this name and type — of the type, without a name. */
|
|
171
|
+
async getRecords(domain, type, fqdn) {
|
|
172
|
+
const zone = await this.zoneId(domain);
|
|
173
|
+
const found = [];
|
|
174
|
+
for (let page = 1;; page++) {
|
|
175
|
+
const query = new URLSearchParams({
|
|
176
|
+
type,
|
|
177
|
+
...fqdn ? { "name.exact": fqdn } : {},
|
|
178
|
+
per_page: "100",
|
|
179
|
+
page: String(page)
|
|
180
|
+
});
|
|
181
|
+
const envelope = await this.request(domain, "GET", `/zones/${zone}/dns_records?${query}`);
|
|
182
|
+
found.push(...envelope.result);
|
|
183
|
+
const info = envelope.result_info;
|
|
184
|
+
if (!info || info.page >= info.total_pages) return found;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
async createRecord(domain, record) {
|
|
188
|
+
const zone = await this.zoneId(domain);
|
|
189
|
+
const { result } = await this.request(domain, "POST", `/zones/${zone}/dns_records`, record);
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
async updateRecord(domain, id, record) {
|
|
193
|
+
const zone = await this.zoneId(domain);
|
|
194
|
+
const { result } = await this.request(domain, "PATCH", `/zones/${zone}/dns_records/${id}`, record);
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* An origin certificate from Cloudflare's Origin CA, for a CSR made where
|
|
199
|
+
* the key is: trusted by Cloudflare's proxy alone, for `hostnames`, valid
|
|
200
|
+
* `days` (one of 7, 30, 90, 365, 730, 1095, 5475). The token needs
|
|
201
|
+
* `Zone → SSL and Certificates → Edit` on the zone.
|
|
202
|
+
*
|
|
203
|
+
* @param domain - the zone, for what an error says
|
|
204
|
+
*/
|
|
205
|
+
async createOriginCertificate(domain, request) {
|
|
206
|
+
const { result } = await this.request(domain, "POST", "/certificates", {
|
|
207
|
+
csr: request.csr,
|
|
208
|
+
hostnames: [...request.hostnames],
|
|
209
|
+
requested_validity: request.days,
|
|
210
|
+
request_type: request.type
|
|
211
|
+
}, ORIGIN_CA_PERMISSIONS);
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
async deleteRecord(domain, id) {
|
|
215
|
+
const zone = await this.zoneId(domain);
|
|
216
|
+
await this.request(domain, "DELETE", `/zones/${zone}/dns_records/${id}`);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/dns/cloudflare/index.ts
|
|
221
|
+
/**
|
|
222
|
+
* Cloudflare DNS Provider
|
|
223
|
+
*
|
|
224
|
+
* Implements DnsProvider against Cloudflare's v4 API, one name and type at a
|
|
225
|
+
* time: it reads and writes only the A, AAAA, CNAME and MX records of the
|
|
226
|
+
* names it is handed, inside the zone found by the domain's name, and never
|
|
227
|
+
* the zone's settings or the account. An API token with `Zone:Read` and
|
|
228
|
+
* `DNS:Edit` on the zone is all it needs.
|
|
229
|
+
*
|
|
230
|
+
* Cloudflare is also a CDN: a record it proxies (`proxied: true`, the orange
|
|
231
|
+
* cloud) is answered by Cloudflare's edge, which forwards to the record's
|
|
232
|
+
* value. A write that says `proxied` sets it; one that does not leaves an
|
|
233
|
+
* existing record's as it was and creates a new one unproxied — so a
|
|
234
|
+
* certificate's validation CNAME, which Cloudflare must not answer, is
|
|
235
|
+
* never proxied by accident. A proxied record's TTL is always Cloudflare's
|
|
236
|
+
* automatic one.
|
|
237
|
+
*
|
|
238
|
+
* Cloudflare can hold several records of one name and type; an upsert leaves
|
|
239
|
+
* exactly one, with its value. An MX value is `<priority> <host>` on the
|
|
240
|
+
* DnsProvider side, as everywhere in gkm; Cloudflare keeps the priority
|
|
241
|
+
* apart, so it is split on a write and joined on a read.
|
|
242
|
+
*
|
|
243
|
+
* Credentials: `CLOUDFLARE_API_TOKEN`, else what `gkm login --provider
|
|
244
|
+
* cloudflare` stored. Never logged.
|
|
245
|
+
*/
|
|
246
|
+
/** Cloudflare refuses a TTL under this, but for its automatic one. */
|
|
247
|
+
const CLOUDFLARE_MIN_TTL = 60;
|
|
248
|
+
/** A value compared the way DNS does: case-insensitive, no trailing dot. */
|
|
249
|
+
function normalize(value) {
|
|
250
|
+
return value.trim().replace(/\.$/, "").replace(/\s+/g, " ").toLowerCase();
|
|
251
|
+
}
|
|
252
|
+
/** `api` under `example.com` → `api.example.com`; `@` → `example.com`. */
|
|
253
|
+
function fullName(name, domain) {
|
|
254
|
+
const zone = domain.toLowerCase().replace(/\.$/, "");
|
|
255
|
+
const label = name.trim().toLowerCase().replace(/\.$/, "");
|
|
256
|
+
if (label === "@" || label === "" || label === zone) return zone;
|
|
257
|
+
return label.endsWith(`.${zone}`) ? label : `${label}.${zone}`;
|
|
258
|
+
}
|
|
259
|
+
/** A Cloudflare record's value in gkm's form: an MX's is `<priority> <host>`. */
|
|
260
|
+
function valueOf(record) {
|
|
261
|
+
return record.type === "MX" ? mxValue(record.priority ?? 0, record.content) : record.content;
|
|
262
|
+
}
|
|
263
|
+
function managed(type) {
|
|
264
|
+
return CLOUDFLARE_MANAGED_TYPES.includes(type);
|
|
265
|
+
}
|
|
266
|
+
/** Only an address or an alias can be proxied. */
|
|
267
|
+
function proxiable(type) {
|
|
268
|
+
return type === "A" || type === "AAAA" || type === "CNAME";
|
|
269
|
+
}
|
|
270
|
+
var CloudflareProvider = class {
|
|
271
|
+
options;
|
|
272
|
+
name = "cloudflare";
|
|
273
|
+
api = null;
|
|
274
|
+
constructor(options) {
|
|
275
|
+
this.options = options;
|
|
276
|
+
}
|
|
277
|
+
async getApi() {
|
|
278
|
+
if (this.api) return this.api;
|
|
279
|
+
const credential = await this.options.credential();
|
|
280
|
+
this.api = new CloudflareApi({
|
|
281
|
+
credential,
|
|
282
|
+
...this.options.baseUrl ? { baseUrl: this.options.baseUrl } : {},
|
|
283
|
+
...this.options.sleep ? { sleep: this.options.sleep } : {},
|
|
284
|
+
...this.options.maxAttempts ? { maxAttempts: this.options.maxAttempts } : {}
|
|
285
|
+
});
|
|
286
|
+
return this.api;
|
|
287
|
+
}
|
|
288
|
+
assertManaged(domain, type, name) {
|
|
289
|
+
if (!managed(type)) throw new CloudflareRecordNotAllowed(domain, type, name);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* The managed records of the whole zone: A, AAAA, CNAME and MX. Prefer
|
|
293
|
+
* {@link readRecords}, which asks for exactly the names it needs.
|
|
294
|
+
*/
|
|
295
|
+
async getRecords(domain) {
|
|
296
|
+
const api = await this.getApi();
|
|
297
|
+
const zone = domain.toLowerCase();
|
|
298
|
+
const out = [];
|
|
299
|
+
for (const type of CLOUDFLARE_MANAGED_TYPES) {
|
|
300
|
+
const records = await api.getRecords(zone, type);
|
|
301
|
+
out.push(...group(zone, type, records));
|
|
302
|
+
}
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
/** The records of exactly these names and types, one request each. */
|
|
306
|
+
async readRecords(domain, wanted) {
|
|
307
|
+
for (const { name, type } of wanted) this.assertManaged(domain, type, name);
|
|
308
|
+
const api = await this.getApi();
|
|
309
|
+
const found = [];
|
|
310
|
+
for (const { name, type } of wanted) {
|
|
311
|
+
const records = await api.getRecords(domain, type, fullName(name, domain));
|
|
312
|
+
found.push(...group(domain, type, records, name));
|
|
313
|
+
}
|
|
314
|
+
return found;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Each name and type set to exactly its one value: read it, and write it
|
|
318
|
+
* only when its value or its proxy differ. The first record of the name
|
|
319
|
+
* is updated in place and any other of that type deleted, so the set
|
|
320
|
+
* ends up with one.
|
|
321
|
+
*/
|
|
322
|
+
async upsertRecords(domain, records) {
|
|
323
|
+
for (const record of records) this.assertManaged(domain, record.type, record.name);
|
|
324
|
+
const api = await this.getApi();
|
|
325
|
+
const results = [];
|
|
326
|
+
for (const record of records) {
|
|
327
|
+
const type = record.type;
|
|
328
|
+
const name = fullName(record.name, domain);
|
|
329
|
+
const [first, ...rest] = await api.getRecords(domain, type, name);
|
|
330
|
+
const proxied = proxiable(type) ? record.proxied ?? first?.proxied ?? false : void 0;
|
|
331
|
+
if (first && rest.length === 0 && normalize(valueOf(first)) === normalize(record.value) && (proxied === void 0 || (first.proxied ?? false) === proxied)) {
|
|
332
|
+
results.push({
|
|
333
|
+
record,
|
|
334
|
+
created: false,
|
|
335
|
+
unchanged: true
|
|
336
|
+
});
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const input = recordInput(type, name, record, proxied);
|
|
340
|
+
if (first) await api.updateRecord(domain, first.id, input);
|
|
341
|
+
else await api.createRecord(domain, input);
|
|
342
|
+
for (const extra of rest) await api.deleteRecord(domain, extra.id);
|
|
343
|
+
results.push({
|
|
344
|
+
record,
|
|
345
|
+
created: !first,
|
|
346
|
+
unchanged: false
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return results;
|
|
350
|
+
}
|
|
351
|
+
async deleteRecords(domain, records) {
|
|
352
|
+
for (const record of records) this.assertManaged(domain, record.type, record.name);
|
|
353
|
+
const api = await this.getApi();
|
|
354
|
+
const results = [];
|
|
355
|
+
for (const record of records) {
|
|
356
|
+
const existing = await api.getRecords(domain, record.type, fullName(record.name, domain));
|
|
357
|
+
if (existing.length === 0) {
|
|
358
|
+
results.push({
|
|
359
|
+
record,
|
|
360
|
+
deleted: false,
|
|
361
|
+
notFound: true
|
|
362
|
+
});
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
for (const found of existing) await api.deleteRecord(domain, found.id);
|
|
366
|
+
results.push({
|
|
367
|
+
record,
|
|
368
|
+
deleted: true,
|
|
369
|
+
notFound: false
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return results;
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
/** What a write sends for one record. */
|
|
376
|
+
function recordInput(type, name, record, proxied) {
|
|
377
|
+
const ttl = proxied ? 1 : Math.max(record.ttl, CLOUDFLARE_MIN_TTL);
|
|
378
|
+
if (type === "MX") {
|
|
379
|
+
const { priority, host } = parseMxValue(record.value);
|
|
380
|
+
return {
|
|
381
|
+
type,
|
|
382
|
+
name,
|
|
383
|
+
content: host,
|
|
384
|
+
priority,
|
|
385
|
+
ttl
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
return {
|
|
389
|
+
type,
|
|
390
|
+
name,
|
|
391
|
+
content: record.value.trim().replace(/\.$/, ""),
|
|
392
|
+
ttl,
|
|
393
|
+
...proxied !== void 0 ? { proxied } : {}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Cloudflare's records as DnsRecords: one per name, its values together,
|
|
398
|
+
* named relative to the domain (`@` for the apex) — or as `asked`, when the
|
|
399
|
+
* caller named it.
|
|
400
|
+
*/
|
|
401
|
+
function group(domain, type, records, asked) {
|
|
402
|
+
const zone = domain.toLowerCase();
|
|
403
|
+
const byName = /* @__PURE__ */ new Map();
|
|
404
|
+
for (const record of records) {
|
|
405
|
+
const fqdn = record.name.toLowerCase();
|
|
406
|
+
byName.set(fqdn, [...byName.get(fqdn) ?? [], record]);
|
|
407
|
+
}
|
|
408
|
+
return [...byName.entries()].map(([fqdn, set]) => ({
|
|
409
|
+
name: asked ?? (fqdn === zone ? "@" : fqdn.slice(0, fqdn.length - zone.length - 1)),
|
|
410
|
+
type,
|
|
411
|
+
ttl: set[0].ttl === 1 ? 300 : set[0].ttl,
|
|
412
|
+
values: set.map(valueOf),
|
|
413
|
+
...proxiable(type) ? { proxied: set[0].proxied ?? false } : {}
|
|
414
|
+
}));
|
|
415
|
+
}
|
|
416
|
+
//#endregion
|
|
417
|
+
export { CLOUDFLARE_API_BASE, CLOUDFLARE_AUTO_TTL, CLOUDFLARE_MANAGED_TYPES, CloudflareApi, CloudflareCredentialsInvalid, CloudflarePermissionMissing, CloudflareProvider, CloudflareRateLimited, CloudflareRecordNotAllowed, CloudflareRequestFailed, CloudflareZoneNotFound, fullName };
|
|
418
|
+
|
|
419
|
+
//# sourceMappingURL=index.mjs.map
|