-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathindex.js
More file actions
453 lines (426 loc) · 14.2 KB
/
index.js
File metadata and controls
453 lines (426 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import util from 'util';
import fs from 'fs';
import { ApiClient } from './lib/apiClient.js';
import _ from 'lodash';
import { MessageFormatter, TableFormatter } from './lib/formatters.js';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class CloudflareCli {
constructor(options) {
this.requiredOptions = ['token'];
this.email = options.email || null;
this.key = options.token || null;
this.perPage = 50;
this.format = util.format;
this.cloudflareClient = new ApiClient(options.token, options.email);
this.commands = {
add: {
aliases: ['add', 'addrecord'],
shortcut: 'a',
callback: this.addRecord.bind(this),
description: 'Add a new record',
params: ['name', 'content'],
optionalParams: [],
mergeAdditionalParams: true,
formatter: new MessageFormatter()
},
alwaysUseHttps: {
aliases: ['always-use-https'],
shortcut: 'https',
callback: this.toggleAlwaysUseHttps.bind(this),
params: ['mode'],
optionalParams: [],
description: "Redirect all requests with scheme 'http' to 'https'",
formatter: new MessageFormatter()
},
devmode: {
aliases: ['devmode'],
shortcut: 'dev',
callback: this.toggleDevMode.bind(this),
params: ['mode'],
optionalParams: [],
description: "Turn dev mode on or off",
formatter: new MessageFormatter()
},
disable: {
aliases: ['disable', 'disablecf', 'disablecache', 'disableproxy'],
shortcut: 'dis',
callback: this.disableProxy.bind(this),
description: 'Disable Cloudflare caching for given record',
params: ['name'],
optionalParams: ['content'],
formatter: new MessageFormatter()
},
edit: {
aliases: ['edit', 'editrecord'],
shortcut: 'e',
callback: this.editRecord.bind(this),
description: 'Edit a DNS record',
params: ['name', 'content'],
optionalParams: ['new-type'],
formatter: new MessageFormatter()
},
enable: {
aliases: ['enable', 'enablecf', 'enablecache', 'enableproxy'],
shortcut: 'en',
callback: this.enableProxy.bind(this),
description: 'Enable Cloudflare caching for given record',
params: ['name'],
optionalParams: ['content'],
formatter: new MessageFormatter()
},
find: {
aliases: ['find', 'findrecord'],
shortcut: 'f',
callback: this.findRecord.bind(this),
description: 'Find a record',
params: ['name'],
optionalParams: ['content'],
formatter: new TableFormatter({
head: ['Type', 'Name', 'Value', 'TTL', 'Active', 'ID'],
colWidths: [8, 40, 50, 10, 8, 34],
values: ['type', 'name', 'content', 'ttl', 'proxied', 'id']
})
},
help: {
aliases: ['help'],
shortcut: 'h',
params: [],
callback: this.showHelp.bind(this),
description: 'Show help',
formatter: new MessageFormatter()
},
purge: {
aliases: ['purge', 'purgefile', 'purgecache'],
shortcut: 'p',
callback: this.purgeCache.bind(this),
params: [],
description: 'Purge files from cache'
},
rm: {
aliases: ['rm', 'remove', 'removerecord'],
shortcut: 'r',
callback: this.removeRecord.bind(this),
description: 'Remove a record',
params: ['name'],
optionalParams: ['content'],
formatter: new MessageFormatter()
},
ls: {
aliases: ['ls', 'listrecords', 'list'],
shortcut: 'l',
callback: this.listRecords.bind(this),
params: [],
optionalParams: [],
description: 'List records for given domain',
formatter: new TableFormatter({
head: ['Type', 'Name', 'Value', 'TTL', 'Active', 'ID'],
colWidths: [8, 40, 50, 10, 8, 34],
values: ['type', 'name', 'content', 'ttl', 'proxied', 'id']
})
},
zoneAdd: {
aliases: ['zone-add', 'add-zone', 'addzone'],
shortcut: 'za',
callback: this.addZone.bind(this),
description: 'Add a new zone to your Cloudflare account',
params: ['name'],
optionalParams: [],
formatter: new MessageFormatter()
},
zones: {
aliases: ['zone-ls', 'zones', 'listdomains', 'listzones'],
shortcut: 'z',
callback: this.listZones.bind(this),
params: [],
description: 'List zones in your cloudflare account',
formatter: new TableFormatter({
head: ['Name', 'Plan', 'Active', 'ID', 'Account'],
colWidths: [50, 20, 10, 40, 40],
values: ['name', 'planName', 'status', 'id', 'accountName']
})
}
};
}
runCommand(command, options) {
let cmd = this.getCommand(command);
if (!cmd || command === 'help') {
cmd = this.getCommand('help');
} else {
try {
this.validateConfig(options);
} catch (error) {
console.log(error.message);
process.exit(1);
}
}
let fn = cmd.callback;
let opts = this.mapParams(cmd, options);
fn(opts).then((result) => {
if (cmd.formatter) {
cmd.formatter.format(result.messages, options);
} else {
console.log(result);
}
}).catch((error) => {
let formatter = new MessageFormatter();
if (error.response && error.response.data.errors) {
formatter.format([`Error response received: ${error.response.data.errors[0].message}`]);
} else if (error.message) {
formatter.format([`Error when communicating with api: ${error.message}`]);
} else {
formatter.format([`Error: ${error}`]);
}
process.exit(1);
});
}
async addRecord(options) {
options.type = options.type || 'CNAME';
const zone = await this.getZone(options.domain);
const response = await this.cloudflareClient.addRecord(
zone.id,
_.extend({ ttl: 1 }, this.mapRecordOptions(options)));
return new Result([
this.format(
'Added %s record %s -> %s',
response.data.result.type,
response.data.result.name,
response.data.result.content
)
]);
}
async toggleAlwaysUseHttps(options) {
const zone = await this.getZone(options.domain);
await this.cloudflareClient.setAlwaysUseHttps(zone.id, options.mode);
return new Result([`Always Use HTTPS mode changed to ${options.mode}`]);
}
enableProxy(options) {
options.activate = true;
return this.editRecord(options);
}
disableProxy(options) {
options.activate = false;
return this.editRecord(options);
}
async editRecord(options) {
const queryResonse = await this.find(options.domain, this.getQueryParams(options, ['name', 'type', 'query']))
const records = queryResonse.data.result;
options = this.mapRecordOptions(options);
if (records.length === 0) {
throw new Error('No matching records found');
} else if (records.length > 1) {
throw new Error(this.format('%d matching records found, unable to update', records.count));
}
const record = records[0];
options.type = options.newtype || record.type;
options.content = options.content || record.content;
const zone = await this.getZone(options.domain);
const editResponse = await this.cloudflareClient.editRecord(zone.id, record.id, options);
const updatedRecord = editResponse.data.result;
return new Result([
this.format('Updated %s record %s (id: %s)', updatedRecord.type, updatedRecord.content, updatedRecord.id)
]);
}
async removeRecord(options) {
let query = this.getQueryParams(options, ['name', 'content', 'type', 'query']);
if (query.name === undefined) {
return Promise.reject('name not provided');
}
const response = await this.find(options.domain, query);
if (response.data.result.length === 0) {
throw new Error('No matching records found');
}
const zone = await this.getZone(options.domain);
let results = [];
_.each(response.data.result, (record) => {
results.push(this.cloudflareClient.removeRecord(zone.id, record.id));
});
const responses = await Promise.all(results);
return new Result(_.map(responses, (response) => {
return `Deleted record ${query.name} with id ${response.data.result.id}`;
}));
}
async findRecord(options) {
let query = this.getQueryParams(options, ['name', 'content', 'type', 'query']);
const result = await this.find(options.domain, query);
return new Result(result.data.result);
}
async find(domain, query) {
if (query.query) {
query = _.extend(query, query.query);
delete query.query;
}
if (query.name && !query.name.includes(domain)) {
query.name = `${query.name}.${domain}`;
}
const zone = await this.getZone(domain);
return this.cloudflareClient.findRecord(zone.id, query);
}
async listRecords(options) {
return this.getZone(options.domain).then((zone) => {
return this.cloudflareClient.findRecord(zone.id, { page: 1, per_page: this.perPage })
.then((response) => {
let promises = [Promise.resolve(response)];
for (let i = 2; i <= response.data['result_info']['total_pages']; i++) {
promises.push(this.cloudflareClient.findRecord(zone.id, { page: i, per_page: this.perPage }));
}
return Promise.all(promises);
});
}).then((responses) => {
let rows = [];
_.each(responses, (response) => {
_.each(response.data.result, (item) => {
item.ttl = (item.ttl === 1) ? 'Auto' : item.ttl;
rows.push(item);
});
});
return new Result(rows);
});
}
addZone(options) {
return this.cloudflareClient.addZone(options.name).then((response) => {
return new Result([
this.format(
'Added zone %s',
response.data.result.name
)
]);
});
}
async removeZone(options) {
return this.getZone(options.name).then((zone) => {
return this.cloudflareClient.removeZone(zone.id);
}).then((response) => {
return new Result([`Deleted zone with id ${response.data.result.id}`]);
});
}
async listZones() {
return this.cloudflareClient.findZones({ page: 1, per_page: this.perPage })
.then((response) => {
let promises = [Promise.resolve(response)];
for (let i = 2; i <= response.data['result_info']['total_pages']; i++) {
promises.push(this.cloudflareClient.findZones({ page: i, per_page: this.perPage }));
}
return Promise.all(promises);
})
.then((responses) => {
let rows = [];
_.each(responses, (response) => {
_.each(response.data.result, (item) => {
rows.push(_.extend(
item,
{
planName: item.plan.name,
accountName: item.account.name
}
));
});
});
return new Result(rows);
});
}
async toggleDevMode(options) {
return this.getZone(options.domain).then((zone) => {
return this.cloudflareClient.setDevelopmentMode(zone.id, options.mode);
}).then(() => {
return new Result([`Dev mode changed to ${options.mode}`]);
});
}
async purgeCache(options) {
return this.getZone(options.domain).then((zone) => {
let query = (options._ && options._[1]) ? { files: options._.slice(1) } : { purge_everything: true };
return this.cloudflareClient.purgeCache(zone.id, query);
}).then(() => {
return new Result('Purged cache successfully');
});
}
async getZone(zoneName) {
return this.cloudflareClient.findZones({ name: zoneName }).then((response) => {
if (response.data.result.length === 0) {
throw new Error(`No matching zones found for "${zoneName}"`);
}
return response.data.result[0];
});
}
showHelp() {
return Promise.resolve(new Result([fs.readFileSync(path.join(__dirname, 'doc', 'help.txt'), 'utf8')]));
}
getCommand(commandName) {
return _.find(this.commands, (command) => {
return _.includes(command.aliases, commandName);
});
}
mapParams(cmd, params) {
let query = [];
if (cmd.mergeAdditionalParams && params._ !== undefined) {
let paramCount = cmd.params.length + cmd.optionalParams.length;
params._[paramCount] = _.slice(params._, paramCount).join(' ');
}
if (params.query) {
query = params.query.split(',');
params.query = _.fromPairs(
_.map(query,
(filter) => filter.split(':')
)
);
}
return _.extend(params, _.fromPairs(_.zip(cmd.params.concat(cmd.optionalParams), _.drop(params._, 1))));
}
mapRecordOptions(options) {
if (options.type === 'SRV') {
let contentParts = options.content.split(' ');
let serverParts = options.name.split('.');
options.data = {
service: serverParts[0],
proto: serverParts[1],
name: _.slice(serverParts, 2).join('.'),
priority: parseInt(contentParts[0]),
weight: parseInt(contentParts[1]),
port: parseInt(contentParts[2]),
target: contentParts[3]
};
}
if (options.activate !== undefined) {
options.proxied = options.activate;
}
if (options.name !== undefined) {
options.name = _.toString(options.name);
}
options = _.omit(options, ['domain', 'email', 'token', '_', 'activate', 'a']);
return options;
}
getQueryParams(options, allowed) {
return _(options).pick(allowed)
.omitBy(_.isUndefined)
.mapValues((val) => {
if (val instanceof Object) {
return val;
} else {
return _.toString(val);
}
}).value();
}
validateConfig(config) {
let missing = [];
_.each(this.requiredOptions, (option) => {
if (config[option] === undefined) {
missing.push(option);
}
});
if (missing.length > 0) {
throw new Error(`The following required parameters were not provided: ${missing.join(',')}`);
}
}
}
/**
*
* @param messages
* @constructor
*/
export class Result {
constructor(messages) {
this.messages = messages;
}
}