forked from elecV2/elecV2P
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunJSFile.js
616 lines (582 loc) · 21.3 KB
/
runJSFile.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
const vm = require('vm')
const path = require('path')
const cheerio = require('cheerio')
const EventEmitter = require('events')
const { logger, feedAddItem, now, sType, sString, surlName, euid, errStack, downloadfile, Jsfile, file, wsSer, sParam, eAxios, sHash } = require('../utils')
const clog = new logger({ head: 'runJSFile', level: 'debug' })
const vmEvent = new EventEmitter()
vmEvent.on('error', err=>clog.error(errStack(err)))
const { context } = require('./context')
const { CONFIG, CONFIG_Port } = require('../config')
const CONFIG_RUNJS = {
timeout: 5000, // JS 运行时间。单位:毫秒
intervals: 86400, // 远程 JS 更新时间,单位:秒。 默认:86400(一天)。0: 有则不更新
numtofeed: 50, // 每运行 { numtofeed } 次 JS, 添加一个 Feed item。0: 不通知
jslogfile: true, // 是否将 JS 运行日志保存到 logs 文件夹
eaxioslog: false, // 打印/保存网络请求 url 到日志
proxy: true, // 是否应用网络请求设置中的代理(如有)
white: { // 白名单脚本。放行所有网络请求,不进行屏蔽检测
enable: false,
list: []
}
}
// 同步 CONFIG 数据
CONFIG.CONFIG_RUNJS = Object.assign(CONFIG_RUNJS, CONFIG.CONFIG_RUNJS)
const efhcache = new Map();
const scriptcache = new Map();
// 初始化脚本运行
if (CONFIG.init?.runjs) {
CONFIG.init.runjs.split(/ ?, ?|,/).filter(s=>s).forEach(js=>{
runJSFile(js, { from: 'initialization' })
})
}
// websocket/通知触发 JS
wsSer.recv.runjs = (data={})=>runJSFile(data.fn, data.addContext)
const runstatus = {
start: now(null, false),
times: CONFIG_RUNJS.numtofeed,
detail: {},
total: 0
}
/**
* JS 运行次数统计
* @param {string} filename JS 文件名
* @return {none}
*/
async function taskCount(filename) {
if (CONFIG_RUNJS.numtofeed === 0) {
clog.debug(filename, 'skip count run times by set')
return
}
if (/test/.test(filename)) {
clog.debug(filename, 'match key word: test, skip count run times')
return
}
if (runstatus.detail[filename]) {
runstatus.detail[filename]++
} else {
runstatus.detail[filename] = 1
}
runstatus.total++
runstatus.times--
wsSer.send({
type: 'jsrunstatus',
data: { total: runstatus.total, detail: runstatus.detail }
})
clog.debug('JS run status:', runstatus)
if (runstatus.times === 0) {
let des = []
for (let jsname in runstatus.detail) {
des.push(`${jsname}: ${runstatus.detail[jsname]}`)
}
runstatus.detail = {}
feedAddItem('run script ' + CONFIG_RUNJS.numtofeed + ' times', des.join(', ') + ` from ${runstatus.start}`)
runstatus.times = CONFIG_RUNJS.numtofeed
runstatus.start = now(null, false)
}
}
/**
* 远程文件 filename 是否需要更新
* @param {string} filename 文件名称
* @return {boolean} true or false
*/
function bOutDate(filename) {
return CONFIG_RUNJS.intervals > 0 && new Date().getTime() - Jsfile.get(filename, 'date') > CONFIG_RUNJS.intervals*1000;
}
/**
* efh 文件处理
* @param {string} filename efh 文件
* @param {object} options title: efh html 缺省 title
* @return {object} efh 文件处理结果 { html, script }
*/
async function efhParse(filename, { title='', type='', name } = {}) {
let efhc = { name: '', date: 0, html: '', script: '', type: '' };
if (type !== 'rawcode' && /^https?:\/\/\S{4}/.test(filename)) {
// 远程 efh 文件
let furl = filename.split(' ')[0];
filename = name || surlName(furl);
let efhfulpath = Jsfile.get(filename, 'path');
let efhIsExist = file.isExist(efhfulpath);
if (efhIsExist && type === 'local') {
clog.info('run', filename, 'locally');
} else if (!efhIsExist || bOutDate(filename)) {
clog.info('downloading', filename, 'from', furl);
try {
await downloadfile(furl, { name: efhfulpath });
clog.info(`success download ${filename}, ready to run`);
} catch(error) {
clog.error(`run ${furl}, error: ${error}`);
clog.info(`try to run ${filename} locally`);
}
}
} else {
if (type === 'rawcode') {
efhc.html = filename
filename = name || 'rawcode.efh'
} else if (name) {
filename = name;
}
}
// 本地 efh 文件,先判断 cache 是否存在,再处理内容
let tdate = type === 'rawcode' ? 0 : Jsfile.get(filename, 'date');
if (tdate && efhcache.has(filename)) {
efhc = efhcache.get(filename);
if (efhc.date === tdate) {
clog.info('run', filename, 'with cache');
} else {
// 非最新文件缓存,清空内容
efhc.date = tdate;
efhc.html = '';
efhc.script = '';
}
} else if (tdate) {
efhc.date = tdate;
efhcache.set(filename, efhc);
}
efhc.name = filename;
if (type === 'rawcode' || !efhc.html) {
let efhcont = efhc.html;
if (type !== 'rawcode') {
efhcont = Jsfile.get(filename);
}
if (!efhcont) {
efhc.html = filename + ' not exist';
clog.info(efhc.html);
} else {
clog.info('deal', filename, 'content');
let $ = cheerio.load(efhcont);
if (title && $('title').length === 0) {
$('head').append('<title>' + title + '</title>');
}
$('head').append(`<script>function $fend(key, data){if(!key) {let msg='a key for $fend is expect';alert(msg);return Promise.reject(msg)};return fetch('', {method: 'post',headers: {'Content-Type': 'application/json'},body: JSON.stringify({key, data})})};let $=(s,a='')=>a?document.querySelectorAll(s):document.querySelector(s);</script>`);
let bcode = $("script[runon='elecV2P']");
if (bcode.length === 0) {
bcode = $("script[runon='backend']");
}
if (bcode.length === 0) {
bcode = $("script[favend]")
}
if (bcode.attr('src')) {
// src 开头 /|./|空,即绝对/相对目录处理
efhc.script = bcode.attr('src');
if (efhc.script.startsWith('/')) {
efhc.script = efhc.script.replace('/', ''); // 仅替换开头/
} else if (!/^https?:\/\/\S{4}/.test(efhc.script)) {
// 非远程 src,则相对当前 efh 文件
let lastslash = filename.lastIndexOf('/');
if (lastslash === -1) {
efhc.script = path.join(efhc.script);
} else {
efhc.script = path.join(path.dirname(filename), efhc.script);
}
}
efhc.type = 'file';
} else {
efhc.script = bcode.html();
efhc.type = 'rawcode';
}
bcode.remove();
efhc.html = $.html();
}
}
return efhc;
}
/**
* JS 执行函数
* @param {string} filename JS 文件名
* @param {string} jscode JS 执行代码
* @param {object} addContext 附加环境变量 context
* @return {promise} JS 执行结果
*/
function runJS(filename, jscode, addContext={}) {
if (!filename || !jscode) {
clog.error('some script code are expect')
return Promise.resolve('no script code to run')
}
clog.notify('run', filename, 'from', addContext.from)
taskCount(filename)
let fconsole = null,
bGrant = false,
compatible = {
surge: false, // Surge 脚本调试模式
quanx: false, // Quanx 脚本调试模式。都为 false 时,会进行自动判断
nodejs: false, // nodejs 运行模式,不对脚本进行兼容判断
require: false // 启用 nodeJS require 函数。不开启时会自动进行判断
}
if (/^\/\/ +@grant/m.test(jscode)) {
bGrant = true
// compatible 判断
if (/^\/\/ +@grant +nodejs$/m.test(jscode)) {
compatible.nodejs = true
} else if (/^\/\/ +@grant +surge$/m.test(jscode)) {
compatible.surge = true
} else if (/^\/\/ +@grant +quanx$/m.test(jscode)) {
compatible.quanx = true
}
// require 可与以上模式同时存在
if (/^\/\/ +@grant +require$/m.test(jscode)) {
compatible.require = true
}
// 日志显示类型判断
if (/^\/\/ +@grant +(still|silent)$/m.test(jscode)) {
clog.notify('log of', filename, 'is disabled')
fconsole = { log(){},err(){},info(){},error(){},notify(){},debug(){},clear(){} }
} else if (/^\/\/ +@grant +calm$/m.test(jscode)) {
clog.notify('log of', filename, 'keep in file, but no stdout')
fconsole = new logger({ head: filename, level: 'error', file: CONFIG_RUNJS.jslogfile ? filename : false })
}
}
if (sType(fconsole) !== 'object') {
fconsole = new logger({ head: filename, level: 'debug', file: CONFIG_RUNJS.jslogfile ? filename : false, cb: addContext.cb })
}
const CONTEXT = new context({ fconsole, name: filename })
CONTEXT.final.__dirname = Jsfile.get(filename, 'dir')
CONTEXT.final.__filename = Jsfile.get(filename, 'path')
CONTEXT.final.__taskname = addContext.__taskname
CONTEXT.final.__taskid = addContext.__taskid
if (compatible.nodejs) {
fconsole.debug(filename, 'run in nodejs mode')
CONTEXT.final.module = module
CONTEXT.final.process = process
CONTEXT.final.exports = exports
CONTEXT.final.Buffer = Buffer
CONTEXT.final.TextEncoder = TextEncoder
CONTEXT.final.TextDecoder = TextDecoder
CONTEXT.final.URL = URL
CONTEXT.final.URLSearchParams = URLSearchParams
} else if (compatible.surge || (compatible.quanx === false && /\$httpClient|\$persistentStore|\$notification/.test(jscode))) {
fconsole.debug(`${filename} compatible with Surge script`)
CONTEXT.add({ surge: true })
} else if (compatible.quanx || /\$task\.fetch|\$prefs|\$notify/.test(jscode)) {
fconsole.debug(`${filename} compatible with QuantumultX script`)
CONTEXT.add({ quanx: true })
} else if (!compatible.require && /require/.test(jscode)) {
compatible.require = true
}
if (compatible.nodejs || compatible.require) {
CONTEXT.final.require = (request)=>{
fconsole.notify('require external resource:', request)
request = require.resolve(request, { paths: [CONTEXT.final.__dirname] })
return require(request)
}
CONTEXT.final.require.resolve = (request)=>require.resolve(request, { paths: [CONTEXT.final.__dirname] })
CONTEXT.final.require.clear = (request)=>delete require.cache[require.resolve(request, { paths: [CONTEXT.final.__dirname] })]
CONTEXT.final.require.cache = require.cache
}
let addtimeout = addContext.timeout, addfrom = addContext.from;
switch (addfrom) {
case 'feedPush':
CONTEXT.final.$feed.push = ()=>fconsole.notify(filename, 'is triggered by notification, $feed.push is disabled to avoid circle callback');
break;
default:
break;
}
if (!addContext.$env) {
CONTEXT.final.$env = { ...process.env, ...addContext.env }
}
CONTEXT.final.$fend.clear = ()=>{
fconsole.info('efh file cache cleared');
efhcache.clear();
}
if (bGrant) {
if (/^\/\/ +@grant +(quiet|silent)$/m.test(jscode)) {
fconsole.notify('default notification is disabled for script', filename);
CONTEXT.final.$feed = { push(){}, bark(){}, ifttt(){}, cust(){} };
if (CONTEXT.final.$notify) {
CONTEXT.final.$notify = ()=>{};
}
if (CONTEXT.final.$notification) {
CONTEXT.final.$notification.post = ()=>{};
}
}
// sudo 模式
if (/^\/\/ +@grant +sudo$/m.test(jscode)) {
fconsole.notify(filename, 'run in sudo mode');
CONTEXT.final.$task = require('../func').taskMa;
CONTEXT.final.$webhook = (type, data=null) => {
const payload = {
token: CONFIG.wbrtoken,
};
if (sType(type) === 'object') {
Object.assign(payload, type);
} else {
payload.type = type;
}
if (data && sType(data) === 'object') {
Object.assign(payload, data);
};
if (payload.type === 'runjs' && addfrom === 'webhook') {
let msg = `${filename} run from webhook, $webhook type runjs is disabled`;
fconsole.error(msg);
return Promise.reject(Error(msg));
}
return eAxios({
url: 'http://localhost:' + CONFIG_Port.webst + '/webhook',
method: 'post',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
data: payload
}, false);
};
}
}
delete addContext.cb
delete addContext.env
delete addContext.type
delete addContext.from
delete addContext.rename
delete addContext.timeout
delete addContext.__taskid
delete addContext.__taskname
CONTEXT.add({ addContext })
return new Promise((resolve, reject)=>{
try {
// 判断脚本中是否使用 $done 函数(待优化多选注释
let bDone = /^(?!\/\/).*\$(done|fend)/m.test(jscode);
let tout = addtimeout ?? CONFIG_RUNJS.timeout;
if (bDone) {
CONTEXT.final.ok = filename + '-' + euid(2) + '-' + Date.now()
let vmtout = null
if (tout > 0) {
vmtout = setTimeout(()=>{
let message = `run ${filename} timeout of ${tout} ms`
if (addtimeout !== undefined) {
message = `${filename} still running after ${tout}ms...`
}
if (addfrom === 'favend') {
message += `\ncheck the favend setting on webUI/efss`
}
vmEvent.emit(CONTEXT.final.ok, message)
clog.debug(message)
}, tout)
}
vmEvent.once(CONTEXT.final.ok, (data)=>{
resolve(data)
clearTimeout(vmtout)
})
CONTEXT.final.$vmEvent = vmEvent
}
let option = {
filename, timeout: tout > 0 ? Number(tout) : undefined,
breakOnSigint: true
}
let result = vm.runInNewContext(jscode, CONTEXT.final, option)
if (bDone === false) {
resolve(result)
}
} catch(error) {
let result = { error: error.message }
if (/^(ruleReq|ruleRes|rewrite|webhook|favend)/.test(addfrom)) {
result.rescode = -1
result.stack = error.stack
}
resolve(result)
fconsole.error(error.stack)
}
})
}
/**
* runJSFile 函数 获取初始的 filename rawcode addContext
* @param {string} filename 文件名。当 addContext.type = rawcode 时表示此项为纯 JS 代码
* @param {object} addContext 附加环境变量 context
* @return {Promise} runJS() 的结果
*/
async function runJSFile(filename, addContext={}) {
if (sType(filename) !== 'string' || (filename = filename.trim()) === '') {
return Promise.resolve('a script filename or code is expect')
}
if (sType(addContext) !== 'object') {
return Promise.resolve('type of addContext must be object')
}
if (sType(addContext.env) !== 'object') {
addContext.env = {}
}
if (addContext.$request?.headers) {
const header = Object.create(null)
const headers = addContext.$request.headers
for (let key in headers) {
header[key.toLowerCase()] = headers[key]
}
addContext.$request.headers = new Proxy(header, {
get(target, prop){
if (typeof(prop) !== 'string') return ''
return target[prop] ?? target[prop.toLowerCase()]
}
})
}
if (addContext.$response?.headers) {
const header = Object.create(null)
const headers = addContext.$response.headers
for (let key in headers) {
header[key.toLowerCase()] = headers[key]
}
addContext.$response.headers = new Proxy(header, {
get(target, prop){
if (typeof(prop) !== 'string') return ''
return target[prop] ?? target[prop.toLowerCase()]
}
})
}
// filename 附带参数处理
if (addContext.type !== 'rawcode' && / -/.test(filename)) {
let { local, timeout, rename, fstr } = sParam(filename);
if (local) {
addContext.type = 'local';
}
if (timeout !== undefined) {
addContext.timeout = timeout;
}
if (rename) {
addContext.rename = rename;
}
filename = fstr;
// -grant 参数添加
let comp = filename.match(/ -grant(=| )([^\- ]+)/)
if (comp && comp[2]) {
addContext.grant = comp[2]
filename = filename.replace(/ -grant(=| )([^\- ]+)/, '')
}
// -env 参数处理
let jobenvs = filename.split(' -env ')
if (jobenvs[1] !== undefined) {
let envlist = jobenvs[1].trim().split(' ')
envlist.forEach(ev=>{
let ei = ev.match(/(.*?)=(.*)/)
if (ei?.length === 3) {
addContext.env[ei[1]] = decodeURI(ei[2])
}
})
filename = jobenvs[0]
}
}
// end filename 附带参数处理
let runclog = addContext.cb
? new logger({ head: addContext.from + 'RunJS', level: 'debug', file: CONFIG_RUNJS.jslogfile ? (addContext.rename || addContext.filename || (/^https?:/.test(filename) && surlName(filename)) || ((addContext.type === 'rawcode') && (addContext.from || 'rawcode.js')) || filename) : false, cb: addContext.cb })
: clog;
if (/\.efh$/.test(addContext.rename || addContext.filename || filename)) {
// 直接运行 efh 文件初版。本地/远程/rawcode 命名
let efhname = addContext.rename || addContext.filename || filename;
let efhc = await efhParse(filename, {
type: addContext.type,
name: addContext.rename || addContext.filename,
title: efhname,
})
if (addContext.env.runon === 'backend' || (efhc.script && addContext.$request?.method === 'POST')) {
runclog.debug('run', efhname, 'backend code from', addContext.from);
filename = efhc.script;
addContext.type = efhc.type;
addContext.filename = efhname;
} else {
runclog.debug('send', efhname, 'html directly');
return new Promise(resolve=>{
if (/^(rule|rewrite|favend|wbrun)/.test(addContext.from)) {
resolve({response: {
statusCode: 200,
header: { ...addContext.$response?.headers, "Content-Type": "text/html;charset=utf-8" },
body: efhc.html
}})
} else {
resolve(efhc.html);
}
let res = efhc.html;
if (res.length > 480) {
runclog.debug(`run ${efhname} result: ${res.slice(0, 1200)}`);
res = res.slice(0, 480) + '...';
}
runclog.info(`run ${efhname} result: ${res}`);
})
}
}
if (/^https?:\/\/\S{4}/.test(filename)) {
let furl = filename;
filename = addContext.rename || surlName(furl);
if (!/\.js$/i.test(filename)) {
filename += '.js'
}
let jsfulpath = Jsfile.get(filename, 'path')
let jsIsExist = file.isExist(jsfulpath)
if (jsIsExist && addContext.type === 'local') {
runclog.info('run', filename, 'locally')
} else if (!jsIsExist || addContext.from === 'webhook' || bOutDate(filename)) {
runclog.info('downloading', filename, 'from', furl)
try {
await downloadfile(furl, { name: jsfulpath });
runclog.info(`success download ${filename}, ready to run`)
} catch(error) {
runclog.error(`run ${furl}, error: ${error}`);
runclog.info(`try to run ${filename} locally`)
}
}
}
let rawcode = ''
if (addContext.type === 'rawcode') {
rawcode = filename
filename = addContext.filename || addContext.from || 'rawcode.js'
addContext.__md5hash = sHash(rawcode)
} else {
let sdate = Jsfile.get(filename, 'date'), scache = {
name: filename,
date: 0,
code: '',
hash: '',
}
if (scriptcache.has(filename)) {
scache = scriptcache.get(filename)
if (scache.date === sdate) {
clog.debug(`get ${filename} cache code`)
rawcode = scache.code
} else {
// cache outdate
scache.date = 0
}
}
if (sdate === false) {
runclog.error(`${filename} not exist`)
return Promise.resolve(`${filename} not exist`)
}
if (scache.date === 0) {
clog.debug(`get ${filename} raw code`)
rawcode = Jsfile.get(filename)
scache.date = sdate
scache.code = rawcode
scache.hash = sHash(rawcode)
scriptcache.set(filename, scache)
}
addContext.__md5hash = scache.hash || sHash(rawcode)
}
if (addContext.rename) {
Jsfile.put(addContext.rename, rawcode);
filename = addContext.rename
}
if (sType(addContext.grant) === 'string') {
let grantcode = ''
addContext.grant.split('|').forEach(val=>{
if (val) {
grantcode += '\n// @grant ' + val
}
})
rawcode += grantcode
delete addContext.grant
}
if (!/\.(js|efh)$/i.test(filename)) {
filename += '.js'
}
return new Promise((resolve, reject)=>{
runJS(filename, rawcode, addContext).then(res=>{
resolve(res)
if (res !== undefined) {
res = sString(res)
if (res.length > 480) {
runclog.debug(`run ${filename} result: ${res.slice(0, 1200)}`)
res = res.slice(0, 480) + '...'
}
runclog.info(`run ${filename} result: ${res}`)
}
}).catch(e=>{
resolve(e.message)
runclog.error(`run ${filename}, error: ${errStack(e)}`)
})
})
}
module.exports = { runJSFile, CONFIG_RUNJS }