melperjs 1.0.0 → 2.0.1
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 +14 -0
- package/README.md +43 -30
- package/lib/index.js +162 -0
- package/lib/node.js +193 -0
- package/package.json +19 -6
- package/src/index.js +62 -212
- package/src/node.js +192 -0
- package/test/script.js +21 -15
package/.babelrc
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# MELPERJS
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Node.js module to use predefined common functions and utilities
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -13,12 +13,15 @@ npm i melperjs
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```javascript
|
|
16
|
+
// import * as helper from "melperjs";
|
|
17
|
+
// import * as nodeHelper from "melperjs/node";
|
|
18
|
+
// import axios from "axios";
|
|
16
19
|
const helper = require("melperjs");
|
|
17
|
-
|
|
20
|
+
const nodeHelper = require("melperjs/node");
|
|
21
|
+
const axios = require("axios");
|
|
18
22
|
|
|
19
23
|
(async () => {
|
|
20
|
-
console.log(
|
|
21
|
-
console.log(helper.exception("something went wrong"));
|
|
24
|
+
console.log(helper.Exception("something went wrong"));
|
|
22
25
|
console.log(helper.time());
|
|
23
26
|
await helper.sleepMs(1000);
|
|
24
27
|
console.log(helper.time());
|
|
@@ -44,50 +47,61 @@ const helper = require("melperjs");
|
|
|
44
47
|
console.log("[] empty", helper.isEmpty([]));
|
|
45
48
|
console.log(helper.limitString("LONG TEXT", 7));
|
|
46
49
|
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
50
|
+
console.log(helper.randomString(32, true, true));
|
|
51
|
+
console.log(helper.randomHex(8));
|
|
52
|
+
console.log(helper.randomUuid(true));
|
|
47
53
|
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
48
|
-
console.log(
|
|
49
|
-
console.log(
|
|
54
|
+
console.log(nodeHelper.tokenString(32, true, true));
|
|
55
|
+
console.log(nodeHelper.tokenHex(8));
|
|
56
|
+
console.log(nodeHelper.tokenUuid(true));
|
|
57
|
+
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
58
|
+
console.log(nodeHelper.md5("data"));
|
|
50
59
|
const password = helper.hashBcrypt("plain");
|
|
51
60
|
console.log(password)
|
|
52
61
|
console.log("passwordHash", helper.verifyBcrypt("plain", password));
|
|
53
62
|
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
54
63
|
console.log(cookies);
|
|
55
64
|
console.log(helper.cookieHeader(cookies));
|
|
56
|
-
const proxy =
|
|
65
|
+
const proxy = nodeHelper.formatProxy("127.0.0.1:8080:id:pw-{SESSION}");
|
|
57
66
|
console.log(proxy);
|
|
58
|
-
console.log(
|
|
59
|
-
console.log(await
|
|
60
|
-
console.log(
|
|
61
|
-
console.log("HTTP CODE: 401 FOREIGN", helper.
|
|
62
|
-
console.log("HTTP CODE:
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
console.log("VERSIONED BY .GIT", "v" +
|
|
67
|
+
console.log(nodeHelper.proxyObject(proxy));
|
|
68
|
+
console.log(await nodeHelper.proxify({mode: 4, proxy}));
|
|
69
|
+
console.log(nodeHelper.serverIp());
|
|
70
|
+
console.log("HTTP CODE: 401 FOREIGN", helper.foreignHttpError(401));
|
|
71
|
+
console.log("HTTP CODE: 407 FOREIGN (Failed Proxy Auth)", helper.foreignHttpError(407));
|
|
72
|
+
nodeHelper.createNumDir("test");
|
|
73
|
+
nodeHelper.createDir("test");
|
|
74
|
+
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
66
75
|
})();
|
|
67
76
|
|
|
68
77
|
/*
|
|
69
|
-
ENDSWITH TH true
|
|
70
78
|
{ message: 'something went wrong', response: { status: 400 } }
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
1700522762
|
|
80
|
+
1700522763
|
|
81
|
+
1700522764
|
|
74
82
|
Promise timed out after 1000ms
|
|
75
83
|
{ x: 1, y: 2, c: { d: true } }
|
|
76
84
|
1 empty false
|
|
77
85
|
[] empty true
|
|
78
86
|
LONG...
|
|
79
87
|
SAFE TEXT
|
|
88
|
+
i9ULmdbboTDL7NPgJRiY1i6O1x0D71wG
|
|
89
|
+
91d59653
|
|
90
|
+
50fe22be-a4e9-46cd-b73b-da5669ca43b4
|
|
91
|
+
strongProbability
|
|
92
|
+
kpG7GBnGf8kK2JGwSNe8EaIdkrKl5OEr
|
|
93
|
+
4f592b6e
|
|
94
|
+
e7947cc3-d975-446c-82ec-fc77d84a67bb
|
|
80
95
|
strongProbability
|
|
81
|
-
16638f79
|
|
82
96
|
8d777f385d3dfec8815d20f7496026dc
|
|
83
|
-
$2b$10$
|
|
97
|
+
$2b$10$m3gbDZMz6P/NKtOBtcJPTe5Dt9TtGaFVE5M.F07QR4bLHoKuezfpm
|
|
84
98
|
passwordHash true
|
|
85
99
|
{
|
|
86
|
-
'1P_JAR': '2023-
|
|
87
|
-
AEC: '
|
|
88
|
-
NID: '511=
|
|
100
|
+
'1P_JAR': '2023-11-20-23',
|
|
101
|
+
AEC: 'Ackid1QUUwFH8ZlfrHR1RFoG3nbxsOvmYckU_mnIw4Z6cE4ue9_mbng2mmU',
|
|
102
|
+
NID: '511=m55MFOJ7XlxlkanJtm0q7eK5PkaePFQiaxEeMcAR_ojuPBWZ0bqw6tbDq4VGuT2CrTNgGWw_lLECx100Qlm1bRBNYEx_0wWtVNCh582q9-zhvQFSDbAI3vDtl-vMlMbpv_6JYJ7YBhGf1S6zbqv6Q84hlbLBOIg8Dt633-b5b94'
|
|
89
103
|
}
|
|
90
|
-
1P_JAR=2023-
|
|
104
|
+
1P_JAR=2023-11-20-23;AEC=Ackid1QUUwFH8ZlfrHR1RFoG3nbxsOvmYckU_mnIw4Z6cE4ue9_mbng2mmU;NID=511=m55MFOJ7XlxlkanJtm0q7eK5PkaePFQiaxEeMcAR_ojuPBWZ0bqw6tbDq4VGuT2CrTNgGWw_lLECx100Qlm1bRBNYEx_0wWtVNCh582q9-zhvQFSDbAI3vDtl-vMlMbpv_6JYJ7YBhGf1S6zbqv6Q84hlbLBOIg8Dt633-b5b94
|
|
91
105
|
http://id:pw-{SESSION}@127.0.0.1:8080
|
|
92
106
|
{
|
|
93
107
|
protocol: 'http',
|
|
@@ -95,12 +109,11 @@ http://id:pw-{SESSION}@127.0.0.1:8080
|
|
|
95
109
|
port: 8080,
|
|
96
110
|
auth: { username: 'id', password: 'pw-{SESSION}' }
|
|
97
111
|
}
|
|
98
|
-
http://id:pw-
|
|
99
|
-
|
|
112
|
+
http://id:pw-06cbcd75@127.0.0.1:8080
|
|
113
|
+
127.0.0.1
|
|
100
114
|
HTTP CODE: 401 FOREIGN false
|
|
101
|
-
HTTP CODE:
|
|
102
|
-
|
|
103
|
-
VERSIONED BY .GIT v2310.151313
|
|
115
|
+
HTTP CODE: 407 FOREIGN (Failed Proxy Auth) true
|
|
116
|
+
VERSIONED BY .GIT v2310.15182
|
|
104
117
|
*/
|
|
105
118
|
```
|
|
106
119
|
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Exception = Exception;
|
|
7
|
+
exports.cookieDict = cookieDict;
|
|
8
|
+
exports.cookieHeader = cookieHeader;
|
|
9
|
+
exports.findKeyNode = findKeyNode;
|
|
10
|
+
exports.foreignHttpError = foreignHttpError;
|
|
11
|
+
exports.hashBcrypt = hashBcrypt;
|
|
12
|
+
exports.isEmpty = isEmpty;
|
|
13
|
+
exports.limitString = limitString;
|
|
14
|
+
exports.promiseTimeout = promiseTimeout;
|
|
15
|
+
exports.randomHex = randomHex;
|
|
16
|
+
exports.randomString = randomString;
|
|
17
|
+
exports.randomUuid = randomUuid;
|
|
18
|
+
exports.randomWeighted = randomWeighted;
|
|
19
|
+
exports.safeString = safeString;
|
|
20
|
+
exports.sleep = sleep;
|
|
21
|
+
exports.sleepMs = sleepMs;
|
|
22
|
+
exports.time = time;
|
|
23
|
+
exports.verifyBcrypt = verifyBcrypt;
|
|
24
|
+
var _ = _interopRequireWildcard(require("lodash"));
|
|
25
|
+
var _xss = _interopRequireDefault(require("xss"));
|
|
26
|
+
var _bcrypt = _interopRequireDefault(require("bcrypt"));
|
|
27
|
+
var _setCookieParser = _interopRequireDefault(require("set-cookie-parser"));
|
|
28
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
+
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); }
|
|
30
|
+
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; }
|
|
31
|
+
function Exception(message, response = {}) {
|
|
32
|
+
response.status = response.status || 400;
|
|
33
|
+
return {
|
|
34
|
+
message: message,
|
|
35
|
+
response: response
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function time() {
|
|
39
|
+
return Math.floor(Date.now() / 1000);
|
|
40
|
+
}
|
|
41
|
+
async function sleepMs(milliseconds) {
|
|
42
|
+
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
43
|
+
}
|
|
44
|
+
async function sleep(seconds) {
|
|
45
|
+
return await sleepMs(seconds * 1000);
|
|
46
|
+
}
|
|
47
|
+
function promiseTimeout(milliseconds, promise) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
51
|
+
}, milliseconds);
|
|
52
|
+
promise.then(value => {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
resolve(value);
|
|
55
|
+
}).catch(reason => {
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
reject(reason);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
function findKeyNode(key, node, pair = null) {
|
|
62
|
+
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
63
|
+
return node;
|
|
64
|
+
} else if (typeof node === 'object') {
|
|
65
|
+
for (let index in node) {
|
|
66
|
+
const result = findKeyNode(key, node[index], pair);
|
|
67
|
+
if (result) {
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
function isEmpty(value) {
|
|
75
|
+
if (typeof value === "number") {
|
|
76
|
+
return value === 0;
|
|
77
|
+
} else {
|
|
78
|
+
return _.isEmpty(value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
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
|
+
function safeString(source) {
|
|
90
|
+
return (0, _xss.default)(source, {
|
|
91
|
+
whiteList: {},
|
|
92
|
+
stripIgnoreTag: true,
|
|
93
|
+
stripIgnoreTagBody: ["script"]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function randomString(length, useNumbers = true, useUppercase = false) {
|
|
97
|
+
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
98
|
+
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
99
|
+
const numbers = '0123456789';
|
|
100
|
+
let characters = lowercaseChars;
|
|
101
|
+
if (useUppercase) characters += uppercaseChars;
|
|
102
|
+
if (useNumbers) characters += numbers;
|
|
103
|
+
let randomString = '';
|
|
104
|
+
for (let i = 0; i < length; i++) {
|
|
105
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
106
|
+
randomString += characters[randomIndex];
|
|
107
|
+
}
|
|
108
|
+
return randomString;
|
|
109
|
+
}
|
|
110
|
+
function randomHex(length) {
|
|
111
|
+
let result = '';
|
|
112
|
+
const characters = '0123456789abcdef';
|
|
113
|
+
for (let i = 0; i < length; i++) {
|
|
114
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
115
|
+
result += characters[randomIndex];
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
function randomUuid(useDashes = true) {
|
|
120
|
+
let d = Date.now();
|
|
121
|
+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
122
|
+
const r = (d + Math.random() * 16) % 16 | 0;
|
|
123
|
+
d = Math.floor(d / 16);
|
|
124
|
+
return (c === 'x' ? r : r & 0x3 | 0x8).toString(16);
|
|
125
|
+
});
|
|
126
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
127
|
+
}
|
|
128
|
+
function randomWeighted(dict, randomFunc = totalWeight => Math.random() * totalWeight) {
|
|
129
|
+
let elements = Object.keys(dict);
|
|
130
|
+
let weights = Object.values(dict);
|
|
131
|
+
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
132
|
+
let randomNum = randomFunc(totalWeight);
|
|
133
|
+
let weightSum = 0;
|
|
134
|
+
for (let i = 0; i < elements.length; i++) {
|
|
135
|
+
weightSum += weights[i];
|
|
136
|
+
if (randomNum <= weightSum) {
|
|
137
|
+
return elements[i];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function hashBcrypt(plainText) {
|
|
142
|
+
return _bcrypt.default.hashSync(plainText, _bcrypt.default.genSaltSync(10));
|
|
143
|
+
}
|
|
144
|
+
function verifyBcrypt(plainText, hash) {
|
|
145
|
+
return _bcrypt.default.compareSync(plainText, hash);
|
|
146
|
+
}
|
|
147
|
+
function cookieDict(res, decodeValues = false) {
|
|
148
|
+
let dict = {};
|
|
149
|
+
const cookies = _setCookieParser.default.parse(res, {
|
|
150
|
+
decodeValues: decodeValues
|
|
151
|
+
});
|
|
152
|
+
for (let cookie of cookies) {
|
|
153
|
+
dict[cookie.name] = cookie.value;
|
|
154
|
+
}
|
|
155
|
+
return dict;
|
|
156
|
+
}
|
|
157
|
+
function cookieHeader(cookieDict) {
|
|
158
|
+
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
159
|
+
}
|
|
160
|
+
function foreignHttpError(httpCode) {
|
|
161
|
+
return httpCode === undefined || httpCode === null || httpCode === 0 || httpCode === 402 || httpCode === 407 || httpCode === 466 || 500 <= httpCode;
|
|
162
|
+
}
|
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
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Nodejs module to use predefined common functions and utilities",
|
|
5
|
-
"main": "src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "node test/script.js"
|
|
8
|
-
},
|
|
9
5
|
"keywords": [
|
|
10
6
|
"helper",
|
|
11
7
|
"mixins",
|
|
@@ -19,12 +15,29 @@
|
|
|
19
15
|
},
|
|
20
16
|
"author": "Mahmuthan Elbir",
|
|
21
17
|
"license": "MIT",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./lib/index.js",
|
|
20
|
+
"./node": "./lib/node.js"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "babel src --out-dir lib",
|
|
24
|
+
"test": "node test/script.js"
|
|
25
|
+
},
|
|
22
26
|
"dependencies": {
|
|
23
|
-
"axios": "^1.
|
|
27
|
+
"axios": "^1.6.2",
|
|
24
28
|
"bcrypt": "^5.1.1",
|
|
25
29
|
"hpagent": "^1.2.0",
|
|
26
30
|
"lodash": "^4.17.21",
|
|
27
31
|
"set-cookie-parser": "^2.6.0",
|
|
28
32
|
"xss": "^1.0.14"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@babel/cli": "^7.23.4",
|
|
36
|
+
"@babel/core": "^7.23.3",
|
|
37
|
+
"@babel/preset-env": "^7.23.3",
|
|
38
|
+
"babel-loader": "^9.1.3",
|
|
39
|
+
"terser-webpack-plugin": "^5.3.9",
|
|
40
|
+
"webpack": "^5.89.0",
|
|
41
|
+
"webpack-cli": "^5.1.4"
|
|
29
42
|
}
|
|
30
43
|
}
|
package/src/index.js
CHANGED
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const {execSync} = require('child_process');
|
|
1
|
+
import * as _ from "lodash";
|
|
2
|
+
import xss from "xss";
|
|
3
|
+
import bcrypt from "bcrypt";
|
|
4
|
+
import setCookieParser from "set-cookie-parser";
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
const xss = require('xss');
|
|
9
|
-
const axios = require('axios');
|
|
10
|
-
const bcrypt = require('bcrypt');
|
|
11
|
-
const setCookieParser = require('set-cookie-parser');
|
|
12
|
-
const {HttpsProxyAgent} = require('hpagent');
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function exception(message, response = {}) {
|
|
6
|
+
export function Exception(message, response = {}) {
|
|
16
7
|
response.status = response.status || 400;
|
|
17
8
|
return {
|
|
18
9
|
message: message,
|
|
@@ -20,19 +11,19 @@ function exception(message, response = {}) {
|
|
|
20
11
|
}
|
|
21
12
|
}
|
|
22
13
|
|
|
23
|
-
function time() {
|
|
14
|
+
export function time() {
|
|
24
15
|
return Math.floor(Date.now() / 1000);
|
|
25
16
|
}
|
|
26
17
|
|
|
27
|
-
async function sleepMs(milliseconds) {
|
|
18
|
+
export async function sleepMs(milliseconds) {
|
|
28
19
|
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
29
20
|
}
|
|
30
21
|
|
|
31
|
-
async function sleep(seconds) {
|
|
22
|
+
export async function sleep(seconds) {
|
|
32
23
|
return await sleepMs(seconds * 1000);
|
|
33
24
|
}
|
|
34
25
|
|
|
35
|
-
function promiseTimeout(milliseconds, promise) {
|
|
26
|
+
export function promiseTimeout(milliseconds, promise) {
|
|
36
27
|
return new Promise((resolve, reject) => {
|
|
37
28
|
const timer = setTimeout(() => {
|
|
38
29
|
reject(new Error('Promise timed out after ' + milliseconds + 'ms'));
|
|
@@ -50,7 +41,7 @@ function promiseTimeout(milliseconds, promise) {
|
|
|
50
41
|
});
|
|
51
42
|
}
|
|
52
43
|
|
|
53
|
-
function findKeyNode(key, node, pair = null) {
|
|
44
|
+
export function findKeyNode(key, node, pair = null) {
|
|
54
45
|
if (node && node.hasOwnProperty(key) && (pair ? node[key] === pair : true)) {
|
|
55
46
|
return node;
|
|
56
47
|
} else if (typeof node === 'object') {
|
|
@@ -64,7 +55,7 @@ function findKeyNode(key, node, pair = null) {
|
|
|
64
55
|
return null;
|
|
65
56
|
}
|
|
66
57
|
|
|
67
|
-
function isEmpty(value) {
|
|
58
|
+
export function isEmpty(value) {
|
|
68
59
|
if (typeof value === "number") {
|
|
69
60
|
return value === 0;
|
|
70
61
|
} else {
|
|
@@ -72,7 +63,7 @@ function isEmpty(value) {
|
|
|
72
63
|
}
|
|
73
64
|
}
|
|
74
65
|
|
|
75
|
-
function limitString(str, limit = 35) {
|
|
66
|
+
export function limitString(str, limit = 35) {
|
|
76
67
|
str = str || "";
|
|
77
68
|
if (str.length <= limit) {
|
|
78
69
|
return str;
|
|
@@ -81,7 +72,7 @@ function limitString(str, limit = 35) {
|
|
|
81
72
|
}
|
|
82
73
|
}
|
|
83
74
|
|
|
84
|
-
function safeString(source) {
|
|
75
|
+
export function safeString(source) {
|
|
85
76
|
return xss(source, {
|
|
86
77
|
whiteList: {},
|
|
87
78
|
stripIgnoreTag: true,
|
|
@@ -89,16 +80,53 @@ function safeString(source) {
|
|
|
89
80
|
});
|
|
90
81
|
}
|
|
91
82
|
|
|
92
|
-
function
|
|
83
|
+
export function randomString(length, useNumbers = true, useUppercase = false) {
|
|
84
|
+
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
|
|
85
|
+
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
86
|
+
const numbers = '0123456789';
|
|
87
|
+
|
|
88
|
+
let characters = lowercaseChars;
|
|
89
|
+
if (useUppercase) characters += uppercaseChars;
|
|
90
|
+
if (useNumbers) characters += numbers;
|
|
91
|
+
|
|
92
|
+
let randomString = '';
|
|
93
|
+
for (let i = 0; i < length; i++) {
|
|
94
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
95
|
+
randomString += characters[randomIndex];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return randomString;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function randomHex(length) {
|
|
102
|
+
let result = '';
|
|
103
|
+
const characters = '0123456789abcdef';
|
|
104
|
+
|
|
105
|
+
for (let i = 0; i < length; i++) {
|
|
106
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
107
|
+
result += characters[randomIndex];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function randomUuid(useDashes = true) {
|
|
114
|
+
let d = Date.now();
|
|
115
|
+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
116
|
+
const r = (d + Math.random() * 16) % 16 | 0;
|
|
117
|
+
d = Math.floor(d / 16);
|
|
118
|
+
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
|
|
119
|
+
});
|
|
120
|
+
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function randomWeighted(dict, randomFunc = (totalWeight) => Math.random() * totalWeight) {
|
|
93
124
|
let elements = Object.keys(dict);
|
|
94
125
|
let weights = Object.values(dict);
|
|
95
126
|
|
|
96
|
-
let totalWeight = 0;
|
|
97
|
-
for (let weight of weights) {
|
|
98
|
-
totalWeight += weight;
|
|
99
|
-
}
|
|
127
|
+
let totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
|
|
100
128
|
|
|
101
|
-
let randomNum =
|
|
129
|
+
let randomNum = randomFunc(totalWeight);
|
|
102
130
|
let weightSum = 0;
|
|
103
131
|
|
|
104
132
|
for (let i = 0; i < elements.length; i++) {
|
|
@@ -109,25 +137,15 @@ function randomWeighted(dict) {
|
|
|
109
137
|
}
|
|
110
138
|
}
|
|
111
139
|
|
|
112
|
-
function
|
|
113
|
-
return crypto.randomBytes(Math.ceil(length / 2))
|
|
114
|
-
.toString('hex')
|
|
115
|
-
.slice(0, length);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function md5(data) {
|
|
119
|
-
return crypto.createHash('md5').update(data).digest("hex");
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function hashBcrypt(plainText) {
|
|
140
|
+
export function hashBcrypt(plainText) {
|
|
123
141
|
return bcrypt.hashSync(plainText, bcrypt.genSaltSync(10));
|
|
124
142
|
}
|
|
125
143
|
|
|
126
|
-
function verifyBcrypt(plainText, hash) {
|
|
144
|
+
export function verifyBcrypt(plainText, hash) {
|
|
127
145
|
return bcrypt.compareSync(plainText, hash);
|
|
128
146
|
}
|
|
129
147
|
|
|
130
|
-
function cookieDict(res, decodeValues = false) {
|
|
148
|
+
export function cookieDict(res, decodeValues = false) {
|
|
131
149
|
let dict = {};
|
|
132
150
|
const cookies = setCookieParser.parse(res, {decodeValues: decodeValues});
|
|
133
151
|
for (let cookie of cookies) {
|
|
@@ -136,119 +154,13 @@ function cookieDict(res, decodeValues = false) {
|
|
|
136
154
|
return dict;
|
|
137
155
|
}
|
|
138
156
|
|
|
139
|
-
function cookieHeader(cookieDict) {
|
|
157
|
+
export function cookieHeader(cookieDict) {
|
|
140
158
|
return Object.entries(cookieDict)
|
|
141
159
|
.map(([key, value]) => `${key}=${value}`)
|
|
142
160
|
.join(';')
|
|
143
161
|
}
|
|
144
162
|
|
|
145
|
-
function
|
|
146
|
-
proxy = proxy.trim();
|
|
147
|
-
const splitByProtocol = proxy.split("://");
|
|
148
|
-
if (1 < splitByProtocol.length)
|
|
149
|
-
protocol = splitByProtocol[0];
|
|
150
|
-
proxy = splitByProtocol[splitByProtocol.length - 1];
|
|
151
|
-
if (!proxy.includes("@")) {
|
|
152
|
-
const proxyParts = proxy.split(":");
|
|
153
|
-
if (4 <= proxyParts.length) {
|
|
154
|
-
proxy = `${proxyParts[proxyParts.length - 2]}:${proxyParts[proxyParts.length - 1]}@`;
|
|
155
|
-
proxyParts.pop();
|
|
156
|
-
proxyParts.pop();
|
|
157
|
-
proxy += proxyParts.join(":");
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
const proxyParts = proxy.split(':');
|
|
161
|
-
const proxyEnd = parseInt(proxyParts[proxyParts.length - 1]);
|
|
162
|
-
const proxyStart = proxyParts[proxyParts.length - 2];
|
|
163
|
-
if (!proxyStart.includes(".")) {
|
|
164
|
-
proxyParts.pop();
|
|
165
|
-
proxyParts[proxyParts.length - 1] = crypto.randomInt(parseInt(proxyStart), proxyEnd + 1).toString();
|
|
166
|
-
}
|
|
167
|
-
return protocol + "://" + proxyParts.join(':');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function proxyObject(...args) {
|
|
171
|
-
let proxy = formatProxy(...args);
|
|
172
|
-
const splitByProtocol = proxy.split('://');
|
|
173
|
-
const splitById = splitByProtocol[splitByProtocol.length - 1].split('@');
|
|
174
|
-
const splitByConn = splitById[splitById.length - 1].split(':');
|
|
175
|
-
proxy = {
|
|
176
|
-
protocol: splitByProtocol[0],
|
|
177
|
-
host: splitByConn[0],
|
|
178
|
-
port: parseInt(splitByConn[1]),
|
|
179
|
-
};
|
|
180
|
-
if (1 < splitById.length) {
|
|
181
|
-
const splitByAuth = splitById[0].split(':');
|
|
182
|
-
proxy.auth = {
|
|
183
|
-
username: splitByAuth[0],
|
|
184
|
-
password: splitByAuth[1]
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
return proxy;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
async function proxify(proxyConfig, callback = formatProxy) {
|
|
191
|
-
proxyConfig = proxyConfig || {};
|
|
192
|
-
const timeout = 7000;
|
|
193
|
-
if (proxyConfig.mode === 1) {
|
|
194
|
-
return callback(proxyConfig.proxy);
|
|
195
|
-
} else if (proxyConfig.mode === 2) {
|
|
196
|
-
const proxy = callback(proxyConfig.proxy);
|
|
197
|
-
try {
|
|
198
|
-
await axios.get(proxyConfig.resetApi, {timeout});
|
|
199
|
-
} catch {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
await sleep(5);
|
|
203
|
-
for (let i = 0; i < 5; i++) {
|
|
204
|
-
try {
|
|
205
|
-
const res = await axios.get("https://api64.ipify.org", {
|
|
206
|
-
timeout,
|
|
207
|
-
httpsAgent: new HttpsProxyAgent({proxy, timeout: 7000})
|
|
208
|
-
});
|
|
209
|
-
if (res.status === 200)
|
|
210
|
-
return proxy;
|
|
211
|
-
} catch {
|
|
212
|
-
await sleep(3);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
} else if (proxyConfig.mode === 3) {
|
|
216
|
-
try {
|
|
217
|
-
const res = await axios.get(proxyConfig.resetApi, {timeout});
|
|
218
|
-
if (res.status === 200)
|
|
219
|
-
return callback(proxyConfig.proxy);
|
|
220
|
-
} catch {
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
} else if (proxyConfig.mode === 4) {
|
|
224
|
-
return callback(proxyConfig.proxy).replace("{SESSION}", tokenHex(8));
|
|
225
|
-
} else if (proxyConfig.mode === 5) {
|
|
226
|
-
try {
|
|
227
|
-
const lines = proxyConfig.proxy.split("\n");
|
|
228
|
-
const line = lines[crypto.randomInt(0, lines.length)];
|
|
229
|
-
return callback(line);
|
|
230
|
-
} catch {
|
|
231
|
-
return false;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
return null;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function serverIP() {
|
|
239
|
-
const interfaces = networkInterfaces();
|
|
240
|
-
for (const devName in interfaces) {
|
|
241
|
-
const interfaceValue = interfaces[devName];
|
|
242
|
-
for (let i = 0; i < interfaceValue.length; i++) {
|
|
243
|
-
const alias = interfaceValue[i];
|
|
244
|
-
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
|
|
245
|
-
return alias.address;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
return '127.0.0.1';
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function foreignError(httpCode) {
|
|
163
|
+
export function foreignHttpError(httpCode) {
|
|
252
164
|
return (
|
|
253
165
|
httpCode === undefined ||
|
|
254
166
|
httpCode === null ||
|
|
@@ -258,66 +170,4 @@ function foreignError(httpCode) {
|
|
|
258
170
|
httpCode === 466 ||
|
|
259
171
|
500 <= httpCode
|
|
260
172
|
);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function createDir(directory) {
|
|
264
|
-
if (!fs.existsSync(directory)) {
|
|
265
|
-
fs.mkdirSync(directory, {recursive: true});
|
|
266
|
-
return true;
|
|
267
|
-
}
|
|
268
|
-
return null;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function createNumDir(mainDirectory) {
|
|
272
|
-
if (createDir(mainDirectory)) {
|
|
273
|
-
for (let i = 0; i <= 9; i++) {
|
|
274
|
-
fs.mkdir(path.join(mainDirectory, i.toString()), (err) => {
|
|
275
|
-
if (err)
|
|
276
|
-
console.error(err);
|
|
277
|
-
});
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
function getVersion() {
|
|
283
|
-
try {
|
|
284
|
-
const date = new Date(execSync('git show -s --format=%ci HEAD').toString().trim());
|
|
285
|
-
const formatDatePart = (value) => value.toString().padStart(2, '0');
|
|
286
|
-
const year = date.getFullYear().toString().slice(-2);
|
|
287
|
-
const month = formatDatePart(date.getMonth() + 1);
|
|
288
|
-
const day = formatDatePart(date.getDate());
|
|
289
|
-
const hour = formatDatePart(date.getHours());
|
|
290
|
-
const minute = formatDatePart(date.getMinutes());
|
|
291
|
-
return parseFloat(`${year}${month}.${day}${hour}${minute}`);
|
|
292
|
-
} catch {
|
|
293
|
-
return 1.0;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
module.exports = {
|
|
298
|
-
_,
|
|
299
|
-
exception,
|
|
300
|
-
time,
|
|
301
|
-
sleepMs,
|
|
302
|
-
sleep,
|
|
303
|
-
promiseTimeout,
|
|
304
|
-
findKeyNode,
|
|
305
|
-
isEmpty,
|
|
306
|
-
limitString,
|
|
307
|
-
safeString,
|
|
308
|
-
randomWeighted,
|
|
309
|
-
tokenHex,
|
|
310
|
-
md5,
|
|
311
|
-
hashBcrypt,
|
|
312
|
-
verifyBcrypt,
|
|
313
|
-
cookieDict,
|
|
314
|
-
cookieHeader,
|
|
315
|
-
formatProxy,
|
|
316
|
-
proxyObject,
|
|
317
|
-
proxify,
|
|
318
|
-
serverIP,
|
|
319
|
-
foreignError,
|
|
320
|
-
createDir,
|
|
321
|
-
createNumDir,
|
|
322
|
-
getVersion
|
|
323
|
-
};
|
|
173
|
+
}
|
package/src/node.js
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
}
|
package/test/script.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import * as helper from "../src/index.js"
|
|
2
|
+
import * as nodeHelper from "../src/node.js"
|
|
3
|
+
import axios from "axios"
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
(async () => {
|
|
6
|
-
console.log(
|
|
7
|
-
console.log(helper.exception("something went wrong"));
|
|
7
|
+
console.log(helper.Exception("something went wrong"));
|
|
8
8
|
console.log(helper.time());
|
|
9
9
|
await helper.sleepMs(1000);
|
|
10
10
|
console.log(helper.time());
|
|
@@ -30,23 +30,29 @@ const axios = require("axios");
|
|
|
30
30
|
console.log("[] empty", helper.isEmpty([]));
|
|
31
31
|
console.log(helper.limitString("LONG TEXT", 7));
|
|
32
32
|
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
33
|
+
console.log(helper.randomString(32, true, true));
|
|
34
|
+
console.log(helper.randomHex(8));
|
|
35
|
+
console.log(helper.randomUuid(true));
|
|
33
36
|
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
34
|
-
console.log(
|
|
35
|
-
console.log(
|
|
37
|
+
console.log(nodeHelper.tokenString(32, true, true));
|
|
38
|
+
console.log(nodeHelper.tokenHex(8));
|
|
39
|
+
console.log(nodeHelper.tokenUuid(true));
|
|
40
|
+
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
41
|
+
console.log(nodeHelper.md5("data"));
|
|
36
42
|
const password = helper.hashBcrypt("plain");
|
|
37
43
|
console.log(password)
|
|
38
44
|
console.log("passwordHash", helper.verifyBcrypt("plain", password));
|
|
39
45
|
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
40
46
|
console.log(cookies);
|
|
41
47
|
console.log(helper.cookieHeader(cookies));
|
|
42
|
-
const proxy =
|
|
48
|
+
const proxy = nodeHelper.formatProxy("127.0.0.1:8080:id:pw-{SESSION}");
|
|
43
49
|
console.log(proxy);
|
|
44
|
-
console.log(
|
|
45
|
-
console.log(await
|
|
46
|
-
console.log(
|
|
47
|
-
console.log("HTTP CODE: 401 FOREIGN", helper.
|
|
48
|
-
console.log("HTTP CODE:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
console.log("VERSIONED BY .GIT", "v" +
|
|
50
|
+
console.log(nodeHelper.proxyObject(proxy));
|
|
51
|
+
console.log(await nodeHelper.proxify({mode: 4, proxy}));
|
|
52
|
+
console.log(nodeHelper.serverIp());
|
|
53
|
+
console.log("HTTP CODE: 401 FOREIGN", helper.foreignHttpError(401));
|
|
54
|
+
console.log("HTTP CODE: 407 FOREIGN (Failed Proxy Auth)", helper.foreignHttpError(407));
|
|
55
|
+
nodeHelper.createNumDir("test");
|
|
56
|
+
nodeHelper.createDir("test");
|
|
57
|
+
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
52
58
|
})();
|