fcdns 0.2.6 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/lib/cli.js +88 -88
- package/lib/hostname-whitelist.js +17 -17
- package/lib/ip-whitelist.js +26 -26
- package/lib/logger.js +67 -65
- package/lib/logger.js.map +1 -1
- package/lib/resource-record-type.js +79 -79
- package/lib/router.js +81 -81
- package/lib/server.js +116 -116
- package/lib/tester.js +56 -56
- package/lib/utils/countup.js +8 -8
- package/lib/utils/create-dns-resolver.js +10 -10
- package/lib/utils/get-elapsed.js +8 -8
- package/lib/utils/get-timestamp.js +7 -7
- package/lib/utils/hostname-list-file.js +18 -18
- package/lib/utils/hostname-pattern.js +14 -14
- package/lib/utils/ip-list-file.js +43 -39
- package/lib/utils/ip-list-file.js.map +1 -1
- package/lib/utils/is-alive.js +24 -24
- package/lib/utils/is-ipv4-address.js +7 -7
- package/lib/utils/is-ipv6-address.js +7 -7
- package/lib/utils/level-to-string.js +15 -15
- package/lib/utils/map-file.js +27 -27
- package/lib/utils/parse-log-level.js +15 -15
- package/lib/utils/parse-server-info.js +11 -11
- package/lib/utils/resolve-a.js +18 -18
- package/package.json +19 -22
- package/lib/utils/address-ranges-file.js +0 -48
- package/lib/utils/address-ranges-file.js.map +0 -1
- package/lib/whitelist.js +0 -31
- package/lib/whitelist.js.map +0 -1
package/lib/router.js
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Router = exports.Target = void 0;
|
|
4
|
-
const map_file_1 = require("./utils/map-file");
|
|
5
|
-
const resolve_a_1 = require("./utils/resolve-a");
|
|
6
|
-
var Target;
|
|
7
|
-
(function (Target) {
|
|
8
|
-
Target[Target["Untrusted"] = 0] = "Untrusted";
|
|
9
|
-
Target[Target["Trusted"] = 1] = "Trusted";
|
|
10
|
-
})(Target = exports.Target || (exports.Target = {}));
|
|
11
|
-
class Router {
|
|
12
|
-
constructor(cacheFilename, cache, looseMode, tester, untrustedResolver, ipWhitelist, hostnameWhitelist) {
|
|
13
|
-
this.cacheFilename = cacheFilename;
|
|
14
|
-
this.cache = cache;
|
|
15
|
-
this.looseMode = looseMode;
|
|
16
|
-
this.tester = tester;
|
|
17
|
-
this.untrustedResolver = untrustedResolver;
|
|
18
|
-
this.ipWhitelist = ipWhitelist;
|
|
19
|
-
this.hostnameWhitelist = hostnameWhitelist;
|
|
20
|
-
}
|
|
21
|
-
static async create(options) {
|
|
22
|
-
const tester = options.tester;
|
|
23
|
-
const untrustedResolver = options.untrustedResolver;
|
|
24
|
-
const ipWhitelist = options.ipWhitelist;
|
|
25
|
-
const hostnameWhitelist = options.hostnameWhitelist;
|
|
26
|
-
const cacheFilename = options.cacheFilename;
|
|
27
|
-
const looseMode = options.looseMode;
|
|
28
|
-
const cache = await (0, map_file_1.readMapFile)(cacheFilename);
|
|
29
|
-
await (0, map_file_1.writeMapFile)(cacheFilename, cache);
|
|
30
|
-
return new Router(cacheFilename, cache, looseMode, tester, untrustedResolver, ipWhitelist, hostnameWhitelist);
|
|
31
|
-
}
|
|
32
|
-
async getTarget(hostname) {
|
|
33
|
-
if (this.inHostnameWhitelist(hostname))
|
|
34
|
-
return Target.Untrusted;
|
|
35
|
-
if (this.cache.has(hostname)) {
|
|
36
|
-
return this.cache.get(hostname);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
if (this.looseMode) {
|
|
40
|
-
queueMicrotask(() => this.getTargetWithoutCache(hostname));
|
|
41
|
-
return Target.Untrusted;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
return await this.getTargetWithoutCache(hostname);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
async getTargetWithoutCache(hostname) {
|
|
49
|
-
if (await this.tester.isPoisoned(hostname)) {
|
|
50
|
-
this.setCache(hostname, Target.Trusted);
|
|
51
|
-
return Target.Trusted;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
const addresses = await (0, resolve_a_1.resolveA)(this.untrustedResolver, hostname);
|
|
55
|
-
if (addresses.length > 0) {
|
|
56
|
-
if (this.inIPWhitelist(addresses)) {
|
|
57
|
-
this.setCache(hostname, Target.Untrusted);
|
|
58
|
-
return Target.Untrusted;
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
this.setCache(hostname, Target.Trusted);
|
|
62
|
-
return Target.Trusted;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
return Target.Trusted;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
inIPWhitelist(addresses) {
|
|
71
|
-
return addresses.some(x => this.ipWhitelist.includes(x));
|
|
72
|
-
}
|
|
73
|
-
inHostnameWhitelist(hostname) {
|
|
74
|
-
return this.hostnameWhitelist.includes(hostname);
|
|
75
|
-
}
|
|
76
|
-
setCache(hostname, result) {
|
|
77
|
-
this.cache.set(hostname, result);
|
|
78
|
-
(0, map_file_1.appendMapFile)(this.cacheFilename, hostname, result);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
exports.Router = Router;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Router = exports.Target = void 0;
|
|
4
|
+
const map_file_1 = require("./utils/map-file");
|
|
5
|
+
const resolve_a_1 = require("./utils/resolve-a");
|
|
6
|
+
var Target;
|
|
7
|
+
(function (Target) {
|
|
8
|
+
Target[Target["Untrusted"] = 0] = "Untrusted";
|
|
9
|
+
Target[Target["Trusted"] = 1] = "Trusted";
|
|
10
|
+
})(Target = exports.Target || (exports.Target = {}));
|
|
11
|
+
class Router {
|
|
12
|
+
constructor(cacheFilename, cache, looseMode, tester, untrustedResolver, ipWhitelist, hostnameWhitelist) {
|
|
13
|
+
this.cacheFilename = cacheFilename;
|
|
14
|
+
this.cache = cache;
|
|
15
|
+
this.looseMode = looseMode;
|
|
16
|
+
this.tester = tester;
|
|
17
|
+
this.untrustedResolver = untrustedResolver;
|
|
18
|
+
this.ipWhitelist = ipWhitelist;
|
|
19
|
+
this.hostnameWhitelist = hostnameWhitelist;
|
|
20
|
+
}
|
|
21
|
+
static async create(options) {
|
|
22
|
+
const tester = options.tester;
|
|
23
|
+
const untrustedResolver = options.untrustedResolver;
|
|
24
|
+
const ipWhitelist = options.ipWhitelist;
|
|
25
|
+
const hostnameWhitelist = options.hostnameWhitelist;
|
|
26
|
+
const cacheFilename = options.cacheFilename;
|
|
27
|
+
const looseMode = options.looseMode;
|
|
28
|
+
const cache = await (0, map_file_1.readMapFile)(cacheFilename);
|
|
29
|
+
await (0, map_file_1.writeMapFile)(cacheFilename, cache);
|
|
30
|
+
return new Router(cacheFilename, cache, looseMode, tester, untrustedResolver, ipWhitelist, hostnameWhitelist);
|
|
31
|
+
}
|
|
32
|
+
async getTarget(hostname) {
|
|
33
|
+
if (this.inHostnameWhitelist(hostname))
|
|
34
|
+
return Target.Untrusted;
|
|
35
|
+
if (this.cache.has(hostname)) {
|
|
36
|
+
return this.cache.get(hostname);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
if (this.looseMode) {
|
|
40
|
+
queueMicrotask(() => this.getTargetWithoutCache(hostname));
|
|
41
|
+
return Target.Untrusted;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return await this.getTargetWithoutCache(hostname);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async getTargetWithoutCache(hostname) {
|
|
49
|
+
if (await this.tester.isPoisoned(hostname)) {
|
|
50
|
+
this.setCache(hostname, Target.Trusted);
|
|
51
|
+
return Target.Trusted;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
const addresses = await (0, resolve_a_1.resolveA)(this.untrustedResolver, hostname);
|
|
55
|
+
if (addresses.length > 0) {
|
|
56
|
+
if (this.inIPWhitelist(addresses)) {
|
|
57
|
+
this.setCache(hostname, Target.Untrusted);
|
|
58
|
+
return Target.Untrusted;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
this.setCache(hostname, Target.Trusted);
|
|
62
|
+
return Target.Trusted;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return Target.Trusted;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
inIPWhitelist(addresses) {
|
|
71
|
+
return addresses.some(x => this.ipWhitelist.includes(x));
|
|
72
|
+
}
|
|
73
|
+
inHostnameWhitelist(hostname) {
|
|
74
|
+
return this.hostnameWhitelist.includes(hostname);
|
|
75
|
+
}
|
|
76
|
+
setCache(hostname, result) {
|
|
77
|
+
this.cache.set(hostname, result);
|
|
78
|
+
(0, map_file_1.appendMapFile)(this.cacheFilename, hostname, result);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.Router = Router;
|
|
82
82
|
//# sourceMappingURL=router.js.map
|
package/lib/server.js
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
-
};
|
|
24
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.startServer = void 0;
|
|
26
|
-
const router_1 = require("./router");
|
|
27
|
-
const dns = __importStar(require("native-dns"));
|
|
28
|
-
const extra_promise_1 = require("extra-promise");
|
|
29
|
-
const get_elapsed_1 = require("./utils/get-elapsed");
|
|
30
|
-
const get_timestamp_1 = require("./utils/get-timestamp");
|
|
31
|
-
const countup_1 = require("./utils/countup");
|
|
32
|
-
const resource_record_type_1 = require("./resource-record-type");
|
|
33
|
-
const return_style_1 = require("return-style");
|
|
34
|
-
const ms_1 = __importDefault(require("ms"));
|
|
35
|
-
function startServer({ logger, port, router, trustedServer, untrustedServer }) {
|
|
36
|
-
const server = dns.createServer();
|
|
37
|
-
server.on('error', console.error);
|
|
38
|
-
server.on('socketError', console.error);
|
|
39
|
-
server.on('request', async (req, res) => {
|
|
40
|
-
const answers = await (0, extra_promise_1.map)(req.question, async (question) => {
|
|
41
|
-
const id = (0, countup_1.countup)().toString();
|
|
42
|
-
const startTime = (0, get_timestamp_1.getTimestamp)();
|
|
43
|
-
var [err, target] = await (0, return_style_1.getErrorResultAsync)(() => router.getTarget(question.name));
|
|
44
|
-
if (err) {
|
|
45
|
-
logger.error({
|
|
46
|
-
hostname: question.name,
|
|
47
|
-
id,
|
|
48
|
-
reason: `${err}`,
|
|
49
|
-
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
50
|
-
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
51
|
-
});
|
|
52
|
-
return [];
|
|
53
|
-
}
|
|
54
|
-
logger.debug({
|
|
55
|
-
hostname: question.name,
|
|
56
|
-
id,
|
|
57
|
-
message: router_1.Target[target],
|
|
58
|
-
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
59
|
-
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
60
|
-
});
|
|
61
|
-
const server = target === router_1.Target.Trusted
|
|
62
|
-
? trustedServer
|
|
63
|
-
: untrustedServer;
|
|
64
|
-
var [err, answers] = await (0, return_style_1.getErrorResultAsync)(() => resolve(server, question));
|
|
65
|
-
if (err) {
|
|
66
|
-
logger.error({
|
|
67
|
-
hostname: question.name,
|
|
68
|
-
id,
|
|
69
|
-
reason: `${err}`,
|
|
70
|
-
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
71
|
-
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
72
|
-
});
|
|
73
|
-
return [];
|
|
74
|
-
}
|
|
75
|
-
logger.info({
|
|
76
|
-
hostname: question.name,
|
|
77
|
-
id,
|
|
78
|
-
message: resource_record_type_1.ResourceRecordType[question.type],
|
|
79
|
-
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
80
|
-
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
81
|
-
});
|
|
82
|
-
return answers;
|
|
83
|
-
});
|
|
84
|
-
answers
|
|
85
|
-
.flat()
|
|
86
|
-
.forEach(answer => res.answer.push(answer));
|
|
87
|
-
res.send();
|
|
88
|
-
});
|
|
89
|
-
return server.serve(port);
|
|
90
|
-
}
|
|
91
|
-
exports.startServer = startServer;
|
|
92
|
-
function resolve(server, question) {
|
|
93
|
-
return new Promise((resolve, reject) => {
|
|
94
|
-
const answers = [];
|
|
95
|
-
const request = dns.Request({
|
|
96
|
-
question,
|
|
97
|
-
server: {
|
|
98
|
-
address: server.host,
|
|
99
|
-
port: server.port,
|
|
100
|
-
type: 'udp'
|
|
101
|
-
},
|
|
102
|
-
timeout: (0, ms_1.default)('30s'),
|
|
103
|
-
cache: false,
|
|
104
|
-
try_edns: true
|
|
105
|
-
});
|
|
106
|
-
request.on('timeout', () => reject());
|
|
107
|
-
request.on('cancelled', () => reject());
|
|
108
|
-
request.on('end', () => resolve(answers));
|
|
109
|
-
request.on('message', (err, msg) => {
|
|
110
|
-
if (err)
|
|
111
|
-
return reject(err);
|
|
112
|
-
msg.answer.forEach(answer => answers.push(answer));
|
|
113
|
-
});
|
|
114
|
-
request.send();
|
|
115
|
-
});
|
|
116
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.startServer = void 0;
|
|
26
|
+
const router_1 = require("./router");
|
|
27
|
+
const dns = __importStar(require("native-dns"));
|
|
28
|
+
const extra_promise_1 = require("extra-promise");
|
|
29
|
+
const get_elapsed_1 = require("./utils/get-elapsed");
|
|
30
|
+
const get_timestamp_1 = require("./utils/get-timestamp");
|
|
31
|
+
const countup_1 = require("./utils/countup");
|
|
32
|
+
const resource_record_type_1 = require("./resource-record-type");
|
|
33
|
+
const return_style_1 = require("return-style");
|
|
34
|
+
const ms_1 = __importDefault(require("ms"));
|
|
35
|
+
function startServer({ logger, port, router, trustedServer, untrustedServer }) {
|
|
36
|
+
const server = dns.createServer();
|
|
37
|
+
server.on('error', console.error);
|
|
38
|
+
server.on('socketError', console.error);
|
|
39
|
+
server.on('request', async (req, res) => {
|
|
40
|
+
const answers = await (0, extra_promise_1.map)(req.question, async (question) => {
|
|
41
|
+
const id = (0, countup_1.countup)().toString();
|
|
42
|
+
const startTime = (0, get_timestamp_1.getTimestamp)();
|
|
43
|
+
var [err, target] = await (0, return_style_1.getErrorResultAsync)(() => router.getTarget(question.name));
|
|
44
|
+
if (err) {
|
|
45
|
+
logger.error({
|
|
46
|
+
hostname: question.name,
|
|
47
|
+
id,
|
|
48
|
+
reason: `${err}`,
|
|
49
|
+
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
50
|
+
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
51
|
+
});
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
logger.debug({
|
|
55
|
+
hostname: question.name,
|
|
56
|
+
id,
|
|
57
|
+
message: router_1.Target[target],
|
|
58
|
+
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
59
|
+
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
60
|
+
});
|
|
61
|
+
const server = target === router_1.Target.Trusted
|
|
62
|
+
? trustedServer
|
|
63
|
+
: untrustedServer;
|
|
64
|
+
var [err, answers] = await (0, return_style_1.getErrorResultAsync)(() => resolve(server, question));
|
|
65
|
+
if (err) {
|
|
66
|
+
logger.error({
|
|
67
|
+
hostname: question.name,
|
|
68
|
+
id,
|
|
69
|
+
reason: `${err}`,
|
|
70
|
+
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
71
|
+
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
72
|
+
});
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
logger.info({
|
|
76
|
+
hostname: question.name,
|
|
77
|
+
id,
|
|
78
|
+
message: resource_record_type_1.ResourceRecordType[question.type],
|
|
79
|
+
timestamp: (0, get_timestamp_1.getTimestamp)(),
|
|
80
|
+
elapsed: (0, get_elapsed_1.getElapsed)(startTime)
|
|
81
|
+
});
|
|
82
|
+
return answers;
|
|
83
|
+
});
|
|
84
|
+
answers
|
|
85
|
+
.flat()
|
|
86
|
+
.forEach(answer => res.answer.push(answer));
|
|
87
|
+
res.send();
|
|
88
|
+
});
|
|
89
|
+
return server.serve(port);
|
|
90
|
+
}
|
|
91
|
+
exports.startServer = startServer;
|
|
92
|
+
function resolve(server, question) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const answers = [];
|
|
95
|
+
const request = dns.Request({
|
|
96
|
+
question,
|
|
97
|
+
server: {
|
|
98
|
+
address: server.host,
|
|
99
|
+
port: server.port,
|
|
100
|
+
type: 'udp'
|
|
101
|
+
},
|
|
102
|
+
timeout: (0, ms_1.default)('30s'),
|
|
103
|
+
cache: false,
|
|
104
|
+
try_edns: true
|
|
105
|
+
});
|
|
106
|
+
request.on('timeout', () => reject());
|
|
107
|
+
request.on('cancelled', () => reject());
|
|
108
|
+
request.on('end', () => resolve(answers));
|
|
109
|
+
request.on('message', (err, msg) => {
|
|
110
|
+
if (err)
|
|
111
|
+
return reject(err);
|
|
112
|
+
msg.answer.forEach(answer => answers.push(answer));
|
|
113
|
+
});
|
|
114
|
+
request.send();
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
117
|
//# sourceMappingURL=server.js.map
|
package/lib/tester.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ServerNotAlive = exports.Tester = void 0;
|
|
4
|
-
const map_file_1 = require("./utils/map-file");
|
|
5
|
-
const is_alive_1 = require("./utils/is-alive");
|
|
6
|
-
const create_dns_resolver_1 = require("./utils/create-dns-resolver");
|
|
7
|
-
const resolve_a_1 = require("./utils/resolve-a");
|
|
8
|
-
const errors_1 = require("@blackglory/errors");
|
|
9
|
-
const return_style_1 = require("return-style");
|
|
10
|
-
class Tester {
|
|
11
|
-
constructor(server, timeout, cacheFilename, testResolver, cache) {
|
|
12
|
-
this.server = server;
|
|
13
|
-
this.timeout = timeout;
|
|
14
|
-
this.cacheFilename = cacheFilename;
|
|
15
|
-
this.testResolver = testResolver;
|
|
16
|
-
this.cache = cache;
|
|
17
|
-
}
|
|
18
|
-
static async create(options) {
|
|
19
|
-
const server = options.server;
|
|
20
|
-
const timeout = options.timeout;
|
|
21
|
-
const cacheFilename = options.cacheFilename;
|
|
22
|
-
const testResolver = (0, create_dns_resolver_1.createDNSResolver)(server);
|
|
23
|
-
const cache = await (0, map_file_1.readMapFile)(cacheFilename);
|
|
24
|
-
await (0, map_file_1.writeMapFile)(cacheFilename, cache);
|
|
25
|
-
return new Tester(server, timeout, cacheFilename, testResolver, cache);
|
|
26
|
-
}
|
|
27
|
-
async isPoisoned(hostname) {
|
|
28
|
-
if (this.cache.has(hostname)) {
|
|
29
|
-
return this.cache.get(hostname);
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
const [alive, poisoned] = await Promise.all([
|
|
33
|
-
(0, is_alive_1.isAlive)(this.server, this.timeout),
|
|
34
|
-
(0, return_style_1.isSuccessPromise)((0, resolve_a_1.resolveA)(this.testResolver, hostname, this.timeout))
|
|
35
|
-
]);
|
|
36
|
-
if (alive) {
|
|
37
|
-
this.setCache(hostname, poisoned);
|
|
38
|
-
return poisoned;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
throw new ServerNotAlive();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
setCache(hostname, result) {
|
|
46
|
-
this.cache.set(hostname, result);
|
|
47
|
-
(0, map_file_1.appendMapFile)(this.cacheFilename, hostname, result);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
exports.Tester = Tester;
|
|
51
|
-
class ServerNotAlive extends errors_1.CustomError {
|
|
52
|
-
constructor() {
|
|
53
|
-
super('The test server is not alive or not sending a echo reply in time');
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
exports.ServerNotAlive = ServerNotAlive;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerNotAlive = exports.Tester = void 0;
|
|
4
|
+
const map_file_1 = require("./utils/map-file");
|
|
5
|
+
const is_alive_1 = require("./utils/is-alive");
|
|
6
|
+
const create_dns_resolver_1 = require("./utils/create-dns-resolver");
|
|
7
|
+
const resolve_a_1 = require("./utils/resolve-a");
|
|
8
|
+
const errors_1 = require("@blackglory/errors");
|
|
9
|
+
const return_style_1 = require("return-style");
|
|
10
|
+
class Tester {
|
|
11
|
+
constructor(server, timeout, cacheFilename, testResolver, cache) {
|
|
12
|
+
this.server = server;
|
|
13
|
+
this.timeout = timeout;
|
|
14
|
+
this.cacheFilename = cacheFilename;
|
|
15
|
+
this.testResolver = testResolver;
|
|
16
|
+
this.cache = cache;
|
|
17
|
+
}
|
|
18
|
+
static async create(options) {
|
|
19
|
+
const server = options.server;
|
|
20
|
+
const timeout = options.timeout;
|
|
21
|
+
const cacheFilename = options.cacheFilename;
|
|
22
|
+
const testResolver = (0, create_dns_resolver_1.createDNSResolver)(server);
|
|
23
|
+
const cache = await (0, map_file_1.readMapFile)(cacheFilename);
|
|
24
|
+
await (0, map_file_1.writeMapFile)(cacheFilename, cache);
|
|
25
|
+
return new Tester(server, timeout, cacheFilename, testResolver, cache);
|
|
26
|
+
}
|
|
27
|
+
async isPoisoned(hostname) {
|
|
28
|
+
if (this.cache.has(hostname)) {
|
|
29
|
+
return this.cache.get(hostname);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const [alive, poisoned] = await Promise.all([
|
|
33
|
+
(0, is_alive_1.isAlive)(this.server, this.timeout),
|
|
34
|
+
(0, return_style_1.isSuccessPromise)((0, resolve_a_1.resolveA)(this.testResolver, hostname, this.timeout))
|
|
35
|
+
]);
|
|
36
|
+
if (alive) {
|
|
37
|
+
this.setCache(hostname, poisoned);
|
|
38
|
+
return poisoned;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw new ServerNotAlive();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
setCache(hostname, result) {
|
|
46
|
+
this.cache.set(hostname, result);
|
|
47
|
+
(0, map_file_1.appendMapFile)(this.cacheFilename, hostname, result);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.Tester = Tester;
|
|
51
|
+
class ServerNotAlive extends errors_1.CustomError {
|
|
52
|
+
constructor() {
|
|
53
|
+
super('The test server is not alive or not sending a echo reply in time');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.ServerNotAlive = ServerNotAlive;
|
|
57
57
|
//# sourceMappingURL=tester.js.map
|
package/lib/utils/countup.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.countup = void 0;
|
|
4
|
-
let count = 0;
|
|
5
|
-
function countup() {
|
|
6
|
-
return ++count;
|
|
7
|
-
}
|
|
8
|
-
exports.countup = countup;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.countup = void 0;
|
|
4
|
+
let count = 0;
|
|
5
|
+
function countup() {
|
|
6
|
+
return ++count;
|
|
7
|
+
}
|
|
8
|
+
exports.countup = countup;
|
|
9
9
|
//# sourceMappingURL=countup.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createDNSResolver = void 0;
|
|
4
|
-
const dns_1 = require("dns");
|
|
5
|
-
function createDNSResolver(...servers) {
|
|
6
|
-
const resolver = new dns_1.promises.Resolver();
|
|
7
|
-
resolver.setServers(servers);
|
|
8
|
-
return resolver;
|
|
9
|
-
}
|
|
10
|
-
exports.createDNSResolver = createDNSResolver;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDNSResolver = void 0;
|
|
4
|
+
const dns_1 = require("dns");
|
|
5
|
+
function createDNSResolver(...servers) {
|
|
6
|
+
const resolver = new dns_1.promises.Resolver();
|
|
7
|
+
resolver.setServers(servers);
|
|
8
|
+
return resolver;
|
|
9
|
+
}
|
|
10
|
+
exports.createDNSResolver = createDNSResolver;
|
|
11
11
|
//# sourceMappingURL=create-dns-resolver.js.map
|
package/lib/utils/get-elapsed.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getElapsed = void 0;
|
|
4
|
-
const get_timestamp_1 = require("./get-timestamp");
|
|
5
|
-
function getElapsed(startTime) {
|
|
6
|
-
return (0, get_timestamp_1.getTimestamp)() - startTime;
|
|
7
|
-
}
|
|
8
|
-
exports.getElapsed = getElapsed;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getElapsed = void 0;
|
|
4
|
+
const get_timestamp_1 = require("./get-timestamp");
|
|
5
|
+
function getElapsed(startTime) {
|
|
6
|
+
return (0, get_timestamp_1.getTimestamp)() - startTime;
|
|
7
|
+
}
|
|
8
|
+
exports.getElapsed = getElapsed;
|
|
9
9
|
//# sourceMappingURL=get-elapsed.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTimestamp = void 0;
|
|
4
|
-
function getTimestamp() {
|
|
5
|
-
return Date.now();
|
|
6
|
-
}
|
|
7
|
-
exports.getTimestamp = getTimestamp;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getTimestamp = void 0;
|
|
4
|
+
function getTimestamp() {
|
|
5
|
+
return Date.now();
|
|
6
|
+
}
|
|
7
|
+
exports.getTimestamp = getTimestamp;
|
|
8
8
|
//# sourceMappingURL=get-timestamp.js.map
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readHostnameListFile = void 0;
|
|
4
|
-
const fs_1 = require("fs");
|
|
5
|
-
const hostname_pattern_1 = require("./hostname-pattern");
|
|
6
|
-
async function readHostnameListFile(filename) {
|
|
7
|
-
const text = await fs_1.promises.readFile(filename, 'utf-8');
|
|
8
|
-
const patterns = text.split('\n')
|
|
9
|
-
.map(x => x.trim())
|
|
10
|
-
.filter(x => x !== '')
|
|
11
|
-
.filter(isHostnamePattern)
|
|
12
|
-
.map(x => new hostname_pattern_1.HostnamePattern(x));
|
|
13
|
-
return patterns;
|
|
14
|
-
}
|
|
15
|
-
exports.readHostnameListFile = readHostnameListFile;
|
|
16
|
-
function isHostnamePattern(text) {
|
|
17
|
-
return /^[\da-zA-Z\-.*]+$/.test(text);
|
|
18
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readHostnameListFile = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const hostname_pattern_1 = require("./hostname-pattern");
|
|
6
|
+
async function readHostnameListFile(filename) {
|
|
7
|
+
const text = await fs_1.promises.readFile(filename, 'utf-8');
|
|
8
|
+
const patterns = text.split('\n')
|
|
9
|
+
.map(x => x.trim())
|
|
10
|
+
.filter(x => x !== '')
|
|
11
|
+
.filter(isHostnamePattern)
|
|
12
|
+
.map(x => new hostname_pattern_1.HostnamePattern(x));
|
|
13
|
+
return patterns;
|
|
14
|
+
}
|
|
15
|
+
exports.readHostnameListFile = readHostnameListFile;
|
|
16
|
+
function isHostnamePattern(text) {
|
|
17
|
+
return /^[\da-zA-Z\-.*]+$/.test(text);
|
|
18
|
+
}
|
|
19
19
|
//# sourceMappingURL=hostname-list-file.js.map
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HostnamePattern = void 0;
|
|
4
|
-
class HostnamePattern {
|
|
5
|
-
constructor(pattern) {
|
|
6
|
-
const re = pattern.replace(/\./g, '\\.')
|
|
7
|
-
.replace(/\*/g, '.*');
|
|
8
|
-
this.re = new RegExp(`^${re}$`, 'i');
|
|
9
|
-
}
|
|
10
|
-
match(hostname) {
|
|
11
|
-
return this.re.test(hostname);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
exports.HostnamePattern = HostnamePattern;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HostnamePattern = void 0;
|
|
4
|
+
class HostnamePattern {
|
|
5
|
+
constructor(pattern) {
|
|
6
|
+
const re = pattern.replace(/\./g, '\\.')
|
|
7
|
+
.replace(/\*/g, '.*');
|
|
8
|
+
this.re = new RegExp(`^${re}$`, 'i');
|
|
9
|
+
}
|
|
10
|
+
match(hostname) {
|
|
11
|
+
return this.re.test(hostname);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.HostnamePattern = HostnamePattern;
|
|
15
15
|
//# sourceMappingURL=hostname-pattern.js.map
|