melperjs 2.0.0 → 3.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 +31 -19
- package/lib/index.js +183 -0
- package/lib/node.js +193 -0
- package/package.json +1 -1
- package/src/index.js +31 -7
- package/test/script.js +7 -3
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ const nodeHelper = require("melperjs/node");
|
|
|
21
21
|
const axios = require("axios");
|
|
22
22
|
|
|
23
23
|
(async () => {
|
|
24
|
-
console.log(helper.Exception("something went wrong"));
|
|
24
|
+
console.log(helper.Exception("something went wrong", {status: 500}, "axios error"));
|
|
25
25
|
console.log(helper.time());
|
|
26
26
|
await helper.sleepMs(1000);
|
|
27
27
|
console.log(helper.time());
|
|
@@ -43,8 +43,12 @@ const axios = require("axios");
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
}));
|
|
46
|
-
console.log("
|
|
47
|
-
console.log("
|
|
46
|
+
console.log("str empty", helper.checkEmpty(""));
|
|
47
|
+
console.log("1 empty", helper.checkEmpty(1));
|
|
48
|
+
console.log("[] empty", helper.checkEmpty([]));
|
|
49
|
+
console.log(helper.upperCaseFirst("first letter upper"));
|
|
50
|
+
console.log(helper.lowerCaseFirst("First Letter Lower"));
|
|
51
|
+
console.log(helper.titleString("THIS mUsT be Title"));
|
|
48
52
|
console.log(helper.limitString("LONG TEXT", 7));
|
|
49
53
|
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
50
54
|
console.log(helper.randomString(32, true, true));
|
|
@@ -75,33 +79,41 @@ const axios = require("axios");
|
|
|
75
79
|
})();
|
|
76
80
|
|
|
77
81
|
/*
|
|
78
|
-
{
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
+
{
|
|
83
|
+
name: 'AxiosError',
|
|
84
|
+
message: 'something went wrong',
|
|
85
|
+
response: { status: 500 }
|
|
86
|
+
}
|
|
87
|
+
1700529489
|
|
88
|
+
1700529490
|
|
89
|
+
1700529491
|
|
82
90
|
Promise timed out after 1000ms
|
|
83
91
|
{ x: 1, y: 2, c: { d: true } }
|
|
92
|
+
str empty true
|
|
84
93
|
1 empty false
|
|
85
94
|
[] empty true
|
|
95
|
+
First letter upper
|
|
96
|
+
first Letter Lower
|
|
97
|
+
This Must Be Title
|
|
86
98
|
LONG...
|
|
87
99
|
SAFE TEXT
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
100
|
+
15WY89Q4yAMCzPmNsU0ANGX2eiOz7Gfy
|
|
101
|
+
9eb93429
|
|
102
|
+
7e1fb299-251c-4a75-bb37-f7cc3a730121
|
|
91
103
|
strongProbability
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
STIhrofUYVAQ4anplyJW2T7GEwSJkuk7
|
|
105
|
+
6db791a1
|
|
106
|
+
0cbef42d-a277-4f66-8bcc-e4de337c2b56
|
|
95
107
|
strongProbability
|
|
96
108
|
8d777f385d3dfec8815d20f7496026dc
|
|
97
|
-
$2b$10$
|
|
109
|
+
$2b$10$IsuTscKKHbcf6sBp7BrCOOcg6A8v32G9UxzdYN3Y6xyMaUynweYX2
|
|
98
110
|
passwordHash true
|
|
99
111
|
{
|
|
100
|
-
'1P_JAR': '2023-11-
|
|
101
|
-
AEC: '
|
|
102
|
-
NID: '511=
|
|
112
|
+
'1P_JAR': '2023-11-21-01',
|
|
113
|
+
AEC: 'Ackid1TlDwA2YJw3rzP5t3x5vBdxZt-4AzkhdwLahUVpj3vhdVHvPlw0VWM',
|
|
114
|
+
NID: '511=tD21gyuziCvCgZSQZd5h_xDFOF6df8AhkFy0iXq9MwHG9K8J3FEkT7L0CACgjJhGDVQFoZG_Pwi2Wo8Kf7NnmvcNGVmk-lDhY768PI9sVSUmSHIYwpfsuVvG4NwNwk3iPNKmbqaC_H-YVGZhEJVn2c6YYUVxE0oEDtfuyPhGOXw'
|
|
103
115
|
}
|
|
104
|
-
1P_JAR=2023-11-
|
|
116
|
+
1P_JAR=2023-11-21-01;AEC=Ackid1TlDwA2YJw3rzP5t3x5vBdxZt-4AzkhdwLahUVpj3vhdVHvPlw0VWM;NID=511=tD21gyuziCvCgZSQZd5h_xDFOF6df8AhkFy0iXq9MwHG9K8J3FEkT7L0CACgjJhGDVQFoZG_Pwi2Wo8Kf7NnmvcNGVmk-lDhY768PI9sVSUmSHIYwpfsuVvG4NwNwk3iPNKmbqaC_H-YVGZhEJVn2c6YYUVxE0oEDtfuyPhGOXw
|
|
105
117
|
http://id:pw-{SESSION}@127.0.0.1:8080
|
|
106
118
|
{
|
|
107
119
|
protocol: 'http',
|
|
@@ -109,7 +121,7 @@ http://id:pw-{SESSION}@127.0.0.1:8080
|
|
|
109
121
|
port: 8080,
|
|
110
122
|
auth: { username: 'id', password: 'pw-{SESSION}' }
|
|
111
123
|
}
|
|
112
|
-
http://id:pw-
|
|
124
|
+
http://id:pw-527ef984@127.0.0.1:8080
|
|
113
125
|
127.0.0.1
|
|
114
126
|
HTTP CODE: 401 FOREIGN false
|
|
115
127
|
HTTP CODE: 407 FOREIGN (Failed Proxy Auth) true
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Exception = Exception;
|
|
7
|
+
exports.checkEmpty = checkEmpty;
|
|
8
|
+
exports.cookieDict = cookieDict;
|
|
9
|
+
exports.cookieHeader = cookieHeader;
|
|
10
|
+
exports.findKeyNode = findKeyNode;
|
|
11
|
+
exports.foreignHttpError = foreignHttpError;
|
|
12
|
+
exports.hashBcrypt = hashBcrypt;
|
|
13
|
+
exports.limitString = limitString;
|
|
14
|
+
exports.lowerCaseFirst = lowerCaseFirst;
|
|
15
|
+
exports.pascalCase = pascalCase;
|
|
16
|
+
exports.promiseTimeout = promiseTimeout;
|
|
17
|
+
exports.randomHex = randomHex;
|
|
18
|
+
exports.randomString = randomString;
|
|
19
|
+
exports.randomUuid = randomUuid;
|
|
20
|
+
exports.randomWeighted = randomWeighted;
|
|
21
|
+
exports.safeString = safeString;
|
|
22
|
+
exports.sleep = sleep;
|
|
23
|
+
exports.sleepMs = sleepMs;
|
|
24
|
+
exports.time = time;
|
|
25
|
+
exports.titleString = titleString;
|
|
26
|
+
exports.upperCaseFirst = upperCaseFirst;
|
|
27
|
+
exports.verifyBcrypt = verifyBcrypt;
|
|
28
|
+
var _ = _interopRequireWildcard(require("lodash"));
|
|
29
|
+
var _xss = _interopRequireDefault(require("xss"));
|
|
30
|
+
var _bcrypt = _interopRequireDefault(require("bcrypt"));
|
|
31
|
+
var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
32
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
33
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
34
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
35
|
+
function Exception(message, response = {}, name = null) {
|
|
36
|
+
response.status = response.status || 400;
|
|
37
|
+
return {
|
|
38
|
+
name: pascalCase(name),
|
|
39
|
+
message,
|
|
40
|
+
response
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function time() {
|
|
44
|
+
return Math.floor(Date.now() / 1000);
|
|
45
|
+
}
|
|
46
|
+
async function sleepMs(milliseconds) {
|
|
47
|
+
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
48
|
+
}
|
|
49
|
+
async function sleep(seconds) {
|
|
50
|
+
return await sleepMs(seconds * 1000);
|
|
51
|
+
}
|
|
52
|
+
function promiseTimeout(milliseconds, promise) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
const timer = setTimeout(() => {
|
|
55
|
+
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
56
|
+
}, milliseconds);
|
|
57
|
+
promise.then(value => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
resolve(value);
|
|
60
|
+
}).catch(reason => {
|
|
61
|
+
clearTimeout(timer);
|
|
62
|
+
reject(reason);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function findKeyNode(key, node, pair = null) {
|
|
67
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
68
|
+
return node;
|
|
69
|
+
} else if (typeof node === 'object') {
|
|
70
|
+
for (let index in node) {
|
|
71
|
+
const result = findKeyNode(key, node[index], pair);
|
|
72
|
+
if (result) {
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
function checkEmpty(value) {
|
|
80
|
+
if (typeof value === "number") {
|
|
81
|
+
return value === 0;
|
|
82
|
+
} else {
|
|
83
|
+
return _.isEmpty(value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function pascalCase(str) {
|
|
87
|
+
return upperCaseFirst(_.camelCase(str));
|
|
88
|
+
}
|
|
89
|
+
function upperCaseFirst(str) {
|
|
90
|
+
str = str || "";
|
|
91
|
+
return _.upperCase(str[0]) + str.slice(1);
|
|
92
|
+
}
|
|
93
|
+
function lowerCaseFirst(str) {
|
|
94
|
+
str = str || "";
|
|
95
|
+
return _.lowerCase(str[0]) + str.slice(1);
|
|
96
|
+
}
|
|
97
|
+
function titleString(str) {
|
|
98
|
+
str = str || "";
|
|
99
|
+
return str.split(' ').map(word => _.capitalize(word)).join(' ');
|
|
100
|
+
}
|
|
101
|
+
function limitString(str, limit = 35) {
|
|
102
|
+
str = str || "";
|
|
103
|
+
if (str.length <= limit) {
|
|
104
|
+
return str;
|
|
105
|
+
} else {
|
|
106
|
+
return str.substring(0, limit - 3) + "...";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function safeString(str) {
|
|
110
|
+
str = str || "";
|
|
111
|
+
return (0, _xss.default)(str, {
|
|
112
|
+
whiteList: {},
|
|
113
|
+
stripIgnoreTag: true,
|
|
114
|
+
stripIgnoreTagBody: ["script"]
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function randomString(length, useNumbers = true, useUppercase = false) {
|
|
118
|
+
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
119
|
+
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
120
|
+
const numbers = '0123456789';
|
|
121
|
+
let characters = lowercaseChars;
|
|
122
|
+
if (useUppercase) characters += uppercaseChars;
|
|
123
|
+
if (useNumbers) characters += numbers;
|
|
124
|
+
let randomString = '';
|
|
125
|
+
for (let i = 0; i < length; i++) {
|
|
126
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
127
|
+
randomString += characters[randomIndex];
|
|
128
|
+
}
|
|
129
|
+
return randomString;
|
|
130
|
+
}
|
|
131
|
+
function randomHex(length) {
|
|
132
|
+
let result = '';
|
|
133
|
+
const characters = '0123456789abcdef';
|
|
134
|
+
for (let i = 0; i < length; i++) {
|
|
135
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
136
|
+
result += characters[randomIndex];
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
function randomUuid(useDashes = true) {
|
|
141
|
+
let d = Date.now();
|
|
142
|
+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
143
|
+
const r = (d + Math.random() * 16) % 16 | 0;
|
|
144
|
+
d = Math.floor(d / 16);
|
|
145
|
+
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
146
|
+
});
|
|
147
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
148
|
+
}
|
|
149
|
+
function randomWeighted(dict, randomFunc = totalWeight => Math.random() * totalWeight) {
|
|
150
|
+
let elements = Object.keys(dict);
|
|
151
|
+
let weights = Object.values(dict);
|
|
152
|
+
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
153
|
+
let randomNum = randomFunc(totalWeight);
|
|
154
|
+
let weightSum = 0;
|
|
155
|
+
for (let i = 0; i < elements.length; i++) {
|
|
156
|
+
weightSum += weights[i];
|
|
157
|
+
if (randomNum <= weightSum) {
|
|
158
|
+
return elements[i];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function hashBcrypt(plainText) {
|
|
163
|
+
return _bcrypt.default.hashSync(plainText, _bcrypt.default.genSaltSync(10));
|
|
164
|
+
}
|
|
165
|
+
function verifyBcrypt(plainText, hash) {
|
|
166
|
+
return _bcrypt.default.compareSync(plainText, hash);
|
|
167
|
+
}
|
|
168
|
+
function cookieDict(res, decodeValues = false) {
|
|
169
|
+
let dict = {};
|
|
170
|
+
const cookies = _setCookieParser.default.parse(res, {
|
|
171
|
+
decodeValues: decodeValues
|
|
172
|
+
});
|
|
173
|
+
for (let cookie of cookies) {
|
|
174
|
+
dict[cookie.name] = cookie.value;
|
|
175
|
+
}
|
|
176
|
+
return dict;
|
|
177
|
+
}
|
|
178
|
+
function cookieHeader(cookieDict) {
|
|
179
|
+
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
180
|
+
}
|
|
181
|
+
function foreignHttpError(httpCode) {
|
|
182
|
+
return httpCode === undefined || httpCode === null || httpCode === 0 || httpCode === 402 || httpCode === 407 || httpCode === 466 || 500 <= httpCode;
|
|
183
|
+
}
|
package/lib/node.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createDir = createDir;
|
|
7
|
+
exports.createNumDir = createNumDir;
|
|
8
|
+
exports.formatProxy = formatProxy;
|
|
9
|
+
exports.getVersion = getVersion;
|
|
10
|
+
exports.md5 = md5;
|
|
11
|
+
exports.proxify = proxify;
|
|
12
|
+
exports.proxyObject = proxyObject;
|
|
13
|
+
exports.serverIp = serverIp;
|
|
14
|
+
exports.tokenHex = tokenHex;
|
|
15
|
+
exports.tokenString = tokenString;
|
|
16
|
+
exports.tokenUuid = tokenUuid;
|
|
17
|
+
exports.tokenWeighted = tokenWeighted;
|
|
18
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
19
|
+
var _path = _interopRequireDefault(require("path"));
|
|
20
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
|
21
|
+
var _os = require("os");
|
|
22
|
+
var _child_process = require("child_process");
|
|
23
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
24
|
+
var _hpagent = require("hpagent");
|
|
25
|
+
var _index = require("./index.js");
|
|
26
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
27
|
+
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
28
|
+
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
29
|
+
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
30
|
+
const numbers = '0123456789';
|
|
31
|
+
let characters = lowercaseChars;
|
|
32
|
+
if (useUppercase) characters += uppercaseChars;
|
|
33
|
+
if (useNumbers) characters += numbers;
|
|
34
|
+
let randomString = '';
|
|
35
|
+
while (randomString.length < length) {
|
|
36
|
+
const byte = _crypto.default.randomBytes(1)[0];
|
|
37
|
+
const index = byte % characters.length;
|
|
38
|
+
if (byte < 256 - 256 % characters.length) {
|
|
39
|
+
randomString += characters[index];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return randomString;
|
|
43
|
+
}
|
|
44
|
+
function tokenHex(length) {
|
|
45
|
+
return _crypto.default.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
46
|
+
}
|
|
47
|
+
function tokenUuid(useDashes = true) {
|
|
48
|
+
const uuid = _crypto.default.randomUUID().toString();
|
|
49
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
50
|
+
}
|
|
51
|
+
function tokenWeighted(dict) {
|
|
52
|
+
return (0, _index.randomWeighted)(dict, _crypto.default.randomInt);
|
|
53
|
+
}
|
|
54
|
+
function serverIp() {
|
|
55
|
+
const interfaces = (0, _os.networkInterfaces)();
|
|
56
|
+
for (const devName in interfaces) {
|
|
57
|
+
const interfaceValue = interfaces[devName];
|
|
58
|
+
for (let i = 0; i < interfaceValue.length; i++) {
|
|
59
|
+
const alias = interfaceValue[i];
|
|
60
|
+
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.address.startsWith("192.168.") && !alias.internal) return alias.address;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return '127.0.0.1';
|
|
64
|
+
}
|
|
65
|
+
function getVersion() {
|
|
66
|
+
try {
|
|
67
|
+
const date = new Date((0, _child_process.execSync)('git show -s --format=%ci HEAD').toString().trim());
|
|
68
|
+
const formatDatePart = value => value.toString().padStart(2, '0');
|
|
69
|
+
const year = date.getFullYear().toString().slice(-2);
|
|
70
|
+
const month = formatDatePart(date.getMonth() + 1);
|
|
71
|
+
const day = formatDatePart(date.getDate());
|
|
72
|
+
const hour = formatDatePart(date.getHours());
|
|
73
|
+
const minute = formatDatePart(date.getMinutes());
|
|
74
|
+
return parseFloat(`${year}${month}.${day}${hour}${minute}`);
|
|
75
|
+
} catch {
|
|
76
|
+
return 1.0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function createDir(directory) {
|
|
80
|
+
if (!_fs.default.existsSync(directory)) {
|
|
81
|
+
_fs.default.mkdirSync(directory, {
|
|
82
|
+
recursive: true
|
|
83
|
+
});
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
function createNumDir(mainDirectory) {
|
|
89
|
+
createDir(mainDirectory);
|
|
90
|
+
for (let i = 0; i <= 9; i++) {
|
|
91
|
+
try {
|
|
92
|
+
createDir(_path.default.join(mainDirectory, i.toString()));
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error(`createNumDir:${i}`, e.message);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function md5(data) {
|
|
99
|
+
return _crypto.default.createHash('md5').update(data).digest("hex");
|
|
100
|
+
}
|
|
101
|
+
function formatProxy(proxy, protocol = "http") {
|
|
102
|
+
proxy = proxy.trim();
|
|
103
|
+
const splitByProtocol = proxy.split("://");
|
|
104
|
+
if (1 < splitByProtocol.length) protocol = splitByProtocol[0];
|
|
105
|
+
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
106
|
+
if (!proxy.includes("@")) {
|
|
107
|
+
const proxyParts = proxy.split(":");
|
|
108
|
+
if (4 <= proxyParts.length) {
|
|
109
|
+
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
110
|
+
proxyParts.pop();
|
|
111
|
+
proxyParts.pop();
|
|
112
|
+
proxy += proxyParts.join(":");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const proxyParts = proxy.split(':');
|
|
116
|
+
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
117
|
+
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
118
|
+
if (!proxyStart.includes(".")) {
|
|
119
|
+
proxyParts.pop();
|
|
120
|
+
proxyParts[proxyParts.length - 1] = _crypto.default.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
121
|
+
}
|
|
122
|
+
return protocol + "://" + proxyParts.join(':');
|
|
123
|
+
}
|
|
124
|
+
function proxyObject(...args) {
|
|
125
|
+
let proxy = formatProxy(...args);
|
|
126
|
+
const splitByProtocol = proxy.split('://');
|
|
127
|
+
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
128
|
+
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
129
|
+
proxy = {
|
|
130
|
+
protocol: splitByProtocol[0],
|
|
131
|
+
host: splitByConn[0],
|
|
132
|
+
port: parseInt(splitByConn[1])
|
|
133
|
+
};
|
|
134
|
+
if (1 < splitById.length) {
|
|
135
|
+
const splitByAuth = splitById[0].split(':');
|
|
136
|
+
proxy.auth = {
|
|
137
|
+
username: splitByAuth[0],
|
|
138
|
+
password: splitByAuth[1]
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return proxy;
|
|
142
|
+
}
|
|
143
|
+
async function proxify(proxyConfig, callback = formatProxy) {
|
|
144
|
+
proxyConfig = proxyConfig || {};
|
|
145
|
+
const timeout = 7000;
|
|
146
|
+
if (proxyConfig.mode === 1) {
|
|
147
|
+
return callback(proxyConfig.proxy);
|
|
148
|
+
} else if (proxyConfig.mode === 2) {
|
|
149
|
+
const proxy = callback(proxyConfig.proxy);
|
|
150
|
+
try {
|
|
151
|
+
await _axios.default.get(proxyConfig.resetApi, {
|
|
152
|
+
timeout
|
|
153
|
+
});
|
|
154
|
+
} catch {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
await (0, _index.sleep)(5);
|
|
158
|
+
for (let i = 0; i < 5; i++) {
|
|
159
|
+
try {
|
|
160
|
+
const res = await _axios.default.get("https://api64.ipify.org", {
|
|
161
|
+
timeout,
|
|
162
|
+
httpsAgent: new _hpagent.HttpsProxyAgent({
|
|
163
|
+
proxy,
|
|
164
|
+
timeout: 7000
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
if (res.status === 200) return proxy;
|
|
168
|
+
} catch {
|
|
169
|
+
await (0, _index.sleep)(3);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} else if (proxyConfig.mode === 3) {
|
|
173
|
+
try {
|
|
174
|
+
const res = await _axios.default.get(proxyConfig.resetApi, {
|
|
175
|
+
timeout
|
|
176
|
+
});
|
|
177
|
+
if (res.status === 200) return callback(proxyConfig.proxy);
|
|
178
|
+
} catch {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
} else if (proxyConfig.mode === 4) {
|
|
182
|
+
return callback(proxyConfig.proxy).replace("{SESSION}", tokenHex(8));
|
|
183
|
+
} else if (proxyConfig.mode === 5) {
|
|
184
|
+
try {
|
|
185
|
+
const lines = proxyConfig.proxy.split("\n");
|
|
186
|
+
const line = lines[_crypto.default.randomInt(0, lines.length)];
|
|
187
|
+
return callback(line);
|
|
188
|
+
} catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _ from "lodash";
|
|
2
2
|
import xss from "xss";
|
|
3
3
|
import bcrypt from "bcrypt";
|
|
4
4
|
import setCookieParser from "set-cookie-parser";
|
|
5
5
|
|
|
6
|
-
export function Exception(message, response = {}) {
|
|
6
|
+
export function Exception(message, response = {}, name = null) {
|
|
7
7
|
response.status = response.status || 400;
|
|
8
8
|
return {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
name: pascalCase(name),
|
|
10
|
+
message,
|
|
11
|
+
response,
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
|
|
@@ -55,7 +56,7 @@ export function findKeyNode(key, node, pair = null) {
|
|
|
55
56
|
return null;
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
export function
|
|
59
|
+
export function checkEmpty(value) {
|
|
59
60
|
if (typeof value === "number") {
|
|
60
61
|
return value === 0;
|
|
61
62
|
} else {
|
|
@@ -63,6 +64,28 @@ export function isEmpty(value) {
|
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
export function pascalCase(str){
|
|
68
|
+
return upperCaseFirst(_.camelCase(str));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function upperCaseFirst(str) {
|
|
72
|
+
str = str || "";
|
|
73
|
+
return _.upperCase(str[0]) + str.slice(1);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function lowerCaseFirst(str) {
|
|
77
|
+
str = str || "";
|
|
78
|
+
return _.lowerCase(str[0]) + str.slice(1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function titleString(str) {
|
|
82
|
+
str = str || "";
|
|
83
|
+
return str
|
|
84
|
+
.split(' ')
|
|
85
|
+
.map(word => _.capitalize(word))
|
|
86
|
+
.join(' ');
|
|
87
|
+
}
|
|
88
|
+
|
|
66
89
|
export function limitString(str, limit = 35) {
|
|
67
90
|
str = str || "";
|
|
68
91
|
if (str.length <= limit) {
|
|
@@ -72,8 +95,9 @@ export function limitString(str, limit = 35) {
|
|
|
72
95
|
}
|
|
73
96
|
}
|
|
74
97
|
|
|
75
|
-
export function safeString(
|
|
76
|
-
|
|
98
|
+
export function safeString(str) {
|
|
99
|
+
str = str || "";
|
|
100
|
+
return xss(str, {
|
|
77
101
|
whiteList: {},
|
|
78
102
|
stripIgnoreTag: true,
|
|
79
103
|
stripIgnoreTagBody: ["script"]
|
package/test/script.js
CHANGED
|
@@ -4,7 +4,7 @@ import axios from "axios"
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
(async () => {
|
|
7
|
-
console.log(helper.Exception("something went wrong"));
|
|
7
|
+
console.log(helper.Exception("something went wrong", {status: 500}, "axios error"));
|
|
8
8
|
console.log(helper.time());
|
|
9
9
|
await helper.sleepMs(1000);
|
|
10
10
|
console.log(helper.time());
|
|
@@ -26,8 +26,12 @@ import axios from "axios"
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}));
|
|
29
|
-
console.log("
|
|
30
|
-
console.log("
|
|
29
|
+
console.log("str empty", helper.checkEmpty(""));
|
|
30
|
+
console.log("1 empty", helper.checkEmpty(1));
|
|
31
|
+
console.log("[] empty", helper.checkEmpty([]));
|
|
32
|
+
console.log(helper.upperCaseFirst("first letter upper"));
|
|
33
|
+
console.log(helper.lowerCaseFirst("First Letter Lower"));
|
|
34
|
+
console.log(helper.titleString("THIS mUsT be Title"));
|
|
31
35
|
console.log(helper.limitString("LONG TEXT", 7));
|
|
32
36
|
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
33
37
|
console.log(helper.randomString(32, true, true));
|