@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,431 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_errors = require("../../errors-DlRLQOyZ.cjs");
|
|
3
|
+
const require_dns_index = require("../index.cjs");
|
|
4
|
+
//#region src/dns/cloudflare/api.ts
|
|
5
|
+
/**
|
|
6
|
+
* Cloudflare's v4 DNS API, as gkm uses it: the records of one name and type
|
|
7
|
+
* at a time, inside a zone found by its name.
|
|
8
|
+
*
|
|
9
|
+
* - zone: `GET /zones?name={domain}` — the one call that is not a record's
|
|
10
|
+
* - read: `GET /zones/{zone}/dns_records?type={type}&name.exact={fqdn}`
|
|
11
|
+
* - create: `POST /zones/{zone}/dns_records`
|
|
12
|
+
* - update: `PATCH /zones/{zone}/dns_records/{id}`
|
|
13
|
+
* - delete: `DELETE /zones/{zone}/dns_records/{id}`
|
|
14
|
+
*
|
|
15
|
+
* Never the zone itself, its settings or the account. An API token with
|
|
16
|
+
* `Zone:Read` and `DNS:Edit` on the zone is all it needs, sent as
|
|
17
|
+
* `Authorization: Bearer <token>`.
|
|
18
|
+
*
|
|
19
|
+
* Every answer is the v4 envelope — `{ success, errors, result }` — and a
|
|
20
|
+
* failure is read from its `errors`, not from the status alone. A 429 is
|
|
21
|
+
* waited out a few times (its `Retry-After`, else a second) before the run
|
|
22
|
+
* gives up with {@link CloudflareRateLimited}.
|
|
23
|
+
*/
|
|
24
|
+
const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
|
|
25
|
+
/**
|
|
26
|
+
* Cloudflare's "automatic" TTL — 300 seconds, and the only TTL a proxied
|
|
27
|
+
* record takes.
|
|
28
|
+
*/
|
|
29
|
+
const CLOUDFLARE_AUTO_TTL = 1;
|
|
30
|
+
/** The record types gkm writes. Never TXT, NS, SOA, SRV or CAA. */
|
|
31
|
+
const CLOUDFLARE_MANAGED_TYPES = [
|
|
32
|
+
"A",
|
|
33
|
+
"AAAA",
|
|
34
|
+
"CNAME",
|
|
35
|
+
"MX"
|
|
36
|
+
];
|
|
37
|
+
/** The token is missing, malformed, expired or revoked. */
|
|
38
|
+
var CloudflareCredentialsInvalid = class extends require_errors.EdgeError {
|
|
39
|
+
detail;
|
|
40
|
+
constructor(detail) {
|
|
41
|
+
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\`.`);
|
|
42
|
+
this.detail = detail;
|
|
43
|
+
this.name = "CloudflareCredentialsInvalid";
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/** The token is good, and may not do this to this zone. */
|
|
47
|
+
var CloudflarePermissionMissing = class extends require_errors.EdgeError {
|
|
48
|
+
domain;
|
|
49
|
+
detail;
|
|
50
|
+
permission;
|
|
51
|
+
constructor(domain, detail, permission = DNS_PERMISSIONS) {
|
|
52
|
+
super(`The Cloudflare token was refused on ${domain}${detail ? ` (${detail})` : ""}. Give it ${permission} on ${domain} (dash.cloudflare.com/profile/api-tokens → the token → Edit).`);
|
|
53
|
+
this.domain = domain;
|
|
54
|
+
this.detail = detail;
|
|
55
|
+
this.permission = permission;
|
|
56
|
+
this.name = "CloudflarePermissionMissing";
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/** No zone of that name is in the token's reach. */
|
|
60
|
+
var CloudflareZoneNotFound = class extends require_errors.EdgeError {
|
|
61
|
+
domain;
|
|
62
|
+
constructor(domain) {
|
|
63
|
+
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.`);
|
|
64
|
+
this.domain = domain;
|
|
65
|
+
this.name = "CloudflareZoneNotFound";
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/** Still rate limited after waiting it out. */
|
|
69
|
+
var CloudflareRateLimited = class extends require_errors.EdgeError {
|
|
70
|
+
attempts;
|
|
71
|
+
constructor(attempts) {
|
|
72
|
+
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.`);
|
|
73
|
+
this.attempts = attempts;
|
|
74
|
+
this.name = "CloudflareRateLimited";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
/** Any other answer that is not a success. */
|
|
78
|
+
var CloudflareRequestFailed = class extends require_errors.EdgeError {
|
|
79
|
+
method;
|
|
80
|
+
path;
|
|
81
|
+
status;
|
|
82
|
+
failures;
|
|
83
|
+
constructor(method, path, status, failures) {
|
|
84
|
+
const detail = failures.length > 0 ? failures.map((e) => `${e.code}: ${e.message}`).join("; ") : `HTTP ${status}`;
|
|
85
|
+
super(`Cloudflare refused ${method} ${path}: ${detail}.`);
|
|
86
|
+
this.method = method;
|
|
87
|
+
this.path = path;
|
|
88
|
+
this.status = status;
|
|
89
|
+
this.failures = failures;
|
|
90
|
+
this.name = "CloudflareRequestFailed";
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** A record type gkm does not write. */
|
|
94
|
+
var CloudflareRecordNotAllowed = class extends require_errors.EdgeError {
|
|
95
|
+
domain;
|
|
96
|
+
type;
|
|
97
|
+
record;
|
|
98
|
+
constructor(domain, type, record) {
|
|
99
|
+
super(`gkm does not manage ${type} records, so it will not touch '${record}' ${type} in ${domain}: only ${CLOUDFLARE_MANAGED_TYPES.join(", ")}.`);
|
|
100
|
+
this.domain = domain;
|
|
101
|
+
this.type = type;
|
|
102
|
+
this.record = record;
|
|
103
|
+
this.name = "CloudflareRecordNotAllowed";
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
/** What reading and writing a zone's records needs. */
|
|
107
|
+
const DNS_PERMISSIONS = "Zone:Read and DNS:Edit";
|
|
108
|
+
/** What an origin certificate needs, beside finding the zone. */
|
|
109
|
+
const ORIGIN_CA_PERMISSIONS = "Zone:Read and SSL and Certificates:Edit";
|
|
110
|
+
/** Cloudflare's authentication error codes: a bad or unusable token. */
|
|
111
|
+
const AUTH_CODES = /* @__PURE__ */ new Set([
|
|
112
|
+
6003,
|
|
113
|
+
6111,
|
|
114
|
+
9106,
|
|
115
|
+
9109,
|
|
116
|
+
1e4
|
|
117
|
+
]);
|
|
118
|
+
var CloudflareApi = class {
|
|
119
|
+
options;
|
|
120
|
+
baseUrl;
|
|
121
|
+
sleep;
|
|
122
|
+
maxAttempts;
|
|
123
|
+
zones = /* @__PURE__ */ new Map();
|
|
124
|
+
constructor(options) {
|
|
125
|
+
this.options = options;
|
|
126
|
+
this.baseUrl = (options.baseUrl ?? "https://api.cloudflare.com/client/v4").replace(/\/$/, "");
|
|
127
|
+
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
128
|
+
this.maxAttempts = options.maxAttempts ?? 4;
|
|
129
|
+
}
|
|
130
|
+
async request(domain, method, path, body, permission = DNS_PERMISSIONS) {
|
|
131
|
+
for (let attempt = 1;; attempt++) {
|
|
132
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
133
|
+
method,
|
|
134
|
+
headers: {
|
|
135
|
+
Authorization: `Bearer ${this.options.credential.token}`,
|
|
136
|
+
...body ? { "Content-Type": "application/json" } : {}
|
|
137
|
+
},
|
|
138
|
+
...body ? { body: JSON.stringify(body) } : {}
|
|
139
|
+
});
|
|
140
|
+
if (response.status === 429) {
|
|
141
|
+
if (attempt >= this.maxAttempts) throw new CloudflareRateLimited(attempt);
|
|
142
|
+
const after = Number(response.headers.get("retry-after"));
|
|
143
|
+
await this.sleep(Number.isFinite(after) && after > 0 ? after * 1e3 : 1e3);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
let envelope;
|
|
147
|
+
try {
|
|
148
|
+
envelope = await response.json();
|
|
149
|
+
} catch {
|
|
150
|
+
envelope = void 0;
|
|
151
|
+
}
|
|
152
|
+
if (response.ok && envelope?.success) return envelope;
|
|
153
|
+
const errors = envelope?.errors ?? [];
|
|
154
|
+
const detail = errors.map((e) => e.message).join("; ") || void 0;
|
|
155
|
+
if (response.status === 401 || errors.some((e) => AUTH_CODES.has(e.code))) throw new CloudflareCredentialsInvalid(detail);
|
|
156
|
+
if (response.status === 403) throw new CloudflarePermissionMissing(domain, detail, permission);
|
|
157
|
+
throw new CloudflareRequestFailed(method, path, response.status, errors);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** The zone's id, found once by its name. */
|
|
161
|
+
async zoneId(domain) {
|
|
162
|
+
const key = domain.toLowerCase();
|
|
163
|
+
const known = this.zones.get(key);
|
|
164
|
+
if (known) return known;
|
|
165
|
+
const { result } = await this.request(key, "GET", `/zones?name=${encodeURIComponent(key)}`);
|
|
166
|
+
const zone = result.find((z) => z.name.toLowerCase() === key);
|
|
167
|
+
if (!zone) throw new CloudflareZoneNotFound(key);
|
|
168
|
+
this.zones.set(key, zone.id);
|
|
169
|
+
return zone.id;
|
|
170
|
+
}
|
|
171
|
+
/** Every record of exactly this name and type — of the type, without a name. */
|
|
172
|
+
async getRecords(domain, type, fqdn) {
|
|
173
|
+
const zone = await this.zoneId(domain);
|
|
174
|
+
const found = [];
|
|
175
|
+
for (let page = 1;; page++) {
|
|
176
|
+
const query = new URLSearchParams({
|
|
177
|
+
type,
|
|
178
|
+
...fqdn ? { "name.exact": fqdn } : {},
|
|
179
|
+
per_page: "100",
|
|
180
|
+
page: String(page)
|
|
181
|
+
});
|
|
182
|
+
const envelope = await this.request(domain, "GET", `/zones/${zone}/dns_records?${query}`);
|
|
183
|
+
found.push(...envelope.result);
|
|
184
|
+
const info = envelope.result_info;
|
|
185
|
+
if (!info || info.page >= info.total_pages) return found;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async createRecord(domain, record) {
|
|
189
|
+
const zone = await this.zoneId(domain);
|
|
190
|
+
const { result } = await this.request(domain, "POST", `/zones/${zone}/dns_records`, record);
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
async updateRecord(domain, id, record) {
|
|
194
|
+
const zone = await this.zoneId(domain);
|
|
195
|
+
const { result } = await this.request(domain, "PATCH", `/zones/${zone}/dns_records/${id}`, record);
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* An origin certificate from Cloudflare's Origin CA, for a CSR made where
|
|
200
|
+
* the key is: trusted by Cloudflare's proxy alone, for `hostnames`, valid
|
|
201
|
+
* `days` (one of 7, 30, 90, 365, 730, 1095, 5475). The token needs
|
|
202
|
+
* `Zone → SSL and Certificates → Edit` on the zone.
|
|
203
|
+
*
|
|
204
|
+
* @param domain - the zone, for what an error says
|
|
205
|
+
*/
|
|
206
|
+
async createOriginCertificate(domain, request) {
|
|
207
|
+
const { result } = await this.request(domain, "POST", "/certificates", {
|
|
208
|
+
csr: request.csr,
|
|
209
|
+
hostnames: [...request.hostnames],
|
|
210
|
+
requested_validity: request.days,
|
|
211
|
+
request_type: request.type
|
|
212
|
+
}, ORIGIN_CA_PERMISSIONS);
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
async deleteRecord(domain, id) {
|
|
216
|
+
const zone = await this.zoneId(domain);
|
|
217
|
+
await this.request(domain, "DELETE", `/zones/${zone}/dns_records/${id}`);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/dns/cloudflare/index.ts
|
|
222
|
+
/**
|
|
223
|
+
* Cloudflare DNS Provider
|
|
224
|
+
*
|
|
225
|
+
* Implements DnsProvider against Cloudflare's v4 API, one name and type at a
|
|
226
|
+
* time: it reads and writes only the A, AAAA, CNAME and MX records of the
|
|
227
|
+
* names it is handed, inside the zone found by the domain's name, and never
|
|
228
|
+
* the zone's settings or the account. An API token with `Zone:Read` and
|
|
229
|
+
* `DNS:Edit` on the zone is all it needs.
|
|
230
|
+
*
|
|
231
|
+
* Cloudflare is also a CDN: a record it proxies (`proxied: true`, the orange
|
|
232
|
+
* cloud) is answered by Cloudflare's edge, which forwards to the record's
|
|
233
|
+
* value. A write that says `proxied` sets it; one that does not leaves an
|
|
234
|
+
* existing record's as it was and creates a new one unproxied — so a
|
|
235
|
+
* certificate's validation CNAME, which Cloudflare must not answer, is
|
|
236
|
+
* never proxied by accident. A proxied record's TTL is always Cloudflare's
|
|
237
|
+
* automatic one.
|
|
238
|
+
*
|
|
239
|
+
* Cloudflare can hold several records of one name and type; an upsert leaves
|
|
240
|
+
* exactly one, with its value. An MX value is `<priority> <host>` on the
|
|
241
|
+
* DnsProvider side, as everywhere in gkm; Cloudflare keeps the priority
|
|
242
|
+
* apart, so it is split on a write and joined on a read.
|
|
243
|
+
*
|
|
244
|
+
* Credentials: `CLOUDFLARE_API_TOKEN`, else what `gkm login --provider
|
|
245
|
+
* cloudflare` stored. Never logged.
|
|
246
|
+
*/
|
|
247
|
+
/** Cloudflare refuses a TTL under this, but for its automatic one. */
|
|
248
|
+
const CLOUDFLARE_MIN_TTL = 60;
|
|
249
|
+
/** A value compared the way DNS does: case-insensitive, no trailing dot. */
|
|
250
|
+
function normalize(value) {
|
|
251
|
+
return value.trim().replace(/\.$/, "").replace(/\s+/g, " ").toLowerCase();
|
|
252
|
+
}
|
|
253
|
+
/** `api` under `example.com` → `api.example.com`; `@` → `example.com`. */
|
|
254
|
+
function fullName(name, domain) {
|
|
255
|
+
const zone = domain.toLowerCase().replace(/\.$/, "");
|
|
256
|
+
const label = name.trim().toLowerCase().replace(/\.$/, "");
|
|
257
|
+
if (label === "@" || label === "" || label === zone) return zone;
|
|
258
|
+
return label.endsWith(`.${zone}`) ? label : `${label}.${zone}`;
|
|
259
|
+
}
|
|
260
|
+
/** A Cloudflare record's value in gkm's form: an MX's is `<priority> <host>`. */
|
|
261
|
+
function valueOf(record) {
|
|
262
|
+
return record.type === "MX" ? require_dns_index.mxValue(record.priority ?? 0, record.content) : record.content;
|
|
263
|
+
}
|
|
264
|
+
function managed(type) {
|
|
265
|
+
return CLOUDFLARE_MANAGED_TYPES.includes(type);
|
|
266
|
+
}
|
|
267
|
+
/** Only an address or an alias can be proxied. */
|
|
268
|
+
function proxiable(type) {
|
|
269
|
+
return type === "A" || type === "AAAA" || type === "CNAME";
|
|
270
|
+
}
|
|
271
|
+
var CloudflareProvider = class {
|
|
272
|
+
options;
|
|
273
|
+
name = "cloudflare";
|
|
274
|
+
api = null;
|
|
275
|
+
constructor(options) {
|
|
276
|
+
this.options = options;
|
|
277
|
+
}
|
|
278
|
+
async getApi() {
|
|
279
|
+
if (this.api) return this.api;
|
|
280
|
+
const credential = await this.options.credential();
|
|
281
|
+
this.api = new CloudflareApi({
|
|
282
|
+
credential,
|
|
283
|
+
...this.options.baseUrl ? { baseUrl: this.options.baseUrl } : {},
|
|
284
|
+
...this.options.sleep ? { sleep: this.options.sleep } : {},
|
|
285
|
+
...this.options.maxAttempts ? { maxAttempts: this.options.maxAttempts } : {}
|
|
286
|
+
});
|
|
287
|
+
return this.api;
|
|
288
|
+
}
|
|
289
|
+
assertManaged(domain, type, name) {
|
|
290
|
+
if (!managed(type)) throw new CloudflareRecordNotAllowed(domain, type, name);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The managed records of the whole zone: A, AAAA, CNAME and MX. Prefer
|
|
294
|
+
* {@link readRecords}, which asks for exactly the names it needs.
|
|
295
|
+
*/
|
|
296
|
+
async getRecords(domain) {
|
|
297
|
+
const api = await this.getApi();
|
|
298
|
+
const zone = domain.toLowerCase();
|
|
299
|
+
const out = [];
|
|
300
|
+
for (const type of CLOUDFLARE_MANAGED_TYPES) {
|
|
301
|
+
const records = await api.getRecords(zone, type);
|
|
302
|
+
out.push(...group(zone, type, records));
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
306
|
+
/** The records of exactly these names and types, one request each. */
|
|
307
|
+
async readRecords(domain, wanted) {
|
|
308
|
+
for (const { name, type } of wanted) this.assertManaged(domain, type, name);
|
|
309
|
+
const api = await this.getApi();
|
|
310
|
+
const found = [];
|
|
311
|
+
for (const { name, type } of wanted) {
|
|
312
|
+
const records = await api.getRecords(domain, type, fullName(name, domain));
|
|
313
|
+
found.push(...group(domain, type, records, name));
|
|
314
|
+
}
|
|
315
|
+
return found;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Each name and type set to exactly its one value: read it, and write it
|
|
319
|
+
* only when its value or its proxy differ. The first record of the name
|
|
320
|
+
* is updated in place and any other of that type deleted, so the set
|
|
321
|
+
* ends up with one.
|
|
322
|
+
*/
|
|
323
|
+
async upsertRecords(domain, records) {
|
|
324
|
+
for (const record of records) this.assertManaged(domain, record.type, record.name);
|
|
325
|
+
const api = await this.getApi();
|
|
326
|
+
const results = [];
|
|
327
|
+
for (const record of records) {
|
|
328
|
+
const type = record.type;
|
|
329
|
+
const name = fullName(record.name, domain);
|
|
330
|
+
const [first, ...rest] = await api.getRecords(domain, type, name);
|
|
331
|
+
const proxied = proxiable(type) ? record.proxied ?? first?.proxied ?? false : void 0;
|
|
332
|
+
if (first && rest.length === 0 && normalize(valueOf(first)) === normalize(record.value) && (proxied === void 0 || (first.proxied ?? false) === proxied)) {
|
|
333
|
+
results.push({
|
|
334
|
+
record,
|
|
335
|
+
created: false,
|
|
336
|
+
unchanged: true
|
|
337
|
+
});
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
const input = recordInput(type, name, record, proxied);
|
|
341
|
+
if (first) await api.updateRecord(domain, first.id, input);
|
|
342
|
+
else await api.createRecord(domain, input);
|
|
343
|
+
for (const extra of rest) await api.deleteRecord(domain, extra.id);
|
|
344
|
+
results.push({
|
|
345
|
+
record,
|
|
346
|
+
created: !first,
|
|
347
|
+
unchanged: false
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
return results;
|
|
351
|
+
}
|
|
352
|
+
async deleteRecords(domain, records) {
|
|
353
|
+
for (const record of records) this.assertManaged(domain, record.type, record.name);
|
|
354
|
+
const api = await this.getApi();
|
|
355
|
+
const results = [];
|
|
356
|
+
for (const record of records) {
|
|
357
|
+
const existing = await api.getRecords(domain, record.type, fullName(record.name, domain));
|
|
358
|
+
if (existing.length === 0) {
|
|
359
|
+
results.push({
|
|
360
|
+
record,
|
|
361
|
+
deleted: false,
|
|
362
|
+
notFound: true
|
|
363
|
+
});
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
for (const found of existing) await api.deleteRecord(domain, found.id);
|
|
367
|
+
results.push({
|
|
368
|
+
record,
|
|
369
|
+
deleted: true,
|
|
370
|
+
notFound: false
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
return results;
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
/** What a write sends for one record. */
|
|
377
|
+
function recordInput(type, name, record, proxied) {
|
|
378
|
+
const ttl = proxied ? 1 : Math.max(record.ttl, CLOUDFLARE_MIN_TTL);
|
|
379
|
+
if (type === "MX") {
|
|
380
|
+
const { priority, host } = require_dns_index.parseMxValue(record.value);
|
|
381
|
+
return {
|
|
382
|
+
type,
|
|
383
|
+
name,
|
|
384
|
+
content: host,
|
|
385
|
+
priority,
|
|
386
|
+
ttl
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
type,
|
|
391
|
+
name,
|
|
392
|
+
content: record.value.trim().replace(/\.$/, ""),
|
|
393
|
+
ttl,
|
|
394
|
+
...proxied !== void 0 ? { proxied } : {}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Cloudflare's records as DnsRecords: one per name, its values together,
|
|
399
|
+
* named relative to the domain (`@` for the apex) — or as `asked`, when the
|
|
400
|
+
* caller named it.
|
|
401
|
+
*/
|
|
402
|
+
function group(domain, type, records, asked) {
|
|
403
|
+
const zone = domain.toLowerCase();
|
|
404
|
+
const byName = /* @__PURE__ */ new Map();
|
|
405
|
+
for (const record of records) {
|
|
406
|
+
const fqdn = record.name.toLowerCase();
|
|
407
|
+
byName.set(fqdn, [...byName.get(fqdn) ?? [], record]);
|
|
408
|
+
}
|
|
409
|
+
return [...byName.entries()].map(([fqdn, set]) => ({
|
|
410
|
+
name: asked ?? (fqdn === zone ? "@" : fqdn.slice(0, fqdn.length - zone.length - 1)),
|
|
411
|
+
type,
|
|
412
|
+
ttl: set[0].ttl === 1 ? 300 : set[0].ttl,
|
|
413
|
+
values: set.map(valueOf),
|
|
414
|
+
...proxiable(type) ? { proxied: set[0].proxied ?? false } : {}
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
//#endregion
|
|
418
|
+
exports.CLOUDFLARE_API_BASE = CLOUDFLARE_API_BASE;
|
|
419
|
+
exports.CLOUDFLARE_AUTO_TTL = CLOUDFLARE_AUTO_TTL;
|
|
420
|
+
exports.CLOUDFLARE_MANAGED_TYPES = CLOUDFLARE_MANAGED_TYPES;
|
|
421
|
+
exports.CloudflareApi = CloudflareApi;
|
|
422
|
+
exports.CloudflareCredentialsInvalid = CloudflareCredentialsInvalid;
|
|
423
|
+
exports.CloudflarePermissionMissing = CloudflarePermissionMissing;
|
|
424
|
+
exports.CloudflareProvider = CloudflareProvider;
|
|
425
|
+
exports.CloudflareRateLimited = CloudflareRateLimited;
|
|
426
|
+
exports.CloudflareRecordNotAllowed = CloudflareRecordNotAllowed;
|
|
427
|
+
exports.CloudflareRequestFailed = CloudflareRequestFailed;
|
|
428
|
+
exports.CloudflareZoneNotFound = CloudflareZoneNotFound;
|
|
429
|
+
exports.fullName = fullName;
|
|
430
|
+
|
|
431
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["EdgeError","mxValue","parseMxValue"],"sources":["../../../src/dns/cloudflare/api.ts","../../../src/dns/cloudflare/index.ts"],"sourcesContent":["/**\n * Cloudflare's v4 DNS API, as gkm uses it: the records of one name and type\n * at a time, inside a zone found by its name.\n *\n * - zone: `GET /zones?name={domain}` — the one call that is not a record's\n * - read: `GET /zones/{zone}/dns_records?type={type}&name.exact={fqdn}`\n * - create: `POST /zones/{zone}/dns_records`\n * - update: `PATCH /zones/{zone}/dns_records/{id}`\n * - delete: `DELETE /zones/{zone}/dns_records/{id}`\n *\n * Never the zone itself, its settings or the account. An API token with\n * `Zone:Read` and `DNS:Edit` on the zone is all it needs, sent as\n * `Authorization: Bearer <token>`.\n *\n * Every answer is the v4 envelope — `{ success, errors, result }` — and a\n * failure is read from its `errors`, not from the status alone. A 429 is\n * waited out a few times (its `Retry-After`, else a second) before the run\n * gives up with {@link CloudflareRateLimited}.\n */\n\nimport { EdgeError } from '../../errors';\n\n/** A Cloudflare API token, sent as `Authorization: Bearer <token>`. */\nexport interface CloudflareCredential {\n\ttoken: string;\n}\n\nexport const CLOUDFLARE_API_BASE = 'https://api.cloudflare.com/client/v4';\n\n/**\n * Cloudflare's \"automatic\" TTL — 300 seconds, and the only TTL a proxied\n * record takes.\n */\nexport const CLOUDFLARE_AUTO_TTL = 1;\n\n/** The record types gkm writes. Never TXT, NS, SOA, SRV or CAA. */\nexport const CLOUDFLARE_MANAGED_TYPES = ['A', 'AAAA', 'CNAME', 'MX'] as const;\n\nexport type CloudflareRecordType = (typeof CLOUDFLARE_MANAGED_TYPES)[number];\n\n/** One record as Cloudflare returns it. */\nexport interface CloudflareRecord {\n\tid: string;\n\ttype: string;\n\t/** The full name: `api.example.com`. */\n\tname: string;\n\tcontent: string;\n\tttl: number;\n\t/** Whether Cloudflare's proxy answers for it — its CDN, the orange cloud. */\n\tproxied?: boolean;\n\t/** An MX record's: Cloudflare keeps it apart from `content`. */\n\tpriority?: number;\n}\n\n/** What a write sends. */\nexport interface CloudflareRecordInput {\n\ttype: CloudflareRecordType;\n\t/** The full name: `api.example.com`. */\n\tname: string;\n\tcontent: string;\n\tttl: number;\n\tproxied?: boolean;\n\tpriority?: number;\n}\n\n/** An origin certificate as the Origin CA returns it. */\nexport interface CloudflareOriginCertificate {\n\t/** Its serial number. */\n\tid: string;\n\t/** PEM. */\n\tcertificate: string;\n\thostnames: string[];\n\t/** `2041-11-22 10:00:00 +0000 UTC` */\n\texpires_on: string;\n}\n\n/** The v4 envelope. */\ninterface Envelope<T> {\n\tsuccess: boolean;\n\terrors?: { code: number; message: string }[];\n\tresult: T;\n\tresult_info?: { page: number; total_pages: number };\n}\n\n/** The token is missing, malformed, expired or revoked. */\nexport class CloudflareCredentialsInvalid extends EdgeError {\n\tconstructor(readonly detail?: string) {\n\t\tsuper(\n\t\t\t`Cloudflare refused the API token${detail ? ` (${detail})` : ''}. ` +\n\t\t\t\t'Create one at dash.cloudflare.com/profile/api-tokens with Zone:Read ' +\n\t\t\t\t'and DNS:Edit on the zone, then set CLOUDFLARE_API_TOKEN or run ' +\n\t\t\t\t'`gkm login --provider cloudflare`.',\n\t\t);\n\t\tthis.name = 'CloudflareCredentialsInvalid';\n\t}\n}\n\n/** The token is good, and may not do this to this zone. */\nexport class CloudflarePermissionMissing extends EdgeError {\n\tconstructor(\n\t\treadonly domain: string,\n\t\treadonly detail?: string,\n\t\t/** What the token needs for it: `Zone:Read and DNS:Edit`. */\n\t\treadonly permission: string = DNS_PERMISSIONS,\n\t) {\n\t\tsuper(\n\t\t\t`The Cloudflare token was refused on ${domain}` +\n\t\t\t\t`${detail ? ` (${detail})` : ''}. Give it ${permission} on ` +\n\t\t\t\t`${domain} (dash.cloudflare.com/profile/api-tokens → the token → Edit).`,\n\t\t);\n\t\tthis.name = 'CloudflarePermissionMissing';\n\t}\n}\n\n/** No zone of that name is in the token's reach. */\nexport class CloudflareZoneNotFound extends EdgeError {\n\tconstructor(readonly domain: string) {\n\t\tsuper(\n\t\t\t`Cloudflare has no zone ${domain} the token can read. Add ${domain} to ` +\n\t\t\t\t'the Cloudflare account and point its nameservers at Cloudflare, and ' +\n\t\t\t\t\"make sure the token's zone resources include it.\",\n\t\t);\n\t\tthis.name = 'CloudflareZoneNotFound';\n\t}\n}\n\n/** Still rate limited after waiting it out. */\nexport class CloudflareRateLimited extends EdgeError {\n\tconstructor(readonly attempts: number) {\n\t\tsuper(\n\t\t\t`Cloudflare kept answering 429 Too Many Requests after ${attempts} ` +\n\t\t\t\t'attempts. Wait a few minutes and deploy again: every record written ' +\n\t\t\t\t'so far is kept.',\n\t\t);\n\t\tthis.name = 'CloudflareRateLimited';\n\t}\n}\n\n/** Any other answer that is not a success. */\nexport class CloudflareRequestFailed extends EdgeError {\n\tconstructor(\n\t\treadonly method: string,\n\t\treadonly path: string,\n\t\treadonly status: number,\n\t\t/** Cloudflare's `errors`, as it answered them. */\n\t\treadonly failures: readonly { code: number; message: string }[],\n\t) {\n\t\tconst detail =\n\t\t\tfailures.length > 0\n\t\t\t\t? failures.map((e) => `${e.code}: ${e.message}`).join('; ')\n\t\t\t\t: `HTTP ${status}`;\n\t\tsuper(`Cloudflare refused ${method} ${path}: ${detail}.`);\n\t\tthis.name = 'CloudflareRequestFailed';\n\t}\n}\n\n/** A record type gkm does not write. */\nexport class CloudflareRecordNotAllowed extends EdgeError {\n\tconstructor(\n\t\treadonly domain: string,\n\t\treadonly type: string,\n\t\t/** The record's name, as it was asked for: `api`, `@`. */\n\t\treadonly record: string,\n\t) {\n\t\tsuper(\n\t\t\t`gkm does not manage ${type} records, so it will not touch '${record}' ` +\n\t\t\t\t`${type} in ${domain}: only ${CLOUDFLARE_MANAGED_TYPES.join(', ')}.`,\n\t\t);\n\t\tthis.name = 'CloudflareRecordNotAllowed';\n\t}\n}\n\n/** What reading and writing a zone's records needs. */\nconst DNS_PERMISSIONS = 'Zone:Read and DNS:Edit';\n\n/** What an origin certificate needs, beside finding the zone. */\nconst ORIGIN_CA_PERMISSIONS = 'Zone:Read and SSL and Certificates:Edit';\n\n/** Cloudflare's authentication error codes: a bad or unusable token. */\nconst AUTH_CODES = new Set([6003, 6111, 9106, 9109, 10000]);\n\nexport interface CloudflareApiOptions {\n\tcredential: CloudflareCredential;\n\t/** The API's base URL, for tests. */\n\tbaseUrl?: string;\n\t/** How a 429 is waited out, for tests. */\n\tsleep?: (ms: number) => Promise<void>;\n\tmaxAttempts?: number;\n}\n\nexport class CloudflareApi {\n\tprivate readonly baseUrl: string;\n\tprivate readonly sleep: (ms: number) => Promise<void>;\n\tprivate readonly maxAttempts: number;\n\tprivate readonly zones = new Map<string, string>();\n\n\tconstructor(private readonly options: CloudflareApiOptions) {\n\t\tthis.baseUrl = (options.baseUrl ?? CLOUDFLARE_API_BASE).replace(/\\/$/, '');\n\t\tthis.sleep =\n\t\t\toptions.sleep ??\n\t\t\t((ms) => new Promise((resolve) => setTimeout(resolve, ms)));\n\t\tthis.maxAttempts = options.maxAttempts ?? 4;\n\t}\n\n\tprivate async request<T>(\n\t\tdomain: string,\n\t\tmethod: 'GET' | 'POST' | 'PATCH' | 'DELETE',\n\t\tpath: string,\n\t\tbody?: unknown,\n\t\tpermission: string = DNS_PERMISSIONS,\n\t): Promise<Envelope<T>> {\n\t\tfor (let attempt = 1; ; attempt++) {\n\t\t\tconst response = await fetch(`${this.baseUrl}${path}`, {\n\t\t\t\tmethod,\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${this.options.credential.token}`,\n\t\t\t\t\t...(body ? { 'Content-Type': 'application/json' } : {}),\n\t\t\t\t},\n\t\t\t\t...(body ? { body: JSON.stringify(body) } : {}),\n\t\t\t});\n\t\t\tif (response.status === 429) {\n\t\t\t\tif (attempt >= this.maxAttempts) {\n\t\t\t\t\tthrow new CloudflareRateLimited(attempt);\n\t\t\t\t}\n\t\t\t\tconst after = Number(response.headers.get('retry-after'));\n\t\t\t\tawait this.sleep(\n\t\t\t\t\tNumber.isFinite(after) && after > 0 ? after * 1000 : 1000,\n\t\t\t\t);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlet envelope: Envelope<T> | undefined;\n\t\t\ttry {\n\t\t\t\tenvelope = (await response.json()) as Envelope<T>;\n\t\t\t} catch {\n\t\t\t\tenvelope = undefined;\n\t\t\t}\n\t\t\tif (response.ok && envelope?.success) return envelope;\n\t\t\tconst errors = envelope?.errors ?? [];\n\t\t\tconst detail = errors.map((e) => e.message).join('; ') || undefined;\n\t\t\tif (\n\t\t\t\tresponse.status === 401 ||\n\t\t\t\terrors.some((e) => AUTH_CODES.has(e.code))\n\t\t\t) {\n\t\t\t\tthrow new CloudflareCredentialsInvalid(detail);\n\t\t\t}\n\t\t\tif (response.status === 403) {\n\t\t\t\tthrow new CloudflarePermissionMissing(domain, detail, permission);\n\t\t\t}\n\t\t\tthrow new CloudflareRequestFailed(method, path, response.status, errors);\n\t\t}\n\t}\n\n\t/** The zone's id, found once by its name. */\n\tasync zoneId(domain: string): Promise<string> {\n\t\tconst key = domain.toLowerCase();\n\t\tconst known = this.zones.get(key);\n\t\tif (known) return known;\n\t\tconst { result } = await this.request<{ id: string; name: string }[]>(\n\t\t\tkey,\n\t\t\t'GET',\n\t\t\t`/zones?name=${encodeURIComponent(key)}`,\n\t\t);\n\t\tconst zone = result.find((z) => z.name.toLowerCase() === key);\n\t\tif (!zone) throw new CloudflareZoneNotFound(key);\n\t\tthis.zones.set(key, zone.id);\n\t\treturn zone.id;\n\t}\n\n\t/** Every record of exactly this name and type — of the type, without a name. */\n\tasync getRecords(\n\t\tdomain: string,\n\t\ttype: CloudflareRecordType,\n\t\tfqdn?: string,\n\t): Promise<CloudflareRecord[]> {\n\t\tconst zone = await this.zoneId(domain);\n\t\tconst found: CloudflareRecord[] = [];\n\t\tfor (let page = 1; ; page++) {\n\t\t\tconst query = new URLSearchParams({\n\t\t\t\ttype,\n\t\t\t\t...(fqdn ? { 'name.exact': fqdn } : {}),\n\t\t\t\tper_page: '100',\n\t\t\t\tpage: String(page),\n\t\t\t});\n\t\t\tconst envelope = await this.request<CloudflareRecord[]>(\n\t\t\t\tdomain,\n\t\t\t\t'GET',\n\t\t\t\t`/zones/${zone}/dns_records?${query}`,\n\t\t\t);\n\t\t\tfound.push(...envelope.result);\n\t\t\tconst info = envelope.result_info;\n\t\t\tif (!info || info.page >= info.total_pages) return found;\n\t\t}\n\t}\n\n\tasync createRecord(\n\t\tdomain: string,\n\t\trecord: CloudflareRecordInput,\n\t): Promise<CloudflareRecord> {\n\t\tconst zone = await this.zoneId(domain);\n\t\tconst { result } = await this.request<CloudflareRecord>(\n\t\t\tdomain,\n\t\t\t'POST',\n\t\t\t`/zones/${zone}/dns_records`,\n\t\t\trecord,\n\t\t);\n\t\treturn result;\n\t}\n\n\tasync updateRecord(\n\t\tdomain: string,\n\t\tid: string,\n\t\trecord: CloudflareRecordInput,\n\t): Promise<CloudflareRecord> {\n\t\tconst zone = await this.zoneId(domain);\n\t\tconst { result } = await this.request<CloudflareRecord>(\n\t\t\tdomain,\n\t\t\t'PATCH',\n\t\t\t`/zones/${zone}/dns_records/${id}`,\n\t\t\trecord,\n\t\t);\n\t\treturn result;\n\t}\n\n\t/**\n\t * An origin certificate from Cloudflare's Origin CA, for a CSR made where\n\t * the key is: trusted by Cloudflare's proxy alone, for `hostnames`, valid\n\t * `days` (one of 7, 30, 90, 365, 730, 1095, 5475). The token needs\n\t * `Zone → SSL and Certificates → Edit` on the zone.\n\t *\n\t * @param domain - the zone, for what an error says\n\t */\n\tasync createOriginCertificate(\n\t\tdomain: string,\n\t\trequest: {\n\t\t\tcsr: string;\n\t\t\thostnames: readonly string[];\n\t\t\tdays: number;\n\t\t\ttype: 'origin-rsa' | 'origin-ecc';\n\t\t},\n\t): Promise<CloudflareOriginCertificate> {\n\t\tconst { result } = await this.request<CloudflareOriginCertificate>(\n\t\t\tdomain,\n\t\t\t'POST',\n\t\t\t'/certificates',\n\t\t\t{\n\t\t\t\tcsr: request.csr,\n\t\t\t\thostnames: [...request.hostnames],\n\t\t\t\trequested_validity: request.days,\n\t\t\t\trequest_type: request.type,\n\t\t\t},\n\t\t\tORIGIN_CA_PERMISSIONS,\n\t\t);\n\t\treturn result;\n\t}\n\n\tasync deleteRecord(domain: string, id: string): Promise<void> {\n\t\tconst zone = await this.zoneId(domain);\n\t\tawait this.request<unknown>(\n\t\t\tdomain,\n\t\t\t'DELETE',\n\t\t\t`/zones/${zone}/dns_records/${id}`,\n\t\t);\n\t}\n}\n","/**\n * Cloudflare DNS Provider\n *\n * Implements DnsProvider against Cloudflare's v4 API, one name and type at a\n * time: it reads and writes only the A, AAAA, CNAME and MX records of the\n * names it is handed, inside the zone found by the domain's name, and never\n * the zone's settings or the account. An API token with `Zone:Read` and\n * `DNS:Edit` on the zone is all it needs.\n *\n * Cloudflare is also a CDN: a record it proxies (`proxied: true`, the orange\n * cloud) is answered by Cloudflare's edge, which forwards to the record's\n * value. A write that says `proxied` sets it; one that does not leaves an\n * existing record's as it was and creates a new one unproxied — so a\n * certificate's validation CNAME, which Cloudflare must not answer, is\n * never proxied by accident. A proxied record's TTL is always Cloudflare's\n * automatic one.\n *\n * Cloudflare can hold several records of one name and type; an upsert leaves\n * exactly one, with its value. An MX value is `<priority> <host>` on the\n * DnsProvider side, as everywhere in gkm; Cloudflare keeps the priority\n * apart, so it is split on a write and joined on a read.\n *\n * Credentials: `CLOUDFLARE_API_TOKEN`, else what `gkm login --provider\n * cloudflare` stored. Never logged.\n */\n\nimport {\n\ttype DeleteDnsRecord,\n\ttype DeleteResult,\n\ttype DnsProvider,\n\ttype DnsRecord,\n\tmxValue,\n\tparseMxValue,\n\ttype UpsertDnsRecord,\n\ttype UpsertResult,\n} from '../index';\nimport {\n\tCLOUDFLARE_AUTO_TTL,\n\tCLOUDFLARE_MANAGED_TYPES,\n\tCloudflareApi,\n\ttype CloudflareApiOptions,\n\ttype CloudflareCredential,\n\ttype CloudflareRecord,\n\ttype CloudflareRecordInput,\n\tCloudflareRecordNotAllowed,\n\ttype CloudflareRecordType,\n} from './api';\n\n/** Cloudflare refuses a TTL under this, but for its automatic one. */\nconst CLOUDFLARE_MIN_TTL = 60;\n\nexport interface CloudflareProviderOptions {\n\t/**\n\t * Where the token comes from, asked the first time the API is called. It\n\t * throws when there is none: what to do about a missing token is the\n\t * caller's to say.\n\t */\n\tcredential: () => Promise<CloudflareCredential>;\n\t/** The API's base URL, for tests. */\n\tbaseUrl?: string;\n\t/** How a 429 is waited out, for tests. */\n\tsleep?: CloudflareApiOptions['sleep'];\n\tmaxAttempts?: number;\n}\n\n/** A value compared the way DNS does: case-insensitive, no trailing dot. */\nfunction normalize(value: string): string {\n\treturn value.trim().replace(/\\.$/, '').replace(/\\s+/g, ' ').toLowerCase();\n}\n\n/** `api` under `example.com` → `api.example.com`; `@` → `example.com`. */\nexport function fullName(name: string, domain: string): string {\n\tconst zone = domain.toLowerCase().replace(/\\.$/, '');\n\tconst label = name.trim().toLowerCase().replace(/\\.$/, '');\n\tif (label === '@' || label === '' || label === zone) return zone;\n\treturn label.endsWith(`.${zone}`) ? label : `${label}.${zone}`;\n}\n\n/** A Cloudflare record's value in gkm's form: an MX's is `<priority> <host>`. */\nfunction valueOf(record: CloudflareRecord): string {\n\treturn record.type === 'MX'\n\t\t? mxValue(record.priority ?? 0, record.content)\n\t\t: record.content;\n}\n\nfunction managed(type: string): type is CloudflareRecordType {\n\treturn (CLOUDFLARE_MANAGED_TYPES as readonly string[]).includes(type);\n}\n\n/** Only an address or an alias can be proxied. */\nfunction proxiable(type: string): boolean {\n\treturn type === 'A' || type === 'AAAA' || type === 'CNAME';\n}\n\nexport class CloudflareProvider implements DnsProvider {\n\treadonly name = 'cloudflare';\n\tprivate api: CloudflareApi | null = null;\n\n\tconstructor(private readonly options: CloudflareProviderOptions) {}\n\n\tprivate async getApi(): Promise<CloudflareApi> {\n\t\tif (this.api) return this.api;\n\t\tconst credential = await this.options.credential();\n\t\tthis.api = new CloudflareApi({\n\t\t\tcredential,\n\t\t\t...(this.options.baseUrl ? { baseUrl: this.options.baseUrl } : {}),\n\t\t\t...(this.options.sleep ? { sleep: this.options.sleep } : {}),\n\t\t\t...(this.options.maxAttempts\n\t\t\t\t? { maxAttempts: this.options.maxAttempts }\n\t\t\t\t: {}),\n\t\t});\n\t\treturn this.api;\n\t}\n\n\tprivate assertManaged(domain: string, type: string, name: string): void {\n\t\tif (!managed(type))\n\t\t\tthrow new CloudflareRecordNotAllowed(domain, type, name);\n\t}\n\n\t/**\n\t * The managed records of the whole zone: A, AAAA, CNAME and MX. Prefer\n\t * {@link readRecords}, which asks for exactly the names it needs.\n\t */\n\tasync getRecords(domain: string): Promise<DnsRecord[]> {\n\t\tconst api = await this.getApi();\n\t\tconst zone = domain.toLowerCase();\n\t\tconst out: DnsRecord[] = [];\n\t\tfor (const type of CLOUDFLARE_MANAGED_TYPES) {\n\t\t\tconst records = await api.getRecords(zone, type);\n\t\t\tout.push(...group(zone, type, records));\n\t\t}\n\t\treturn out;\n\t}\n\n\t/** The records of exactly these names and types, one request each. */\n\tasync readRecords(\n\t\tdomain: string,\n\t\twanted: readonly DeleteDnsRecord[],\n\t): Promise<DnsRecord[]> {\n\t\tfor (const { name, type } of wanted) this.assertManaged(domain, type, name);\n\t\tconst api = await this.getApi();\n\t\tconst found: DnsRecord[] = [];\n\t\tfor (const { name, type } of wanted) {\n\t\t\tconst records = await api.getRecords(\n\t\t\t\tdomain,\n\t\t\t\ttype as CloudflareRecordType,\n\t\t\t\tfullName(name, domain),\n\t\t\t);\n\t\t\tfound.push(...group(domain, type as CloudflareRecordType, records, name));\n\t\t}\n\t\treturn found;\n\t}\n\n\t/**\n\t * Each name and type set to exactly its one value: read it, and write it\n\t * only when its value or its proxy differ. The first record of the name\n\t * is updated in place and any other of that type deleted, so the set\n\t * ends up with one.\n\t */\n\tasync upsertRecords(\n\t\tdomain: string,\n\t\trecords: UpsertDnsRecord[],\n\t): Promise<UpsertResult[]> {\n\t\t// Every record is checked before any is written, so a refused one\n\t\t// leaves the zone as it was.\n\t\tfor (const record of records) {\n\t\t\tthis.assertManaged(domain, record.type, record.name);\n\t\t}\n\t\tconst api = await this.getApi();\n\n\t\tconst results: UpsertResult[] = [];\n\t\tfor (const record of records) {\n\t\t\tconst type = record.type as CloudflareRecordType;\n\t\t\tconst name = fullName(record.name, domain);\n\t\t\tconst existing = await api.getRecords(domain, type, name);\n\t\t\tconst [first, ...rest] = existing;\n\t\t\tconst proxied = proxiable(type)\n\t\t\t\t? (record.proxied ?? first?.proxied ?? false)\n\t\t\t\t: undefined;\n\n\t\t\tif (\n\t\t\t\tfirst &&\n\t\t\t\trest.length === 0 &&\n\t\t\t\tnormalize(valueOf(first)) === normalize(record.value) &&\n\t\t\t\t(proxied === undefined || (first.proxied ?? false) === proxied)\n\t\t\t) {\n\t\t\t\tresults.push({ record, created: false, unchanged: true });\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst input = recordInput(type, name, record, proxied);\n\t\t\tif (first) await api.updateRecord(domain, first.id, input);\n\t\t\telse await api.createRecord(domain, input);\n\t\t\tfor (const extra of rest) await api.deleteRecord(domain, extra.id);\n\t\t\tresults.push({ record, created: !first, unchanged: false });\n\t\t}\n\t\treturn results;\n\t}\n\n\tasync deleteRecords(\n\t\tdomain: string,\n\t\trecords: DeleteDnsRecord[],\n\t): Promise<DeleteResult[]> {\n\t\tfor (const record of records) {\n\t\t\tthis.assertManaged(domain, record.type, record.name);\n\t\t}\n\t\tconst api = await this.getApi();\n\t\tconst results: DeleteResult[] = [];\n\t\tfor (const record of records) {\n\t\t\tconst existing = await api.getRecords(\n\t\t\t\tdomain,\n\t\t\t\trecord.type as CloudflareRecordType,\n\t\t\t\tfullName(record.name, domain),\n\t\t\t);\n\t\t\tif (existing.length === 0) {\n\t\t\t\tresults.push({ record, deleted: false, notFound: true });\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tfor (const found of existing) await api.deleteRecord(domain, found.id);\n\t\t\tresults.push({ record, deleted: true, notFound: false });\n\t\t}\n\t\treturn results;\n\t}\n}\n\n/** What a write sends for one record. */\nfunction recordInput(\n\ttype: CloudflareRecordType,\n\tname: string,\n\trecord: UpsertDnsRecord,\n\tproxied: boolean | undefined,\n): CloudflareRecordInput {\n\tconst ttl = proxied\n\t\t? CLOUDFLARE_AUTO_TTL\n\t\t: Math.max(record.ttl, CLOUDFLARE_MIN_TTL);\n\tif (type === 'MX') {\n\t\tconst { priority, host } = parseMxValue(record.value);\n\t\treturn { type, name, content: host, priority, ttl };\n\t}\n\treturn {\n\t\ttype,\n\t\tname,\n\t\tcontent: record.value.trim().replace(/\\.$/, ''),\n\t\tttl,\n\t\t...(proxied !== undefined ? { proxied } : {}),\n\t};\n}\n\n/**\n * Cloudflare's records as DnsRecords: one per name, its values together,\n * named relative to the domain (`@` for the apex) — or as `asked`, when the\n * caller named it.\n */\nfunction group(\n\tdomain: string,\n\ttype: CloudflareRecordType,\n\trecords: readonly CloudflareRecord[],\n\tasked?: string,\n): DnsRecord[] {\n\tconst zone = domain.toLowerCase();\n\tconst byName = new Map<string, CloudflareRecord[]>();\n\tfor (const record of records) {\n\t\tconst fqdn = record.name.toLowerCase();\n\t\tbyName.set(fqdn, [...(byName.get(fqdn) ?? []), record]);\n\t}\n\treturn [...byName.entries()].map(([fqdn, set]) => ({\n\t\tname:\n\t\t\tasked ??\n\t\t\t(fqdn === zone ? '@' : fqdn.slice(0, fqdn.length - zone.length - 1)),\n\t\ttype,\n\t\tttl: set[0]!.ttl === CLOUDFLARE_AUTO_TTL ? 300 : set[0]!.ttl,\n\t\tvalues: set.map(valueOf),\n\t\t...(proxiable(type) ? { proxied: set[0]!.proxied ?? false } : {}),\n\t}));\n}\n\nexport {\n\tCLOUDFLARE_API_BASE,\n\tCLOUDFLARE_AUTO_TTL,\n\tCLOUDFLARE_MANAGED_TYPES,\n\tCloudflareApi,\n\ttype CloudflareApiOptions,\n\ttype CloudflareCredential,\n\tCloudflareCredentialsInvalid,\n\ttype CloudflareOriginCertificate,\n\tCloudflarePermissionMissing,\n\tCloudflareRateLimited,\n\ttype CloudflareRecord,\n\ttype CloudflareRecordInput,\n\tCloudflareRecordNotAllowed,\n\ttype CloudflareRecordType,\n\tCloudflareRequestFailed,\n\tCloudflareZoneNotFound,\n} from './api';\n"],"mappings":";;;;;;;;;;;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OAAO,KAAK,YAAY;EACrC,OAAO,IAAI,MAAM,CAAC,GAAI,OAAO,IAAI,IAAI,KAAK,CAAC,GAAI,MAAM,CAAC;CACvD;CACA,OAAO,CAAC,GAAG,OAAO,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,UAAU;EAClD,MACC,UACC,SAAS,OAAO,MAAM,KAAK,MAAM,GAAG,KAAK,SAAS,KAAK,SAAS,CAAC;EACnE;EACA,KAAK,IAAI,EAAE,CAAE,QAAA,IAA8B,MAAM,IAAI,EAAE,CAAE;EACzD,QAAQ,IAAI,IAAI,OAAO;EACvB,GAAI,UAAU,IAAI,IAAI,EAAE,SAAS,IAAI,EAAE,CAAE,WAAW,MAAM,IAAI,CAAC;CAChE,EAAE;AACH"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { t as EdgeError } from "../../errors-DBZfelEN.cjs";
|
|
2
|
+
import { DeleteDnsRecord, DeleteResult, DnsProvider, DnsRecord, UpsertDnsRecord, UpsertResult } from "../index.cjs";
|
|
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.cts.map
|