Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Chatgpt Puppeteer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
Chatgpt Puppeteer
Commits
b35c106b
Commit
b35c106b
authored
Dec 24, 2022
by
Travis Fischer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: improve browser 403/401 refresh handling
parent
f66db372
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
9 deletions
+51
-9
src/chatgpt-api-browser.ts
src/chatgpt-api-browser.ts
+51
-9
No files found.
src/chatgpt-api-browser.ts
View file @
b35c106b
...
@@ -126,9 +126,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -126,9 +126,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
(
await
this
.
_browser
.
pages
())[
0
]
||
(
await
this
.
_browser
.
newPage
())
(
await
this
.
_browser
.
pages
())[
0
]
||
(
await
this
.
_browser
.
newPage
())
if
(
this
.
_proxyServer
&&
this
.
_proxyServer
.
includes
(
'
@
'
))
{
if
(
this
.
_proxyServer
&&
this
.
_proxyServer
.
includes
(
'
@
'
))
{
const
proxyUsername
=
this
.
_proxyServer
.
split
(
'
@
'
)[
0
].
split
(
'
:
'
)[
0
]
const
proxyPassword
=
this
.
_proxyServer
.
split
(
'
@
'
)[
0
].
split
(
'
:
'
)[
1
]
try
{
try
{
const
proxyUsername
=
this
.
_proxyServer
.
split
(
'
@
'
)[
0
].
split
(
'
:
'
)[
0
]
const
proxyPassword
=
this
.
_proxyServer
.
split
(
'
@
'
)[
0
].
split
(
'
:
'
)[
1
]
await
this
.
_page
.
authenticate
({
await
this
.
_page
.
authenticate
({
username
:
proxyUsername
,
username
:
proxyUsername
,
password
:
proxyPassword
password
:
proxyPassword
...
@@ -150,7 +151,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -150,7 +151,7 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
)
)
})
})
await
maximizePage
(
this
.
_page
)
//
await maximizePage(this._page)
this
.
_page
.
on
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
on
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
this
.
_page
.
on
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
...
@@ -282,9 +283,13 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -282,9 +283,13 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
}
}
}
else
if
(
url
.
endsWith
(
'
api/auth/session
'
))
{
}
else
if
(
url
.
endsWith
(
'
api/auth/session
'
))
{
if
(
status
===
401
)
{
if
(
status
===
401
)
{
await
this
.
resetSession
()
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" error 401...`
)
// this will be handled in the sendMessage error handler
// await this.resetSession()
}
else
if
(
status
===
403
)
{
}
else
if
(
status
===
403
)
{
await
this
.
refreshSession
()
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" error 403...`
)
// this will be handled in the sendMessage error handler
// await this.refreshSession()
}
else
{
}
else
{
const
session
:
types
.
SessionResult
=
body
const
session
:
types
.
SessionResult
=
body
...
@@ -303,7 +308,9 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -303,7 +308,9 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
`ChatGPT "
${
this
.
_email
}
" session expired; re-authenticating...`
`ChatGPT "
${
this
.
_email
}
" session expired; re-authenticating...`
)
)
try
{
try
{
console
.
log
(
'
>>> closing session
'
,
this
.
_email
)
await
this
.
closeSession
()
await
this
.
closeSession
()
console
.
log
(
'
<<< closing session
'
,
this
.
_email
)
await
this
.
initSession
()
await
this
.
initSession
()
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" re-authenticated successfully`
)
console
.
log
(
`ChatGPT "
${
this
.
_email
}
" re-authenticated successfully`
)
}
catch
(
err
)
{
}
catch
(
err
)
{
...
@@ -453,9 +460,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -453,9 +460,10 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
let
result
:
types
.
ChatResponse
|
types
.
ChatError
let
result
:
types
.
ChatResponse
|
types
.
ChatError
let
numTries
=
0
let
numTries
=
0
let
is401
=
false
do
{
do
{
if
(
!
(
await
this
.
getIsAuthenticated
()))
{
if
(
is401
||
!
(
await
this
.
getIsAuthenticated
()))
{
console
.
log
(
`chatgpt re-authenticating
${
this
.
_email
}
`
)
console
.
log
(
`chatgpt re-authenticating
${
this
.
_email
}
`
)
try
{
try
{
...
@@ -506,9 +514,19 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -506,9 +514,19 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
error
.
statusCode
=
result
.
error
.
statusCode
error
.
statusCode
=
result
.
error
.
statusCode
error
.
statusText
=
result
.
error
.
statusText
error
.
statusText
=
result
.
error
.
statusText
if
(
error
.
statusCode
!==
403
)
{
++
numTries
if
(
error
.
statusCode
===
401
)
{
is401
=
true
if
(
numTries
>=
2
)
{
throw
error
}
else
{
continue
}
}
else
if
(
error
.
statusCode
!==
403
)
{
throw
error
throw
error
}
else
if
(
++
numTries
>=
2
)
{
}
else
if
(
numTries
>=
2
)
{
await
this
.
refreshSession
()
await
this
.
refreshSession
()
throw
error
throw
error
}
else
{
}
else
{
...
@@ -583,11 +601,35 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
...
@@ -583,11 +601,35 @@ export class ChatGPTAPIBrowser extends AChatGPTAPI {
if
(
this
.
_page
)
{
if
(
this
.
_page
)
{
this
.
_page
.
off
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
off
(
'
request
'
,
this
.
_onRequest
.
bind
(
this
))
this
.
_page
.
off
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
this
.
_page
.
off
(
'
response
'
,
this
.
_onResponse
.
bind
(
this
))
await
this
.
_page
.
deleteCookie
({
name
:
'
cf_clearance
'
,
domain
:
'
.chat.openai.com
'
})
// TODO; test this
// const client = await this._page.target().createCDPSession()
// await client.send('Network.clearBrowserCookies')
// await client.send('Network.clearBrowserCache')
await
this
.
_page
.
close
()
}
}
}
catch
(
_
)
{}
}
catch
(
err
)
{
console
.
warn
(
'
closeSession error
'
,
err
)
}
if
(
this
.
_browser
)
{
if
(
this
.
_browser
)
{
const
pages
=
await
this
.
_browser
.
pages
()
for
(
const
page
of
pages
)
{
await
page
.
close
()
}
await
this
.
_browser
.
close
()
await
this
.
_browser
.
close
()
// Rule number 1 of zombie process hunting: double-tap
if
(
this
.
_browser
.
process
())
{
this
.
_browser
.
process
().
kill
(
'
SIGINT
'
)
}
}
}
this
.
_page
=
null
this
.
_page
=
null
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment