@gibme/ipinfo 1.0.1 → 2.0.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016-2023 Brandon Lehmann
1
+ Copyright (c) 2016-2025 Brandon Lehmann
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -1,15 +1,38 @@
1
- import { PrefixInfo } from './types';
2
- import { CheckIP } from './web';
3
- export { CheckIP };
1
+ export { ipInfo } from './web';
4
2
  /**
5
- * Looks up information regarding the IP address supplied
3
+ * Looks up information on the Autonomous System Number (ASN) via DNS
4
+ *
5
+ * @param as
6
+ * @param max_retries
7
+ * @ignore
8
+ */
9
+ export declare function getASN(as: string | number, max_retries?: number): Promise<getASN.Entry | undefined>;
10
+ export declare namespace getASN {
11
+ type Entry = {
12
+ asn: number;
13
+ country: string;
14
+ registry: string;
15
+ allocated: Date;
16
+ name: string;
17
+ };
18
+ }
19
+ /**
20
+ * Looks up the prefix information via DNS
6
21
  *
7
22
  * @param address
8
23
  * @param max_retries
24
+ * @ignore
9
25
  */
10
- export declare const IPInfo: (address: string, max_retries?: number) => Promise<PrefixInfo | undefined>;
11
- declare const _default: {
12
- IPInfo: (address: string, max_retries?: number) => Promise<PrefixInfo | undefined>;
13
- CheckIP: (address?: string) => Promise<import("./types").CheckIPResponse | undefined>;
14
- };
15
- export default _default;
26
+ export declare function getPrefix(address: string, max_retries?: number): Promise<getPrefix.Info | undefined>;
27
+ export declare namespace getPrefix {
28
+ type Info = {
29
+ address: string;
30
+ zone: string;
31
+ asn: number;
32
+ prefix: string;
33
+ country: string;
34
+ registry: string;
35
+ allocated: Date;
36
+ as?: getASN.Entry;
37
+ };
38
+ }
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright (c) 2016-2024, Brandon Lehmann <brandonlehmann@gmail.com>
2
+ // Copyright (c) 2016-2025, Brandon Lehmann <brandonlehmann@gmail.com>
3
3
  //
4
4
  // Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  // of this software and associated documentation files (the "Software"), to deal
@@ -27,26 +27,126 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
27
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
28
  });
29
29
  };
30
- var __importDefault = (this && this.__importDefault) || function (mod) {
31
- return (mod && mod.__esModule) ? mod : { "default": mod };
32
- };
33
30
  Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.IPInfo = exports.CheckIP = void 0;
35
- const helpers_1 = __importDefault(require("./helpers"));
36
- const web_1 = require("./web");
37
- Object.defineProperty(exports, "CheckIP", { enumerable: true, get: function () { return web_1.CheckIP; } });
31
+ exports.ipInfo = void 0;
32
+ exports.getASN = getASN;
33
+ exports.getPrefix = getPrefix;
34
+ const promises_1 = require("dns/promises");
35
+ var web_1 = require("./web");
36
+ Object.defineProperty(exports, "ipInfo", { enumerable: true, get: function () { return web_1.ipInfo; } });
37
+ /** @ignore */
38
+ const sleep = (timeout) => __awaiter(void 0, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, timeout)); });
39
+ /** @ignore */
40
+ const countColons = (value) => {
41
+ let count = 0;
42
+ value.replace(/:/g, val => { count++; return val; });
43
+ return count;
44
+ };
38
45
  /**
39
- * Looks up information regarding the IP address supplied
46
+ * @ignore
47
+ * Expands compressed IPv6 address to uncompressed form
40
48
  *
41
- * @param address
49
+ * @param value
50
+ */
51
+ const expandIPv6 = (value) => {
52
+ return value.replace(/::/, () => {
53
+ return `:${Array((7 - countColons(value)) + 1).join(':')}:`;
54
+ })
55
+ .split(':')
56
+ .map(val => {
57
+ return Array(4 - val.length).fill('0').join('') + val;
58
+ })
59
+ .join(':');
60
+ };
61
+ /**
62
+ * DNS TXT record resolution with automatic retries
63
+ *
64
+ * @param hostname
42
65
  * @param max_retries
66
+ * @param retry
67
+ * @ignore
43
68
  */
