From 251ee1c30b30e4e24f9184e665d312fa4210aacb Mon Sep 17 00:00:00 2001 From: kwaroran Date: Fri, 23 Jun 2023 12:25:02 +0900 Subject: [PATCH] [feat] hide headers in web by proxy --- src/ts/process/request.ts | 33 +++++++++++++++++++++++---------- src/ts/storage/globalApi.ts | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index b81eeadd..6735f62f 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -4,7 +4,7 @@ import { DataBase, setDatabase, type character } from "../storage/database"; import { pluginProcess } from "./plugins"; import { language } from "../../lang"; import { stringlizeChat, unstringlizeChat } from "./stringlize"; -import { globalFetch, isTauri } from "../storage/globalApi"; +import { globalFetch, isNodeServer, isTauri } from "../storage/globalApi"; import { sleep } from "../util"; import { createDeep } from "./deepai"; @@ -167,15 +167,28 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' if(db.useStreaming && arg.useStreaming){ body.stream = true - const da = await fetch(replacerURL, { - body: JSON.stringify(body), - method: "POST", - headers: { - "Authorization": "Bearer " + db.openAIKey, - "Content-Type": "application/json" - }, - signal: abortSignal - }) + const da = ((!isTauri) && (!isNodeServer)) + ? await fetch(`/proxy?url=${encodeURIComponent(replacerURL)}`, { + body: JSON.stringify(body), + headers: { + "risu-header": encodeURIComponent(JSON.stringify({ + "Authorization": "Bearer " + db.openAIKey, + "Content-Type": "application/json" + })), + "Content-Type": "application/json" + }, + method: "POST", + signal: abortSignal + }) + : await fetch(replacerURL, { + body: JSON.stringify(body), + method: "POST", + headers: { + "Authorization": "Bearer " + db.openAIKey, + "Content-Type": "application/json" + }, + signal: abortSignal + }) if(da.status !== 200){ return { diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index 205ae892..763fef6f 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -373,7 +373,7 @@ export async function loadData() { } } -const knownHostes = ["localhost","127.0.0.1","api.openai.com"] +const knownHostes = ["localhost","127.0.0.1"] export async function globalFetch(url:string, arg:{body?:any,headers?:{[key:string]:string}, rawResponse?:boolean, method?:"POST"|"GET", abortSignal?:AbortSignal} = {}): Promise<{ ok: boolean;