This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
dns.js
118 lines (106 loc) · 3.58 KB
/
dns.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
const
http = require('http'),
xml2js = new (require('xml2js')).Parser(),
jsel = require('JSONSelect');
function doRequest(api_key, method, path, body, cb) {
var req = http.request({
auth: api_key,
host: 'ns.zerigo.com',
port: 80,
path: path,
method: method,
headers: {
'Content-Type': 'application/xml',
'Content-Length': body ? body.length : 0
}
}, function(r) {
var buf = "";
r.on('data', function(chunk) {
buf += chunk;
});
r.on('end', function() {
if ((r.statusCode / 100).toFixed(0) != 2 &&
r.statusCode != 404) {
return cb("non 200 status: " + r.statusCode + "(" + body + ")");
}
xml2js.parseString(buf, cb);
});
});
if (body) req.write(body);
req.end();
};
exports.updateRecord = function (key, hostname, ip, cb) {
if (typeof key !== 'string' || !hostname) return process.nextTick(function() { cb(null) });
exports.deleteRecord(key, hostname, function(err) {
if (err && err !== 'no such DNS record') return cb(err);
doRequest(key, 'GET', '/api/1.1/zones.xml', null, function(err, r) {
var host = "";
if (err) return cb(err);
var m = jsel.match('object:has(:root > .domain:val(?)) > .id .#',
[ hostname ], r);
if (m.length != 1) {
host = hostname.split('.')[0];
hostname = hostname.split('.').slice(1).join('.');
var m = jsel.match('object:has(:root > .domain:val(?)) > .id .#',
[ hostname ], r);
if (m.length != 1) return cb("couldn't extract domain id from zerigo");
}
var path = '/api/1.1/hosts.xml?zone_id=' + m[0];
var body = '<host><data>' + ip + '</data><host-type>A</host-type>';
body += '<hostname>' + host + '</hostname>'
body += '</host>';
doRequest(key, 'POST', path, body, function(err, r) {
cb(err);
});
});
});
};
exports.findByIP = function (key, ip, cb) {
if (!key) return cb(null, null);
doRequest(key, 'GET', '/api/1.1/zones.xml', null, function(err, r) {
if (err) return cb(err);
var zones = jsel.match('.id .#', r);
if (!zones.length) return cb(null, []);
var done = 0;
var found = [];
var failed;
zones.forEach(function(zone) {
doRequest(key, 'GET', '/api/1.1/zones/' + zone + '/hosts.xml?per_page=1000', null, function(err, r) {
if (failed) return;
if (err) {
failed = err;
return cb(err);
}
var m = jsel.match('object:has(:root > .data:val(?)) > .fqdn', [ ip ], r);
found = found.concat(m);
if (++done === zones.length) cb(null, found);
});
});
});
};
exports.deleteRecord = function (key, hostname, cb) {
doRequest(key, 'GET', '/api/1.1/hosts.xml?fqdn=' + hostname, null, function(err, r) {
if (err) return cb(err);
var m = jsel.match('.host .id > .#', r);
if (!m.length) return cb("no such DNS record");
function deleteOne() {
if (!m.length) return cb(null);
var one = m.shift();
doRequest(key, 'DELETE', '/api/1.1/hosts/' + one + '.xml', null, function(err) {
if (err) return cb(err);
deleteOne();
});
}
deleteOne();
});
};
exports.inUse = function (key, hostname, cb) {
if (!key || !hostname) return process.nextTick(function() { cb(null) });
doRequest(key, 'GET', '/api/1.1/hosts.xml?fqdn=' + hostname, null, function(err, r) {
if (err) return cb(err);
var m = jsel.match('.host', r);
// we shouldn't have multiple! oops! let's return the first one
if (m.length) return cb(null, m[0]);
cb(null, null);
});
}