mybase 1.1.34 → 1.1.36
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/jest/isLANIp.test.js +36 -0
- package/jest/isLoopbackIP.test.js +31 -0
- package/jest.config.js +6 -0
- package/package.json +1 -5
- package/ts/funcs/Geoip2Paths.d.ts +7 -0
- package/ts/funcs/Geoip2Paths.js +26 -0
- package/ts/funcs/Geoip2Paths.test.ts +11 -0
- package/ts/funcs/Geoip2Paths.ts +26 -0
- package/ts/funcs/MaxRuntimeHours.d.ts +1 -0
- package/ts/funcs/MaxRuntimeHours.js +11 -0
- package/ts/funcs/MaxRuntimeHours.ts +7 -0
- package/ts/funcs/asJSON.d.ts +1 -0
- package/ts/funcs/asJSON.js +14 -0
- package/ts/funcs/asJSON.test.js +72 -0
- package/ts/funcs/asJSON.ts +6 -0
- package/ts/funcs/ensureFolder.d.ts +3 -0
- package/ts/funcs/ensureFolder.js +50 -0
- package/ts/funcs/ensureFolder.test.ts +79 -0
- package/ts/funcs/ensureFolder.ts +21 -0
- package/ts/funcs/fileCacheIsValid.d.ts +1 -0
- package/ts/funcs/fileCacheIsValid.js +40 -0
- package/ts/funcs/fileCacheIsValid.ts +13 -0
- package/ts/funcs/getMysql1.d.ts +8 -0
- package/ts/funcs/getMysql1.js +62 -0
- package/ts/funcs/getMysql1.ts +41 -0
- package/ts/funcs/getMysql2.d.ts +9 -0
- package/ts/funcs/getMysql2.js +62 -0
- package/ts/funcs/getMysql2.ts +42 -0
- package/ts/funcs/getWeekNumber.d.ts +1 -0
- package/ts/funcs/getWeekNumber.js +36 -0
- package/ts/funcs/getWeekNumber.test.ts +9 -0
- package/ts/funcs/getWeekNumber.ts +31 -0
- package/ts/funcs/hash_sha512.d.ts +1 -0
- package/ts/funcs/hash_sha512.js +11 -0
- package/ts/funcs/hash_sha512.ts +7 -0
- package/ts/funcs/initMysql2Pool.d.ts +3 -0
- package/ts/funcs/initMysql2Pool.js +39 -0
- package/ts/funcs/initMysql2Pool.ts +11 -0
- package/ts/funcs/int2ip.d.ts +1 -0
- package/ts/funcs/int2ip.js +18 -0
- package/ts/funcs/int2ip.test.js +33 -0
- package/ts/funcs/int2ip.ts +9 -0
- package/ts/funcs/ip2int.d.ts +1 -0
- package/ts/funcs/ip2int.js +18 -0
- package/ts/funcs/ip2int.test.js +30 -0
- package/ts/funcs/ip2int.ts +10 -0
- package/ts/funcs/isLANIp.d.ts +4 -0
- package/ts/funcs/isLANIp.js +47 -0
- package/ts/funcs/isLANIp.test.ts +59 -0
- package/ts/funcs/isLANIp.ts +37 -0
- package/ts/funcs/isLocal.d.ts +1 -0
- package/ts/funcs/isLocal.js +12 -0
- package/ts/funcs/isLocal.test.ts +16 -0
- package/ts/funcs/isLocal.ts +5 -0
- package/ts/funcs/isLoopbackIP.d.ts +4 -0
- package/ts/funcs/isLoopbackIP.js +42 -0
- package/ts/funcs/isLoopbackIP.test.ts +72 -0
- package/ts/funcs/isLoopbackIP.ts +32 -0
- package/ts/funcs/promiseTimeout.d.ts +1 -0
- package/ts/funcs/promiseTimeout.js +19 -0
- package/ts/funcs/promiseTimeout.test.js +104 -0
- package/ts/funcs/promiseTimeout.ts +19 -0
- package/ts/funcs/randomIP.d.ts +1 -0
- package/ts/funcs/randomIP.js +11 -0
- package/ts/funcs/randomIP.test.ts +16 -0
- package/ts/funcs/randomIP.ts +10 -0
- package/ts/funcs/randomIP6.d.ts +1 -0
- package/ts/funcs/randomIP6.js +17 -0
- package/ts/funcs/randomIP6.test.js +18 -0
- package/ts/funcs/randomIP6.ts +12 -0
- package/ts/funcs/randomString.d.ts +1 -0
- package/ts/funcs/randomString.js +8 -0
- package/ts/funcs/randomString.test.ts +15 -0
- package/ts/funcs/randomString.ts +5 -0
- package/ts/funcs/randomTCPPort.d.ts +12 -0
- package/ts/funcs/randomTCPPort.js +88 -0
- package/ts/funcs/randomTCPPort.test.ts +62 -0
- package/ts/funcs/randomTCPPort.ts +58 -0
- package/ts/funcs/randomUTFString.d.ts +1 -0
- package/ts/funcs/randomUTFString.js +31 -0
- package/ts/funcs/randomUTFString.test.ts +44 -0
- package/ts/funcs/randomUTFString.ts +33 -0
- package/ts/funcs/utcnow.d.ts +2 -0
- package/ts/funcs/utcnow.js +8 -0
- package/ts/funcs/utcnow.ts +5 -0
- package/ts/funcs/validEmail.d.ts +1 -0
- package/ts/funcs/validEmail.js +21 -0
- package/ts/funcs/validEmail.test.js +31 -0
- package/ts/funcs/validEmail.ts +13 -0
- package/ts/funcs/validIp.d.ts +1 -0
- package/ts/funcs/validIp.js +32 -0
- package/ts/funcs/validIp.test.ts +33 -0
- package/ts/funcs/validIp.ts +5 -0
- package/ts/funcs/vaultFill.d.ts +2 -0
- package/ts/funcs/vaultFill.js +86 -0
- package/ts/funcs/vaultFill.ts +89 -0
- package/ts/funcs/vaultRead.d.ts +2 -0
- package/ts/funcs/vaultRead.js +52 -0
- package/ts/funcs/vaultRead.ts +45 -0
- package/ts/funcs/wait.d.ts +1 -0
- package/ts/funcs/wait.js +10 -0
- package/ts/funcs/wait.ts +7 -0
- package/ts/global.d.ts +1 -0
- package/ts/global.js +16 -0
- package/ts/global.ts +10 -0
- package/ts/index.d.ts +30 -0
- package/ts/index.js +48 -0
- package/ts/index.test.ts +7 -0
- package/ts/index.ts +33 -0
- package/ts/models/IPAddress.d.ts +40 -0
- package/ts/models/IPAddress.js +255 -0
- package/ts/models/IPAddress.test.ts +393 -0
- package/ts/models/IPAddress.ts +272 -0
- package/ts/models/Timespan.d.ts +18 -0
- package/ts/models/Timespan.js +52 -0
- package/ts/models/Timespan.ts +63 -0
- package/ts/models/Unixtime.d.ts +32 -0
- package/ts/models/Unixtime.js +137 -0
- package/ts/models/Unixtime.test.ts +102 -0
- package/ts/models/Unixtime.ts +164 -0
- package/ts/types.d.ts +1 -0
- package/ts/types.js +3 -0
- package/ts/types.ts +1 -0
- package/tsconfig.json +36 -0
package/ts/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./funcs/getWeekNumber"), exports);
|
|
18
|
+
__exportStar(require("./funcs/Geoip2Paths"), exports);
|
|
19
|
+
__exportStar(require("./funcs/isLocal"), exports);
|
|
20
|
+
__exportStar(require("./funcs/randomString"), exports);
|
|
21
|
+
__exportStar(require("./funcs/utcnow"), exports);
|
|
22
|
+
__exportStar(require("./funcs/validIp"), exports);
|
|
23
|
+
__exportStar(require("./funcs/wait"), exports);
|
|
24
|
+
__exportStar(require("./funcs/fileCacheIsValid"), exports);
|
|
25
|
+
__exportStar(require("./funcs/hash_sha512"), exports);
|
|
26
|
+
__exportStar(require("./funcs/vaultRead"), exports);
|
|
27
|
+
__exportStar(require("./funcs/vaultFill"), exports);
|
|
28
|
+
__exportStar(require("./funcs/asJSON"), exports);
|
|
29
|
+
__exportStar(require("./funcs/promiseTimeout"), exports);
|
|
30
|
+
__exportStar(require("./funcs/randomIP"), exports);
|
|
31
|
+
__exportStar(require("./funcs/ip2int"), exports);
|
|
32
|
+
__exportStar(require("./funcs/int2ip"), exports);
|
|
33
|
+
__exportStar(require("./funcs/randomIP6"), exports);
|
|
34
|
+
__exportStar(require("./funcs/randomTCPPort"), exports);
|
|
35
|
+
__exportStar(require("./funcs/validEmail"), exports);
|
|
36
|
+
__exportStar(require("./funcs/isLoopbackIP"), exports);
|
|
37
|
+
__exportStar(require("./funcs/isLANIp"), exports);
|
|
38
|
+
__exportStar(require("./funcs/getMysql1"), exports);
|
|
39
|
+
__exportStar(require("./funcs/getMysql2"), exports);
|
|
40
|
+
__exportStar(require("./funcs/initMysql2Pool"), exports);
|
|
41
|
+
__exportStar(require("./funcs/randomUTFString"), exports);
|
|
42
|
+
__exportStar(require("./funcs/MaxRuntimeHours"), exports);
|
|
43
|
+
__exportStar(require("./funcs/ensureFolder"), exports);
|
|
44
|
+
// models
|
|
45
|
+
__exportStar(require("./models/Unixtime"), exports);
|
|
46
|
+
__exportStar(require("./models/Timespan"), exports);
|
|
47
|
+
__exportStar(require("./models/IPAddress"), exports);
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
package/ts/index.test.ts
ADDED
package/ts/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export * from "./funcs/getWeekNumber"
|
|
2
|
+
export * from "./funcs/Geoip2Paths";
|
|
3
|
+
export * from "./funcs/isLocal"
|
|
4
|
+
export * from "./funcs/randomString"
|
|
5
|
+
export * from "./funcs/utcnow"
|
|
6
|
+
export * from "./funcs/validIp"
|
|
7
|
+
export * from "./funcs/wait"
|
|
8
|
+
export * from "./funcs/fileCacheIsValid"
|
|
9
|
+
export * from "./funcs/hash_sha512"
|
|
10
|
+
export * from "./funcs/vaultRead"
|
|
11
|
+
export * from "./funcs/vaultFill"
|
|
12
|
+
export * from "./funcs/asJSON"
|
|
13
|
+
export * from "./funcs/promiseTimeout"
|
|
14
|
+
export * from "./funcs/randomIP"
|
|
15
|
+
export * from "./funcs/ip2int"
|
|
16
|
+
export * from "./funcs/int2ip"
|
|
17
|
+
export * from "./funcs/randomIP6"
|
|
18
|
+
export * from "./funcs/randomTCPPort"
|
|
19
|
+
export * from "./funcs/validEmail"
|
|
20
|
+
export * from "./funcs/isLoopbackIP"
|
|
21
|
+
export * from "./funcs/isLANIp"
|
|
22
|
+
export * from "./funcs/getMysql1"
|
|
23
|
+
export * from "./funcs/getMysql2"
|
|
24
|
+
export * from "./funcs/initMysql2Pool"
|
|
25
|
+
export * from "./funcs/randomUTFString"
|
|
26
|
+
export * from "./funcs/MaxRuntimeHours"
|
|
27
|
+
export * from "./funcs/ensureFolder"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
// models
|
|
31
|
+
export * from "./models/Unixtime"
|
|
32
|
+
export * from "./models/Timespan"
|
|
33
|
+
export * from "./models/IPAddress"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/// <reference types="@root/ip6addr" />
|
|
2
|
+
import ip6addr, { ToStringOpts } from 'ip6addr';
|
|
3
|
+
export declare class IPAddress {
|
|
4
|
+
private _ip;
|
|
5
|
+
static loopback_cidrs4: ip6addr.CIDR[];
|
|
6
|
+
static loopback_cidrs6: ip6addr.CIDR[];
|
|
7
|
+
static loopback_cidrs: ip6addr.CIDR[];
|
|
8
|
+
static lan_cidrs4: ip6addr.CIDR[];
|
|
9
|
+
static lan_cidrs6: ip6addr.CIDR[];
|
|
10
|
+
static lan_cidrs: ip6addr.CIDR[];
|
|
11
|
+
static local_cidrs: ip6addr.CIDR[];
|
|
12
|
+
static local_cidrs4: ip6addr.CIDR[];
|
|
13
|
+
static local_cidrs6: ip6addr.CIDR[];
|
|
14
|
+
constructor(ip: string);
|
|
15
|
+
abbreviated(): string;
|
|
16
|
+
toString(opts?: ToStringOpts): string;
|
|
17
|
+
static validate(ip: string): boolean;
|
|
18
|
+
static try(ip: string): IPAddress | undefined;
|
|
19
|
+
static fromString(ip: string): IPAddress;
|
|
20
|
+
static get Empty(): IPAddress;
|
|
21
|
+
isLoopbackIP(): boolean;
|
|
22
|
+
isLANIP(): boolean;
|
|
23
|
+
isLocalIP(): boolean;
|
|
24
|
+
partOfCIDR(cidr_networks: ip6addr.CIDR[]): boolean;
|
|
25
|
+
static partOfCIDR(_ip: string, cidr_networks: ip6addr.CIDR[]): boolean;
|
|
26
|
+
isPublicIP(): boolean;
|
|
27
|
+
static unserialize(serializedIpString: string): IPAddress;
|
|
28
|
+
static randomIP4(): IPAddress;
|
|
29
|
+
static randomIP6(): IPAddress;
|
|
30
|
+
static randomCIDRIp(cidr: ip6addr.CIDR[] | 'loopback' | 'lan' | 'local' | 'loopback4' | 'loopback6' | 'lan4' | 'lan6' | 'local4' | 'local6'): IPAddress;
|
|
31
|
+
serialize(): string;
|
|
32
|
+
kind(): number;
|
|
33
|
+
equals(secondIp: IPAddress): boolean;
|
|
34
|
+
randomPad(): string;
|
|
35
|
+
normalized(): string;
|
|
36
|
+
private padIp6;
|
|
37
|
+
private padIp4;
|
|
38
|
+
asciiChecksum(): string;
|
|
39
|
+
checksum1(): string;
|
|
40
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.IPAddress = void 0;
|
|
7
|
+
/// <reference path="./../../ip6addr.d.ts" />
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const __1 = require("./../");
|
|
10
|
+
const ip6addr_1 = __importDefault(require("ip6addr"));
|
|
11
|
+
const net_1 = __importDefault(require("net"));
|
|
12
|
+
const ip6 = require('ip6'); // "url": "https://github.com/elgs/ip6"
|
|
13
|
+
const dbg = (0, debug_1.default)('_IPAddress');
|
|
14
|
+
dbg.enabled = true;
|
|
15
|
+
// need version "ip6": "0.2.7",!!
|
|
16
|
+
class IPAddress {
|
|
17
|
+
constructor(ip) {
|
|
18
|
+
if (ip === '-')
|
|
19
|
+
ip = '0.0.0.0'; // is this really required?
|
|
20
|
+
try {
|
|
21
|
+
this._ip = ip6addr_1.default.parse(ip);
|
|
22
|
+
if (this._ip._attrs.ipv4Mapped) {
|
|
23
|
+
// we wany to convert it to ipv4
|
|
24
|
+
this._ip = ip6addr_1.default.parse(this._ip.toString({ format: 'v4' }));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
if (err instanceof Error)
|
|
29
|
+
throw new Error(`IPAddress: '${ip}' Error: ${err.message}`);
|
|
30
|
+
throw err;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
abbreviated() {
|
|
34
|
+
return this.toString(); // default behaviour of ip6addr
|
|
35
|
+
}
|
|
36
|
+
toString(opts = { format: 'auto', zeroElide: false, zeroPad: false }) {
|
|
37
|
+
return this._ip.toString();
|
|
38
|
+
}
|
|
39
|
+
static validate(ip) {
|
|
40
|
+
try {
|
|
41
|
+
new IPAddress(ip);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
catch (_) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
static try(ip) {
|
|
49
|
+
try {
|
|
50
|
+
return new IPAddress(ip);
|
|
51
|
+
}
|
|
52
|
+
catch (_) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static fromString(ip) {
|
|
57
|
+
return new IPAddress(ip);
|
|
58
|
+
}
|
|
59
|
+
static get Empty() {
|
|
60
|
+
//The IP address 0.0.0.0 is a non-routable meta-address used to denote an invalid, unknown,
|
|
61
|
+
// or non-applicable target. It's not assigned to any individual device.
|
|
62
|
+
// It is used in different contexts to mean "any IPv4-enabled network interface" or as a default route
|
|
63
|
+
return new IPAddress('0.0.0.0');
|
|
64
|
+
}
|
|
65
|
+
isLoopbackIP() {
|
|
66
|
+
return this.partOfCIDR(IPAddress.loopback_cidrs);
|
|
67
|
+
}
|
|
68
|
+
isLANIP() {
|
|
69
|
+
return this.partOfCIDR(IPAddress.lan_cidrs);
|
|
70
|
+
}
|
|
71
|
+
isLocalIP() {
|
|
72
|
+
return this.partOfCIDR(IPAddress.local_cidrs);
|
|
73
|
+
}
|
|
74
|
+
partOfCIDR(cidr_networks) {
|
|
75
|
+
const ipNormalized = this.toString();
|
|
76
|
+
let ipVersion = net_1.default.isIPv4(ipNormalized) ? 'ipv4' : 'ipv6';
|
|
77
|
+
for (let cidr of cidr_networks) {
|
|
78
|
+
if (!cidr)
|
|
79
|
+
continue;
|
|
80
|
+
let first = cidr.first();
|
|
81
|
+
if (first.kind() !== ipVersion)
|
|
82
|
+
continue;
|
|
83
|
+
if (cidr.contains(ipNormalized))
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
static partOfCIDR(_ip, cidr_networks) {
|
|
89
|
+
const ip = IPAddress.try(_ip);
|
|
90
|
+
if (ip === undefined)
|
|
91
|
+
return false;
|
|
92
|
+
return ip.partOfCIDR(cidr_networks);
|
|
93
|
+
}
|
|
94
|
+
isPublicIP() {
|
|
95
|
+
return this.toString() !== '0.0.0.0' && !this.isLoopbackIP() && !this.isLANIP();
|
|
96
|
+
}
|
|
97
|
+
static unserialize(serializedIpString) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
if (serializedIpString.length === 32) {
|
|
100
|
+
// we have possible ipv6
|
|
101
|
+
let ip = (_a = serializedIpString.match(/.{1,4}/g)) === null || _a === void 0 ? void 0 : _a.join(':');
|
|
102
|
+
if (ip)
|
|
103
|
+
return new IPAddress(ip);
|
|
104
|
+
}
|
|
105
|
+
// we have possible ipv4
|
|
106
|
+
let ip = (_b = serializedIpString.match(/.{1,3}/g)) === null || _b === void 0 ? void 0 : _b.join('.');
|
|
107
|
+
if (ip)
|
|
108
|
+
try {
|
|
109
|
+
return new IPAddress(ip);
|
|
110
|
+
}
|
|
111
|
+
catch (_) { }
|
|
112
|
+
throw new Error(`Invalid SerializationString: ${serializedIpString}`);
|
|
113
|
+
}
|
|
114
|
+
static randomIP4() {
|
|
115
|
+
return new IPAddress((0, __1.randomIP)());
|
|
116
|
+
}
|
|
117
|
+
static randomIP6() {
|
|
118
|
+
return new IPAddress((0, __1.randomIP6)());
|
|
119
|
+
}
|
|
120
|
+
static randomCIDRIp(cidr) {
|
|
121
|
+
if (cidr === 'loopback')
|
|
122
|
+
cidr = IPAddress.loopback_cidrs;
|
|
123
|
+
if (cidr === 'loopback4')
|
|
124
|
+
cidr = IPAddress.loopback_cidrs4;
|
|
125
|
+
if (cidr === 'loopback6')
|
|
126
|
+
cidr = IPAddress.loopback_cidrs6;
|
|
127
|
+
if (cidr === 'lan')
|
|
128
|
+
cidr = IPAddress.lan_cidrs;
|
|
129
|
+
if (cidr === 'lan4')
|
|
130
|
+
cidr = IPAddress.lan_cidrs4;
|
|
131
|
+
if (cidr === 'lan6')
|
|
132
|
+
cidr = IPAddress.lan_cidrs6;
|
|
133
|
+
if (cidr === 'local')
|
|
134
|
+
cidr = IPAddress.local_cidrs;
|
|
135
|
+
if (cidr === 'local4')
|
|
136
|
+
cidr = IPAddress.local_cidrs4;
|
|
137
|
+
if (cidr === 'local6')
|
|
138
|
+
cidr = IPAddress.local_cidrs6;
|
|
139
|
+
// dbg('randomCIDRIp', cidr)
|
|
140
|
+
if (cidr.length === 0)
|
|
141
|
+
throw new Error('CIDR array is empty');
|
|
142
|
+
// Pick a random CIDR network
|
|
143
|
+
const network = cidr[Math.floor(Math.random() * cidr.length)];
|
|
144
|
+
// dbg('randomCIDRIp:network', network)
|
|
145
|
+
// Get the range of IPs in this CIDR
|
|
146
|
+
const start = network.first().toBigInt();
|
|
147
|
+
// dbg('randomCIDRIp:first', network.first().toString(),start)
|
|
148
|
+
const end = network.last().toBigInt();
|
|
149
|
+
// dbg('randomCIDRIp:last', network.last().toString(),end)
|
|
150
|
+
let range = end - start;
|
|
151
|
+
// dbg('randomCIDRIp:first:last', start, end)
|
|
152
|
+
// Generate a random Long between the start and end
|
|
153
|
+
// if start-end > Maximum integer number
|
|
154
|
+
// we need to use BigInt
|
|
155
|
+
if (range > BigInt(Number.MAX_SAFE_INTEGER))
|
|
156
|
+
range = (Number.MAX_SAFE_INTEGER);
|
|
157
|
+
const randomBigInt = BigInt(Math.floor(Math.random() * (Number(range) + 1))) + start;
|
|
158
|
+
// dbg('randomBigInt', randomBigInt)
|
|
159
|
+
// Convert the BigInt back to an IP address
|
|
160
|
+
const randomIp = ip6addr_1.default.parse(randomBigInt);
|
|
161
|
+
// dbg('randomCIDRIp', randomIp.toString())
|
|
162
|
+
return new IPAddress(randomIp.toString());
|
|
163
|
+
}
|
|
164
|
+
serialize() {
|
|
165
|
+
if (this.kind() === 4) {
|
|
166
|
+
// zero pad ipv4
|
|
167
|
+
return this._ip.toString().split('.').map((octet) => {
|
|
168
|
+
return octet.padStart(3, '0');
|
|
169
|
+
}).join('.').replace(/\./g, '');
|
|
170
|
+
}
|
|
171
|
+
// zero pad ipv6
|
|
172
|
+
return ip6.normalize(this._ip.toString()).split(':').map((hextet) => {
|
|
173
|
+
return hextet.padStart(4, '0');
|
|
174
|
+
}).join(':').replace(/:/g, '');
|
|
175
|
+
}
|
|
176
|
+
kind() {
|
|
177
|
+
return this._ip.kind() === 'ipv6' ? 6 : 4;
|
|
178
|
+
}
|
|
179
|
+
equals(secondIp) {
|
|
180
|
+
return this._ip.compare(secondIp._ip) === 0;
|
|
181
|
+
}
|
|
182
|
+
randomPad() {
|
|
183
|
+
if (this.kind() === 4)
|
|
184
|
+
return this.padIp4();
|
|
185
|
+
return this.padIp6();
|
|
186
|
+
}
|
|
187
|
+
normalized() {
|
|
188
|
+
if (this.kind() === 4)
|
|
189
|
+
return this._ip.toString();
|
|
190
|
+
return ip6.normalize(this._ip.toString());
|
|
191
|
+
}
|
|
192
|
+
padIp6() {
|
|
193
|
+
const hextets = this.normalized().split(':'); // we need to have the full form
|
|
194
|
+
const paddedHextets = hextets.map(hextet => {
|
|
195
|
+
const cleanHextet = Number(`0x${hextet}`).toString(16);
|
|
196
|
+
const padLength = Math.floor(Math.random() * (4 - cleanHextet.length));
|
|
197
|
+
return cleanHextet.padStart(padLength + cleanHextet.length, '0');
|
|
198
|
+
});
|
|
199
|
+
return paddedHextets.join(':');
|
|
200
|
+
}
|
|
201
|
+
padIp4() {
|
|
202
|
+
const octets = this._ip.toString().split('.');
|
|
203
|
+
const paddedOctets = octets.map(octet => {
|
|
204
|
+
const cleanOctet = String(Number(octet));
|
|
205
|
+
const padLength = Math.floor(Math.random() * (3 - cleanOctet.length));
|
|
206
|
+
return cleanOctet.padStart(padLength + cleanOctet.length, '0');
|
|
207
|
+
});
|
|
208
|
+
return paddedOctets.join('.');
|
|
209
|
+
}
|
|
210
|
+
asciiChecksum() {
|
|
211
|
+
// support ipv4 and ipv6
|
|
212
|
+
// FNV-1a hash parameters
|
|
213
|
+
const FNV_PRIME = 0x01000193;
|
|
214
|
+
const FNV_OFFSET_BASIS = 0x811c9dc5;
|
|
215
|
+
const CHAR_SET = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
216
|
+
let hash = FNV_OFFSET_BASIS;
|
|
217
|
+
const ip = this.normalized();
|
|
218
|
+
for (let i = 0; i < ip.length; i++) {
|
|
219
|
+
hash ^= ip.charCodeAt(i);
|
|
220
|
+
hash *= FNV_PRIME;
|
|
221
|
+
hash >>>= 0; // Ensure hash is always a positive 32-bit integer
|
|
222
|
+
}
|
|
223
|
+
// Map the hash to a character in the CHAR_SET
|
|
224
|
+
const index = hash % CHAR_SET.length;
|
|
225
|
+
return CHAR_SET.charAt(index);
|
|
226
|
+
}
|
|
227
|
+
checksum1() {
|
|
228
|
+
// this shall check that the ip is ipv4 and return the last octet as checksum
|
|
229
|
+
if (this.kind() !== 4)
|
|
230
|
+
return 'x';
|
|
231
|
+
return this._ip.toString().split('.').pop() || '-';
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.IPAddress = IPAddress;
|
|
235
|
+
IPAddress.loopback_cidrs4 = [
|
|
236
|
+
ip6addr_1.default.createCIDR('127.0.0.0/8'),
|
|
237
|
+
];
|
|
238
|
+
IPAddress.loopback_cidrs6 = [
|
|
239
|
+
ip6addr_1.default.createCIDR('::1/128') // yes ipv6 has single address
|
|
240
|
+
];
|
|
241
|
+
IPAddress.loopback_cidrs = IPAddress.loopback_cidrs4.concat(IPAddress.loopback_cidrs6);
|
|
242
|
+
IPAddress.lan_cidrs4 = [
|
|
243
|
+
ip6addr_1.default.createCIDR('10.0.0.0/8'),
|
|
244
|
+
ip6addr_1.default.createCIDR('172.16.0.0/12'),
|
|
245
|
+
ip6addr_1.default.createCIDR('192.168.0.0/16'),
|
|
246
|
+
];
|
|
247
|
+
IPAddress.lan_cidrs6 = [
|
|
248
|
+
ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
|
|
249
|
+
ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
|
|
250
|
+
];
|
|
251
|
+
IPAddress.lan_cidrs = IPAddress.lan_cidrs4.concat(IPAddress.lan_cidrs6);
|
|
252
|
+
IPAddress.local_cidrs = IPAddress.loopback_cidrs.concat(IPAddress.lan_cidrs);
|
|
253
|
+
IPAddress.local_cidrs4 = IPAddress.loopback_cidrs4.concat(IPAddress.lan_cidrs4);
|
|
254
|
+
IPAddress.local_cidrs6 = IPAddress.loopback_cidrs6.concat(IPAddress.lan_cidrs6);
|
|
255
|
+
//# sourceMappingURL=IPAddress.js.map
|