forked from smlie-cyber/faker2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
280 lines (280 loc) · 8.18 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
let request = require('request');
let CryptoJS = require('crypto-js');
let qs = require('querystring');
let urls = require('url');
let path = require('path');
let notify = require('./sendNotify');
let mainEval = require("./eval");
let assert = require('assert');
let jxAlgo = require("./jxAlgo");
let config = require("./config");
let user = {}
try {
user = require("./user")
} catch (e) {}
class env {
constructor(name) {
this.config = { ...config,
...process.env,
...user,
};
this.name = name;
this.message = [];
this.sharecode = [];
this.code = [];
this.timestamp = new Date().getTime();
this.time = this.start = parseInt(this.timestamp / 1000);
this.options = {
'headers': {}
};
console.log(`\n🔔${this.name}, 开始!\n`)
console.log(`=========== 脚本执行-北京时间(UTC+8):${new Date(new Date().getTime() + new Date().getTimezoneOffset()*60*1000 + 8*60*60*1000).toLocaleString()} ===========\n`)
}
done() {
let timestamp = new Date().getTime();
let work = ((timestamp - this.timestamp) / 1000).toFixed(2)
console.log(`=========================脚本执行完成,耗时${work}s============================\n`)
console.log(`🔔${this.name}, 结束!\n`)
}
notify(array) {
let text = [];
let type = 0
for (let i of array) {
text.push(`${i.user} -- ${i.msg}`)
type = i.type
}
console.log(`\n=============================开始发送提醒消息=============================`)
if (type == 1) {
for (let i of text) {
notify.sendNotify(this.name + "消息提醒", i)
}
} else {
notify.sendNotify(this.name + "消息提醒", text.join('\n'))
}
}
wait(t) {
return new Promise(e => setTimeout(e, t))
}
setOptions(params) {
this.options = params;
}
setCookie(cookie) {
this.options.headers.cookie = cookie
}
jsonParse(str) {
try {
return JSON.parse(str);
} catch (e) {
try {
let data = this.match([/try\s*\{\w+\s*\(([^\)]+)/, /\w+\s*\(([^\)]+)/], str)
return JSON.parse(data);
} catch (ee) {
try {
let cb = this.match(/try\s*\{\s*(\w+)/, str)
if (cb) {
let func = "";
let data = str.replace(cb, `func=`)
eval(data);
return func
}
} catch (eee) {
return str
}
}
}
}
curl(params, extra = '') {
if (typeof(params) != 'object') {
params = {
'url': params
}
}
params = Object.assign({ ...this.options
}, params);
params.method = params.body ? 'POST' : 'GET';
if (params.hasOwnProperty('cookie')) {
params.headers.cookie = params.cookie
}
if (params.hasOwnProperty('ua') || params.hasOwnProperty('useragent')) {
params.headers['user-agent'] = params.ua
}
if (params.hasOwnProperty('referer')) {
params.headers.referer = params.referer
}
if (params.hasOwnProperty('params')) {
params.url += '?' + qs.stringify(params.params)
}
if (params.hasOwnProperty('form')) {
params.method = 'POST'
}
return new Promise(resolve => {
request(params, async (err, resp, data) => {
try {
if (params.console) {
console.log(data)
}
this.source = this.jsonParse(data);
if (extra) {
this[extra] = this.source
}
} catch (e) {
console.log(e, resp)
} finally {
resolve(data);
}
})
})
}
dumps(dict) {
return JSON.stringify(dict)
}
loads(str) {
return JSON.parse(str)
}
notice(msg, type = 0) {
this.message.push({
'index': this.index,
'user': this.user,
'msg': msg,
type
})
}
notices(msg, user, type = 0) {
this.message.push({
'user': user,
'msg': msg,
// 'index': index,
type
})
}
urlparse(url) {
return urls.parse(url, true, true)
}
md5(encryptString) {
return CryptoJS.MD5(encryptString).toString()
}
haskey(data, key, value) {
value = typeof value !== 'undefined' ? value : '';
var spl = key.split('.');
for (var i of spl) {
i = !isNaN(i) ? parseInt(i) : i;
try {
data = data[i];
} catch (error) {
return '';
}
}
if (data == undefined) {
return ''
}
if (value !== '') {
return data === value ? true : false;
} else {
return data
}
}
match(pattern, string) {
pattern = (pattern instanceof Array) ? pattern : [pattern];
for (let pat of pattern) {
// var match = string.match(pat);
var match = pat.exec(string)
if (match) {
var len = match.length;
if (len == 1) {
return match;
} else if (len == 2) {
return match[1];
} else {
var r = [];
for (let i = 1; i < len; i++) {
r.push(match[i])
}
return r;
}
break;
}
// console.log(pat.exec(string))
}
return '';
}
matchall(pattern, string) {
pattern = (pattern instanceof Array) ? pattern : [pattern];
var match;
var result = [];
for (var pat of pattern) {
while ((match = pat.exec(string)) != null) {
var len = match.length;
if (len == 1) {
result.push(match);
} else if (len == 2) {
result.push(match[1]);
} else {
var r = [];
for (let i = 1; i < len; i++) {
r.push(match[i])
}
result.push(r);
}
}
}
return result;
}
compare(property) {
return function(a, b) {
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
filename(file, rename = '') {
if (!this.runfile) {
this.runfile = path.basename(file).replace(".js", '').replace(/-/g, '_')
}
if (rename) {
rename = `_${rename}`;
}
return path.basename(file).replace(".js", rename).replace(/-/g, '_');
}
rand(n, m) {
var random = Math.floor(Math.random() * (m - n + 1) + n);
return random;
}
random(arr, num) {
var temp_array = new Array();
for (var index in arr) {
temp_array.push(arr[index]);
}
var return_array = new Array();
for (var i = 0; i < num; i++) {
if (temp_array.length > 0) {
var arrIndex = Math.floor(Math.random() * temp_array.length);
return_array[i] = temp_array[arrIndex];
temp_array.splice(arrIndex, 1);
} else {
break;
}
}
return return_array;
}
compact(lists, keys) {
let array = {};
for (let i of keys) {
if (lists[i]) {
array[i] = lists[i];
}
}
return array;
}
unique(arr) {
return Array.from(new Set(arr));
}
end(args) {
return args[args.length - 1]
}
}
module.exports = {
env,
eval: mainEval,
assert,
jxAlgo,
}