1
1
import { readFile } from 'fs/promises' ;
2
- import crypto from 'crypto' ;
2
+ import { readFileSync } from 'fs' ;
3
+ import path from "path" ;
3
4
import vm from 'vm' ;
5
+ import '../libs_drpy/es6-extend.js'
4
6
import * as utils from '../utils/utils.js' ;
5
7
// const { req } = await import('../utils/req.js');
6
8
import { matchesAll , stringUtils , cut } from '../libs_drpy/external.js'
@@ -14,21 +16,16 @@ import '../libs_drpy/node-rsa.js';
14
16
import '../libs_drpy/pako.min.js' ;
15
17
import '../libs_drpy/json5.js'
16
18
import '../libs_drpy/jinja.js'
19
+ import { fileURLToPath } from "url" ;
17
20
18
- const { sleep, sleepSync} = utils
19
-
21
+ const { sleep, sleepSync, computeHash} = utils
22
+ const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
23
+ const es6JsPath = path . join ( __dirname , '../libs_drpy/es6-extend.js' ) ;
24
+ // 读取扩展代码
25
+ const es6_extend_code = readFileSync ( es6JsPath , 'utf8' ) ;
20
26
// 缓存已初始化的模块和文件 hash 值
21
27
const moduleCache = new Map ( ) ;
22
28
23
- /**
24
- * 计算文件内容的 hash 值
25
- * @param {string } content - 文件内容
26
- * @returns {string } - 文件内容的 hash 值
27
- */
28
- function computeHash ( content ) {
29
- return crypto . createHash ( 'sha256' ) . update ( content , 'utf8' ) . digest ( 'hex' ) ;
30
- }
31
-
32
29
/**
33
30
* 初始化模块:加载并执行模块文件,存储初始化后的 rule 对象
34
31
* 如果存在 `预处理` 属性且为函数,会在缓存前执行
@@ -40,7 +37,6 @@ export async function init(filePath, refresh) {
40
37
try {
41
38
// 读取文件内容
42
39
const fileContent = await readFile ( filePath , 'utf-8' ) ;
43
-
44
40
// 计算文件的 hash 值
45
41
const fileHash = computeHash ( fileContent ) ;
46
42
@@ -59,6 +55,7 @@ export async function init(filePath, refresh) {
59
55
sleep,
60
56
sleepSync,
61
57
utils,
58
+ computeHash,
62
59
}
63
60
const drpySanbox = {
64
61
jsp,
@@ -101,7 +98,13 @@ export async function init(filePath, refresh) {
101
98
102
99
// 创建一个沙箱上下文,注入需要的全局变量和函数
103
100
const sandbox = {
104
- console,
101
+ console, // 将 console 注入沙箱,便于调试
102
+ setTimeout, // 注入定时器方法
103
+ setInterval,
104
+ clearTimeout,
105
+ clearInterval,
106
+ module : { } , // 模块支持
107
+ exports : { } , // 模块支持
105
108
rule : { } , // 用于存放导出的 rule 对象
106
109
...utilsSanbox ,
107
110
...drpySanbox ,
@@ -111,12 +114,63 @@ export async function init(filePath, refresh) {
111
114
// 创建一个上下文
112
115
const context = vm . createContext ( sandbox ) ;
113
116
117
+ // 注入扩展代码到沙箱中
118
+ const polyfillsScript = new vm . Script ( es6_extend_code ) ;
119
+ polyfillsScript . runInContext ( context ) ;
120
+
114
121
// 执行文件内容,将其放入沙箱中
115
122
const script = new vm . Script ( fileContent ) ;
116
123
script . runInContext ( context ) ;
117
124
118
125
// 访问沙箱中的 rule 对象
119
126
const moduleObject = utils . deepCopy ( sandbox . rule ) ;
127
+ moduleObject . injectMethodVars = async function ( method , args , vars ) {
128
+ async function _inner ( ) {
129
+ let input ;
130
+ let MY_URL ;
131
+ // 遍历 vars 对象,将其中的键值对转化为局部变量
132
+ for ( let key in vars ) {
133
+ let value = vars [ key ] ;
134
+
135
+ // 根据类型判断并转化值
136
+ if ( value === undefined ) {
137
+ value = 'undefined' ; // undefined转为 'undefined'
138
+ } else if ( value === null ) {
139
+ value = 'null' ; // null 转为 'null'
140
+ } else if ( value === '' ) {
141
+ value = "''" ; // 空字符串转为 "''"
142
+ } else if ( typeof value === 'boolean' ) {
143
+ value = value ? 'true' : 'false' ; // 布尔值转为 'true' 或 'false'
144
+ } else if ( typeof value === 'object' ) {
145
+ if ( Array . isArray ( value ) ) {
146
+ value = JSON . stringify ( value ) ; // 数组转为 JSON 字符串
147
+ } else if ( value instanceof Date ) {
148
+ value = `new Date("${ value . toISOString ( ) } ")` ; // Date 对象转为日期字符串
149
+ } else if ( value instanceof RegExp ) {
150
+ value = value . toString ( ) ; // 正则表达式转为字符串表示
151
+ } else {
152
+ value = JSON . stringify ( value ) ; // 普通对象转为 JSON 字符串
153
+ }
154
+ }
155
+
156
+ // 构造赋值代码,并通过 eval 动态执行
157
+ let _code = `${ key } = ${ value } ` ;
158
+ console . log ( _code ) ; // 打印每个注入的变量代码
159
+ eval ( _code ) ; // 使用 eval 在当前作用域中定义变量
160
+ }
161
+
162
+ // 打印 inject 的变量值,确保它们在 eval 中被正确注入
163
+ console . log ( '=====inject vars=====' ) ;
164
+ console . log ( input ) ; // 现在 input 应该是定义好的
165
+ console . log ( MY_URL ) ; // MY_URL 应该被注入并可用
166
+
167
+ // 执行传入的 method
168
+ return await method ( ...args ) ;
169
+ }
170
+
171
+ return await _inner ( ) ;
172
+ } ;
173
+
120
174
121
175
// 检查并执行 `预处理` 方法
122
176
if ( typeof moduleObject . 预处理 === 'function' ) {
@@ -129,7 +183,7 @@ export async function init(filePath, refresh) {
129
183
130
184
// 缓存模块和文件的 hash 值
131
185
moduleCache . set ( filePath , { moduleObject, hash : fileHash } ) ;
132
-
186
+ console . log ( moduleObject ) ;
133
187
return moduleObject ;
134
188
} catch ( error ) {
135
189
console . error ( 'Error in drpy.init:' , error ) ;
@@ -142,43 +196,62 @@ export async function init(filePath, refresh) {
142
196
* 调用模块的指定方法
143
197
* @param {string } filePath - 模块文件路径
144
198
* @param {string } method - 要调用的属性方法名称
145
- * @param args
199
+ * @param args - 传递给方法的普通参数
200
+ * @param {object } injectVars - 需要注入的变量(如 input 和 MY_URL)
146
201
* @returns {Promise<any> } - 方法调用的返回值
147
202
*/
148
- async function invokeMethod ( filePath , method , ... args ) {
203
+ async function invokeMethod ( filePath , method , args = [ ] , injectVars = { } ) {
149
204
const moduleObject = await init ( filePath ) ; // 确保模块已初始化
205
+
150
206
if ( moduleObject [ method ] && typeof moduleObject [ method ] === 'function' ) {
151
- return await moduleObject [ method ] ( ...args ) ; // 调用对应的方法并传递参数
207
+ return await moduleObject . injectMethodVars ( moduleObject [ method ] , args , injectVars ) ;
208
+ // return await moduleObject[method](...args); // 调用对应的方法并传递参数
152
209
} else {
153
210
throw new Error ( `Method ${ method } not found in module ${ filePath } ` ) ;
154
211
}
155
212
}
156
213
157
- // 各种接口调用方法
158
-
159
- export async function home ( filePath , filter = 1 ) {
160
- return await invokeMethod ( filePath , 'class_parse' , { filter} ) ;
214
+ export async function home ( filePath , filter = 1 , inputValue , urlValue ) {
215
+ return await invokeMethod ( filePath , 'class_parse' , [ filter ] , {
216
+ input : inputValue || '' ,
217
+ MY_URL : urlValue || ''
218
+ } ) ;
161
219
}
162
220
163
- export async function homeVod ( filePath ) {
164
- return await invokeMethod ( filePath , '推荐' ) ;
221
+ export async function homeVod ( filePath , inputValue , urlValue ) {
222
+ return await invokeMethod ( filePath , '推荐' , [ ] , {
223
+ input : inputValue || '' ,
224
+ MY_URL : urlValue || ''
225
+ } ) ;
165
226
}
166
227
167
- export async function cate ( filePath , tid , pg = 1 , filter = 1 , extend = { } ) {
168
- return await invokeMethod ( filePath , '一级' , { tid, pg, filter, extend} ) ;
228
+ export async function cate ( filePath , tid , pg = 1 , filter = 1 , extend = { } , inputValue , urlValue ) {
229
+ return await invokeMethod ( filePath , '一级' , [ tid , pg , filter , extend ] , {
230
+ input : inputValue || '' ,
231
+ MY_URL : urlValue || ''
232
+ } ) ;
169
233
}
170
234
171
- export async function detail ( filePath , ids ) {
235
+ export async function detail ( filePath , ids , inputValue , urlValue ) {
172
236
if ( ! Array . isArray ( ids ) ) throw new Error ( 'Parameter "ids" must be an array' ) ;
173
- return await invokeMethod ( filePath , '二级' , { ids} ) ;
237
+ return await invokeMethod ( filePath , '二级' , [ ids ] , {
238
+ input : inputValue || '' ,
239
+ MY_URL : urlValue || ''
240
+ } ) ;
174
241
}
175
242
176
- export async function search ( filePath , wd , quick = 0 , pg = 1 ) {
177
- return await invokeMethod ( filePath , '搜索' , { wd, quick, pg} ) ;
243
+ export async function search ( filePath , wd , quick = 0 , pg = 1 , inputValue , urlValue ) {
244
+ return await invokeMethod ( filePath , '搜索' , [ wd , quick , pg ] , {
245
+ input : inputValue || '' ,
246
+ MY_URL : urlValue || ''
247
+ } ) ;
178
248
}
179
249
180
- export async function play ( filePath , flag , id , flags ) {
250
+ export async function play ( filePath , flag , id , flags , inputValue , urlValue ) {
181
251
flags = flags || [ ] ;
182
252
if ( ! Array . isArray ( flags ) ) throw new Error ( 'Parameter "flags" must be an array' ) ;
183
- return await invokeMethod ( filePath , 'lazy' , { flag, id, flags} ) ;
253
+ return await invokeMethod ( filePath , 'lazy' , [ flag , id , flags ] , {
254
+ input : inputValue || '' ,
255
+ MY_URL : urlValue || ''
256
+ } ) ;
184
257
}
0 commit comments