Try storing cookies

This commit is contained in:
Tulir Asokan 2023-07-02 17:19:15 +03:00
parent 27b48bc6e8
commit f3a0e51a20

View file

@ -2,8 +2,10 @@ package main
import ( import (
"bufio" "bufio"
"encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"net/http"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -23,6 +25,7 @@ type Session struct {
*libgm.DevicePair *libgm.DevicePair
*crypto.Cryptor *crypto.Cryptor
*binary.WebAuthKey *binary.WebAuthKey
Cookies []*http.Cookie
} }
func must(err error) { func must(err error) {
@ -59,13 +62,20 @@ func main() {
sess.Cryptor = crypto.NewCryptor(nil, nil) sess.Cryptor = crypto.NewCryptor(nil, nil)
} }
cli = libgm.NewClient(sess.DevicePair, sess.Cryptor, log, nil) cli = libgm.NewClient(sess.DevicePair, sess.Cryptor, log, nil)
if sess.Cookies != nil {
cli.SetCookies(sess.Cookies)
}
cli.SetEventHandler(evtHandler) cli.SetEventHandler(evtHandler)
log.Debug().Msg(base64.StdEncoding.EncodeToString(sess.GetWebAuthKey()))
if sess.DevicePair == nil { if sess.DevicePair == nil {
pairer := mustReturn(cli.NewPairer(nil, 20)) pairer := mustReturn(cli.NewPairer(nil, 20))
registered := mustReturn(pairer.RegisterPhoneRelay()) registered := mustReturn(pairer.RegisterPhoneRelay())
must(cli.Connect(registered.Field5.RpcKey)) must(cli.Connect(registered.Field5.RpcKey))
} else { } else {
must(cli.Connect(sess.WebAuthKey.WebAuthKey)) //pairer := mustReturn(cli.NewPairer(nil, 20))
//newKey := pairer.GetWebEncryptionKey(sess.GetWebAuthKey())
//log.Debug().Msg(base64.StdEncoding.EncodeToString(newKey))
must(cli.Connect(sess.GetWebAuthKey()))
} }
c := make(chan os.Signal) c := make(chan os.Signal)
@ -101,6 +111,7 @@ func main() {
} }
func saveSession() { func saveSession() {
sess.Cookies = cli.GetCookies()
file := mustReturn(os.OpenFile("session.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)) file := mustReturn(os.OpenFile("session.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600))
must(json.NewEncoder(file).Encode(sess)) must(json.NewEncoder(file).Encode(sess))
_ = file.Close() _ = file.Close()