-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (29 loc) · 841 Bytes
/
Copy pathindex.js
File metadata and controls
39 lines (29 loc) · 841 Bytes
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
const dns = require('dns');
const {URL} = require('url');
const cached = {};
function resolveName(tokens) {
const url = tokens.join('.');
return cached[url] = cached[url] || new Promise((resolve, reject) => {
if (tokens.length === 0) {
reject();
}
dns.lookup(url, (err, addr) => {
if (!err) {
console.log(url + ' => ' + addr);
resolve([url, addr]);
} else {
resolve(resolveName(tokens.slice(1)));
}
});
});
}
exports.rulesServer = (server, options) => {
server.on('request', (req, res) => {
res.on('error', function() {});
resolveName(new URL('http://' + req.headers['host']).hostname.split('.')).then(([name, ip]) => {
res.end(JSON.stringify({
rules: '**.' + name + ' host://' + ip
}));
}).catch((err) => res.end());
});
};