Skip to content

Commit

Permalink
we only care about the session token
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 6, 2022
1 parent c950a26 commit 401e262
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
GIT_COMMIT=$(shell git describe --always)

.PHONY: all build clean test

all: build
default: build

Expand Down
45 changes: 26 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/joho/godotenv"
"github.com/m1guelpf/chatgpt-telegram/util/ref"
"github.com/playwright-community/playwright-go"
)

Expand All @@ -35,24 +36,22 @@ func main() {
browser, page := launchBrowser(pw, "https://chat.openai.com", true)

for !isLoggedIn(page) {
cookies := <-logIn(pw)
for _, cookie := range cookies {
convertedCookie := playwright.BrowserContextAddCookiesOptionsCookies{
Name: &cookie.Name,
Value: &cookie.Value,
Domain: &cookie.Domain,
Path: &cookie.Path,
Expires: &cookie.Expires,
Secure: &cookie.Secure,
HttpOnly: &cookie.HttpOnly,
SameSite: &cookie.SameSite,
}
if err := browser.AddCookies(convertedCookie); err != nil {
log.Fatalf("Couldn't add cookies: %v", err)
}
authCookie := playwright.BrowserContextAddCookiesOptionsCookies{
Path: ref.Of("/"),
Secure: ref.Of(true),
HttpOnly: ref.Of(true),
Value: ref.Of(<-logIn(pw)),
Domain: ref.Of("chat.openai.com"),
SameSite: playwright.SameSiteAttributeLax,
Name: ref.Of("__Secure-next-auth.session-token"),
Expires: ref.Of(float64(time.Now().AddDate(0, 1, 0).Unix())),
}

if err := browser.AddCookies(authCookie); err != nil {
log.Fatalf("Couldn't add cookie: %v", err)
}

if _, err = page.Goto("https://chat.openai.com"); err != nil {
if _, err = page.Goto("https://chat.openai.com/chat"); err != nil {
log.Fatalf("Couldn't reload website: %v", err)
}
}
Expand Down Expand Up @@ -270,9 +269,9 @@ func getChatBox(page playwright.Page) playwright.ElementHandle {
return input
}

func logIn(pw *playwright.Playwright) <-chan []*playwright.BrowserContextCookiesResult {
func logIn(pw *playwright.Playwright) <-chan string {
var lock sync.Mutex
r := make(chan []*playwright.BrowserContextCookiesResult)
r := make(chan string)

lock.Lock()
go func() {
Expand All @@ -297,11 +296,19 @@ func logIn(pw *playwright.Playwright) <-chan []*playwright.BrowserContextCookies
log.Fatalf("Couldn't store authentication: %v", err)
}

var sessionToken string
for _, cookie := range cookies {
if cookie.Name == "__Secure-next-auth.session-token" {
sessionToken = cookie.Value
break
}
}

if err := browser.Close(); err != nil {
log.Fatalf("could not close browser: %v", err)
}

r <- cookies
r <- sessionToken
}()

return r
Expand Down
5 changes: 5 additions & 0 deletions util/ref/ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ref

func Of[E any](e E) *E {
return &e
}

0 comments on commit 401e262

Please sign in to comment.