mybase 1.1.15 → 1.1.17
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/mybase.js +6 -3
- package/package.json +4 -1
- package/ts/funcs/getMysql1.d.ts +8 -0
- package/ts/funcs/getMysql1.js +72 -0
- package/ts/funcs/getMysql1.ts +41 -0
- package/ts/funcs/getMysql2.d.ts +8 -0
- package/ts/funcs/getMysql2.js +72 -0
- package/ts/funcs/getMysql2.ts +42 -0
- package/ts/funcs/initMysql2Pool.d.ts +3 -0
- package/ts/funcs/initMysql2Pool.js +38 -0
- package/ts/funcs/initMysql2Pool.ts +11 -0
- 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.js +0 -1
- package/ts/funcs/isLoopbackIP.ts +0 -1
- package/ts/index.d.ts +4 -0
- package/ts/index.js +4 -0
- package/ts/index.ts +4 -1
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.17",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "mybase.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
"ip6": "=0.2.7",
|
|
18
18
|
"ip6addr": "^0.2.5",
|
|
19
19
|
"js-sha512": "^0.8.0",
|
|
20
|
+
"mysql": "^2.18.1",
|
|
21
|
+
"mysql2": "^3.9.1",
|
|
20
22
|
"node-vault": "^0.10.2",
|
|
21
23
|
"psl": "^1.9.0",
|
|
22
24
|
"punycode": "^2.1.1",
|
|
@@ -27,6 +29,7 @@
|
|
|
27
29
|
"@types/debug": "^4.1.12",
|
|
28
30
|
"@types/ip6addr": "^0.2.6",
|
|
29
31
|
"@types/jest": "^29.5.11",
|
|
32
|
+
"@types/mysql": "^2.15.25",
|
|
30
33
|
"@types/node": "^20.11.5",
|
|
31
34
|
"@types/validator": "^13.11.8",
|
|
32
35
|
"chai": "^4.2.0",
|
|
@@ -0,0 +1,72 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.getMysql1 = void 0;
|
|
39
|
+
const mysql1 = __importStar(require("mysql"));
|
|
40
|
+
const debug_1 = __importDefault(require("debug"));
|
|
41
|
+
const dbg = (0, debug_1.default)("getMysql1");
|
|
42
|
+
function getMysql1(config, pingInterval = 0) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const mysql_client = mysql1.createConnection({
|
|
46
|
+
host: config.host,
|
|
47
|
+
port: config.port ? config.port : 3306,
|
|
48
|
+
user: config.login,
|
|
49
|
+
password: config.password,
|
|
50
|
+
database: config.db
|
|
51
|
+
});
|
|
52
|
+
mysql_client.connect((err, res) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err);
|
|
55
|
+
return reject(err);
|
|
56
|
+
}
|
|
57
|
+
dbg(`mysql connection has been established to ${config.host}@${config.db}`);
|
|
58
|
+
// install a pinger every 30 seconds to keep the connection alive
|
|
59
|
+
if (pingInterval > 0)
|
|
60
|
+
setInterval(() => {
|
|
61
|
+
mysql_client.ping(function (err) {
|
|
62
|
+
if (err)
|
|
63
|
+
return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code);
|
|
64
|
+
dbg(`mysql ${config.host}@${config.db} is pinging properly`);
|
|
65
|
+
});
|
|
66
|
+
}, pingInterval);
|
|
67
|
+
resolve(mysql_client);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
exports.getMysql1 = getMysql1;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as mysql1 from "mysql"
|
|
2
|
+
import { MysqlError } from "mysql"
|
|
3
|
+
import debug from "debug"
|
|
4
|
+
const dbg = debug("getMysql1")
|
|
5
|
+
|
|
6
|
+
export type Mysql1ServerConfiguration = {
|
|
7
|
+
host: string,
|
|
8
|
+
port: number,
|
|
9
|
+
login: string,
|
|
10
|
+
password: string,
|
|
11
|
+
db: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function getMysql1(config: Mysql1ServerConfiguration, pingInterval: number = 0) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const mysql_client = mysql1.createConnection({
|
|
17
|
+
host: config.host,
|
|
18
|
+
port: config.port ? config.port : 3306,
|
|
19
|
+
user: config.login,
|
|
20
|
+
password: config.password,
|
|
21
|
+
database: config.db
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
mysql_client.connect((err:MysqlError, res) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err)
|
|
27
|
+
return reject(err)
|
|
28
|
+
}
|
|
29
|
+
dbg(`mysql connection has been established to ${config.host}@${config.db}`)
|
|
30
|
+
// install a pinger every 30 seconds to keep the connection alive
|
|
31
|
+
if (pingInterval > 0)
|
|
32
|
+
setInterval(() => {
|
|
33
|
+
mysql_client.ping(function (err:MysqlError) {
|
|
34
|
+
if (err) return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code)
|
|
35
|
+
dbg(`mysql ${config.host}@${config.db} is pinging properly`)
|
|
36
|
+
})
|
|
37
|
+
}, pingInterval)
|
|
38
|
+
resolve(mysql_client)
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.getMysql2 = void 0;
|
|
39
|
+
const mysql2 = __importStar(require("mysql2"));
|
|
40
|
+
const debug_1 = __importDefault(require("debug"));
|
|
41
|
+
const dbg = (0, debug_1.default)("getMysql2");
|
|
42
|
+
function getMysql2(config, pingInterval = 0) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const mysql_client = mysql2.createConnection({
|
|
46
|
+
host: config.host,
|
|
47
|
+
port: config.port ? config.port : 3306,
|
|
48
|
+
user: config.login,
|
|
49
|
+
password: config.password,
|
|
50
|
+
database: config.db
|
|
51
|
+
});
|
|
52
|
+
mysql_client.connect((err) => {
|
|
53
|
+
if (err) {
|
|
54
|
+
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err);
|
|
55
|
+
return reject(err);
|
|
56
|
+
}
|
|
57
|
+
dbg(`mysql connection has been established to ${config.host}@${config.db}`);
|
|
58
|
+
// install a pinger every 30 seconds to keep the connection alive
|
|
59
|
+
if (pingInterval > 0)
|
|
60
|
+
setInterval(() => {
|
|
61
|
+
mysql_client.ping(function (err) {
|
|
62
|
+
if (err)
|
|
63
|
+
return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code);
|
|
64
|
+
dbg(`mysql ${config.host}@${config.db} is pinging properly`);
|
|
65
|
+
});
|
|
66
|
+
}, pingInterval);
|
|
67
|
+
resolve(mysql_client);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
exports.getMysql2 = getMysql2;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as mysql2 from "mysql2"
|
|
2
|
+
import { QueryError } from "mysql2"
|
|
3
|
+
import debug from "debug"
|
|
4
|
+
const dbg = debug("getMysql2")
|
|
5
|
+
|
|
6
|
+
export type Mysql2ServerConfiguration = {
|
|
7
|
+
host: string,
|
|
8
|
+
port: number,
|
|
9
|
+
login: string,
|
|
10
|
+
password: string,
|
|
11
|
+
db: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function getMysql2(config: Mysql2ServerConfiguration, pingInterval: number = 0) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const mysql_client = mysql2.createConnection({
|
|
17
|
+
host: config.host,
|
|
18
|
+
port: config.port ? config.port : 3306,
|
|
19
|
+
user: config.login,
|
|
20
|
+
password: config.password,
|
|
21
|
+
database: config.db
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
mysql_client.connect((err: QueryError | null) => {
|
|
25
|
+
if (err) {
|
|
26
|
+
dbg(`mysql connection has failed to ${config.host}@${config.db}`, err)
|
|
27
|
+
return reject(err)
|
|
28
|
+
}
|
|
29
|
+
dbg(`mysql connection has been established to ${config.host}@${config.db}`)
|
|
30
|
+
// install a pinger every 30 seconds to keep the connection alive
|
|
31
|
+
if (pingInterval > 0)
|
|
32
|
+
setInterval(() => {
|
|
33
|
+
mysql_client.ping(function (err:QueryError | null) {
|
|
34
|
+
if (err)
|
|
35
|
+
return console.log(`could not ping mysql ${config.host}@${config.db}`, err.code)
|
|
36
|
+
dbg(`mysql ${config.host}@${config.db} is pinging properly`)
|
|
37
|
+
})
|
|
38
|
+
}, pingInterval)
|
|
39
|
+
resolve(mysql_client)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.initMysql2Pool = void 0;
|
|
30
|
+
const mysql2 = __importStar(require("mysql2"));
|
|
31
|
+
const debug_1 = __importDefault(require("debug"));
|
|
32
|
+
const dbg = (0, debug_1.default)("initMysql2Pool");
|
|
33
|
+
function initMysql2Pool(sqlServer) {
|
|
34
|
+
const connectionUri = `mysql://${sqlServer.login}:${sqlServer.password}@${sqlServer.host}`;
|
|
35
|
+
dbg(`creating mysql pool to ${sqlServer.host}@${sqlServer.db} as connectionUri=${connectionUri}`);
|
|
36
|
+
return mysql2.createPool(connectionUri);
|
|
37
|
+
}
|
|
38
|
+
exports.initMysql2Pool = initMysql2Pool;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as mysql2 from "mysql2"
|
|
2
|
+
import { Pool } from "mysql2"
|
|
3
|
+
import debug from "debug"
|
|
4
|
+
import { Mysql2ServerConfiguration } from "./getMysql2"
|
|
5
|
+
const dbg = debug("initMysql2Pool")
|
|
6
|
+
|
|
7
|
+
export function initMysql2Pool(sqlServer: Mysql2ServerConfiguration): Pool {
|
|
8
|
+
const connectionUri = `mysql://${sqlServer.login}:${sqlServer.password}@${sqlServer.host}`
|
|
9
|
+
dbg(`creating mysql pool to ${sqlServer.host}@${sqlServer.db} as connectionUri=${connectionUri}`)
|
|
10
|
+
return mysql2.createPool(connectionUri)
|
|
11
|
+
}
|
|
@@ -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
|
+
}
|
package/ts/funcs/isLoopbackIP.js
CHANGED
package/ts/funcs/isLoopbackIP.ts
CHANGED
package/ts/index.d.ts
CHANGED
|
@@ -17,3 +17,7 @@ export * from "./funcs/randomIP6";
|
|
|
17
17
|
export * from "./funcs/randomTCPPort";
|
|
18
18
|
export * from "./funcs/validEmail";
|
|
19
19
|
export * from "./funcs/isLoopbackIP";
|
|
20
|
+
export * from "./funcs/isLANIp";
|
|
21
|
+
export * from "./funcs/getMysql1";
|
|
22
|
+
export * from "./funcs/getMysql2";
|
|
23
|
+
export * from "./funcs/initMysql2Pool";
|
package/ts/index.js
CHANGED
|
@@ -33,3 +33,7 @@ __exportStar(require("./funcs/randomIP6"), exports);
|
|
|
33
33
|
__exportStar(require("./funcs/randomTCPPort"), exports);
|
|
34
34
|
__exportStar(require("./funcs/validEmail"), exports);
|
|
35
35
|
__exportStar(require("./funcs/isLoopbackIP"), exports);
|
|
36
|
+
__exportStar(require("./funcs/isLANIp"), exports);
|
|
37
|
+
__exportStar(require("./funcs/getMysql1"), exports);
|
|
38
|
+
__exportStar(require("./funcs/getMysql2"), exports);
|
|
39
|
+
__exportStar(require("./funcs/initMysql2Pool"), exports);
|
package/ts/index.ts
CHANGED
|
@@ -17,4 +17,7 @@ export * from "./funcs/randomIP6"
|
|
|
17
17
|
export * from "./funcs/randomTCPPort"
|
|
18
18
|
export * from "./funcs/validEmail"
|
|
19
19
|
export * from "./funcs/isLoopbackIP"
|
|
20
|
-
|
|
20
|
+
export * from "./funcs/isLANIp"
|
|
21
|
+
export * from "./funcs/getMysql1"
|
|
22
|
+
export * from "./funcs/getMysql2"
|
|
23
|
+
export * from "./funcs/initMysql2Pool"
|