@@ -13,13 +13,21 @@ export default (fastify, options, done) => {
13
13
reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
14
14
return ;
15
15
}
16
-
16
+ const protocol = request . protocol ;
17
+ const hostname = request . hostname ;
18
+ const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( '/api/' , '/proxy/' ) + '/?do=js' ;
19
+ // console.log(`proxyUrl:${proxyUrl}`);
20
+ const env = {
21
+ proxyUrl, getProxyUrl : function ( ) {
22
+ return proxyUrl
23
+ }
24
+ } ;
17
25
const pg = Number ( query . pg ) || 1 ;
18
26
try {
19
27
// 根据 query 参数决定执行逻辑
20
28
if ( 'play' in query ) {
21
29
// 处理播放逻辑
22
- const result = await drpy . play ( modulePath , query . flag , query . play ) ;
30
+ const result = await drpy . play ( modulePath , env , query . flag , query . play ) ;
23
31
return reply . send ( result ) ;
24
32
}
25
33
@@ -33,35 +41,35 @@ export default (fastify, options, done) => {
33
41
}
34
42
}
35
43
// 分类逻辑
36
- const result = await drpy . cate ( modulePath , query . t , pg , 1 , extend ) ;
44
+ const result = await drpy . cate ( modulePath , env , query . t , pg , 1 , extend ) ;
37
45
return reply . send ( result ) ;
38
46
}
39
47
40
48
if ( 'ac' in query && 'ids' in query ) {
41
49
// 详情逻辑
42
- const result = await drpy . detail ( modulePath , query . ids . split ( ',' ) ) ;
50
+ const result = await drpy . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
43
51
return reply . send ( result ) ;
44
52
}
45
53
46
54
if ( 'wd' in query ) {
47
55
// 搜索逻辑
48
56
const quick = 'quick' in query ? query . quick : 0 ;
49
- const result = await drpy . search ( modulePath , query . wd , quick , pg ) ;
57
+ const result = await drpy . search ( modulePath , env , query . wd , quick , pg ) ;
50
58
return reply . send ( result ) ;
51
59
}
52
60
53
61
if ( 'refresh' in query ) {
54
62
// 强制刷新初始化逻辑
55
- const refreshedObject = await drpy . init ( modulePath , true ) ;
63
+ const refreshedObject = await drpy . init ( modulePath , env , true ) ;
56
64
return reply . send ( refreshedObject ) ;
57
65
}
58
66
if ( ! ( 'filter' in query ) ) {
59
67
query . filter = 1
60
68
}
61
69
// 默认逻辑,返回 home + homeVod 接口
62
70
const filter = 'filter' in query ? query . filter : 1 ;
63
- const resultHome = await drpy . home ( modulePath , filter ) ;
64
- const resultHomeVod = await drpy . homeVod ( modulePath ) ;
71
+ const resultHome = await drpy . home ( modulePath , env , filter ) ;
72
+ const resultHomeVod = await drpy . homeVod ( modulePath , env ) ;
65
73
const result = {
66
74
...resultHome ,
67
75
list : resultHomeVod ,
@@ -77,5 +85,76 @@ export default (fastify, options, done) => {
77
85
reply . status ( 500 ) . send ( { error : `Failed to process module ${ moduleName } : ${ error . message } ` } ) ;
78
86
}
79
87
} ) ;
88
+
89
+ fastify . get ( '/proxy/:module/*' , async ( request , reply ) => {
90
+ const moduleName = request . params . module ;
91
+ const query = request . query ; // 获取 query 参数
92
+ const modulePath = path . join ( options . jsDir , `${ moduleName } .js` ) ;
93
+ if ( ! existsSync ( modulePath ) ) {
94
+ reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
95
+ return ;
96
+ }
97
+ const proxy_url = request . params [ '*' ] ; // 捕获整个路径
98
+ fastify . log . info ( `try proxy for ${ moduleName } -> ${ proxy_url } :` ) ;
99
+ const protocol = request . protocol ;
100
+ const hostname = request . hostname ;
101
+ const proxyUrl = `${ protocol } ://${ hostname } ${ request . url } ` . split ( '?' ) [ 0 ] . replace ( proxy_url , '' ) + '?do=js' ;
102
+ // console.log(`proxyUrl:${proxyUrl}`);
103
+ const env = {
104
+ proxyUrl, getProxyUrl : function ( ) {
105
+ return proxyUrl
106
+ }
107
+ } ;
108
+ try {
109
+ const backRespList = await drpy . proxy ( modulePath , env , query ) ;
110
+ const statusCode = backRespList [ 0 ] ;
111
+ const mediaType = backRespList [ 1 ] ;
112
+ let content = backRespList [ 2 ] ;
113
+ const headers = backRespList . length > 3 ? backRespList [ 3 ] : null ;
114
+ const toBytes = backRespList . length > 4 ? backRespList [ 4 ] : null ;
115
+ // 如果需要转换为字节内容
116
+ if ( toBytes ) {
117
+ try {
118
+ if ( content . includes ( 'base64,' ) ) {
119
+ content = unescape ( content . split ( "base64," ) [ 1 ] ) ;
120
+ }
121
+ content = Buffer . from ( content , 'base64' ) ;
122
+ } catch ( e ) {
123
+ fastify . log . error ( `Local Proxy toBytes error: ${ e } ` ) ;
124
+ }
125
+ }
126
+
127
+ // 根据媒体类型来决定如何设置字符编码
128
+ if ( typeof content === 'string' ) {
129
+ // 如果返回的是文本内容(例如 JSON 或字符串)
130
+ if ( mediaType . includes ( 'text' ) || mediaType === 'application/json' ) {
131
+ // 对于文本类型,设置 UTF-8 编码
132
+ reply
133
+ . code ( statusCode )
134
+ . type ( `${ mediaType } ; charset=utf-8` ) // 设置编码为 UTF-8
135
+ . headers ( headers || { } ) // 如果有headers, 则加上
136
+ . send ( content ) ;
137
+ } else {
138
+ // 对于其他类型的文本(例如 XML),直接返回,不指定 UTF-8 编码
139
+ reply
140
+ . code ( statusCode )
141
+ . type ( mediaType )
142
+ . headers ( headers || { } )
143
+ . send ( content ) ;
144
+ }
145
+ } else {
146
+ // 如果返回的是二进制内容(例如图片或其他文件)
147
+ reply
148
+ . code ( statusCode )
149
+ . type ( mediaType ) // 使用合适的媒体类型,如 image/png
150
+ . headers ( headers || { } )
151
+ . send ( content ) ;
152
+ }
153
+
154
+ } catch ( error ) {
155
+ fastify . log . error ( `Error proxy module ${ moduleName } :` , error ) ;
156
+ reply . status ( 500 ) . send ( { error : `Failed to proxy module ${ moduleName } : ${ error . message } ` } ) ;
157
+ }
158
+ } ) ;
80
159
done ( ) ;
81
160
} ;
0 commit comments