-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdns.js
More file actions
337 lines (295 loc) · 7.58 KB
/
dns.js
File metadata and controls
337 lines (295 loc) · 7.58 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
var http = require("http"),
url = require("url"),
util = require("util"),
AWS = require("aws-sdk"),
dns = require("dns"),
Q = require("q"),
Marathon = require("./lib/marathon.js"),
utils = require("./lib/utils.js"),
debug = require("debug")("dns");
debug.marathon = require("debug")("dns:marathon");
debug.route53 = require("debug")("dns:route53");
// Connect to AWS
AWS.config.region = process.env.AWS_REGION;
AWS.config.credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY
};
// Create Route53 client
var route53 = new AWS.Route53();
var marathon = new Marathon({base_url: process.env.MARATHON_HOST});
// State of the previous update
var state = null;
var stateTable = null;
// Start the polling timer
setInterval(function(){
marathon.apps.list().then(function(res) {
// Get the apps and filter them out by 'DNS' environment variable
debug.marathon('querying marathon');
var targets = [];
res.apps.forEach(function(app){
if(typeof app.env['DNS'] != 'undefined')
targets.push({
id: app.id,
dns: app.env['DNS']
});
});
return targets;
})
.then(function(apps){
var q = Q.defer()
//debug.marathon('querying marathon tasks');
marathon.tasks.list().then(function(res){
var records = [];
res.tasks.forEach(function(task){
for(var i=0; i< apps.length; ++i){
var app = apps[i];
if(app.id != task.appId)
continue;
//debug.marathon('creating record: ' + app.dns + '@' + task.host);
records.push({
name: app.dns,
host: task.host
})
}
});
q.resolve(records);
});
return q.promise;
})
.then(function(records){
update(records);
})
.fail(function(err){
debug(err);
});
}, 15000);
// Update route53
function update(records){
// Add the IP Addresses to the table
buildTable(records).then(function(t){
// Quick comparison of the tables to avoid additional calls to Route53
var t0 = stateTable;
var t1 = utils.clone(t);
stateTable = t1;
// Compare now
if(JSON.stringify(t0) == JSON.stringify(t1)){
debug('no changes detected');
return;
}
// Retrieve the hosted zones
var q = Q.defer();
debug.route53('retrieving route53 hosted zones');
route53.listHostedZones({}, function(err, data){
if (err) return q.reject(err);
return q.resolve({table: t, zones: data});
});
return q.promise;
}).then(function(data){
// We must have a data to proceed
if(typeof data === 'undefined' || data == null)
return;
// Group by zone for Route53
var groupByZone = {};
for(var name in data.table){
var hname = name.split('.')
if(hname.length < 3)
continue;
// Get a hosted zone name and a prefix
var zone = hname[hname.length - 2] + '.' + hname[hname.length - 1];
var zone = zone + '.';
// Find the appropriate hosting zone (must exist)
for(var i=0; i< data.zones.HostedZones.length; ++i){
var hostedZone = data.zones.HostedZones[i];
if(hostedZone.Name == zone){
if(typeof groupByZone[zone] === 'undefined'){
groupByZone[zone] = {
id: hostedZone.Id,
rec: [],
del: []
}
}
delete data.table[name]['resolve'];
groupByZone[zone].rec.push({
name: name,
records: data.table[name]
});
break;
}
}
}
// create also public records
var qPubRec = [];
for(var zoneName in groupByZone){
var zone = groupByZone[zoneName];
// for each subdomain
zone.rec.forEach(function(r){
var qAddr = [];
var vAddr = [];
var q = Q.defer();
qPubRec.push(q.promise);
r.records.addr.forEach(function(addr){
qAddr.push(utils.beacon(addr).then(function(b){
vAddr.push(JSON.parse(b).public)
}).fail(function(err){
debug(err);
}));
});
// When we have the vAddr populated
Q.allSettled(qAddr).then(function(){
var pub = utils.clone(r);
pub.name = 'pub.' + pub.name;
pub.records.name = pub.name;
pub.records.addr = vAddr;
groupByZone[zoneName].rec.push(pub);
q.resolve();
});
});
}
// Wait for every ip resolved
Q.allSettled(qPubRec).then(function(){
// make sure the data is comparable
groupByZone = utils.clone(groupByZone);
// Compare current and previous states, modifying it
var current = utils.diff(utils.clone(state), utils.clone(groupByZone));
state = utils.clone(groupByZone);
// Update the records
for(var zoneName in current){
var zone = current[zoneName];
updateRecords(zone).then(function(){
debug.route53('updated ' + zoneName);
})
.fail(function(err){
debug.route53(err);
});
}
})
.fail(function(err){
debug.route53(err);
})
})
.fail(function(err){
debug.route53(err);
});
}
// Updates a single hosted zone
function updateRecords(zone){
var q = Q.defer();
var changes = [];
// Create the list of records
zone.rec.forEach(function(change){
var recordSet = [];
var addresses = utils.distinct(change.records.addr);
addresses.forEach(function(addr){
recordSet.push({
Value: addr
})
});
debug.route53('changing ' + change.name + " to " +addresses);
changes.push({
Action: 'UPSERT',
ResourceRecordSet: {
Name: change.name,
Type: 'A',
ResourceRecords: recordSet,
TTL: 300
}
});
});
// Create the list of deletions
zone.del.forEach(function(deletion){
var recordSet = [];
var addresses = utils.distinct(deletion.records.addr);
addresses.forEach(function(addr){
recordSet.push({
Value: addr
})
});
debug.route53('deleting ' + deletion.name);
changes.push({
Action: 'DELETE',
ResourceRecordSet: {
Name: deletion.name,
Type: 'A',
ResourceRecords: recordSet,
TTL: 300
}
});
});
// Prepare the request
var request = {
HostedZoneId: zone.id,
ChangeBatch: {
Changes: changes
}
};
// Send the request
debug.route53('changing resource record sets: ' + zone.id);
route53.changeResourceRecordSets(request, function(err, data) {
if(err) {
debug.route53(err);
return q.reject(err);
}
return q.resolve()
});
return q.promise;
}
// Build a dns table from the records provided
function buildTable(records){
var table = {};
//debug('building dns table');
// Create the records in the table
records.forEach(function(record){
record.name = (record.name.indexOf('://') != -1)
? url.parse(record.name).hostname
: record.name;
if(typeof table[record.name] === 'undefined')
table[record.name] = {
name: record.name,
resolve: [],
addr: []
};
// Push resolve functions
table[record.name].resolve.push(utils.ipv4(record.host));
});
// Resolve each record
var q = [];
for(var name in table){
table[name].resolve.forEach(function(promise){
q.push(promise);
})
}
return Q.allSettled(q).then(function(results){
for(var name in table){
var requests = table[name].resolve;
for(var i=0; i< requests.length; ++i){
var request = requests[i];
if(request.isFulfilled()){
var value = request.inspect().value;
debug(name + ' => ' + value);
value.forEach(function(addr){
// If we have an array, iterate through
if( Object.prototype.toString.call( addr ) === '[object Array]' ) {
addr.forEach(function(subAddr){
table[name].addr.push(subAddr);
});
}
// Single address
if( typeof addr === 'string' ){
table[name].addr.push(addr);
}
})
}
}
}
return table;
}).fail(function(err){
debug(err);
});
}
// Start the server
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Misakai.Dns')
res.end();
}).listen(8053);