fix: continuous 403 error caused by still requesting the official website when using reverse proxy (#207)

This commit is contained in:
josc146
2023-04-13 16:54:53 +08:00
parent 1ab53b4383
commit 40366ff7bd
5 changed files with 13 additions and 68 deletions
-6
View File
@@ -73,9 +73,6 @@ export async function generateAnswersWithGptCompletionApi(
async onError(resp) {
port.onMessage.removeListener(messageListener)
if (resp instanceof Error) throw resp
if (resp.status === 403) {
throw new Error('CLOUDFLARE')
}
const error = await resp.json().catch(() => ({}))
throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`)
},
@@ -139,9 +136,6 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
async onError(resp) {
port.onMessage.removeListener(messageListener)
if (resp instanceof Error) throw resp
if (resp.status === 403) {
throw new Error('CLOUDFLARE')
}
const error = await resp.json().catch(() => ({}))
throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`)
},
+8 -19
View File
@@ -1,5 +1,4 @@
import Browser from 'webextension-polyfill'
import ExpiryMap from 'expiry-map'
import {
deleteConversation,
generateAnswersWithChatgptWebApi,
@@ -18,6 +17,7 @@ import {
bingWebModelKeys,
chatgptApiModelKeys,
chatgptWebModelKeys,
clearOldAccessToken,
customApiModelKeys,
defaultConfig,
getPreferredLanguageKey,
@@ -25,27 +25,18 @@ import {
githubThirdPartyApiModelKeys,
gptApiModelKeys,
Models,
setAccessToken,
} from '../config/index.mjs'
import { isSafari } from '../utils/is-safari'
import { config as menuConfig } from '../content-script/menu-tools'
import { t, changeLanguage } from 'i18next'
import '../_locales/i18n'
import { openUrl } from '../utils/open-url'
const KEY_ACCESS_TOKEN = 'accessToken'
const cache = new ExpiryMap(10 * 1000)
async function getChatGptAccessToken() {
if (cache.get(KEY_ACCESS_TOKEN)) {
return cache.get(KEY_ACCESS_TOKEN)
}
if (isSafari()) {
const userConfig = await getUserConfig()
if (userConfig.accessToken) {
cache.set(KEY_ACCESS_TOKEN, userConfig.accessToken)
} else {
throw new Error('UNAUTHORIZED')
}
await clearOldAccessToken()
const userConfig = await getUserConfig()
if (userConfig.accessToken) {
return userConfig.accessToken
} else {
const resp = await fetch('https://chat.openai.com/api/auth/session')
if (resp.status === 403) {
@@ -55,9 +46,9 @@ async function getChatGptAccessToken() {
if (!data.accessToken) {
throw new Error('UNAUTHORIZED')
}
cache.set(KEY_ACCESS_TOKEN, data.accessToken)
await setAccessToken(data.accessToken)
return data.accessToken
}
return cache.get(KEY_ACCESS_TOKEN)
}
async function getBingAccessToken() {
@@ -118,8 +109,6 @@ Browser.runtime.onConnect.addListener((port) => {
} catch (err) {
console.error(err)
if (!err.message.includes('aborted')) {
cache.delete(KEY_ACCESS_TOKEN)
if (
['message you submitted was too long', 'maximum context length'].some((m) =>
err.message.includes(m),
+5 -11
View File
@@ -5,19 +5,13 @@ import DecisionCard from '../components/DecisionCard'
import { config as siteConfig } from './site-adapters'
import { config as toolsConfig } from './selection-tools'
import { config as menuConfig } from './menu-tools'
import {
clearOldAccessToken,
getPreferredLanguageKey,
getUserConfig,
setAccessToken,
} from '../config/index.mjs'
import { getPreferredLanguageKey, getUserConfig, setAccessToken } from '../config/index.mjs'
import {
createElementAtPosition,
cropText,
getClientPosition,
getPossibleElementByQuerySelector,
initSession,
isSafari,
} from '../utils'
import FloatingToolbar from '../components/FloatingToolbar'
import Browser from 'webextension-polyfill'
@@ -100,9 +94,7 @@ async function getInput(inputQuery) {
}
}
async function prepareForSafari() {
await clearOldAccessToken()
async function overwriteAccessToken() {
if (location.hostname !== 'chat.openai.com' || location.pathname !== '/api/auth/session') return
const response = document.querySelector('pre').textContent
@@ -116,6 +108,7 @@ async function prepareForSafari() {
}
if (data.accessToken) {
await setAccessToken(data.accessToken)
console.log(data.accessToken)
}
}
@@ -308,7 +301,8 @@ async function run() {
}
})
if (isSafari()) await prepareForSafari()
await overwriteAccessToken()
prepareForSelectionTools()
prepareForSelectionToolsTouch()
prepareForStaticCard()