-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
140 lines (125 loc) · 4.05 KB
/
app.js
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
#!/usr/bin/env node
// UTILS
const _ = require('underscore');
// IP
const publicIp = require('public-ip');
// OVH
const credentials = require('./credentials.json');
const ovh = require('ovh')({
endpoint: 'ovh-eu',
appKey: credentials.appKey,
appSecret: credentials.appSecret,
consumerKey: credentials.consumerKey
});
function listSubdomainsPromise(domain) {
function domainInfoPromise(id) {
return ovh.requestPromised('GET', `/domain/zone/${domain}/record/${id}`);
}
return ovh.requestPromised('GET', `/domain/zone/${domain}/record`, {
fieldType: 'A', // Required: Resource record Name (type: zone.NamedResolutionFieldTypeEnum)
}).then(ids => {
return Promise.all(ids.map(domainInfoPromise));
});
}
function addSubdomainPromise(domain, ip, subdomain) {
return ovh.requestPromised('POST', `/domain/zone/${domain}/record`, {
fieldType: 'A', // Required: Resource record Name (type: zone.NamedResolutionFieldTypeEnum)
subDomain: subdomain, // Resource record subdomain (type: string)
target: ip // Required: Resource record target (type: string)
});
}
function removeSubdomainPromise(domain, all, subdomain) {
function deleteEntryPromise(id) {
return ovh.requestPromised('DELETE', `/domain/zone/${domain}/record/${id}`);
}
return ovh.requestPromised('GET', `/domain/zone/${domain}/record`, {
fieldType: 'A', // Required: Resource record Name (type: zone.NamedResolutionFieldTypeEnum)
subDomain: subdomain // Resource record subdomain (type: string)
}).then(ids => {
if(all && ids.length) {
return Promise.all(ids.map(deleteEntryPromise));
} else if(ids.length) {
return deleteEntryPromise(ids[0]);
} else {
return Promise.reject(`No subdomain '${subdomain}' found.`);
}
});
}
function refreshPromise(domain) {
return ovh.requestPromised('POST', `/domain/zone/${domain}/refresh`);
}
function actionsPromise(domain, ip, add, remove, all) {
return Promise.all(
add.map(addSubdomainPromise.bind(null, domain, ip))
.concat(remove.map(removeSubdomainPromise.bind(null, domain, all)))
);
}
function extractKey(key) {
return _.isArray(key) ? _.last(key) : key
}
function prepareKey(key) {
if(!key) return []; // Empty string.
if(_.isString(key)) return [key]; // Embed in an array.
return _.compact(key); // Remove falsly values.
}
const argv = require('yargs')
.usage('Usage: $0 -d domain [-a subdomain] [-r subdomain [--all]]')
.example('$0 -d google.com --ip=8.8.8.8 -a dns -r dev --all', 'Create the subdomain "dns" and make it targets to 8.8.8.8, delete all subdomains "dev".')
.option('d', {
type: 'string',
alias: 'domain',
description: 'Domain name you want to modify',
demandOption: true,
coerce: extractKey
})
.option('i', {
type: 'string',
implies: 'a',
alias: 'ip',
description: 'IP target of the subdomain',
coerce: extractKey
})
.option('a', {
type: 'string',
implies: 'd',
alias: 'add',
description: 'Subdomain to add',
coerce: prepareKey,
})
.option('r', {
type: 'string',
implies: 'd',
alias: 'remove',
description: 'Subdomain to remove',
coerce: prepareKey,
})
.option('all', {
implies: 'r',
description: 'Remove all the DNS entries matching the name, rather than only one.',
})
.check(({domain, ip}) => {
if(!domain) return false;
if(_.isString(ip) && !/^(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})(.(25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}$/.test(ip)) return false;
return true;
})
.help('h').alias('h', 'help')
.epilog('More info at: https://github.com/Scotow/subdomain')
.argv;
argv.add = argv.add || [];
argv.remove = argv.remove || [];
if(!(argv.add.length + argv.remove.length)) {
listSubdomainsPromise(argv.domain)
.then(infos => console.log(infos.map(({subDomain, target}) => `${subDomain} -> ${target}`).join('\n')))
.catch(console.error);
} else {
if(argv.ip) {
actionsPromise(argv.domain, argv.ip, argv.add, argv.remove, argv.all)
.then(refreshPromise(argv.domain))
.catch(console.error);
} else {
publicIp.v4()
.then(ip => actionsPromise(argv.domain, ip, argv.add, argv.remove, argv.all))
.then(refreshPromise(argv.domain))
.catch(console.error);
}
}