-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.js
56 lines (43 loc) · 1.87 KB
/
host.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
"use strict";
var host = exports;
const dgram = require('dgram')
const http = require('http')
host.createServer = function (settings) {
host.port = settings.httpListenerPort;
host.server = http.createServer((req, res) => {
if (req.headers['content-type'] === settings.httpContentType && settings.httpMethod === 'POST') {
let body;
req.on('data', chunk => body=chunk);
req.on('end', () => {
let fallback;
(function queryns (nameserver, message) {
const sock = dgram.createSocket('udp4')
sock.send(message, 0, message.length, settings.dnsNameServers[nameserver].port, settings.dnsNameServers[nameserver].address, function () {
const nextNameServer = nameserver++;
if (nextNameServer < settings.dnsNameServers.length)
{
fallback = setTimeout(function () {
queryns(nextNameServer, message);
}, fallbacktimeout);
}
});
sock.on('message', function (response) {
clearTimeout(fallback);
if (!res.finished)
{
res.statusCode = 200;
res.setHeader('Content-Type', settings.httpContentType);
res.end(response);
}
sock.close();
});
sock.on('error', (e) => console.log({date: Date.now(), error: e}));
}(0, body));
});
}
});
};
host.startServer = function () {
console.log('Host listening on port: ', host.port);
host.server.listen(host.port);
};