-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
74 lines (59 loc) · 2.01 KB
/
server.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
var dnsd = require('dnsd');
// dnsd = dnsd.defaults({convenient: false});
var http = require('http');
var hash = {};
// Sends HTTP GET request to Docker's JSON API
function callDocker(endpoint, done) {
const req = http.request({socketPath: '/var/run/docker.sock', path: endpoint}, (res) => {
var data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => done(JSON.parse(data)));
});
req.on('error', console.error);
req.end();
}
// Sends HTTP GET request to Docker's JSON API
function callDocker(endpoint, done) {
const req = http.request({socketPath: '/var/run/docker.sock', path: endpoint}, (res) => {
var data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => done(JSON.parse(data)));
});
req.on('socket', (socket) => {
socket.setTimeout(500);
socket.on('timeout', () => req.abort());
});
req.on('error', console.error);
req.end();
}
// Call docker client every 1 second and maintain a hash of containerName => IP
setInterval(() => {
callDocker('/containers/json', (containers) => {
containers.forEach(container => {
container.Names.forEach(containerName => {
const name = containerName.replace('/', '');
const ips = [];
for(let network in container.NetworkSettings.Networks) {
ips.push(container.NetworkSettings.Networks[network].IPAddress);
}
hash[name] = ips;
});
});
});
}, 1000);
// DNS Server
var dns = dnsd.createServer((req, res) => {
var question = res.question[0];
var hostname = question.name;
// Only send answer if we have it
if(question.type == 'A' && hash[hostname] !== undefined) {
hash[hostname].forEach(ip => res.answer.push({name: hostname, type: 'A', data: ip, 'ttl': 5}))
console.log(`A [${hostname}] :`, hash[hostname].join(','));
} else {
console.log(`! [${hostname}]`);
}
res.end();
});
dns.on('error', (error) => console.error(error));
dns.listen(53);
console.log('Server running at *:53')