@willh/ip-checker 0.1.1 → 0.2.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/README.md +19 -0
- package/dist/chunk-6VBQPI5M.js +80 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +107 -0
- package/dist/index.js +8 -71
- package/package.json +52 -1
package/README.md
CHANGED
|
@@ -13,6 +13,25 @@ Look up full IP intelligence — geolocation, ASN/ISP, RDAP, reverse DNS, Shodan
|
|
|
13
13
|
npm install @willh/ip-checker
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
+
## CLI (npx)
|
|
17
|
+
|
|
18
|
+
The package ships an `ip-checker` binary, so you can query straight from a terminal — no install needed:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
# Full intelligence for a specific IP, as pretty JSON
|
|
22
|
+
npx @willh/ip-checker 8.8.8.8
|
|
23
|
+
|
|
24
|
+
# One-line summary: <ip> | <city, region, country> | <ASN org> | tz <timezone> | <flags>
|
|
25
|
+
npx @willh/ip-checker --summary 1.1.1.1
|
|
26
|
+
|
|
27
|
+
# Your own IP
|
|
28
|
+
npx @willh/ip-checker
|
|
29
|
+
|
|
30
|
+
# Options: --base-url <url>, --timeout <ms>, -h/--help
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Exit codes: `0` success, `1` usage error, `2` API or network error (the API error code is printed to stderr).
|
|
34
|
+
|
|
16
35
|
## Usage
|
|
17
36
|
|
|
18
37
|
```ts
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://open-env-check.miniasp.com";
|
|
3
|
+
var OpenEnvCheckApiError = class extends Error {
|
|
4
|
+
status;
|
|
5
|
+
/** API 錯誤代碼,例如 invalid_ip_address、private_or_reserved_ip_not_allowed、unable_to_determine_ip */
|
|
6
|
+
code;
|
|
7
|
+
ip;
|
|
8
|
+
constructor(status, code, ip = null) {
|
|
9
|
+
super(`OpenEnvCheck API error ${status}: ${code}${ip ? ` (ip: ${ip})` : ""}`);
|
|
10
|
+
this.name = "OpenEnvCheckApiError";
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.ip = ip;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var OpenEnvCheckIpClient = class {
|
|
17
|
+
baseUrl;
|
|
18
|
+
fetchImpl;
|
|
19
|
+
timeoutMs;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
22
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
23
|
+
if (typeof fetchImpl !== "function") {
|
|
24
|
+
throw new Error(
|
|
25
|
+
"No fetch implementation available. Use Node >= 18 or pass options.fetch."
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
this.fetchImpl = fetchImpl;
|
|
29
|
+
this.timeoutMs = options.timeoutMs ?? 1e4;
|
|
30
|
+
}
|
|
31
|
+
/** 查詢指定公開 IPv4/IPv6 位址的完整情報。私人/保留 IP 會拋出 422 的 OpenEnvCheckApiError。 */
|
|
32
|
+
async lookup(ip) {
|
|
33
|
+
const target = ip.trim();
|
|
34
|
+
return this.request(`/api/v1/ip/${encodeURIComponent(target)}`);
|
|
35
|
+
}
|
|
36
|
+
/** 查詢呼叫端自身 IP 的完整情報。 */
|
|
37
|
+
async lookupOwn() {
|
|
38
|
+
return this.request("/api/v1/ip");
|
|
39
|
+
}
|
|
40
|
+
async request(path) {
|
|
41
|
+
const res = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
42
|
+
headers: { Accept: "application/json" },
|
|
43
|
+
signal: AbortSignal.timeout(this.timeoutMs)
|
|
44
|
+
});
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
let code = `http_${res.status}`;
|
|
47
|
+
let ip = null;
|
|
48
|
+
try {
|
|
49
|
+
const body = await res.json();
|
|
50
|
+
if (body.error) code = body.error;
|
|
51
|
+
ip = body.ip ?? null;
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
throw new OpenEnvCheckApiError(res.status, code, ip);
|
|
55
|
+
}
|
|
56
|
+
const data = await res.json();
|
|
57
|
+
if (!data || typeof data !== "object" || !("ip" in data)) {
|
|
58
|
+
throw new OpenEnvCheckApiError(res.status, "invalid_response");
|
|
59
|
+
}
|
|
60
|
+
return data;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
function createIpClient(options) {
|
|
64
|
+
return new OpenEnvCheckIpClient(options);
|
|
65
|
+
}
|
|
66
|
+
async function lookupIp(ip, options) {
|
|
67
|
+
return new OpenEnvCheckIpClient(options).lookup(ip);
|
|
68
|
+
}
|
|
69
|
+
async function lookupOwnIp(options) {
|
|
70
|
+
return new OpenEnvCheckIpClient(options).lookupOwn();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
DEFAULT_BASE_URL,
|
|
75
|
+
OpenEnvCheckApiError,
|
|
76
|
+
OpenEnvCheckIpClient,
|
|
77
|
+
createIpClient,
|
|
78
|
+
lookupIp,
|
|
79
|
+
lookupOwnIp
|
|
80
|
+
};
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { IpIntelligenceData } from './index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ip-checker CLI:讓 `npx @willh/ip-checker [ip]` 直接在終端機查詢 IP 情報。
|
|
6
|
+
* 未帶 IP 時查呼叫端自身 IP;預設輸出完整情報的 pretty JSON,--summary 則輸出一行摘要。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
interface CliOptions {
|
|
10
|
+
ip: string | null;
|
|
11
|
+
summary: boolean;
|
|
12
|
+
help: boolean;
|
|
13
|
+
baseUrl?: string;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
}
|
|
16
|
+
declare const USAGE = "Usage: ip-checker [ip] [options]\n\nLook up full IP intelligence via the OpenEnvCheck API.\nWithout [ip], looks up your own IP.\n\nOptions:\n --summary Print a one-line summary instead of full JSON\n --base-url <url> API base URL (default https://open-env-check.miniasp.com)\n --timeout <ms> Request timeout in milliseconds (default 10000)\n -h, --help Show this help\n\nExamples:\n npx @willh/ip-checker 8.8.8.8\n npx @willh/ip-checker --summary 1.1.1.1\n npx @willh/ip-checker\n";
|
|
17
|
+
declare function parseArgs(argv: string[]): CliOptions;
|
|
18
|
+
/** 一行摘要:IP、位置、ASN/ISP 與主要分類旗標;null 欄位以 - 表示。 */
|
|
19
|
+
declare function formatSummary(d: IpIntelligenceData): string;
|
|
20
|
+
declare function main(argv: string[]): Promise<number>;
|
|
21
|
+
|
|
22
|
+
export { type CliOptions, USAGE, formatSummary, main, parseArgs };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
OpenEnvCheckApiError,
|
|
4
|
+
createIpClient
|
|
5
|
+
} from "./chunk-6VBQPI5M.js";
|
|
6
|
+
|
|
7
|
+
// src/cli.ts
|
|
8
|
+
var USAGE = `Usage: ip-checker [ip] [options]
|
|
9
|
+
|
|
10
|
+
Look up full IP intelligence via the OpenEnvCheck API.
|
|
11
|
+
Without [ip], looks up your own IP.
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--summary Print a one-line summary instead of full JSON
|
|
15
|
+
--base-url <url> API base URL (default https://open-env-check.miniasp.com)
|
|
16
|
+
--timeout <ms> Request timeout in milliseconds (default 10000)
|
|
17
|
+
-h, --help Show this help
|
|
18
|
+
|
|
19
|
+
Examples:
|
|
20
|
+
npx @willh/ip-checker 8.8.8.8
|
|
21
|
+
npx @willh/ip-checker --summary 1.1.1.1
|
|
22
|
+
npx @willh/ip-checker
|
|
23
|
+
`;
|
|
24
|
+
function parseArgs(argv) {
|
|
25
|
+
const options = { ip: null, summary: false, help: false };
|
|
26
|
+
for (let i = 0; i < argv.length; i++) {
|
|
27
|
+
const arg = argv[i];
|
|
28
|
+
if (arg === "-h" || arg === "--help") {
|
|
29
|
+
options.help = true;
|
|
30
|
+
} else if (arg === "--summary") {
|
|
31
|
+
options.summary = true;
|
|
32
|
+
} else if (arg === "--base-url") {
|
|
33
|
+
const value = argv[++i];
|
|
34
|
+
if (!value) throw new Error("--base-url requires a value");
|
|
35
|
+
options.baseUrl = value;
|
|
36
|
+
} else if (arg === "--timeout") {
|
|
37
|
+
const value = argv[++i];
|
|
38
|
+
const ms = value ? Number(value) : NaN;
|
|
39
|
+
if (!Number.isFinite(ms) || ms <= 0) throw new Error("--timeout requires a positive number");
|
|
40
|
+
options.timeoutMs = ms;
|
|
41
|
+
} else if (arg.startsWith("-")) {
|
|
42
|
+
throw new Error(`unknown option: ${arg}`);
|
|
43
|
+
} else if (options.ip === null) {
|
|
44
|
+
options.ip = arg;
|
|
45
|
+
} else {
|
|
46
|
+
throw new Error(`unexpected extra argument: ${arg}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return options;
|
|
50
|
+
}
|
|
51
|
+
function formatSummary(d) {
|
|
52
|
+
const place = [d.city, d.region, d.countryName ?? d.countryCode].filter((v, i, arr) => v && arr.indexOf(v) === i).join(", ") || "-";
|
|
53
|
+
const asn = d.asn ? `AS${d.asn}` : "-";
|
|
54
|
+
const org = d.organization ?? d.isp ?? "-";
|
|
55
|
+
const flags = [];
|
|
56
|
+
if (d.isDatacenter) flags.push(`datacenter${d.datacenterProvider ? ` (${d.datacenterProvider})` : ""}`);
|
|
57
|
+
if (d.isVpnOrProxy) flags.push("vpn/proxy");
|
|
58
|
+
if (d.torExit) flags.push("tor-exit");
|
|
59
|
+
const flagText = flags.length ? ` | ${flags.join(" | ")}` : "";
|
|
60
|
+
return `${d.ip ?? "-"} | ${place} | ${asn} ${org} | tz ${d.timezone ?? "-"}${flagText}`;
|
|
61
|
+
}
|
|
62
|
+
async function main(argv) {
|
|
63
|
+
let options;
|
|
64
|
+
try {
|
|
65
|
+
options = parseArgs(argv);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
console.error(`ip-checker: ${err instanceof Error ? err.message : String(err)}`);
|
|
68
|
+
console.error("Run with --help for usage.");
|
|
69
|
+
return 1;
|
|
70
|
+
}
|
|
71
|
+
if (options.help) {
|
|
72
|
+
console.log(USAGE);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
const client = createIpClient({
|
|
76
|
+
baseUrl: options.baseUrl,
|
|
77
|
+
timeoutMs: options.timeoutMs
|
|
78
|
+
});
|
|
79
|
+
try {
|
|
80
|
+
const data = options.ip ? await client.lookup(options.ip) : await client.lookupOwn();
|
|
81
|
+
console.log(options.summary ? formatSummary(data) : JSON.stringify(data, null, 2));
|
|
82
|
+
return 0;
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (err instanceof OpenEnvCheckApiError) {
|
|
85
|
+
console.error(`ip-checker: API error ${err.status}: ${err.code}${err.ip ? ` (ip: ${err.ip})` : ""}`);
|
|
86
|
+
} else {
|
|
87
|
+
console.error(`ip-checker: ${err instanceof Error ? err.message : String(err)}`);
|
|
88
|
+
}
|
|
89
|
+
return 2;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
var isDirectRun = typeof process !== "undefined" && process.argv[1] !== void 0 && /(^|[\\/])cli\.(js|ts)$/.test(process.argv[1]);
|
|
93
|
+
if (isDirectRun) {
|
|
94
|
+
main(process.argv.slice(2)).then(
|
|
95
|
+
(code) => process.exit(code),
|
|
96
|
+
(err) => {
|
|
97
|
+
console.error(err);
|
|
98
|
+
process.exit(2);
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
USAGE,
|
|
104
|
+
formatSummary,
|
|
105
|
+
main,
|
|
106
|
+
parseArgs
|
|
107
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,74 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
super(`OpenEnvCheck API error ${status}: ${code}${ip ? ` (ip: ${ip})` : ""}`);
|
|
10
|
-
this.name = "OpenEnvCheckApiError";
|
|
11
|
-
this.status = status;
|
|
12
|
-
this.code = code;
|
|
13
|
-
this.ip = ip;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var OpenEnvCheckIpClient = class {
|
|
17
|
-
baseUrl;
|
|
18
|
-
fetchImpl;
|
|
19
|
-
timeoutMs;
|
|
20
|
-
constructor(options = {}) {
|
|
21
|
-
this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
22
|
-
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
23
|
-
if (typeof fetchImpl !== "function") {
|
|
24
|
-
throw new Error(
|
|
25
|
-
"No fetch implementation available. Use Node >= 18 or pass options.fetch."
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
this.fetchImpl = fetchImpl;
|
|
29
|
-
this.timeoutMs = options.timeoutMs ?? 1e4;
|
|
30
|
-
}
|
|
31
|
-
/** 查詢指定公開 IPv4/IPv6 位址的完整情報。私人/保留 IP 會拋出 422 的 OpenEnvCheckApiError。 */
|
|
32
|
-
async lookup(ip) {
|
|
33
|
-
const target = ip.trim();
|
|
34
|
-
return this.request(`/api/v1/ip/${encodeURIComponent(target)}`);
|
|
35
|
-
}
|
|
36
|
-
/** 查詢呼叫端自身 IP 的完整情報。 */
|
|
37
|
-
async lookupOwn() {
|
|
38
|
-
return this.request("/api/v1/ip");
|
|
39
|
-
}
|
|
40
|
-
async request(path) {
|
|
41
|
-
const res = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
42
|
-
headers: { Accept: "application/json" },
|
|
43
|
-
signal: AbortSignal.timeout(this.timeoutMs)
|
|
44
|
-
});
|
|
45
|
-
if (!res.ok) {
|
|
46
|
-
let code = `http_${res.status}`;
|
|
47
|
-
let ip = null;
|
|
48
|
-
try {
|
|
49
|
-
const body = await res.json();
|
|
50
|
-
if (body.error) code = body.error;
|
|
51
|
-
ip = body.ip ?? null;
|
|
52
|
-
} catch {
|
|
53
|
-
}
|
|
54
|
-
throw new OpenEnvCheckApiError(res.status, code, ip);
|
|
55
|
-
}
|
|
56
|
-
const data = await res.json();
|
|
57
|
-
if (!data || typeof data !== "object" || !("ip" in data)) {
|
|
58
|
-
throw new OpenEnvCheckApiError(res.status, "invalid_response");
|
|
59
|
-
}
|
|
60
|
-
return data;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
function createIpClient(options) {
|
|
64
|
-
return new OpenEnvCheckIpClient(options);
|
|
65
|
-
}
|
|
66
|
-
async function lookupIp(ip, options) {
|
|
67
|
-
return new OpenEnvCheckIpClient(options).lookup(ip);
|
|
68
|
-
}
|
|
69
|
-
async function lookupOwnIp(options) {
|
|
70
|
-
return new OpenEnvCheckIpClient(options).lookupOwn();
|
|
71
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_BASE_URL,
|
|
3
|
+
OpenEnvCheckApiError,
|
|
4
|
+
OpenEnvCheckIpClient,
|
|
5
|
+
createIpClient,
|
|
6
|
+
lookupIp,
|
|
7
|
+
lookupOwnIp
|
|
8
|
+
} from "./chunk-6VBQPI5M.js";
|
|
72
9
|
export {
|
|
73
10
|
DEFAULT_BASE_URL,
|
|
74
11
|
OpenEnvCheckApiError,
|
package/package.json
CHANGED
|
@@ -1 +1,52 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"author": {
|
|
3
|
+
"name": "Will 保哥",
|
|
4
|
+
"url": "https://www.facebook.com/will.fans/"
|
|
5
|
+
},
|
|
6
|
+
"description": "A tiny, zero-dependency TypeScript client for the OpenEnvCheck IP Intelligence REST API (https://open-env-check.miniasp.com/api/v1/ip). Look up full IP intelligence for any public IPv4/IPv6 address, or for the caller's own IP.",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=18.0.0"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"homepage": "https://open-env-check.miniasp.com/",
|
|
21
|
+
"keywords": [
|
|
22
|
+
"ip",
|
|
23
|
+
"ip-intelligence",
|
|
24
|
+
"geolocation",
|
|
25
|
+
"asn",
|
|
26
|
+
"ip-lookup",
|
|
27
|
+
"open-env-check"
|
|
28
|
+
],
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"main": "./dist/index.js",
|
|
31
|
+
"name": "@willh/ip-checker",
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"directory": "packages/ip-client",
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/doggy8088/open-env-check.git"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/index.ts src/cli.ts --format esm --dts --clean",
|
|
42
|
+
"prepublishOnly": "npm run build",
|
|
43
|
+
"typecheck": "tsc --noEmit"
|
|
44
|
+
},
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"type": "module",
|
|
47
|
+
"types": "./dist/index.d.ts",
|
|
48
|
+
"version": "0.2.0",
|
|
49
|
+
"bin": {
|
|
50
|
+
"ip-checker": "./dist/cli.js"
|
|
51
|
+
}
|
|
52
|
+
}
|