-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdap-validator
executable file
·130 lines (94 loc) · 3.68 KB
/
rdap-validator
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
#!/usr/bin/env node
process.stdout.say = (l) => process.stdout.write(l + "\n");
/**
* start by parsing the command-line arguments
*/
let opt = {
named: {},
positional: [],
};
for (var i = 2 ; i < process.argv.length ; i++) {
const arg = process.argv[i];
if ("--" == arg.substr(0, 2)) {
if (arg.indexOf("=") >= 0) {
opt.named[arg.substr(2, arg.indexOf("=")-2)] = arg.substr(1+arg.indexOf("="));
} else {
opt.named[arg.substr(2)] = true;
}
} else {
opt.positional.push(arg);
}
}
const url = opt.positional.shift();
const responseType = opt.named.type ?? "domain";
const serverType = opt.named["server-type"] ?? "vanilla";
import rdapValidator from "../lib/rdap-validator.js";
const jsonResultData = {
rdapValidatorVersion: rdapValidator.version,
testedURL: url,
expectedResponseType: responseType,
serverType: serverType,
results: [],
};
let lastDepth = 0;
const INDENT_STRING = " ";
const MSG_FAIL = "\x1b[1;31m✘\x1b[0m";
const MSG_PASS = "\x1b[1;32m✓\x1b[0m";
const MSG_INFO = "\x1b[1;33mi\x1b[0m";
const cliResultCallback = function(result, message, path, ref) {
const onlyErrors = opt.named.hasOwnProperty("only-errors");
const depth = rdapValidator.path.length;
const indent = INDENT_STRING.repeat(onlyErrors ? 0 : Math.max(0, depth-1)) + "- ";
if (false === result) {
process.stdout.say(indent + MSG_FAIL + " " + message + " (" + (ref ? path + ", see " + ref : path) + ")");
} else if (false === onlyErrors) {
if (true === result) {
process.stdout.say(indent + MSG_PASS + " " + message);
} else {
process.stdout.say(indent + MSG_INFO + " " + message);
}
}
lastDepth = depth;
};
const jsonResultCallback = function(result, message, path, ref) {
jsonResultData.results.push({
testPassed: result,
message: message,
path: path,
reference: ref,
});
};
if (opt.named.json) {
rdapValidator.setResultCallback(jsonResultCallback);
rdapValidator.setTestCompleteCallback(function() {
jsonResultData.errorCount = jsonResultData.results.filter((r) => false === r.result).length
jsonResultData.responseHeaders = rdapValidator.lastTestedResponseHeaders;
jsonResultData.jsonResponse = rdapValidator.lastTestedResponse;
process.stdout.say(JSON.stringify(jsonResultData, null, 2));
});
} else {
rdapValidator.setResultCallback(cliResultCallback);
}
function help(x) {
process.stdout.say("Usage: " + process.argv[1] + " [OPTIONS] URL");
process.stdout.say("Options:");
process.stdout.say(" --help Show this help.");
process.stdout.say(" --only-errors Only show errors.");
process.stdout.say(" --json Output result in JSON.");
process.stdout.say(" --type=TYPE Expected response type, which must be one of:");
Object.keys(rdapValidator.responseTypes).forEach((t) => process.stdout.say(" ".repeat(25) + "- " + t.replace("ip network", "ip-network")));
process.stdout.say(" --server-type=TYPE Server type, which must be one of:");
Object.keys(rdapValidator.serverTypes).forEach((t) => process.stdout.say(" ".repeat(25) + "- " + t));
process.stdout.say("");
process.stdout.say("Copyright 2024 Gavin Brown <https://about.rdap.org>.");
process.exit(x);
}
if (opt.named.hasOwnProperty("help")) {
help(0);
} else {
rdapValidator.testURL(
url,
responseType.replace("ip-network", "ip network"),
serverType,
);
}