melperjs 2.0.1 → 4.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/.babelrc +31 -11
- package/README.md +31 -19
- package/{lib → cjs}/index.js +72 -43
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +3 -0
- package/mjs/index.js +235 -0
- package/mjs/index.js.map +1 -0
- package/mjs/package.json +3 -0
- package/package.json +43 -43
- package/src/index.js +63 -29
- package/test/script.js +41 -38
- package/lib/node.js +0 -193
- package/src/node.js +0 -192
package/src/node.js
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
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
|
-
|
|
7
|
-
import axios from "axios";
|
|
8
|
-
import {HttpsProxyAgent} from "hpagent";
|
|
9
|
-
|
|
10
|
-
import {randomWeighted, sleep} from "./index.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
14
|
-
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
15
|
-
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
16
|
-
const numbers = '0123456789';
|
|
17
|
-
|
|
18
|
-
let characters = lowercaseChars;
|
|
19
|
-
if (useUppercase) characters += uppercaseChars;
|
|
20
|
-
if (useNumbers) characters += numbers;
|
|
21
|
-
|
|
22
|
-
let randomString = '';
|
|
23
|
-
while (randomString.length < length) {
|
|
24
|
-
const byte = crypto.randomBytes(1)[0];
|
|
25
|
-
const index = byte % characters.length;
|
|
26
|
-
if (byte < (256 - (256 % characters.length))) {
|
|
27
|
-
randomString += characters[index];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return randomString;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function tokenHex(length) {
|
|
35
|
-
return crypto
|
|
36
|
-
.randomBytes(Math.ceil(length / 2))
|
|
37
|
-
.toString('hex')
|
|
38
|
-
.slice(0, length);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function tokenUuid(useDashes = true) {
|
|
42
|
-
const uuid = crypto.randomUUID().toString();
|
|
43
|
-
return useDashes ? uuid : uuid.replaceAll("-", "")
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function tokenWeighted(dict) {
|
|
47
|
-
return randomWeighted(dict, crypto.randomInt);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function serverIp() {
|
|
51
|
-
const interfaces = networkInterfaces();
|
|
52
|
-
for (const devName in interfaces) {
|
|
53
|
-
const interfaceValue = interfaces[devName];
|
|
54
|
-
for (let i = 0; i < interfaceValue.length; i++) {
|
|
55
|
-
const alias = interfaceValue[i];
|
|
56
|
-
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal)
|
|
57
|
-
return alias.address;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return '127.0.0.1';
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function getVersion() {
|
|
64
|
-
try {
|
|
65
|
-
const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
|
|
66
|
-
const formatDatePart = (value) => value.toString().padStart(2, '0');
|
|
67
|
-
const year = date.getFullYear().toString().slice(-2);
|
|
68
|
-
const month = formatDatePart(date.getMonth() + 1);
|
|
69
|
-
const day = formatDatePart(date.getDate());
|
|
70
|
-
const hour = formatDatePart(date.getHours());
|
|
71
|
-
const minute = formatDatePart(date.getMinutes());
|
|
72
|
-
return parseFloat(`${year}${month}.${day}${hour}${minute}`);
|
|
73
|
-
} catch {
|
|
74
|
-
return 1.0;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function createDir(directory) {
|
|
79
|
-
if (!fs.existsSync(directory)) {
|
|
80
|
-
fs.mkdirSync(directory, {recursive: true});
|
|
81
|
-
return true;
|
|
82
|
-
}
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function createNumDir(mainDirectory) {
|
|
87
|
-
createDir(mainDirectory);
|
|
88
|
-
for (let i = 0; i <= 9; i++) {
|
|
89
|
-
try {
|
|
90
|
-
createDir(path.join(mainDirectory, i.toString()));
|
|
91
|
-
} catch (e) {
|
|
92
|
-
console.error(`createNumDir:${i}`, e.message);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export function md5(data) {
|
|
98
|
-
return crypto.createHash('md5').update(data).digest("hex");
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function formatProxy(proxy, protocol = "http") {
|
|
102
|
-
proxy = proxy.trim();
|
|
103
|
-
const splitByProtocol = proxy.split("://");
|
|
104
|
-
if (1 < splitByProtocol.length)
|
|
105
|
-
protocol = splitByProtocol[0];
|
|
106
|
-
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
107
|
-
if (!proxy.includes("@")) {
|
|
108
|
-
const proxyParts = proxy.split(":");
|
|
109
|
-
if (4 <= proxyParts.length) {
|
|
110
|
-
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
111
|
-
proxyParts.pop();
|
|
112
|
-
proxyParts.pop();
|
|
113
|
-
proxy += proxyParts.join(":");
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
const proxyParts = proxy.split(':');
|
|
117
|
-
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
118
|
-
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
119
|
-
if (!proxyStart.includes(".")) {
|
|
120
|
-
proxyParts.pop();
|
|
121
|
-
proxyParts[proxyParts.length - 1] = crypto.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
122
|
-
}
|
|
123
|
-
return protocol + "://" + proxyParts.join(':');
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export function proxyObject(...args) {
|
|
127
|
-
let proxy = formatProxy(...args);
|
|
128
|
-
const splitByProtocol = proxy.split('://');
|
|
129
|
-
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
130
|
-
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
131
|
-
proxy = {
|
|
132
|
-
protocol: splitByProtocol[0],
|
|
133
|
-
host: splitByConn[0],
|
|
134
|
-
port: parseInt(splitByConn[1]),
|
|
135
|
-
};
|
|
136
|
-
if (1 < splitById.length) {
|
|
137
|
-
const splitByAuth = splitById[0].split(':');
|
|
138
|
-
proxy.auth = {
|
|
139
|
-
username: splitByAuth[0],
|
|
140
|
-
password: splitByAuth[1]
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
return proxy;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export async function proxify(proxyConfig, callback = formatProxy) {
|
|
147
|
-
proxyConfig = proxyConfig || {};
|
|
148
|
-
const timeout = 7000;
|
|
149
|
-
if (proxyConfig.mode === 1) {
|
|
150
|
-
return callback(proxyConfig.proxy);
|
|
151
|
-
} else if (proxyConfig.mode === 2) {
|
|
152
|
-
const proxy = callback(proxyConfig.proxy);
|
|
153
|
-
try {
|
|
154
|
-
await axios.get(proxyConfig.resetApi, {timeout});
|
|
155
|
-
} catch {
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
await sleep(5);
|
|
159
|
-
for (let i = 0; i < 5; i++) {
|
|
160
|
-
try {
|
|
161
|
-
const res = await axios.get("https://api64.ipify.org", {
|
|
162
|
-
timeout,
|
|
163
|
-
httpsAgent: new HttpsProxyAgent({proxy, timeout: 7000})
|
|
164
|
-
});
|
|
165
|
-
if (res.status === 200)
|
|
166
|
-
return proxy;
|
|
167
|
-
} catch {
|
|
168
|
-
await sleep(3);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
} else if (proxyConfig.mode === 3) {
|
|
172
|
-
try {
|
|
173
|
-
const res = await axios.get(proxyConfig.resetApi, {timeout});
|
|
174
|
-
if (res.status === 200)
|
|
175
|
-
return callback(proxyConfig.proxy);
|
|
176
|
-
} catch {
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
|
-
} else if (proxyConfig.mode === 4) {
|
|
180
|
-
return callback(proxyConfig.proxy).replace("{SESSION}", tokenHex(8));
|
|
181
|
-
} else if (proxyConfig.mode === 5) {
|
|
182
|
-
try {
|
|
183
|
-
const lines = proxyConfig.proxy.split("\n");
|
|
184
|
-
const line = lines[crypto.randomInt(0, lines.length)];
|
|
185
|
-
return callback(line);
|
|
186
|
-
} catch {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
return null;
|
|
192
|
-
}
|