netriskscan-cli 1.2.0 → 1.3.0
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 +7 -0
- package/README.md +258 -75
- package/dist/cli.js +228 -67
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +77 -7
- package/dist/index.js +46 -18
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -10,6 +10,7 @@ import { Command, CommanderError } from "commander";
|
|
|
10
10
|
import ora from "ora";
|
|
11
11
|
|
|
12
12
|
// src/client/errors.ts
|
|
13
|
+
var ANONYMOUS_DAILY_LIMIT_CODE = "anonymous_daily_limit_reached";
|
|
13
14
|
var NetRiskScanError = class extends Error {
|
|
14
15
|
constructor(message) {
|
|
15
16
|
super(message);
|
|
@@ -27,6 +28,8 @@ var NetRiskScanApiError = class extends NetRiskScanError {
|
|
|
27
28
|
code;
|
|
28
29
|
requestId;
|
|
29
30
|
retryAfter;
|
|
31
|
+
/** Present when the server reported anonymous-trial allowance alongside the error. */
|
|
32
|
+
anonymousUsage;
|
|
30
33
|
constructor(message, options) {
|
|
31
34
|
super(message);
|
|
32
35
|
this.name = "NetRiskScanApiError";
|
|
@@ -34,6 +37,7 @@ var NetRiskScanApiError = class extends NetRiskScanError {
|
|
|
34
37
|
this.code = options.code;
|
|
35
38
|
this.requestId = options.requestId;
|
|
36
39
|
this.retryAfter = options.retryAfter;
|
|
40
|
+
this.anonymousUsage = options.anonymousUsage;
|
|
37
41
|
}
|
|
38
42
|
};
|
|
39
43
|
var NetRiskScanNetworkError = class extends NetRiskScanError {
|
|
@@ -80,6 +84,10 @@ function parseQuota(headers) {
|
|
|
80
84
|
function isRetryableStatus(status) {
|
|
81
85
|
return status === 429 || status === 503;
|
|
82
86
|
}
|
|
87
|
+
function isRetryableFailure(status, code) {
|
|
88
|
+
if (code === ANONYMOUS_DAILY_LIMIT_CODE) return false;
|
|
89
|
+
return isRetryableStatus(status);
|
|
90
|
+
}
|
|
83
91
|
var BASE_DELAY_MS = 1e3;
|
|
84
92
|
var MAX_JITTER_MS = 250;
|
|
85
93
|
function computeBackoffMs(attempt, retryAfterSeconds) {
|
|
@@ -108,18 +116,22 @@ async function safeJson(res) {
|
|
|
108
116
|
return void 0;
|
|
109
117
|
}
|
|
110
118
|
}
|
|
119
|
+
function parseAnonymousLimitInfo(body) {
|
|
120
|
+
const error = body?.error;
|
|
121
|
+
if (!error) return void 0;
|
|
122
|
+
const { dailyLimit, used, remaining, resetAt, signupUrl } = error;
|
|
123
|
+
if (dailyLimit === void 0 && used === void 0 && remaining === void 0 && resetAt === void 0 && signupUrl === void 0) {
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
return { dailyLimit, used, remaining, resetAt, signupUrl };
|
|
127
|
+
}
|
|
111
128
|
var NetRiskScanClient = class {
|
|
112
129
|
apiKey;
|
|
113
130
|
baseUrl;
|
|
114
131
|
timeout;
|
|
115
132
|
maxRetries;
|
|
116
|
-
constructor(options) {
|
|
117
|
-
|
|
118
|
-
throw new NetRiskScanConfigError(
|
|
119
|
-
"NetRiskScan API key is required.\n\nSet NETRISKSCAN_API_KEY or use --api-key."
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
this.apiKey = options.apiKey;
|
|
133
|
+
constructor(options = {}) {
|
|
134
|
+
this.apiKey = options.apiKey || void 0;
|
|
123
135
|
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
124
136
|
this.timeout = options.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
125
137
|
this.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
@@ -127,7 +139,14 @@ var NetRiskScanClient = class {
|
|
|
127
139
|
async checkIp(ip, options = {}) {
|
|
128
140
|
return this.request(`/v1/ip-risk/${encodeURIComponent(ip)}`, options);
|
|
129
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Account plan, billing period and quota. Unlike {@link checkIp} this is account data, so it
|
|
144
|
+
* has no anonymous equivalent and requires an API key.
|
|
145
|
+
*/
|
|
130
146
|
async getUsage(options = {}) {
|
|
147
|
+
if (!this.apiKey) {
|
|
148
|
+
throw new NetRiskScanConfigError("An API key is required to query account usage.");
|
|
149
|
+
}
|
|
131
150
|
return this.request("/v1/usage", options);
|
|
132
151
|
}
|
|
133
152
|
async request(path, options) {
|
|
@@ -139,13 +158,16 @@ var NetRiskScanClient = class {
|
|
|
139
158
|
const onExternalAbort = () => timeoutController.abort();
|
|
140
159
|
options.signal?.addEventListener("abort", onExternalAbort);
|
|
141
160
|
try {
|
|
161
|
+
const headers = {
|
|
162
|
+
Accept: "application/json",
|
|
163
|
+
"X-Request-Id": requestId
|
|
164
|
+
};
|
|
165
|
+
if (this.apiKey) {
|
|
166
|
+
headers.Authorization = `Bearer ${this.apiKey}`;
|
|
167
|
+
}
|
|
142
168
|
const res = await fetch(`${this.baseUrl}${path}`, {
|
|
143
169
|
method: "GET",
|
|
144
|
-
headers
|
|
145
|
-
Authorization: `Bearer ${this.apiKey}`,
|
|
146
|
-
Accept: "application/json",
|
|
147
|
-
"X-Request-Id": requestId
|
|
148
|
-
},
|
|
170
|
+
headers,
|
|
149
171
|
signal: timeoutController.signal
|
|
150
172
|
});
|
|
151
173
|
const rateLimit = parseRateLimit(res.headers);
|
|
@@ -158,7 +180,7 @@ var NetRiskScanClient = class {
|
|
|
158
180
|
const body = await safeJson(res);
|
|
159
181
|
const retryAfterHeader = res.headers.get("retry-after");
|
|
160
182
|
const retryAfter = retryAfterHeader !== null ? Number(retryAfterHeader) : void 0;
|
|
161
|
-
if (
|
|
183
|
+
if (isRetryableFailure(res.status, body?.error?.code) && attempt < this.maxRetries) {
|
|
162
184
|
attempt += 1;
|
|
163
185
|
await sleep(computeBackoffMs(attempt, retryAfter));
|
|
164
186
|
continue;
|
|
@@ -167,7 +189,8 @@ var NetRiskScanClient = class {
|
|
|
167
189
|
status: res.status,
|
|
168
190
|
code: body?.error?.code ?? "unknown_error",
|
|
169
191
|
requestId: body?.error?.requestId ?? responseRequestId,
|
|
170
|
-
retryAfter
|
|
192
|
+
retryAfter,
|
|
193
|
+
anonymousUsage: parseAnonymousLimitInfo(body)
|
|
171
194
|
});
|
|
172
195
|
} catch (err) {
|
|
173
196
|
if (err instanceof NetRiskScanApiError) {
|
|
@@ -182,10 +205,13 @@ var NetRiskScanClient = class {
|
|
|
182
205
|
requestId
|
|
183
206
|
});
|
|
184
207
|
}
|
|
185
|
-
throw new NetRiskScanNetworkError(
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
208
|
+
throw new NetRiskScanNetworkError(
|
|
209
|
+
err instanceof Error ? err.message : "Network request failed",
|
|
210
|
+
{
|
|
211
|
+
cause: err,
|
|
212
|
+
requestId
|
|
213
|
+
}
|
|
214
|
+
);
|
|
189
215
|
} finally {
|
|
190
216
|
clearTimeout(timer);
|
|
191
217
|
options.signal?.removeEventListener("abort", onExternalAbort);
|
|
@@ -195,11 +221,14 @@ var NetRiskScanClient = class {
|
|
|
195
221
|
};
|
|
196
222
|
|
|
197
223
|
// src/utils/env.ts
|
|
198
|
-
function
|
|
199
|
-
|
|
224
|
+
function resolveOptionalApiKey(cliValue) {
|
|
225
|
+
return cliValue || process.env.NETRISKSCAN_API_KEY || void 0;
|
|
226
|
+
}
|
|
227
|
+
function resolveRequiredApiKey(cliValue) {
|
|
228
|
+
const apiKey = resolveOptionalApiKey(cliValue);
|
|
200
229
|
if (!apiKey) {
|
|
201
230
|
throw new NetRiskScanConfigError(
|
|
202
|
-
"
|
|
231
|
+
"An API key is required for the usage command.\n\nSet NETRISKSCAN_API_KEY or use --api-key."
|
|
203
232
|
);
|
|
204
233
|
}
|
|
205
234
|
return apiKey;
|
|
@@ -231,8 +260,11 @@ function exitCodeForApiErrorCode(code) {
|
|
|
231
260
|
case "api_key_disabled":
|
|
232
261
|
case "scope_not_allowed":
|
|
233
262
|
return ExitCode.AuthError;
|
|
263
|
+
// The anonymous daily trial is an allowance like any other, so it reuses code 5 rather than
|
|
264
|
+
// adding an eighth exit code that every script would have to learn.
|
|
234
265
|
case "rate_limit_exceeded":
|
|
235
266
|
case "quota_exceeded":
|
|
267
|
+
case "anonymous_daily_limit_reached":
|
|
236
268
|
return ExitCode.RateLimitOrQuota;
|
|
237
269
|
default:
|
|
238
270
|
return ExitCode.ApiError;
|
|
@@ -260,12 +292,33 @@ function formatNumber(value) {
|
|
|
260
292
|
function formatDate(iso) {
|
|
261
293
|
return iso.slice(0, 10);
|
|
262
294
|
}
|
|
263
|
-
|
|
264
|
-
// src/output/human.ts
|
|
265
295
|
var LABEL_WIDTH = 18;
|
|
266
296
|
function row(label, value) {
|
|
267
297
|
return `${label.padEnd(LABEL_WIDTH)}${value}`;
|
|
268
298
|
}
|
|
299
|
+
function formatUtcTimestamp(iso) {
|
|
300
|
+
if (!iso) return void 0;
|
|
301
|
+
const date = new Date(iso);
|
|
302
|
+
if (Number.isNaN(date.getTime())) return void 0;
|
|
303
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
304
|
+
return `${date.getUTCFullYear()}-${pad(date.getUTCMonth() + 1)}-${pad(date.getUTCDate())} ${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())} UTC`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// src/output/human.ts
|
|
308
|
+
function anonymousUsageLines(usage) {
|
|
309
|
+
const lines = [];
|
|
310
|
+
if (typeof usage.remaining === "number") {
|
|
311
|
+
lines.push(row("Available", formatNumber(usage.remaining)));
|
|
312
|
+
}
|
|
313
|
+
if (typeof usage.dailyLimit === "number") {
|
|
314
|
+
lines.push(row("Daily Limit", formatNumber(usage.dailyLimit)));
|
|
315
|
+
}
|
|
316
|
+
const reset = formatUtcTimestamp(usage.resetAt);
|
|
317
|
+
if (reset !== void 0) {
|
|
318
|
+
lines.push(row("Reset", reset));
|
|
319
|
+
}
|
|
320
|
+
return lines;
|
|
321
|
+
}
|
|
269
322
|
function bandColor(band) {
|
|
270
323
|
switch (band) {
|
|
271
324
|
case "excellent":
|
|
@@ -286,7 +339,9 @@ function renderCheckResult(ip, data) {
|
|
|
286
339
|
lines.push("");
|
|
287
340
|
lines.push(row("IP", ip));
|
|
288
341
|
lines.push(row("Index", formatIndex(data.risk.index)));
|
|
289
|
-
lines.push(
|
|
342
|
+
lines.push(
|
|
343
|
+
row("Band", data.risk.band ? bandColor(data.risk.band)(data.risk.band) : chalk.gray("N/A"))
|
|
344
|
+
);
|
|
290
345
|
lines.push(row("Assessment", data.risk.assessmentGrade));
|
|
291
346
|
lines.push("");
|
|
292
347
|
if (data.risk.assessmentGrade === "insufficient" && data.risk.index === null) {
|
|
@@ -313,10 +368,18 @@ function renderCheckResult(ip, data) {
|
|
|
313
368
|
lines.push(row("Scanner", formatTriState(data.flags.scanner)));
|
|
314
369
|
lines.push(row("Abuse", formatTriState(data.flags.abuse)));
|
|
315
370
|
lines.push("");
|
|
371
|
+
if (data.usage?.mode === "anonymous") {
|
|
372
|
+
const usageLines = anonymousUsageLines(data.usage);
|
|
373
|
+
if (usageLines.length > 0) {
|
|
374
|
+
lines.push(chalk.bold("Usage"));
|
|
375
|
+
lines.push(...usageLines);
|
|
376
|
+
lines.push("");
|
|
377
|
+
}
|
|
378
|
+
}
|
|
316
379
|
lines.push(row("Request ID", data.requestId));
|
|
317
380
|
lines.push("");
|
|
318
381
|
lines.push(chalk.bold("Explore"));
|
|
319
|
-
lines.push(row("Web", "https://netriskscan.com"));
|
|
382
|
+
lines.push(row("Web", "https://www.netriskscan.com"));
|
|
320
383
|
lines.push(row("GitHub", "https://github.com/TeamQQ/netriskscan-cli"));
|
|
321
384
|
process.stdout.write(`${lines.join("\n")}
|
|
322
385
|
`);
|
|
@@ -325,11 +388,19 @@ function renderVerboseMeta(meta) {
|
|
|
325
388
|
const lines = [""];
|
|
326
389
|
if (meta.rateLimit.remaining !== void 0 && meta.rateLimit.limit !== void 0) {
|
|
327
390
|
lines.push(
|
|
328
|
-
row(
|
|
391
|
+
row(
|
|
392
|
+
"Rate limit",
|
|
393
|
+
`${formatNumber(meta.rateLimit.remaining)} / ${formatNumber(meta.rateLimit.limit)} remaining`
|
|
394
|
+
)
|
|
329
395
|
);
|
|
330
396
|
}
|
|
331
397
|
if (meta.quota.remaining !== void 0 && meta.quota.limit !== void 0) {
|
|
332
|
-
lines.push(
|
|
398
|
+
lines.push(
|
|
399
|
+
row(
|
|
400
|
+
"Quota",
|
|
401
|
+
`${formatNumber(meta.quota.remaining)} / ${formatNumber(meta.quota.limit)} remaining`
|
|
402
|
+
)
|
|
403
|
+
);
|
|
333
404
|
}
|
|
334
405
|
lines.push(row("Request ID", meta.requestId));
|
|
335
406
|
process.stdout.write(`${lines.join("\n")}
|
|
@@ -363,13 +434,42 @@ function printJson(data) {
|
|
|
363
434
|
|
|
364
435
|
// src/output/errors.ts
|
|
365
436
|
import chalk2 from "chalk";
|
|
437
|
+
var SIGNUP_URL = "https://www.netriskscan.com/developers";
|
|
366
438
|
function printConfigError(err) {
|
|
367
439
|
process.stderr.write(`Error: ${err.message}
|
|
368
440
|
`);
|
|
369
441
|
if (err.message.includes("API key")) {
|
|
370
|
-
process.stderr.write(
|
|
442
|
+
process.stderr.write(
|
|
443
|
+
"\nGet a free API key (no credit card required): https://www.netriskscan.com\n"
|
|
444
|
+
);
|
|
371
445
|
}
|
|
372
446
|
}
|
|
447
|
+
function printAnonymousLimitError(err) {
|
|
448
|
+
const usage = err.anonymousUsage;
|
|
449
|
+
const lines = [
|
|
450
|
+
"",
|
|
451
|
+
chalk2.bold("NetRiskScan"),
|
|
452
|
+
"",
|
|
453
|
+
chalk2.yellow("Anonymous daily trial limit reached."),
|
|
454
|
+
""
|
|
455
|
+
];
|
|
456
|
+
const usageLines = [
|
|
457
|
+
// The request that returned this error was refused, so nothing is left regardless of what the
|
|
458
|
+
// body reports; `remaining` is only trusted when the server actually sent it.
|
|
459
|
+
row("Available", formatNumber(typeof usage?.remaining === "number" ? usage.remaining : 0))
|
|
460
|
+
];
|
|
461
|
+
if (typeof usage?.dailyLimit === "number") {
|
|
462
|
+
usageLines.push(row("Daily Limit", formatNumber(usage.dailyLimit)));
|
|
463
|
+
}
|
|
464
|
+
const reset = formatUtcTimestamp(usage?.resetAt);
|
|
465
|
+
if (reset !== void 0) {
|
|
466
|
+
usageLines.push(row("Reset", reset));
|
|
467
|
+
}
|
|
468
|
+
lines.push(chalk2.bold("Usage"), ...usageLines, "");
|
|
469
|
+
lines.push(chalk2.bold("Get more queries"), usage?.signupUrl || SIGNUP_URL, "");
|
|
470
|
+
process.stderr.write(`${lines.join("\n")}
|
|
471
|
+
`);
|
|
472
|
+
}
|
|
373
473
|
var LABEL_WIDTH2 = 11;
|
|
374
474
|
function printApiError(err, options = {}) {
|
|
375
475
|
const lines = [
|
|
@@ -408,12 +508,22 @@ ${err.message}
|
|
|
408
508
|
}
|
|
409
509
|
|
|
410
510
|
// src/commands/check.ts
|
|
411
|
-
var KNOWN_FLAGS = [
|
|
511
|
+
var KNOWN_FLAGS = [
|
|
512
|
+
"proxy",
|
|
513
|
+
"vpn",
|
|
514
|
+
"tor",
|
|
515
|
+
"datacenter",
|
|
516
|
+
"scanner",
|
|
517
|
+
"abuse"
|
|
518
|
+
];
|
|
412
519
|
function collect(value, previous) {
|
|
413
520
|
return [...previous, value];
|
|
414
521
|
}
|
|
415
522
|
function registerCheckCommand(program2) {
|
|
416
|
-
program2.command("check <ip>").description("Check IP risk and network intelligence").option(
|
|
523
|
+
program2.command("check <ip>").description("Check IP risk and network intelligence").option(
|
|
524
|
+
"--api-key <key>",
|
|
525
|
+
"NetRiskScan API key (optional; uses the anonymous trial when omitted)"
|
|
526
|
+
).option("--base-url <url>", "Override the API base URL (advanced)").option("--json", "Output machine-readable JSON").option("--verbose", "Show rate limit, quota and request id").option("--debug", "Show debug information on failure").option("--timeout <ms>", "Request timeout in milliseconds", "10000").option("--max-retries <n>", "Maximum automatic retries for 429/503", "3").option("--fail-below <index>", "Exit non-zero if risk.index is below this threshold (CI use)").option(
|
|
417
527
|
"--fail-on <flag>",
|
|
418
528
|
"Exit non-zero if the given flag is true (repeatable): proxy, vpn, tor, datacenter, scanner, abuse",
|
|
419
529
|
collect,
|
|
@@ -426,6 +536,10 @@ Examples:
|
|
|
426
536
|
$ netriskscan check 1.1.1.1 --json
|
|
427
537
|
$ netriskscan check 1.2.3.4 --fail-below 60
|
|
428
538
|
$ netriskscan check 1.2.3.4 --fail-on tor --fail-on proxy
|
|
539
|
+
|
|
540
|
+
Notes:
|
|
541
|
+
An API key is optional. Without one, requests use the anonymous daily trial,
|
|
542
|
+
metered by public IP; the response reports how many are left.
|
|
429
543
|
`
|
|
430
544
|
).action(async (ip, options) => {
|
|
431
545
|
await runCheck(ip, options);
|
|
@@ -435,8 +549,10 @@ async function runCheck(ip, options) {
|
|
|
435
549
|
const useJson = Boolean(options.json);
|
|
436
550
|
for (const flag of options.failOn) {
|
|
437
551
|
if (!KNOWN_FLAGS.includes(flag)) {
|
|
438
|
-
process.stderr.write(
|
|
439
|
-
`)
|
|
552
|
+
process.stderr.write(
|
|
553
|
+
`Error: unknown --fail-on flag "${flag}". Expected one of: ${KNOWN_FLAGS.join(", ")}
|
|
554
|
+
`
|
|
555
|
+
);
|
|
440
556
|
process.exitCode = ExitCode.InvalidArgument;
|
|
441
557
|
return;
|
|
442
558
|
}
|
|
@@ -447,24 +563,17 @@ async function runCheck(ip, options) {
|
|
|
447
563
|
process.exitCode = ExitCode.InvalidArgument;
|
|
448
564
|
return;
|
|
449
565
|
}
|
|
450
|
-
let apiKey;
|
|
451
|
-
try {
|
|
452
|
-
apiKey = resolveApiKey(options.apiKey);
|
|
453
|
-
} catch (err) {
|
|
454
|
-
if (err instanceof NetRiskScanConfigError) {
|
|
455
|
-
printConfigError(err);
|
|
456
|
-
process.exitCode = ExitCode.AuthError;
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
throw err;
|
|
460
|
-
}
|
|
461
566
|
const client = new NetRiskScanClient({
|
|
462
|
-
apiKey,
|
|
567
|
+
apiKey: resolveOptionalApiKey(options.apiKey),
|
|
463
568
|
baseUrl: resolveBaseUrl(options.baseUrl),
|
|
464
569
|
timeout: Number(options.timeout),
|
|
465
570
|
maxRetries: Number(options.maxRetries)
|
|
466
571
|
});
|
|
467
|
-
const spinner = useJson ? void 0 : ora({
|
|
572
|
+
const spinner = useJson ? void 0 : ora({
|
|
573
|
+
text: `Checking ${ip}...`,
|
|
574
|
+
stream: process.stderr,
|
|
575
|
+
isEnabled: process.stderr.isTTY
|
|
576
|
+
}).start();
|
|
468
577
|
try {
|
|
469
578
|
const { data, meta } = await client.checkIp(ip);
|
|
470
579
|
spinner?.stop();
|
|
@@ -502,7 +611,11 @@ function evaluateCiPolicy(data, options) {
|
|
|
502
611
|
}
|
|
503
612
|
function handleCheckError(err, options) {
|
|
504
613
|
if (err instanceof NetRiskScanApiError) {
|
|
505
|
-
|
|
614
|
+
if (err.code === ANONYMOUS_DAILY_LIMIT_CODE) {
|
|
615
|
+
printAnonymousLimitError(err);
|
|
616
|
+
} else {
|
|
617
|
+
printApiError(err, { debug: options.debug });
|
|
618
|
+
}
|
|
506
619
|
process.exitCode = exitCodeForApiErrorCode(err.code);
|
|
507
620
|
return;
|
|
508
621
|
}
|
|
@@ -524,7 +637,15 @@ function handleCheckError(err, options) {
|
|
|
524
637
|
// src/commands/usage.ts
|
|
525
638
|
import ora2 from "ora";
|
|
526
639
|
function registerUsageCommand(program2) {
|
|
527
|
-
program2.command("usage").description("Show API usage and quota for the current billing period").option("--api-key <key>", "NetRiskScan API key (
|
|
640
|
+
program2.command("usage").description("Show API usage and quota for the current billing period (requires an API key)").option("--api-key <key>", "NetRiskScan API key (required for this command)").option("--base-url <url>", "Override the API base URL (advanced)").option("--json", "Output machine-readable JSON").option("--verbose", "Show rate limit and request id").option("--debug", "Show debug information on failure").option("--timeout <ms>", "Request timeout in milliseconds", "10000").option("--max-retries <n>", "Maximum automatic retries for 429/503", "3").addHelpText(
|
|
641
|
+
"after",
|
|
642
|
+
`
|
|
643
|
+
Notes:
|
|
644
|
+
Requires a Developer API key. This reports Developer Account plan, billing
|
|
645
|
+
period and quota, which has no anonymous equivalent - unlike check and batch,
|
|
646
|
+
which work without a key.
|
|
647
|
+
`
|
|
648
|
+
).action(async (options) => {
|
|
528
649
|
await runUsage(options);
|
|
529
650
|
});
|
|
530
651
|
}
|
|
@@ -532,7 +653,7 @@ async function runUsage(options) {
|
|
|
532
653
|
const useJson = Boolean(options.json);
|
|
533
654
|
let apiKey;
|
|
534
655
|
try {
|
|
535
|
-
apiKey =
|
|
656
|
+
apiKey = resolveRequiredApiKey(options.apiKey);
|
|
536
657
|
} catch (err) {
|
|
537
658
|
if (err instanceof NetRiskScanConfigError) {
|
|
538
659
|
printConfigError(err);
|
|
@@ -547,7 +668,11 @@ async function runUsage(options) {
|
|
|
547
668
|
timeout: Number(options.timeout),
|
|
548
669
|
maxRetries: Number(options.maxRetries)
|
|
549
670
|
});
|
|
550
|
-
const spinner = useJson ? void 0 : ora2({
|
|
671
|
+
const spinner = useJson ? void 0 : ora2({
|
|
672
|
+
text: "Fetching usage...",
|
|
673
|
+
stream: process.stderr,
|
|
674
|
+
isEnabled: process.stderr.isTTY
|
|
675
|
+
}).start();
|
|
551
676
|
try {
|
|
552
677
|
const { data, meta } = await client.getUsage();
|
|
553
678
|
spinner?.stop();
|
|
@@ -603,6 +728,30 @@ function printJsonLine(data) {
|
|
|
603
728
|
// src/output/table.ts
|
|
604
729
|
import Table from "cli-table3";
|
|
605
730
|
import chalk3 from "chalk";
|
|
731
|
+
function anonymousSummaryLines(results) {
|
|
732
|
+
const remainings = [];
|
|
733
|
+
const dailyLimits = [];
|
|
734
|
+
let exhausted = false;
|
|
735
|
+
for (const item of results) {
|
|
736
|
+
if (item.ok) {
|
|
737
|
+
const usage = item.result.usage;
|
|
738
|
+
if (usage?.mode !== "anonymous") continue;
|
|
739
|
+
if (typeof usage.remaining === "number") remainings.push(usage.remaining);
|
|
740
|
+
if (typeof usage.dailyLimit === "number") dailyLimits.push(usage.dailyLimit);
|
|
741
|
+
} else if (item.error.code === ANONYMOUS_DAILY_LIMIT_CODE) {
|
|
742
|
+
exhausted = true;
|
|
743
|
+
const limit = item.error.anonymousUsage?.dailyLimit;
|
|
744
|
+
if (typeof limit === "number") dailyLimits.push(limit);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (!exhausted && remainings.length === 0) return [];
|
|
748
|
+
const available = exhausted ? 0 : Math.min(...remainings);
|
|
749
|
+
const lines = ["", chalk3.bold("Anonymous trial"), row("Available", formatNumber(available))];
|
|
750
|
+
if (dailyLimits.length > 0) {
|
|
751
|
+
lines.push(row("Daily Limit", formatNumber(Math.max(...dailyLimits))));
|
|
752
|
+
}
|
|
753
|
+
return lines;
|
|
754
|
+
}
|
|
606
755
|
function renderBatchTable(results) {
|
|
607
756
|
const table = new Table({
|
|
608
757
|
head: ["IP", "Index", "Band", "Proxy", "VPN", "Tor", "Status"],
|
|
@@ -631,13 +780,21 @@ function renderBatchTable(results) {
|
|
|
631
780
|
process.stdout.write(`
|
|
632
781
|
${okCount} succeeded, ${failCount} failed
|
|
633
782
|
`);
|
|
783
|
+
const anonymous = anonymousSummaryLines(results);
|
|
784
|
+
if (anonymous.length > 0) {
|
|
785
|
+
process.stdout.write(`${anonymous.join("\n")}
|
|
786
|
+
`);
|
|
787
|
+
}
|
|
634
788
|
}
|
|
635
789
|
|
|
636
790
|
// src/commands/batch.ts
|
|
637
791
|
var MAX_CONCURRENCY = 20;
|
|
638
792
|
var DEFAULT_CONCURRENCY = 5;
|
|
639
793
|
function registerBatchCommand(program2) {
|
|
640
|
-
program2.command("batch <file>").description("Check multiple IP addresses (client-side batch over GET /v1/ip-risk/{ip})").option(
|
|
794
|
+
program2.command("batch <file>").description("Check multiple IP addresses (client-side batch over GET /v1/ip-risk/{ip})").option(
|
|
795
|
+
"--api-key <key>",
|
|
796
|
+
"NetRiskScan API key (optional; uses the anonymous trial when omitted)"
|
|
797
|
+
).option("--base-url <url>", "Override the API base URL (advanced)").option(
|
|
641
798
|
"--concurrency <n>",
|
|
642
799
|
`Number of concurrent requests (default: ${DEFAULT_CONCURRENCY}, max: ${MAX_CONCURRENCY})`,
|
|
643
800
|
String(DEFAULT_CONCURRENCY)
|
|
@@ -650,6 +807,10 @@ Notes:
|
|
|
650
807
|
offer a server-side batch endpoint. Make sure the selected concurrency
|
|
651
808
|
complies with the limits shown in your NetRiskScan Developer Dashboard.
|
|
652
809
|
|
|
810
|
+
An API key is optional. Without an API key, requests use the anonymous daily
|
|
811
|
+
trial, metered by public IP. How many remain is decided by the server, not by
|
|
812
|
+
this CLI, and is reported in the summary after the table.
|
|
813
|
+
|
|
653
814
|
Input format:
|
|
654
815
|
One IP per line. Blank lines and lines starting with # are ignored.
|
|
655
816
|
|
|
@@ -688,7 +849,12 @@ Check your NetRiskScan Developer Dashboard for your plan's concurrency limit.
|
|
|
688
849
|
}
|
|
689
850
|
function toBatchError(err) {
|
|
690
851
|
if (err instanceof NetRiskScanApiError) {
|
|
691
|
-
return {
|
|
852
|
+
return {
|
|
853
|
+
code: err.code,
|
|
854
|
+
message: err.message,
|
|
855
|
+
requestId: err.requestId,
|
|
856
|
+
anonymousUsage: err.anonymousUsage
|
|
857
|
+
};
|
|
692
858
|
}
|
|
693
859
|
if (err instanceof NetRiskScanNetworkError) {
|
|
694
860
|
return { code: "network_error", message: err.message, requestId: err.requestId };
|
|
@@ -696,17 +862,6 @@ function toBatchError(err) {
|
|
|
696
862
|
return { code: "unknown_error", message: err instanceof Error ? err.message : String(err) };
|
|
697
863
|
}
|
|
698
864
|
async function runBatch(file, options) {
|
|
699
|
-
let apiKey;
|
|
700
|
-
try {
|
|
701
|
-
apiKey = resolveApiKey(options.apiKey);
|
|
702
|
-
} catch (err) {
|
|
703
|
-
if (err instanceof NetRiskScanConfigError) {
|
|
704
|
-
printConfigError(err);
|
|
705
|
-
process.exitCode = ExitCode.AuthError;
|
|
706
|
-
return;
|
|
707
|
-
}
|
|
708
|
-
throw err;
|
|
709
|
-
}
|
|
710
865
|
const ips = await readIps(file);
|
|
711
866
|
if (ips.length === 0) {
|
|
712
867
|
process.stderr.write("Error: no IP addresses found in input.\n");
|
|
@@ -714,7 +869,7 @@ async function runBatch(file, options) {
|
|
|
714
869
|
return;
|
|
715
870
|
}
|
|
716
871
|
const client = new NetRiskScanClient({
|
|
717
|
-
apiKey,
|
|
872
|
+
apiKey: resolveOptionalApiKey(options.apiKey),
|
|
718
873
|
baseUrl: resolveBaseUrl(options.baseUrl),
|
|
719
874
|
timeout: Number(options.timeout),
|
|
720
875
|
maxRetries: Number(options.maxRetries)
|
|
@@ -757,17 +912,23 @@ function computeBatchExitCode(results) {
|
|
|
757
912
|
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
758
913
|
var pkg = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf8"));
|
|
759
914
|
var program = new Command();
|
|
760
|
-
program.name("netriskscan").description(
|
|
915
|
+
program.name("netriskscan").description(
|
|
916
|
+
"Query IP risk, network intelligence and reputation data from the NetRiskScan Developer API."
|
|
917
|
+
).version(pkg.version, "-V, --version", "Show the CLI version").addHelpText(
|
|
761
918
|
"after",
|
|
762
919
|
`
|
|
763
920
|
Examples:
|
|
764
|
-
$ netriskscan check 1.1.1.1
|
|
921
|
+
$ netriskscan check 1.1.1.1 # no account or API key needed
|
|
765
922
|
$ netriskscan check 1.1.1.1 --json
|
|
766
|
-
$ netriskscan usage
|
|
923
|
+
$ netriskscan usage # needs an API key
|
|
767
924
|
$ netriskscan batch ips.txt --jsonl
|
|
768
925
|
|
|
926
|
+
check and batch work with no setup at all: without an API key they use the
|
|
927
|
+
anonymous daily trial, metered by public IP. The server reports how many
|
|
928
|
+
requests are left; the CLI never estimates it.
|
|
929
|
+
|
|
769
930
|
Environment variables:
|
|
770
|
-
NETRISKSCAN_API_KEY
|
|
931
|
+
NETRISKSCAN_API_KEY Optional for check/batch; required for account usage.
|
|
771
932
|
|
|
772
933
|
NetRiskScan provides diagnostic network intelligence. Results should not be
|
|
773
934
|
treated as proof of malicious activity or used as the sole basis for legal,
|