melperjs 5.0.0 → 6.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/README.md +4 -2
- package/cjs/index.js +185 -185
- package/cjs/node.js +194 -194
- package/lib/index.js +234 -234
- package/mjs/index.js +156 -156
- package/mjs/node.js +175 -175
- package/package.json +1 -1
- package/src/index.js +36 -5
- package/src/node.js +11 -49
- package/test/script.js +3 -2
package/mjs/index.js
CHANGED
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
import xss from "xss";
|
|
2
|
-
import camelCase from "lodash/camelCase.js";
|
|
3
|
-
import capitalize from "lodash/capitalize.js";
|
|
4
|
-
import isEmpty from "lodash/isEmpty.js";
|
|
5
|
-
import setCookieParser from "set-cookie-parser";
|
|
6
|
-
export const CONSTANTS = {
|
|
7
|
-
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
8
|
-
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
9
|
-
HEXADECIMAL: "0123456789abcdef",
|
|
10
|
-
NUMBERS: "0123456789"
|
|
11
|
-
};
|
|
12
|
-
export function Exception(message, response = {}, name = null) {
|
|
13
|
-
response.status = response.status || 400;
|
|
14
|
-
return {
|
|
15
|
-
name: pascalCase(name),
|
|
16
|
-
message,
|
|
17
|
-
response
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
export function time() {
|
|
21
|
-
return Math.floor(Date.now() / 1000);
|
|
22
|
-
}
|
|
23
|
-
export async function sleepMs(milliseconds) {
|
|
24
|
-
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
25
|
-
}
|
|
26
|
-
export async function sleep(seconds) {
|
|
27
|
-
return await sleepMs(seconds * 1000);
|
|
28
|
-
}
|
|
29
|
-
export function promiseTimeout(milliseconds, promise) {
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
const timer = setTimeout(() => {
|
|
32
|
-
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
33
|
-
}, milliseconds);
|
|
34
|
-
promise.then(value => {
|
|
35
|
-
clearTimeout(timer);
|
|
36
|
-
resolve(value);
|
|
37
|
-
}).catch(reason => {
|
|
38
|
-
clearTimeout(timer);
|
|
39
|
-
reject(reason);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
export function splitLines(text) {
|
|
44
|
-
return text.split(/\r?\n/).filter(item => !checkEmpty(item)).map(item => item.trim());
|
|
45
|
-
}
|
|
46
|
-
export function findKeyNode(key, node, pair = null) {
|
|
47
|
-
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
48
|
-
return node;
|
|
49
|
-
} else if (typeof node === 'object') {
|
|
50
|
-
for (let index in node) {
|
|
51
|
-
const result = findKeyNode(key, node[index], pair);
|
|
52
|
-
if (result) {
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
export function checkEmpty(value) {
|
|
60
|
-
if (typeof value === "number") {
|
|
61
|
-
return value === 0;
|
|
62
|
-
} else {
|
|
63
|
-
return isEmpty(value);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
export function pascalCase(str) {
|
|
67
|
-
return upperCaseFirst(camelCase(str));
|
|
68
|
-
}
|
|
69
|
-
export function upperCaseFirst(str) {
|
|
70
|
-
str = str || "";
|
|
71
|
-
return str[0].toUpperCase() + str.slice(1);
|
|
72
|
-
}
|
|
73
|
-
export function lowerCaseFirst(str) {
|
|
74
|
-
str = str || "";
|
|
75
|
-
return str[0].toLowerCase() + str.slice(1);
|
|
76
|
-
}
|
|
77
|
-
export function titleString(str) {
|
|
78
|
-
str = str || "";
|
|
79
|
-
return str.split(' ').map(word => capitalize(word)).join(' ');
|
|
80
|
-
}
|
|
81
|
-
export function limitString(str, limit = 35) {
|
|
82
|
-
str = str || "";
|
|
83
|
-
if (str.length <= limit) {
|
|
84
|
-
return str;
|
|
85
|
-
} else {
|
|
86
|
-
return str.substring(0, limit - 3) + "...";
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
export function safeString(str) {
|
|
90
|
-
str = str || "";
|
|
91
|
-
return xss(str, {
|
|
92
|
-
whiteList: {},
|
|
93
|
-
stripIgnoreTag: true,
|
|
94
|
-
stripIgnoreTagBody: ["script"]
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
export function randomString(length, useNumbers = true, useUppercase = false) {
|
|
98
|
-
let characters = CONSTANTS.LOWER_CASE;
|
|
99
|
-
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
100
|
-
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
101
|
-
let randomString = '';
|
|
102
|
-
for (let i = 0; i < length; i++) {
|
|
103
|
-
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
104
|
-
randomString += characters[randomIndex];
|
|
105
|
-
}
|
|
106
|
-
return randomString;
|
|
107
|
-
}
|
|
108
|
-
export function randomHex(length) {
|
|
109
|
-
let result = '';
|
|
110
|
-
for (let i = 0; i < length; i++) {
|
|
111
|
-
const randomIndex = Math.floor(Math.random() * CONSTANTS.HEXADECIMAL.length);
|
|
112
|
-
result += CONSTANTS.HEXADECIMAL[randomIndex];
|
|
113
|
-
}
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
export function randomUuid(useDashes = true) {
|
|
117
|
-
let d = Date.now();
|
|
118
|
-
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
119
|
-
const r = (d + Math.random() * 16) % 16 | 0;
|
|
120
|
-
d = Math.floor(d / 16);
|
|
121
|
-
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
122
|
-
});
|
|
123
|
-
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
124
|
-
}
|
|
125
|
-
export function randomWeighted(dict, randomFunc = totalWeight => Math.random() * totalWeight) {
|
|
126
|
-
let elements = Object.keys(dict);
|
|
127
|
-
let weights = Object.values(dict);
|
|
128
|
-
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
129
|
-
let randomNum = randomFunc(totalWeight);
|
|
130
|
-
let weightSum = 0;
|
|
131
|
-
for (let i = 0; i < elements.length; i++) {
|
|
132
|
-
weightSum += weights[i];
|
|
133
|
-
if (randomNum <= weightSum) {
|
|
134
|
-
return elements[i];
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
export function cookieDict(res, decodeValues = false) {
|
|
139
|
-
let dict = {};
|
|
140
|
-
const cookies = setCookieParser.parse(res, {
|
|
141
|
-
decodeValues: decodeValues
|
|
142
|
-
});
|
|
143
|
-
for (let cookie of cookies) {
|
|
144
|
-
dict[cookie.name] = cookie.value;
|
|
145
|
-
}
|
|
146
|
-
return dict;
|
|
147
|
-
}
|
|
148
|
-
export function cookieHeader(cookieDict) {
|
|
149
|
-
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
150
|
-
}
|
|
151
|
-
export function isIntlHttpCode(httpCode) {
|
|
152
|
-
return httpCode === undefined || httpCode === null || httpCode === 0 || httpCode === 402 || httpCode === 407 || httpCode === 466 || 500 <= httpCode;
|
|
153
|
-
}
|
|
154
|
-
export function isIntlError(e) {
|
|
155
|
-
return e?.message?.toLowerCase?.()?.includes?.("timeout") || e?.message?.toLowerCase?.()?.includes?.("aborted") || e?.message?.toLowerCase?.()?.includes?.("tls connection") || e?.message?.toLowerCase?.()?.includes?.("socket hang") || isIntlHttpCode(e?.response?.status);
|
|
156
|
-
}
|
|
1
|
+
import xss from "xss";
|
|
2
|
+
import camelCase from "lodash/camelCase.js";
|
|
3
|
+
import capitalize from "lodash/capitalize.js";
|
|
4
|
+
import isEmpty from "lodash/isEmpty.js";
|
|
5
|
+
import setCookieParser from "set-cookie-parser";
|
|
6
|
+
export const CONSTANTS = {
|
|
7
|
+
LOWER_CASE: "abcdefghijklmnopqrstuvwxyz",
|
|
8
|
+
UPPER_CASE: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
9
|
+
HEXADECIMAL: "0123456789abcdef",
|
|
10
|
+
NUMBERS: "0123456789"
|
|
11
|
+
};
|
|
12
|
+
export function Exception(message, response = {}, name = null) {
|
|
13
|
+
response.status = response.status || 400;
|
|
14
|
+
return {
|
|
15
|
+
name: pascalCase(name),
|
|
16
|
+
message,
|
|
17
|
+
response
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export function time() {
|
|
21
|
+
return Math.floor(Date.now() / 1000);
|
|
22
|
+
}
|
|
23
|
+
export async function sleepMs(milliseconds) {
|
|
24
|
+
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
25
|
+
}
|
|
26
|
+
export async function sleep(seconds) {
|
|
27
|
+
return await sleepMs(seconds * 1000);
|
|
28
|
+
}
|
|
29
|
+
export function promiseTimeout(milliseconds, promise) {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const timer = setTimeout(() => {
|
|
32
|
+
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
33
|
+
}, milliseconds);
|
|
34
|
+
promise.then(value => {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
resolve(value);
|
|
37
|
+
}).catch(reason => {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
reject(reason);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
export function splitLines(text) {
|
|
44
|
+
return text.split(/\r?\n/).filter(item => !checkEmpty(item)).map(item => item.trim());
|
|
45
|
+
}
|
|
46
|
+
export function findKeyNode(key, node, pair = null) {
|
|
47
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
48
|
+
return node;
|
|
49
|
+
} else if (typeof node === 'object') {
|
|
50
|
+
for (let index in node) {
|
|
51
|
+
const result = findKeyNode(key, node[index], pair);
|
|
52
|
+
if (result) {
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
export function checkEmpty(value) {
|
|
60
|
+
if (typeof value === "number") {
|
|
61
|
+
return value === 0;
|
|
62
|
+
} else {
|
|
63
|
+
return isEmpty(value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export function pascalCase(str) {
|
|
67
|
+
return upperCaseFirst(camelCase(str));
|
|
68
|
+
}
|
|
69
|
+
export function upperCaseFirst(str) {
|
|
70
|
+
str = str || "";
|
|
71
|
+
return str[0].toUpperCase() + str.slice(1);
|
|
72
|
+
}
|
|
73
|
+
export function lowerCaseFirst(str) {
|
|
74
|
+
str = str || "";
|
|
75
|
+
return str[0].toLowerCase() + str.slice(1);
|
|
76
|
+
}
|
|
77
|
+
export function titleString(str) {
|
|
78
|
+
str = str || "";
|
|
79
|
+
return str.split(' ').map(word => capitalize(word)).join(' ');
|
|
80
|
+
}
|
|
81
|
+
export function limitString(str, limit = 35) {
|
|
82
|
+
str = str || "";
|
|
83
|
+
if (str.length <= limit) {
|
|
84
|
+
return str;
|
|
85
|
+
} else {
|
|
86
|
+
return str.substring(0, limit - 3) + "...";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export function safeString(str) {
|
|
90
|
+
str = str || "";
|
|
91
|
+
return xss(str, {
|
|
92
|
+
whiteList: {},
|
|
93
|
+
stripIgnoreTag: true,
|
|
94
|
+
stripIgnoreTagBody: ["script"]
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
export function randomString(length, useNumbers = true, useUppercase = false) {
|
|
98
|
+
let characters = CONSTANTS.LOWER_CASE;
|
|
99
|
+
if (useUppercase) characters += CONSTANTS.UPPER_CASE;
|
|
100
|
+
if (useNumbers) characters += CONSTANTS.NUMBERS;
|
|
101
|
+
let randomString = '';
|
|
102
|
+
for (let i = 0; i < length; i++) {
|
|
103
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
104
|
+
randomString += characters[randomIndex];
|
|
105
|
+
}
|
|
106
|
+
return randomString;
|
|
107
|
+
}
|
|
108
|
+
export function randomHex(length) {
|
|
109
|
+
let result = '';
|
|
110
|
+
for (let i = 0; i < length; i++) {
|
|
111
|
+
const randomIndex = Math.floor(Math.random() * CONSTANTS.HEXADECIMAL.length);
|
|
112
|
+
result += CONSTANTS.HEXADECIMAL[randomIndex];
|
|
113
|
+
}
|
|
114
|
+
return result;
|
|
115
|
+
}
|
|
116
|
+
export function randomUuid(useDashes = true) {
|
|
117
|
+
let d = Date.now();
|
|
118
|
+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
119
|
+
const r = (d + Math.random() * 16) % 16 | 0;
|
|
120
|
+
d = Math.floor(d / 16);
|
|
121
|
+
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
122
|
+
});
|
|
123
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
124
|
+
}
|
|
125
|
+
export function randomWeighted(dict, randomFunc = totalWeight => Math.random() * totalWeight) {
|
|
126
|
+
let elements = Object.keys(dict);
|
|
127
|
+
let weights = Object.values(dict);
|
|
128
|
+
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
129
|
+
let randomNum = randomFunc(totalWeight);
|
|
130
|
+
let weightSum = 0;
|
|
131
|
+
for (let i = 0; i < elements.length; i++) {
|
|
132
|
+
weightSum += weights[i];
|
|
133
|
+
if (randomNum <= weightSum) {
|
|
134
|
+
return elements[i];
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export function cookieDict(res, decodeValues = false) {
|
|
139
|
+
let dict = {};
|
|
140
|
+
const cookies = setCookieParser.parse(res, {
|
|
141
|
+
decodeValues: decodeValues
|
|
142
|
+
});
|
|
143
|
+
for (let cookie of cookies) {
|
|
144
|
+
dict[cookie.name] = cookie.value;
|
|
145
|
+
}
|
|
146
|
+
return dict;
|
|
147
|
+
}
|
|
148
|
+
export function cookieHeader(cookieDict) {
|
|
149
|
+
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
150
|
+
}
|
|
151
|
+
export function isIntlHttpCode(httpCode) {
|
|
152
|
+
return httpCode === undefined || httpCode === null || httpCode === 0 || httpCode === 402 || httpCode === 407 || httpCode === 466 || 500 <= httpCode;
|
|
153
|
+
}
|
|
154
|
+
export function isIntlError(e) {
|
|
155
|
+
return e?.message?.toLowerCase?.()?.includes?.("timeout") || e?.message?.toLowerCase?.()?.includes?.("aborted") || e?.message?.toLowerCase?.()?.includes?.("tls connection") || e?.message?.toLowerCase?.()?.includes?.("socket hang") || isIntlHttpCode(e?.response?.status);
|
|
156
|
+
}
|
|
157
157
|
//# sourceMappingURL=index.js.map
|
package/mjs/node.js
CHANGED
|
@@ -1,176 +1,176 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import crypto from "crypto";
|
|
4
|
-
import { networkInterfaces } from "os";
|
|
5
|
-
import { execSync } from "child_process";
|
|
6
|
-
import bcrypt from "bcrypt";
|
|
7
|
-
import axios from "axios";
|
|
8
|
-
import { HttpsProxyAgent } from "hpagent";
|
|
9
|
-
import { CONSTANTS, randomWeighted, sleep } from "./index.js";
|
|
10
|
-
export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
11
|
-
const lowercaseChars = CONSTANTS.LOWER_CASE;
|
|
12
|
-
const uppercaseChars = CONSTANTS.UPPER_CASE;
|
|
13
|
-
const numbers = CONSTANTS.NUMBERS;
|
|
14
|
-
let characters = lowercaseChars;
|
|
15
|
-
if (useUppercase) characters += uppercaseChars;
|
|
16
|
-
if (useNumbers) characters += numbers;
|
|
17
|
-
let randomString = '';
|
|
18
|
-
while (randomString.length < length) {
|
|
19
|
-
const byte = crypto.randomBytes(1)[0];
|
|
20
|
-
const index = byte % characters.length;
|
|
21
|
-
if (byte < 256 - 256 % characters.length) {
|
|
22
|
-
randomString += characters[index];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return randomString;
|
|
26
|
-
}
|
|
27
|
-
export function tokenHex(length) {
|
|
28
|
-
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
29
|
-
}
|
|
30
|
-
export function tokenUuid(useDashes = true) {
|
|
31
|
-
const uuid = crypto.randomUUID().toString();
|
|
32
|
-
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
33
|
-
}
|
|
34
|
-
export function tokenWeighted(dict) {
|
|
35
|
-
return randomWeighted(dict, crypto.randomInt);
|
|
36
|
-
}
|
|
37
|
-
export function serverIp() {
|
|
38
|
-
const interfaces = networkInterfaces();
|
|
39
|
-
for (const devName in interfaces) {
|
|
40
|
-
const interfaceValue = interfaces[devName];
|
|
41
|
-
for (let i = 0; i < interfaceValue.length; i++) {
|
|
42
|
-
const alias = interfaceValue[i];
|
|
43
|
-
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return '127.0.0.1';
|
|
47
|
-
}
|
|
48
|
-
export function getVersion() {
|
|
49
|
-
try {
|
|
50
|
-
const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
|
|
51
|
-
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
52
|
-
const year = date.getFullYear().toString().slice(-2);
|
|
53
|
-
const month = formatDatePart(date.getMonth() + 1);
|
|
54
|
-
const day = formatDatePart(date.getDate());
|
|
55
|
-
const hour = formatDatePart(date.getHours());
|
|
56
|
-
const minute = formatDatePart(date.getMinutes());
|
|
57
|
-
return parseFloat(`${year}${month}.${day}${hour}${minute}`);
|
|
58
|
-
} catch {
|
|
59
|
-
return 1.0;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export function createNumDir(mainDirectory) {
|
|
63
|
-
fs.mkdirSync(mainDirectory, {
|
|
64
|
-
recursive: true
|
|
65
|
-
});
|
|
66
|
-
for (let i = 0; i <= 9; i++) {
|
|
67
|
-
try {
|
|
68
|
-
fs.mkdirSync(path.join(mainDirectory, i.toString()));
|
|
69
|
-
} catch (e) {
|
|
70
|
-
console.error(`createNumDir:${i}`, e.message);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
export function md5(data) {
|
|
75
|
-
return crypto.createHash('md5').update(data).digest("hex");
|
|
76
|
-
}
|
|
77
|
-
export function hashBcrypt(plainText) {
|
|
78
|
-
return bcrypt.hashSync(plainText, bcrypt.genSaltSync(10));
|
|
79
|
-
}
|
|
80
|
-
export function verifyBcrypt(plainText, hash) {
|
|
81
|
-
return bcrypt.compareSync(plainText, hash);
|
|
82
|
-
}
|
|
83
|
-
export function formatProxy(proxy, protocol = "http") {
|
|
84
|
-
proxy = proxy.trim();
|
|
85
|
-
const splitByProtocol = proxy.split("://");
|
|
86
|
-
if (1 < splitByProtocol.length) protocol = splitByProtocol[0];
|
|
87
|
-
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
88
|
-
if (!proxy.includes("@")) {
|
|
89
|
-
const proxyParts = proxy.split(":");
|
|
90
|
-
if (4 <= proxyParts.length) {
|
|
91
|
-
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
92
|
-
proxyParts.pop();
|
|
93
|
-
proxyParts.pop();
|
|
94
|
-
proxy += proxyParts.join(":");
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const proxyParts = proxy.split(':');
|
|
98
|
-
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
99
|
-
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
100
|
-
if (!proxyStart.includes(".")) {
|
|
101
|
-
proxyParts.pop();
|
|
102
|
-
proxyParts[proxyParts.length - 1] = crypto.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
103
|
-
}
|
|
104
|
-
return protocol + "://" + proxyParts.join(':');
|
|
105
|
-
}
|
|
106
|
-
export function proxyObject(...args) {
|
|
107
|
-
let proxy = formatProxy(...args);
|
|
108
|
-
const splitByProtocol = proxy.split('://');
|
|
109
|
-
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
110
|
-
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
111
|
-
proxy = {
|
|
112
|
-
protocol: splitByProtocol[0],
|
|
113
|
-
host: splitByConn[0],
|
|
114
|
-
port: parseInt(splitByConn[1])
|
|
115
|
-
};
|
|
116
|
-
if (1 < splitById.length) {
|
|
117
|
-
const splitByAuth = splitById[0].split(':');
|
|
118
|
-
proxy.auth = {
|
|
119
|
-
username: splitByAuth[0],
|
|
120
|
-
password: splitByAuth[1]
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
return proxy;
|
|
124
|
-
}
|
|
125
|
-
export async function proxify(proxyConfig, callback = formatProxy) {
|
|
126
|
-
proxyConfig = proxyConfig || {};
|
|
127
|
-
const timeout = 7000;
|
|
128
|
-
if (proxyConfig.mode === 1) {
|
|
129
|
-
return callback(proxyConfig.proxy);
|
|
130
|
-
} else if (proxyConfig.mode === 2) {
|
|
131
|
-
const proxy = callback(proxyConfig.proxy);
|
|
132
|
-
try {
|
|
133
|
-
await axios.get(proxyConfig.resetApi, {
|
|
134
|
-
timeout
|
|
135
|
-
});
|
|
136
|
-
} catch {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
await sleep(5);
|
|
140
|
-
for (let i = 0; i < 5; i++) {
|
|
141
|
-
try {
|
|
142
|
-
const res = await axios.get("https://api64.ipify.org", {
|
|
143
|
-
timeout,
|
|
144
|
-
httpsAgent: new HttpsProxyAgent({
|
|
145
|
-
proxy,
|
|
146
|
-
timeout: 7000
|
|
147
|
-
})
|
|
148
|
-
});
|
|
149
|
-
if (res.status === 200) return proxy;
|
|
150
|
-
} catch {
|
|
151
|
-
await sleep(3);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
} else if (proxyConfig.mode === 3) {
|
|
155
|
-
try {
|
|
156
|
-
const res = await axios.get(proxyConfig.resetApi, {
|
|
157
|
-
timeout
|
|
158
|
-
});
|
|
159
|
-
if (res.status === 200) return callback(proxyConfig.proxy);
|
|
160
|
-
} catch {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
} else if (proxyConfig.mode === 4) {
|
|
164
|
-
return callback(proxyConfig.proxy).replace("{SESSION}", tokenHex(8));
|
|
165
|
-
} else if (proxyConfig.mode === 5) {
|
|
166
|
-
try {
|
|
167
|
-
const lines = proxyConfig.proxy.split("\n");
|
|
168
|
-
const line = lines[crypto.randomInt(0, lines.length)];
|
|
169
|
-
return callback(line);
|
|
170
|
-
} catch {
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import crypto from "crypto";
|
|
4
|
+
import { networkInterfaces } from "os";
|
|
5
|
+
import { execSync } from "child_process";
|
|
6
|
+
import bcrypt from "bcrypt";
|
|
7
|
+
import axios from "axios";
|
|
8
|
+
import { HttpsProxyAgent } from "hpagent";
|
|
9
|
+
import { CONSTANTS, randomWeighted, sleep } from "./index.js";
|
|
10
|
+
export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
11
|
+
const lowercaseChars = CONSTANTS.LOWER_CASE;
|
|
12
|
+
const uppercaseChars = CONSTANTS.UPPER_CASE;
|
|
13
|
+
const numbers = CONSTANTS.NUMBERS;
|
|
14
|
+
let characters = lowercaseChars;
|
|
15
|
+
if (useUppercase) characters += uppercaseChars;
|
|
16
|
+
if (useNumbers) characters += numbers;
|
|
17
|
+
let randomString = '';
|
|
18
|
+
while (randomString.length < length) {
|
|
19
|
+
const byte = crypto.randomBytes(1)[0];
|
|
20
|
+
const index = byte % characters.length;
|
|
21
|
+
if (byte < 256 - 256 % characters.length) {
|
|
22
|
+
randomString += characters[index];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return randomString;
|
|
26
|
+
}
|
|
27
|
+
export function tokenHex(length) {
|
|
28
|
+
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
29
|
+
}
|
|
30
|
+
export function tokenUuid(useDashes = true) {
|
|
31
|
+
const uuid = crypto.randomUUID().toString();
|
|
32
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
33
|
+
}
|
|
34
|
+
export function tokenWeighted(dict) {
|
|
35
|
+
return randomWeighted(dict, crypto.randomInt);
|
|
36
|
+
}
|
|
37
|
+
export function serverIp() {
|
|
38
|
+
const interfaces = networkInterfaces();
|
|
39
|
+
for (const devName in interfaces) {
|
|
40
|
+
const interfaceValue = interfaces[devName];
|
|
41
|
+
for (let i = 0; i < interfaceValue.length; i++) {
|
|
42
|
+
const alias = interfaceValue[i];
|
|
43
|
+
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return '127.0.0.1';
|
|
47
|
+
}
|
|
48
|
+
export function getVersion() {
|
|
49
|
+
try {
|
|
50
|
+
const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
|
|
51
|
+
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
52
|
+
const year = date.getFullYear().toString().slice(-2);
|
|
53
|
+
const month = formatDatePart(date.getMonth() + 1);
|
|
54
|
+
const day = formatDatePart(date.getDate());
|
|
55
|
+
const hour = formatDatePart(date.getHours());
|
|
56
|
+
const minute = formatDatePart(date.getMinutes());
|
|
57
|
+
return parseFloat(`${year}${month}.${day}${hour}${minute}`);
|
|
58
|
+
} catch {
|
|
59
|
+
return 1.0;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function createNumDir(mainDirectory) {
|
|
63
|
+
fs.mkdirSync(mainDirectory, {
|
|
64
|
+
recursive: true
|
|
65
|
+
});
|
|
66
|
+
for (let i = 0; i <= 9; i++) {
|
|
67
|
+
try {
|
|
68
|
+
fs.mkdirSync(path.join(mainDirectory, i.toString()));
|
|
69
|
+
} catch (e) {
|
|
70
|
+
console.error(`createNumDir:${i}`, e.message);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function md5(data) {
|
|
75
|
+
return crypto.createHash('md5').update(data).digest("hex");
|
|
76
|
+
}
|
|
77
|
+
export function hashBcrypt(plainText) {
|
|
78
|
+
return bcrypt.hashSync(plainText, bcrypt.genSaltSync(10));
|
|
79
|
+
}
|
|
80
|
+
export function verifyBcrypt(plainText, hash) {
|
|
81
|
+
return bcrypt.compareSync(plainText, hash);
|
|
82
|
+
}
|
|
83
|
+
export function formatProxy(proxy, protocol = "http") {
|
|
84
|
+
proxy = proxy.trim();
|
|
85
|
+
const splitByProtocol = proxy.split("://");
|
|
86
|
+
if (1 < splitByProtocol.length) protocol = splitByProtocol[0];
|
|
87
|
+
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
88
|
+
if (!proxy.includes("@")) {
|
|
89
|
+
const proxyParts = proxy.split(":");
|
|
90
|
+
if (4 <= proxyParts.length) {
|
|
91
|
+
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
92
|
+
proxyParts.pop();
|
|
93
|
+
proxyParts.pop();
|
|
94
|
+
proxy += proxyParts.join(":");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const proxyParts = proxy.split(':');
|
|
98
|
+
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
99
|
+
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
100
|
+
if (!proxyStart.includes(".")) {
|
|
101
|
+
proxyParts.pop();
|
|
102
|
+
proxyParts[proxyParts.length - 1] = crypto.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
103
|
+
}
|
|
104
|
+
return protocol + "://" + proxyParts.join(':');
|
|
105
|
+
}
|
|
106
|
+
export function proxyObject(...args) {
|
|
107
|
+
let proxy = formatProxy(...args);
|
|
108
|
+
const splitByProtocol = proxy.split('://');
|
|
109
|
+
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
110
|
+
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
111
|
+
proxy = {
|
|
112
|
+
protocol: splitByProtocol[0],
|
|
113
|
+
host: splitByConn[0],
|
|
114
|
+
port: parseInt(splitByConn[1])
|
|
115
|
+
};
|
|
116
|
+
if (1 < splitById.length) {
|
|
117
|
+
const splitByAuth = splitById[0].split(':');
|
|
118
|
+
proxy.auth = {
|
|
119
|
+
username: splitByAuth[0],
|
|
120
|
+
password: splitByAuth[1]
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return proxy;
|
|
124
|
+
}
|
|
125
|
+
export async function proxify(proxyConfig, callback = formatProxy) {
|
|
126
|
+
proxyConfig = proxyConfig || {};
|
|
127
|
+
const timeout = 7000;
|
|
128
|
+
if (proxyConfig.mode === 1) {
|
|
129
|
+
return callback(proxyConfig.proxy);
|
|
130
|
+
} else if (proxyConfig.mode === 2) {
|
|
131
|
+
const proxy = callback(proxyConfig.proxy);
|
|
132
|
+
try {
|
|
133
|
+
await axios.get(proxyConfig.resetApi, {
|
|
134
|
+
timeout
|
|
135
|
+
});
|
|
136
|
+
} catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
await sleep(5);
|
|
140
|
+
for (let i = 0; i < 5; i++) {
|
|
141
|
+
try {
|
|
142
|
+
const res = await axios.get("https://api64.ipify.org", {
|
|
143
|
+
timeout,
|
|
144
|
+
httpsAgent: new HttpsProxyAgent({
|
|
145
|
+
proxy,
|
|
146
|
+
timeout: 7000
|
|
147
|
+
})
|
|
148
|
+
});
|
|
149
|
+
if (res.status === 200) return proxy;
|
|
150
|
+
} catch {
|
|
151
|
+
await sleep(3);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
} else if (proxyConfig.mode === 3) {
|
|
155
|
+
try {
|
|
156
|
+
const res = await axios.get(proxyConfig.resetApi, {
|
|
157
|
+
timeout
|
|
158
|
+
});
|
|
159
|
+
if (res.status === 200) return callback(proxyConfig.proxy);
|
|
160
|
+
} catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
} else if (proxyConfig.mode === 4) {
|
|
164
|
+
return callback(proxyConfig.proxy).replace("{SESSION}", tokenHex(8));
|
|
165
|
+
} else if (proxyConfig.mode === 5) {
|
|
166
|
+
try {
|
|
167
|
+
const lines = proxyConfig.proxy.split("\n");
|
|
168
|
+
const line = lines[crypto.randomInt(0, lines.length)];
|
|
169
|
+
return callback(line);
|
|
170
|
+
} catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
176
|
//# sourceMappingURL=node.js.map
|