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
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"local/storage"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -12,24 +13,33 @@ import (
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func TestBrokerInterface(t *testing.T) {
|
||||
var b Broker
|
||||
b = NTGVision{}
|
||||
_ = b
|
||||
}
|
||||
|
||||
func TestDoCookies(t *testing.T) {
|
||||
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
|
||||
calls := 0
|
||||
db := storage.NewMap()
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls += 1
|
||||
if calls == 1 {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "name",
|
||||
Value: "value",
|
||||
Expires: time.Now().Add(time.Hour),
|
||||
})
|
||||
} else {
|
||||
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "name=value") {
|
||||
if calls != 1 {
|
||||
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "name=value"+strconv.Itoa(calls-1)) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
t.Error("cookie not set on calls after first")
|
||||
t.Error("cookie not set as latest")
|
||||
}
|
||||
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "Expires") {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
t.Error("cookie not expiration: ", r.Header["Cookie"])
|
||||
}
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "name",
|
||||
Value: "value" + strconv.Itoa(calls),
|
||||
Expires: time.Now().Add(time.Hour),
|
||||
})
|
||||
}))
|
||||
defer s.Close()
|
||||
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
|
||||
|
||||
@@ -335,6 +335,7 @@ func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) {
|
||||
}
|
||||
setNTGHeaders(request)
|
||||
request.Header.Set("Authorization", "Bearer "+getNTGToken())
|
||||
request.Header.Set("Cookie", "NTGAuthToken="+getNTGToken())
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user