try another cookie format, ntg submits ntgauthtoken as cookie
This commit is contained in:
@@ -3,12 +3,10 @@ package broker
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"local/storage"
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/logtr"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -37,41 +35,64 @@ func _do(db storage.DB, r *http.Request) (*http.Response, error) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Hour,
|
||||
}
|
||||
newjar, err := cookiejar.New(&cookiejar.Options{})
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to make a cookie jar: " + err.Error())
|
||||
}
|
||||
client.Jar = newjar
|
||||
|
||||
cookieJarKey := "cookies_" + r.URL.Host
|
||||
cookies, err := getCookies(db, cookieJarKey)
|
||||
if err != nil {
|
||||
logtr.Errorf("failed to load cookies: %v", err)
|
||||
logtr.Errorf("failed to get cookies: %v", err)
|
||||
} else {
|
||||
client.Jar.SetCookies(r.URL, cookies)
|
||||
logtr.Verbosef("got cookies for %s: %+v", cookieJarKey, cookies)
|
||||
for _, cookie := range cookies {
|
||||
r.Header.Add("Cookie", cookie)
|
||||
}
|
||||
}
|
||||
logtr.Verbosef("_do: %+v", r)
|
||||
resp, err := client.Do(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := setCookies(db, cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
|
||||
if err := setCookies(db, cookieJarKey, resp); err != nil {
|
||||
logtr.Errorf("failed to set cookies: %v", err)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
|
||||
func getCookies(db storage.DB, host string) ([]string, error) {
|
||||
b, err := db.Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []*http.Cookie
|
||||
var result []string
|
||||
err = json.Unmarshal(b, &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
||||
b, err := json.Marshal(cookies)
|
||||
func setCookies(db storage.DB, host string, resp *http.Response) error {
|
||||
nameValues := [][2]string{}
|
||||
old, _ := getCookies(db, host)
|
||||
for _, value := range old {
|
||||
name := strings.Split(value, "=")[0]
|
||||
nameValues = append(nameValues, [2]string{name, value})
|
||||
}
|
||||
for _, value := range resp.Header["Set-Cookie"] {
|
||||
name := strings.Split(value, "=")[0]
|
||||
found := false
|
||||
for i := range nameValues {
|
||||
if nameValues[i][0] == name {
|
||||
found = true
|
||||
nameValues[i][1] = value
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
nameValues = append(nameValues, [2]string{name, value})
|
||||
}
|
||||
}
|
||||
result := []string{}
|
||||
for i := range nameValues {
|
||||
result = append(result, nameValues[i][1])
|
||||
}
|
||||
logtr.Verbosef("setting cookies for %s: %+v", host, result)
|
||||
b, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user