@@ -41,9 +41,14 @@ app.all(`*`, async (req, res) => {
41
41
if ( ! openai_key ) return res . status ( 403 ) . send ( 'Forbidden' ) ;
42
42
if ( openai_key . startsWith ( "fk" ) ) url = url . replaceAll ( "api.openai.com" , "openai.api2d.net" ) ;
43
43
44
- const proxy_key = token . split ( ':' ) [ 1 ] || "" ;
45
- if ( process . env . PROXY_KEY && proxy_key !== process . env . PROXY_KEY )
44
+ const proxy_key = token . split ( ':' ) [ 1 ] || "" ;
45
+ console . log ( "PROXY_KEY:" + proxy_key ) ;
46
+ const validProxyKeys = process . env . PROXY_KEY ? process . env . PROXY_KEY . split ( ',' ) : [ ] ;
47
+ // 检查传入的proxy_key是否在有效的PROXY_KEY列表中
48
+ if ( process . env . PROXY_KEY && ! validProxyKeys . includes ( proxy_key ) ) {
49
+ console . log ( "拒绝访问, PROXY_KEY无效" ) ;
46
50
return res . status ( 403 ) . send ( 'Forbidden' ) ;
51
+ }
47
52
48
53
// console.log( req );
49
54
const { moderation, moderation_level, ...restBody } = req . body ;
@@ -228,27 +233,41 @@ app.all(`*`, async (req, res) => {
228
233
{
229
234
console . log ( "使用 fetch" ) ;
230
235
const response = await myFetch ( url , options ) ;
231
- // console.log(response);
232
- const data = await response . json ( ) ;
233
- // 审核结果
234
- if ( moderation && mdClient )
235
- {
236
- const params = { "Content" : Buffer . from ( data . choices [ 0 ] . message . content ) . toString ( 'base64' ) } ;
237
- const md_result = await mdClient . TextModeration ( params ) ;
238
- // console.log("审核结果", md_result);
239
- let md_check = moderation_level == 'high' ? md_result . Suggestion != 'Pass' : md_result . Suggestion == 'Block' ;
240
- if ( md_check )
241
- {
242
- // 终止输出
243
- console . log ( "审核不通过" , data . choices [ 0 ] . message . content , md_result ) ;
244
- data . choices [ 0 ] . message . content = "这个话题不适合讨论,换个话题吧。" ;
245
- } else
246
- {
247
- console . log ( "审核通过" , data . choices [ 0 ] . message . content ) ;
236
+ // 检查返回的内容类型
237
+ const contentType = response . headers . get ( "Content-Type" ) ;
238
+ // 根据内容类型处理返回的数据
239
+ if ( contentType . includes ( "application/json" ) ) {
240
+ // 处理JSON数据
241
+ const data = await response . json ( ) ;
242
+ // 审核结果
243
+ if ( moderation && mdClient ) {
244
+ const params = { "Content" : Buffer . from ( data . choices [ 0 ] . message . content ) . toString ( 'base64' ) } ;
245
+ const md_result = await mdClient . TextModeration ( params ) ;
246
+ console . log ( "审核结果" , md_result ) ;
247
+ let md_check = moderation_level == 'high' ? md_result . Suggestion != 'Pass' : md_result . Suggestion == 'Block' ;
248
+ if ( md_check ) {
249
+ // 终止输出
250
+ console . log ( "审核不通过" , data . choices [ 0 ] . message . content , md_result ) ;
251
+ data . choices [ 0 ] . message . content = "这个话题不适合讨论,换个话题吧。" ;
252
+ } else {
253
+ console . log ( "审核通过" , data . choices [ 0 ] . message . content ) ;
254
+ }
248
255
}
256
+ // 返回JSON数据
257
+ res . json ( data ) ;
258
+ } else if ( contentType . includes ( "audio" ) ) {
259
+ // 处理Audio数据
260
+ const audioBlob = await response . blob ( ) ;
261
+ // 需要设置正确的Content-Type
262
+ res . setHeader ( 'Content-Type' , 'audio/mpeg' ) ;
263
+ // 发送音频数据给客户端
264
+ const audioStream = audioBlob . stream ( ) ;
265
+ audioStream . pipe ( res ) ;
266
+ } else {
267
+ // 处理其他类型的返回或抛出错误
268
+ console . log ( "返回了未知类型的数据" ) ;
269
+ res . status ( 500 ) . send ( "返回了未知类型的数据" ) ;
249
270
}
250
-
251
- res . json ( data ) ;
252
271
}
253
272
254
273
0 commit comments