mybase 1.1.14 → 1.1.16
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/isLoopbackIP.test.js +19 -20
- package/jest.config.js +1 -0
- package/mybase.js +6 -3
- package/package.json +2 -1
- package/ts/funcs/isLANIp.d.ts +1 -0
- package/ts/funcs/isLANIp.js +46 -0
- package/ts/funcs/isLANIp.test.ts +59 -0
- package/ts/funcs/isLANIp.ts +38 -0
- package/ts/funcs/isLoopbackIP.d.ts +1 -0
- package/ts/funcs/isLoopbackIP.js +39 -0
- package/ts/funcs/isLoopbackIP.test.ts +69 -0
- package/ts/funcs/isLoopbackIP.ts +32 -0
- package/ts/funcs/randomTCPPort.test.ts +1 -1
- package/ts/index.d.ts +2 -0
- package/ts/index.js +2 -0
- package/ts/index.ts +2 -1
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
const { isLoopbackIP } = require('./../mybase')
|
|
1
|
+
const { isLoopbackIP } = require('./../mybase') // adjust the path to mybase.js
|
|
2
2
|
|
|
3
3
|
test('should return true for local IPv4', () => {
|
|
4
|
-
const ip = '127.0.0.1'
|
|
5
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
6
|
-
})
|
|
4
|
+
const ip = '127.0.0.1'
|
|
5
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
6
|
+
})
|
|
7
7
|
|
|
8
8
|
test('should return true for local IPv4', () => {
|
|
9
|
-
const ip = '127.127.127.127'
|
|
10
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
11
|
-
})
|
|
12
|
-
|
|
9
|
+
const ip = '127.127.127.127'
|
|
10
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
11
|
+
})
|
|
13
12
|
|
|
14
13
|
test('should return true for local IPv6', () => {
|
|
15
|
-
const ip = '::1'
|
|
16
|
-
expect(isLoopbackIP(ip)).toBe(true)
|
|
17
|
-
})
|
|
14
|
+
const ip = '::1'
|
|
15
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
16
|
+
})
|
|
18
17
|
|
|
19
18
|
test('should return null for non-local IPv4', () => {
|
|
20
|
-
const ip = '8.8.8.8'
|
|
21
|
-
expect(isLoopbackIP(ip)).toBe(null)
|
|
22
|
-
})
|
|
19
|
+
const ip = '8.8.8.8'
|
|
20
|
+
expect(isLoopbackIP(ip)).toBe(null)
|
|
21
|
+
})
|
|
23
22
|
|
|
24
23
|
test('should return null for non-local IPv6', () => {
|
|
25
|
-
const ip = '2001:4860:4860::8888'
|
|
26
|
-
expect(isLoopbackIP(ip)).toBe(null)
|
|
27
|
-
})
|
|
24
|
+
const ip = '2001:4860:4860::8888'
|
|
25
|
+
expect(isLoopbackIP(ip)).toBe(null)
|
|
26
|
+
})
|
|
28
27
|
|
|
29
28
|
test('should return false for invalid IP', () => {
|
|
30
|
-
const ip = 'invalid'
|
|
31
|
-
expect(isLoopbackIP(ip)).toBe(false)
|
|
32
|
-
})
|
|
29
|
+
const ip = 'invalid'
|
|
30
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
31
|
+
})
|
package/jest.config.js
CHANGED
package/mybase.js
CHANGED
|
@@ -14,8 +14,10 @@ const ip6addr = require('ip6addr')
|
|
|
14
14
|
const private_network_cidrs = [ip6addr.createCIDR('10.0.0.0/8')
|
|
15
15
|
,ip6addr.createCIDR('172.16.0.0/12')
|
|
16
16
|
,ip6addr.createCIDR('192.168.0.0/16')
|
|
17
|
-
,ip6addr.createCIDR('fd00::/8')
|
|
17
|
+
,ip6addr.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
|
|
18
|
+
,ip6addr.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
|
|
18
19
|
]
|
|
20
|
+
|
|
19
21
|
const local_network_cidrs = [ip6addr.createCIDR('127.0.0.0/8')
|
|
20
22
|
,ip6addr.createCIDR('::1/128')]
|
|
21
23
|
|
|
@@ -47,7 +49,7 @@ function normalizeIp(ip) {
|
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
|
|
50
|
-
function isLoopbackIP(ip) {
|
|
52
|
+
function isLoopbackIP(ip) { // ported
|
|
51
53
|
ip=normalizeIp(ip)
|
|
52
54
|
if (net.isIP(ip) === 0) return false
|
|
53
55
|
try {
|
|
@@ -66,13 +68,14 @@ function isLoopbackIP(ip) {
|
|
|
66
68
|
return false
|
|
67
69
|
}
|
|
68
70
|
|
|
69
|
-
function isLANIp(ip) {
|
|
71
|
+
function isLANIp(ip) { // ported
|
|
70
72
|
ip=normalizeIp(ip)
|
|
71
73
|
if (net.isIP(ip) === 0) return false
|
|
72
74
|
try {
|
|
73
75
|
// speed optimized
|
|
74
76
|
let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
|
|
75
77
|
for (let cidr of private_network_cidrs) {
|
|
78
|
+
if (!cidr) continue
|
|
76
79
|
let first = cidr.first()
|
|
77
80
|
if (first.kind() !== ipVersion) continue
|
|
78
81
|
if (cidr.contains(ip))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mybase",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.16",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "mybase.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@jest/globals": "^29.7.0",
|
|
27
27
|
"@types/debug": "^4.1.12",
|
|
28
|
+
"@types/ip6addr": "^0.2.6",
|
|
28
29
|
"@types/jest": "^29.5.11",
|
|
29
30
|
"@types/node": "^20.11.5",
|
|
30
31
|
"@types/validator": "^13.11.8",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isLANIp(ip: string): boolean;
|
|
@@ -0,0 +1,46 @@
|
|
|
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.isLANIp = void 0;
|
|
7
|
+
const ip6addr_1 = __importDefault(require("ip6addr"));
|
|
8
|
+
const net_1 = __importDefault(require("net"));
|
|
9
|
+
const private_network_cidrs = [ip6addr_1.default.createCIDR('10.0.0.0/8'),
|
|
10
|
+
ip6addr_1.default.createCIDR('172.16.0.0/12'),
|
|
11
|
+
ip6addr_1.default.createCIDR('192.168.0.0/16'),
|
|
12
|
+
ip6addr_1.default.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
|
|
13
|
+
,
|
|
14
|
+
ip6addr_1.default.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
|
|
15
|
+
];
|
|
16
|
+
function normalizeIp(ip) {
|
|
17
|
+
// also support ipv6
|
|
18
|
+
if (net_1.default.isIP(ip) === 0)
|
|
19
|
+
return '';
|
|
20
|
+
if (net_1.default.isIPv6(ip))
|
|
21
|
+
return ip.toLowerCase().replace(/^::ffff:/, '');
|
|
22
|
+
return ip;
|
|
23
|
+
}
|
|
24
|
+
function isLANIp(ip) {
|
|
25
|
+
ip = normalizeIp(ip);
|
|
26
|
+
if (net_1.default.isIP(ip) === 0)
|
|
27
|
+
return false;
|
|
28
|
+
try {
|
|
29
|
+
// speed optimized
|
|
30
|
+
let ipVersion = net_1.default.isIPv4(ip) ? 'ipv4' : 'ipv6';
|
|
31
|
+
for (let cidr of private_network_cidrs) {
|
|
32
|
+
if (!cidr)
|
|
33
|
+
continue;
|
|
34
|
+
let first = cidr.first();
|
|
35
|
+
if (first.kind() !== ipVersion)
|
|
36
|
+
continue;
|
|
37
|
+
if (cidr.contains(ip))
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
console.log(err);
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
exports.isLANIp = isLANIp;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isLANIp } from './isLANIp';
|
|
2
|
+
|
|
3
|
+
describe('isLANIp', () => {
|
|
4
|
+
it('should return true for LAN IP addresses', () => {
|
|
5
|
+
expect(isLANIp('192.168.0.1')).toBe(true);
|
|
6
|
+
expect(isLANIp('10.0.0.1')).toBe(true);
|
|
7
|
+
expect(isLANIp('172.16.0.1')).toBe(true);
|
|
8
|
+
|
|
9
|
+
// Add more test cases for LAN IP addresses
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should return false for non-LAN IP addresses', () => {
|
|
13
|
+
expect(isLANIp('8.8.8.8')).toBe(false);
|
|
14
|
+
expect(isLANIp('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
|
|
15
|
+
// Add more test cases for non-LAN IP addresses
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should return false for invalid IP addresses', () => {
|
|
19
|
+
expect(isLANIp('')).toBe(false);
|
|
20
|
+
expect(isLANIp('192.168.0.')).toBe(false);
|
|
21
|
+
expect(isLANIp('256.0.0.1')).toBe(false);
|
|
22
|
+
// Add more test cases for invalid IP addresses
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should return true for private IPv4', () => {
|
|
26
|
+
const ip = '192.168.1.1';
|
|
27
|
+
expect(isLANIp(ip)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('should return true for private IPv6', () => {
|
|
31
|
+
const ip = 'fd00::';
|
|
32
|
+
expect(isLANIp(ip)).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('should return null for public IPv4', () => {
|
|
36
|
+
const ip = '8.8.8.8';
|
|
37
|
+
expect(isLANIp(ip)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('should return false for public IPv6', () => {
|
|
41
|
+
const ip = '2001:4860:4860::8888';
|
|
42
|
+
expect(isLANIp(ip)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('should return false for invalid IP', () => {
|
|
46
|
+
const ip = 'invalid';
|
|
47
|
+
expect(isLANIp(ip)).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('should return false for invalid IP', () => {
|
|
51
|
+
const ip = '1.2.3';
|
|
52
|
+
expect(isLANIp(ip)).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('should return null for non private IPv6', () => {
|
|
56
|
+
const ip = '::2';
|
|
57
|
+
expect(isLANIp(ip)).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import ip6addr from 'ip6addr'
|
|
2
|
+
import net from 'net'
|
|
3
|
+
|
|
4
|
+
const private_network_cidrs = [ip6addr.createCIDR('10.0.0.0/8')
|
|
5
|
+
,ip6addr.createCIDR('172.16.0.0/12')
|
|
6
|
+
,ip6addr.createCIDR('192.168.0.0/16')
|
|
7
|
+
,ip6addr.createCIDR('fd00::/8'), // Reserved by IETF for future use, but not currently in active use.
|
|
8
|
+
,ip6addr.createCIDR('fc00::/8'), // The range currently in use for local communications within a site or organization.
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
function normalizeIp(ip:string):string {
|
|
13
|
+
// also support ipv6
|
|
14
|
+
if (net.isIP(ip) === 0) return ''
|
|
15
|
+
if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
|
|
16
|
+
return ip
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export function isLANIp(ip:string) : boolean {
|
|
21
|
+
ip=normalizeIp(ip)
|
|
22
|
+
if (net.isIP(ip) === 0) return false
|
|
23
|
+
try {
|
|
24
|
+
// speed optimized
|
|
25
|
+
let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
|
|
26
|
+
for (let cidr of private_network_cidrs) {
|
|
27
|
+
if (!cidr) continue
|
|
28
|
+
|
|
29
|
+
let first = cidr.first()
|
|
30
|
+
if (first.kind() !== ipVersion) continue
|
|
31
|
+
if (cidr.contains(ip))
|
|
32
|
+
return true
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
console.log(err)
|
|
36
|
+
}
|
|
37
|
+
return false
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isLoopbackIP(ip: string): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
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.isLoopbackIP = void 0;
|
|
7
|
+
const ip6addr_1 = __importDefault(require("ip6addr"));
|
|
8
|
+
const net_1 = __importDefault(require("net"));
|
|
9
|
+
const local_network_cidrs = [ip6addr_1.default.createCIDR('127.0.0.0/8'),
|
|
10
|
+
ip6addr_1.default.createCIDR('::1/128')];
|
|
11
|
+
function normalizeIp(ip) {
|
|
12
|
+
// also support ipv6
|
|
13
|
+
if (net_1.default.isIP(ip) === 0)
|
|
14
|
+
return '';
|
|
15
|
+
if (net_1.default.isIPv6(ip))
|
|
16
|
+
return ip.toLowerCase().replace(/^::ffff:/, '');
|
|
17
|
+
return ip;
|
|
18
|
+
}
|
|
19
|
+
function isLoopbackIP(ip) {
|
|
20
|
+
ip = normalizeIp(ip);
|
|
21
|
+
if (net_1.default.isIP(ip) === 0)
|
|
22
|
+
return false;
|
|
23
|
+
try {
|
|
24
|
+
// speed optimized
|
|
25
|
+
let ipVersion = net_1.default.isIPv4(ip) ? 'ipv4' : 'ipv6';
|
|
26
|
+
for (let cidr of local_network_cidrs) {
|
|
27
|
+
let first = cidr.first();
|
|
28
|
+
if (first.kind() !== ipVersion)
|
|
29
|
+
continue;
|
|
30
|
+
if (cidr.contains(ip))
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
console.log(err);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
exports.isLoopbackIP = isLoopbackIP;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { randomIP } from './randomIP';
|
|
2
|
+
import { isLoopbackIP } from './isLoopbackIP';
|
|
3
|
+
|
|
4
|
+
describe('isLoopbackIP', () => {
|
|
5
|
+
test('should return true for loopback IPv4 address', () => {
|
|
6
|
+
expect(isLoopbackIP('127.0.0.1')).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
test('should return true for loopback IPv6 address', () => {
|
|
10
|
+
expect(isLoopbackIP('::1')).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('should return false for non-loopback IPv4 address', () => {
|
|
14
|
+
expect(isLoopbackIP('192.168.0.1')).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('should return false for non-loopback IPv6 address', () => {
|
|
18
|
+
expect(isLoopbackIP('2001:0db8:85a3:0000:0000:8a2e:0370:7334')).toBe(false);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should return false for invalid IP address', () => {
|
|
22
|
+
expect(isLoopbackIP('invalid')).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should return true for local IPv4', () => {
|
|
26
|
+
const ip = '127.0.0.1'
|
|
27
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('should return true for local IPv4', () => {
|
|
31
|
+
const ip = '127.2.0.1'
|
|
32
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('should return true for local IPv4', () => {
|
|
36
|
+
const ip = '127.255.0.1'
|
|
37
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('should return true for local IPv4', () => {
|
|
41
|
+
const ip = '127.127.127.127'
|
|
42
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('should return true for local IPv6', () => {
|
|
46
|
+
const ip = '::1'
|
|
47
|
+
expect(isLoopbackIP(ip)).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('should return null for non-local IPv4', () => {
|
|
51
|
+
const ip = '8.8.8.8'
|
|
52
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('should return null for non-local IPv6', () => {
|
|
56
|
+
const ip = '2001:4860:4860::8888'
|
|
57
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('should return false for invalid IP', () => {
|
|
61
|
+
const ip = 'invalid'
|
|
62
|
+
expect(isLoopbackIP(ip)).toBe(false)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('random ips should not be Loopback', () => {
|
|
66
|
+
for(let i=0;i<100;i++)
|
|
67
|
+
expect(isLoopbackIP(randomIP())).toBe(false)
|
|
68
|
+
})
|
|
69
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import ip6addr from 'ip6addr'
|
|
2
|
+
import net from 'net'
|
|
3
|
+
|
|
4
|
+
const local_network_cidrs = [ip6addr.createCIDR('127.0.0.0/8')
|
|
5
|
+
,ip6addr.createCIDR('::1/128')]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function normalizeIp(ip:string):string {
|
|
9
|
+
// also support ipv6
|
|
10
|
+
if (net.isIP(ip) === 0) return ''
|
|
11
|
+
if (net.isIPv6(ip)) return ip.toLowerCase().replace(/^::ffff:/, '')
|
|
12
|
+
return ip
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export function isLoopbackIP(ip:string) : boolean {
|
|
17
|
+
ip=normalizeIp(ip)
|
|
18
|
+
if (net.isIP(ip) === 0) return false
|
|
19
|
+
try {
|
|
20
|
+
// speed optimized
|
|
21
|
+
let ipVersion = net.isIPv4(ip) ? 'ipv4' : 'ipv6'
|
|
22
|
+
for (let cidr of local_network_cidrs) {
|
|
23
|
+
let first = cidr.first()
|
|
24
|
+
if (first.kind() !== ipVersion) continue
|
|
25
|
+
if (cidr.contains(ip))
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
} catch (err) {
|
|
29
|
+
console.log(err)
|
|
30
|
+
}
|
|
31
|
+
return false
|
|
32
|
+
}
|
package/ts/index.d.ts
CHANGED
package/ts/index.js
CHANGED
|
@@ -32,3 +32,5 @@ __exportStar(require("./funcs/int2ip"), exports);
|
|
|
32
32
|
__exportStar(require("./funcs/randomIP6"), exports);
|
|
33
33
|
__exportStar(require("./funcs/randomTCPPort"), exports);
|
|
34
34
|
__exportStar(require("./funcs/validEmail"), exports);
|
|
35
|
+
__exportStar(require("./funcs/isLoopbackIP"), exports);
|
|
36
|
+
__exportStar(require("./funcs/isLANIp"), exports);
|
package/ts/index.ts
CHANGED