Skip to content

Commit

Permalink
fix: continuous 403 error caused by still requesting the official web…
Browse files Browse the repository at this point in the history
…site when using reverse proxy (#207)
  • Loading branch information
josStorer committed Apr 13, 2023
1 parent d19d41d commit af28c63
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 68 deletions.
31 changes: 0 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"@primer/octicons-react": "^18.2.0",
"countries-list": "^2.6.1",
"eventsource-parser": "^0.1.0",
"expiry-map": "^2.0.0",
"file-saver": "^2.0.5",
"github-markdown-css": "^5.2.0",
"gpt-3-encoder": "^1.1.4",
Expand Down
6 changes: 0 additions & 6 deletions src/background/apis/openai-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
},
Expand Down Expand Up @@ -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}`)
},
Expand Down
27 changes: 8 additions & 19 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Browser from 'webextension-polyfill'
import ExpiryMap from 'expiry-map'
import {
deleteConversation,
generateAnswersWithChatgptWebApi,
Expand All @@ -18,34 +17,26 @@ import {
bingWebModelKeys,
chatgptApiModelKeys,
chatgptWebModelKeys,
clearOldAccessToken,
customApiModelKeys,
defaultConfig,
getPreferredLanguageKey,
getUserConfig,
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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 5 additions & 11 deletions src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -116,6 +108,7 @@ async function prepareForSafari() {
}
if (data.accessToken) {
await setAccessToken(data.accessToken)
console.log(data.accessToken)
}
}

Expand Down Expand Up @@ -308,7 +301,8 @@ async function run() {
}
})

if (isSafari()) await prepareForSafari()
await overwriteAccessToken()

prepareForSelectionTools()
prepareForSelectionToolsTouch()
prepareForStaticCard()
Expand Down

0 comments on commit af28c63

Please sign in to comment.