44
- const IPInfo = (address_1, ...args_1) => __awaiter(void 0, [address_1, ...args_1], void 0, function* (address, max_retries = 3) {
45
- return helpers_1.default.getPrefix(address, max_retries);
69
+ const getTxtRecord = (hostname_1, ...args_1) => __awaiter(void 0, [hostname_1, ...args_1], void 0, function* (hostname, max_retries = 3, retry = 0) {
70
+ try {
71
+ return yield (0, promises_1.resolveTxt)(hostname);
72
+ }
73
+ catch (error) {
74
+ if (error.toString().toLowerCase().includes('refused') && retry < max_retries) {
75
+ yield sleep(1000);
76
+ return getTxtRecord(hostname, max_retries, retry);
77
+ }
78
+ throw error;
79
+ }
46
80
  });
47
- exports.IPInfo = IPInfo;
48
- exports.default = {
49
- IPInfo: exports.IPInfo,
50
- CheckIP: web_1.CheckIP
51
- };
81
+ /**
82
+ * Looks up information on the Autonomous System Number (ASN) via DNS
83
+ *
84
+ * @param as
85
+ * @param max_retries
86
+ * @ignore
87
+ */
88
+ function getASN(as_1) {
89
+ return __awaiter(this, arguments, void 0, function* (as, max_retries = 3) {
90
+ if (typeof as === 'string') {
91
+ as = parseInt(as);
92
+ }
93
+ const records = yield getTxtRecord(`AS${as}.asn.cymru.com`, max_retries);
94
+ if (records.length === 0 || records[0].length === 0) {
95
+ return undefined;
96
+ }
97
+ const [, country, registry, allocated, name] = records[0][0].split('|')
98
+ .map(elem => elem.trim());
99
+ return {
100
+ asn: as,
101
+ country: country.toUpperCase(),
102
+ registry: registry.toUpperCase(),
103
+ allocated: new Date(allocated),
104
+ name: name.split(',')[0]
105
+ };
106
+ });
107
+ }
108
+ /**
109
+ * Looks up the prefix information via DNS
110
+ *
111
+ * @param address
112
+ * @param max_retries
113
+ * @ignore
114
+ */
115
+ function getPrefix(address_1) {
116
+ return __awaiter(this, arguments, void 0, function* (address, max_retries = 3) {
117
+ const original_address = address;
118
+ address = address.split('/')[0]; // strip off any CIDR notation
119
+ if (address.includes(':')) {
120
+ address = expandIPv6(address);
121
+ address = address.split(':')
122
+ .join('')
123
+ .split('')
124
+ .reverse()
125
+ .join('.');
126
+ address = `${address}.origin6.asn.cymru.com`;
127
+ }
128
+ else {
129
+ address = address.split('.')
130
+ .reverse()
131
+ .join('.');
132
+ address = `${address}.origin.asn.cymru.com`;
133
+ }
134
+ const records = yield getTxtRecord(address, max_retries);
135
+ if (records.length === 0 || records[0].length === 0) {
136
+ return undefined;
137
+ }
138
+ const [asn, prefix, country, registry, allocated] = records[0][0].split('|')
139
+ .map(elem => elem.trim());
140
+ return {
141
+ address: original_address,
142
+ zone: address,
143
+ asn: parseInt(asn),
144
+ prefix,
145
+ country: country.toUpperCase(),
146
+ registry: registry.toUpperCase(),
147
+ allocated: new Date(allocated),
148
+ as: yield getASN(asn)
149
+ };
150
+ });
151
+ }
52
152
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,sEAAsE;AACtE,EAAE;AACF,+EAA+E;AAC/E,gFAAgF;AAChF,+EAA+E;AAC/E,4EAA4E;AAC5E,wEAAwE;AACxE,2DAA2D;AAC3D,EAAE;AACF,iFAAiF;AACjF,kDAAkD;AAClD,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,gFAAgF;AAChF,gFAAgF;AAChF,YAAY;;;;;;;;;;;;;;;AAGZ,wDAAgC;AAChC,+BAAgC;AACvB,wFADA,aAAO,OACA;AAEhB;;;;;GAKG;AACI,MAAM,MAAM,GAAG,uBAGa,EAAE,8DAFjC,OAAe,EACf,WAAW,GAAG,CAAC;IAEf,OAAO,iBAAO,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;AACnD,CAAC,CAAA,CAAC;AALW,QAAA,MAAM,UAKjB;AAEF,kBAAe;IACX,MAAM,EAAN,cAAM;IACN,OAAO,EAAP,aAAO;CACV,CAAC"}
1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