@@ -13,13 +13,21 @@ export default (fastify, options, done) => {
1313 reply . status ( 404 ) . send ( { error : `Module ${ moduleName } not found` } ) ;
1414 return ;
1515 }
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+ } ;
1725 const pg = Number ( query . pg ) || 1 ;
1826 try {
1927 // 根据 query 参数决定执行逻辑
2028 if ( 'play' in query ) {
2129 // 处理播放逻辑
22- const result = await drpy . play ( modulePath , query . flag , query . play ) ;
30+ const result = await drpy . play ( modulePath , env , query . flag , query . play ) ;
2331 return reply . send ( result ) ;
2432 }
2533
@@ -33,35 +41,35 @@ export default (fastify, options, done) => {
3341 }
3442 }
3543 // 分类逻辑
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 ) ;
3745 return reply . send ( result ) ;
3846 }
3947
4048 if ( 'ac' in query && 'ids' in query ) {
4149 // 详情逻辑
42- const result = await drpy . detail ( modulePath , query . ids . split ( ',' ) ) ;
50+ const result = await drpy . detail ( modulePath , env , query . ids . split ( ',' ) ) ;
4351 return reply . send ( result ) ;
4452 }
4553
4654 if ( 'wd' in query ) {
4755 // 搜索逻辑
4856 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 ) ;
5058 return reply . send ( result ) ;
5159 }
5260
5361 if ( 'refresh' in query ) {
5462 // 强制刷新初始化逻辑
55- const refreshedObject = await drpy . init ( modulePath , true ) ;
63+ const refreshedObject = await drpy . init ( modulePath , env , true ) ;
5664 return reply . send ( refreshedObject ) ;
5765 }
5866 if ( ! ( 'filter' in query ) ) {
5967 query . filter = 1
6068 }
6169 // 默认逻辑,返回 home + homeVod 接口
6270 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 ) ;
6573 const result = {
6674 ...resultHome ,
6775 list : resultHomeVod ,
@@ -77,5 +85,76 @@ export default (fastify, options, done) => {
7785 reply . status ( 500 ) . send ( { error : `Failed to process module ${ moduleName } : ${ error . message } ` } ) ;
7886 }
7987 } ) ;
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+ } ) ;
80159 done ( ) ;
81160} ;
0 commit comments