@willh/ip-checker 0.0.0-stage → 0.1.1

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 CHANGED
@@ -1,3 +1,66 @@
1
- # Temporary Holding Version
1
+ # @willh/ip-checker
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A tiny, zero-dependency TypeScript client for the [OpenEnvCheck](https://open-env-check.miniasp.com/) IP Intelligence REST API.
4
+ Look up full IP intelligence — geolocation, ASN/ISP, RDAP, reverse DNS, Shodan ports/CVEs, Tor / datacenter / VPN classification, and abuse reputation — for any public IPv4/IPv6 address, or for the caller's own IP.
5
+
6
+ - **No API key, no authentication** (API v1).
7
+ - **Server-side only**: the API sends no CORS headers by design, so call it from Node, a backend, a CLI, or a script — not from cross-origin browser pages.
8
+ - Requires Node >= 18 (uses the global `fetch`). Works with any `fetch`-compatible runtime.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ npm install @willh/ip-checker
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { lookupIp, lookupOwnIp, createIpClient } from '@willh/ip-checker';
20
+
21
+ // One-off lookups
22
+ const intel = await lookupIp('8.8.8.8');
23
+ console.log(intel.countryCode, intel.asn, intel.isp, intel.isDatacenter);
24
+
25
+ const mine = await lookupOwnIp();
26
+ console.log(mine.ip, mine.countryName, mine.timezone);
27
+
28
+ // Reusable client (custom base URL for self-hosting, timeout, or fetch)
29
+ const client = createIpClient({ baseUrl: 'https://open-env-check.miniasp.com', timeoutMs: 8000 });
30
+ const torCheck = await client.lookup('185.220.101.4');
31
+ console.log(torCheck.torExit);
32
+ ```
33
+
34
+ ## Error handling
35
+
36
+ Non-2xx responses throw `OpenEnvCheckApiError` with `status`, `code`, and `ip`:
37
+
38
+ ```ts
39
+ import { lookupIp, OpenEnvCheckApiError } from '@willh/ip-checker';
40
+
41
+ try {
42
+ await lookupIp('192.168.1.1');
43
+ } catch (err) {
44
+ if (err instanceof OpenEnvCheckApiError) {
45
+ console.log(err.status); // 422
46
+ console.log(err.code); // "private_or_reserved_ip_not_allowed"
47
+ }
48
+ }
49
+ ```
50
+
51
+ | HTTP | `code` | Meaning |
52
+ |---|---|---|
53
+ | 400 | `invalid_ip_address` | Input is not a valid IPv4/IPv6 address |
54
+ | 422 | `private_or_reserved_ip_not_allowed` | Private/reserved IPs have no public intelligence |
55
+ | 400 | `unable_to_determine_ip` | Own-IP lookup could not determine the caller's IP |
56
+
57
+ ## Notes
58
+
59
+ - Response fields can be `null` when an upstream source has no data — always null-check (e.g. `city`, `abuseIpDb.confidenceScore`). `abuseIpDb.status === 'unknown'` means "no data", not "clean".
60
+ - Results for a specific IP are cacheable for 300 seconds; the response's `cached` flag tells you whether the edge cache served it.
61
+
62
+ Full API contract: `GET https://open-env-check.miniasp.com/api/v1/openapi.json`.
63
+
64
+ ## License
65
+
66
+ MIT © Will 保哥
@@ -0,0 +1,87 @@
1
+ /**
2
+ * @willh/ip-checker
3
+ *
4
+ * OpenEnvCheck IP Intelligence REST API v1 的輕量 TypeScript 包裝。
5
+ * 零相依、只用全域 fetch(Node >= 18),供伺服器端、CLI、腳本與 AI Agent 使用。
6
+ * 注意:API v1 不回 CORS Headers,請勿從跨來源瀏覽器頁面直接呼叫。
7
+ *
8
+ * 文件:https://open-env-check.miniasp.com/api/v1/openapi.json
9
+ */
10
+ declare const DEFAULT_BASE_URL = "https://open-env-check.miniasp.com";
11
+ interface ShodanInfo {
12
+ available: boolean;
13
+ ports: number[];
14
+ vulns: string[];
15
+ hostnames: string[];
16
+ tags: string[];
17
+ }
18
+ interface AbuseIpDbInfo {
19
+ status: 'checked' | 'unknown' | 'error';
20
+ confidenceScore: number | null;
21
+ totalReports: number | null;
22
+ }
23
+ /** 與 API 回應一對一對應的完整 IP 情報。欄位可能為 null(上游無資料),使用前請先判空。 */
24
+ interface IpIntelligenceData {
25
+ ip: string | null;
26
+ ipVersion: 'v4' | 'v6' | 'unknown';
27
+ isPrivate: boolean;
28
+ countryCode: string | null;
29
+ countryName: string | null;
30
+ city: string | null;
31
+ region: string | null;
32
+ timezone: string | null;
33
+ latitude: number | null;
34
+ longitude: number | null;
35
+ asn: number | null;
36
+ isp: string | null;
37
+ organization: string | null;
38
+ cidr: string | null;
39
+ rdapHandle: string | null;
40
+ abuseContact: string | null;
41
+ reverseDns: string[];
42
+ shodan: ShodanInfo;
43
+ torExit: boolean;
44
+ isDatacenter: boolean;
45
+ datacenterProvider: string | null;
46
+ isVpnOrProxy: boolean;
47
+ vpnProxyReason: string | null;
48
+ abuseIpDb: AbuseIpDbInfo;
49
+ sourcesUsed: string[];
50
+ sourceErrors: Record<string, string>;
51
+ cached: boolean;
52
+ }
53
+ /** API 回傳非 2xx 時拋出的錯誤,帶 HTTP 狀態與 API 錯誤代碼。 */
54
+ declare class OpenEnvCheckApiError extends Error {
55
+ readonly status: number;
56
+ /** API 錯誤代碼,例如 invalid_ip_address、private_or_reserved_ip_not_allowed、unable_to_determine_ip */
57
+ readonly code: string;
58
+ readonly ip: string | null;
59
+ constructor(status: number, code: string, ip?: string | null);
60
+ }
61
+ interface IpClientOptions {
62
+ /** API 基底網址,預設 https://open-env-check.miniasp.com(可指向自架站台) */
63
+ baseUrl?: string;
64
+ /** 自訂 fetch 實作(測試或特殊執行環境用),預設使用全域 fetch */
65
+ fetch?: typeof fetch;
66
+ /** 單次請求逾時毫秒數,預設 10000 */
67
+ timeoutMs?: number;
68
+ }
69
+ declare class OpenEnvCheckIpClient {
70
+ private readonly baseUrl;
71
+ private readonly fetchImpl;
72
+ private readonly timeoutMs;
73
+ constructor(options?: IpClientOptions);
74
+ /** 查詢指定公開 IPv4/IPv6 位址的完整情報。私人/保留 IP 會拋出 422 的 OpenEnvCheckApiError。 */
75
+ lookup(ip: string): Promise<IpIntelligenceData>;
76
+ /** 查詢呼叫端自身 IP 的完整情報。 */
77
+ lookupOwn(): Promise<IpIntelligenceData>;
78
+ private request;
79
+ }
80
+ /** 建立可重複使用的 client(多次查詢、共用設定時建議用這個)。 */
81
+ declare function createIpClient(options?: IpClientOptions): OpenEnvCheckIpClient;
82
+ /** 一次性查詢指定 IP 的完整情報。 */
83
+ declare function lookupIp(ip: string, options?: IpClientOptions): Promise<IpIntelligenceData>;
84
+ /** 一次性查詢呼叫端自身 IP 的完整情報。 */
85
+ declare function lookupOwnIp(options?: IpClientOptions): Promise<IpIntelligenceData>;
86
+
87
+ export { type AbuseIpDbInfo, DEFAULT_BASE_URL, type IpClientOptions, type IpIntelligenceData, OpenEnvCheckApiError, OpenEnvCheckIpClient, type ShodanInfo, createIpClient, lookupIp, lookupOwnIp };
package/dist/index.js ADDED
@@ -0,0 +1,79 @@
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
+ export {
73
+ DEFAULT_BASE_URL,
74
+ OpenEnvCheckApiError,
75
+ OpenEnvCheckIpClient,
76
+ createIpClient,
77
+ lookupIp,
78
+ lookupOwnIp
79
+ };
package/package.json CHANGED
@@ -1,6 +1 @@
1
- {
2
- "name": "@willh/ip-checker",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {"author":{"name":"Will 保哥","url":"https://www.facebook.com/will.fans/"},"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.","engines":{"node":">=18.0.0"},"exports":{".":{"import":"./dist/index.js","types":"./dist/index.d.ts"}},"files":["dist","README.md"],"homepage":"https://open-env-check.miniasp.com/","keywords":["ip","ip-intelligence","geolocation","asn","ip-lookup","open-env-check"],"license":"MIT","main":"./dist/index.js","name":"@willh/ip-checker","publishConfig":{"access":"public"},"repository":{"directory":"packages/ip-client","type":"git","url":"https://github.com/doggy8088/open-env-check.git"},"scripts":{"build":"tsup src/index.ts --format esm --dts --clean","prepublishOnly":"npm run build","typecheck":"tsc --noEmit"},"sideEffects":false,"type":"module","types":"./dist/index.d.ts","version":"0.1.1"}