From e338940708f55a95e20089dc60b39f2330a469e7 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Fri, 7 Jul 2023 06:30:04 +0900 Subject: [PATCH] [refactor] proxy url by header --- functions/proxy2.js | 4 +++- src/ts/process/request.ts | 3 ++- src/ts/storage/globalApi.ts | 6 ++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/functions/proxy2.js b/functions/proxy2.js index 02b1a0fc..fa322b1b 100644 --- a/functions/proxy2.js +++ b/functions/proxy2.js @@ -12,7 +12,9 @@ async function fetchProxy(request) { let response = null; let rurl = new URL(request.url); - const urlParam = rurl.searchParams.get('url') + const nheader = request.headers.get('risu-url') ? decodeURIComponent(request.headers.get('risu-url')) : null + + const urlParam = nheader ?? rurl.searchParams.get('url') if(!urlParam){ return new Response('Access denied', { diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index c600ea7a..df73c879 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -181,13 +181,14 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' if(db.useStreaming && arg.useStreaming){ body.stream = true const da = ((!isTauri) && (!isNodeServer)) - ? await fetch(`/proxy2?url=${encodeURIComponent(replacerURL)}`, { + ? await fetch(`/proxy2`, { body: JSON.stringify(body), headers: { "risu-header": encodeURIComponent(JSON.stringify({ "Authorization": "Bearer " + db.openAIKey, "Content-Type": "application/json" })), + "risu-url": encodeURIComponent(replacerURL), "Content-Type": "application/json" }, method: "POST", diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index 67d48b59..e96167eb 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -617,12 +617,13 @@ export async function globalFetch(url:string, arg:{plainFetchForce?:boolean,body } } if(arg.rawResponse){ - const furl = `/proxy2?url=${encodeURIComponent(url)}` + const furl = `/proxy2` const da = await fetch(furl, { body: body, headers: { "risu-header": encodeURIComponent(JSON.stringify(arg.headers)), + "risu-url": encodeURIComponent(url), "Content-Type": "application/json" }, method: method, @@ -637,12 +638,13 @@ export async function globalFetch(url:string, arg:{plainFetchForce?:boolean,body } } else{ - const furl = `/proxy2?url=${encodeURIComponent(url)}` + const furl = `/proxy2` const da = await fetch(furl, { body: body, headers: { "risu-header": encodeURIComponent(JSON.stringify(arg.headers)), + "risu-url": encodeURIComponent(url), "Content-Type": "application/json" }, method: method