parent
ba2156133a
commit
2e9e0c5816
|
|
@ -3,12 +3,10 @@ package broker
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"local/storage"
|
"local/storage"
|
||||||
"local/truckstop/config"
|
"local/truckstop/config"
|
||||||
"local/truckstop/logtr"
|
"local/truckstop/logtr"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
@ -37,41 +35,64 @@ func _do(db storage.DB, r *http.Request) (*http.Response, error) {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: time.Hour,
|
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
|
cookieJarKey := "cookies_" + r.URL.Host
|
||||||
cookies, err := getCookies(db, cookieJarKey)
|
cookies, err := getCookies(db, cookieJarKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logtr.Errorf("failed to load cookies: %v", err)
|
logtr.Errorf("failed to get cookies: %v", err)
|
||||||
} else {
|
} 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)
|
resp, err := client.Do(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
logtr.Errorf("failed to set cookies: %v", err)
|
||||||
}
|
}
|
||||||
return resp, 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)
|
b, err := db.Get(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var result []*http.Cookie
|
var result []string
|
||||||
err = json.Unmarshal(b, &result)
|
err = json.Unmarshal(b, &result)
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
func setCookies(db storage.DB, host string, resp *http.Response) error {
|
||||||
b, err := json.Marshal(cookies)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"local/storage"
|
"local/storage"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -12,24 +13,33 @@ import (
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestBrokerInterface(t *testing.T) {
|
||||||
|
var b Broker
|
||||||
|
b = NTGVision{}
|
||||||
|
_ = b
|
||||||
|
}
|
||||||
|
|
||||||
func TestDoCookies(t *testing.T) {
|
func TestDoCookies(t *testing.T) {
|
||||||
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
|
limiter = rate.NewLimiter(rate.Limit(20.0), 1)
|
||||||
calls := 0
|
calls := 0
|
||||||
db := storage.NewMap()
|
db := storage.NewMap()
|
||||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
calls += 1
|
calls += 1
|
||||||
if calls == 1 {
|
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 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{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
Value: "value",
|
Value: "value" + strconv.Itoa(calls),
|
||||||
Expires: time.Now().Add(time.Hour),
|
Expires: time.Now().Add(time.Hour),
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
if !strings.Contains(fmt.Sprint(r.Header["Cookie"]), "name=value") {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
t.Error("cookie not set on calls after first")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}))
|
}))
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
|
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
|
||||||
|
|
|
||||||
|
|
@ -335,6 +335,7 @@ func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) {
|
||||||
}
|
}
|
||||||
setNTGHeaders(request)
|
setNTGHeaders(request)
|
||||||
request.Header.Set("Authorization", "Bearer "+getNTGToken())
|
request.Header.Set("Authorization", "Bearer "+getNTGToken())
|
||||||
|
request.Header.Set("Cookie", "NTGAuthToken="+getNTGToken())
|
||||||
|
|
||||||
return request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,7 +77,7 @@
|
||||||
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
|
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
|
||||||
"LoadPageAPIURIFormat": "https://ntgvision.com/api/v1/load/LoadDetails?loadId==%d",
|
"LoadPageAPIURIFormat": "https://ntgvision.com/api/v1/load/LoadDetails?loadId==%d",
|
||||||
"Username": "noeasyrunstrucking@gmail.com",
|
"Username": "noeasyrunstrucking@gmail.com",
|
||||||
"Password": "thumper1234"
|
"Password": "thumper12345"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
{
|
||||||
|
"Log": {
|
||||||
|
"Path": "/tmp/truckstop.log",
|
||||||
|
"Level": "sos",
|
||||||
|
"SOSMatrix": {
|
||||||
|
"ReceiveEnabled": true,
|
||||||
|
"Mock": true,
|
||||||
|
"Homeserver": "https://m.bltrucks.top",
|
||||||
|
"Username": "@bot.m.bltrucks.top",
|
||||||
|
"Token": "mvDWB96KXMF8XhOam8EC5XVdQvSEw0CDeClcSWocBcYkwZX3FPNWZ5uOnQk2EmT1cjpzfeuD7gDYPPjOuyZlI3bE9TE35UjNOlZgi0Tugm25s91iVsbIF6kMZsCIhVMSmEf6w3jxX6wQYOWvmDZ4mu6f5c8wr221EMDcOpEzQV09d1zuBSWgKLBgjqAkYHJZ5dTRIWpEDpPgujhOFZa2ld1HiAOxrJKlIrlfDBN0CUsTlGOGplujDAr4VtpFzNRS",
|
||||||
|
"Device": "TGNIOGKATZ",
|
||||||
|
"Room": "!OYZqtInrBCn1cyz90D:m.bltrucks.top"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Interval": {
|
||||||
|
"Input": "5s..10s",
|
||||||
|
"OK": "6h0m0s..6h0m0s",
|
||||||
|
"Error": "6h0m0s..6h0m0s",
|
||||||
|
"JobInfo": "10s..20s"
|
||||||
|
},
|
||||||
|
"Images": {
|
||||||
|
"ClientID": "d9ac7cabe813d10",
|
||||||
|
"ClientSecret": "9d0b3d82800b30ca88f595d3bcd6985f627d7d82",
|
||||||
|
"RefreshToken": "171417741bf762b99b0b9f9137491b7a69874a77",
|
||||||
|
"AccessToken": "e63db98f92d2db7ac7f56914a2030c889b378e9b",
|
||||||
|
"RefreshURI": "https://api.imgur.com/oauth2/token",
|
||||||
|
"RefreshFormat": "refresh_token=%s\u0026client_id=%s\u0026client_secret=%s\u0026grant_type=refresh_token",
|
||||||
|
"RefreshMethod": "POST",
|
||||||
|
"UploadURI": "https://api.imgur.com/3/image?name=something.jpeg",
|
||||||
|
"UploadMethod": "POST"
|
||||||
|
},
|
||||||
|
"Maps": {
|
||||||
|
"URIFormat": "https://maps.googleapis.com/maps/api/staticmap?center=%s\u0026markers=label=A|%s\u0026zoom=5\u0026size=250x250\u0026scale=1\u0026format=jpeg\u0026maptype=roadmap\u0026key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg",
|
||||||
|
"Pickup": false,
|
||||||
|
"Dropoff": false,
|
||||||
|
"Pathed": {
|
||||||
|
"Enabled": true,
|
||||||
|
"DirectionsURIFormat": "https://maps.googleapis.com/maps/api/directions/json?origin=%s\u0026destination=%s\u0026mode=driving\u0026key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg",
|
||||||
|
"PathedURIFormat": "https://maps.googleapis.com/maps/api/staticmap?size=250x250\u0026path=%s\u0026format=jpeg\u0026maptype=roadmap\u0026key=AIzaSyBkACm-LQkoSfsTO5_XAzBVZE9-JQzcNkg\u0026size=250x250\u0026markers=%s|%s",
|
||||||
|
"Zoom": {
|
||||||
|
"AcceptableLatLngDelta": 2,
|
||||||
|
"Override": 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Clients": {
|
||||||
|
"bel": {
|
||||||
|
"States": [
|
||||||
|
"NC"
|
||||||
|
],
|
||||||
|
"IDs": {
|
||||||
|
"Matrix": "@bel:m.bltrucks.top"
|
||||||
|
},
|
||||||
|
"Available": 1512328400
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Storage": [
|
||||||
|
"files",
|
||||||
|
"/tmp/play"
|
||||||
|
],
|
||||||
|
"Message": {
|
||||||
|
"Matrix": {
|
||||||
|
"ReceiveEnabled": true,
|
||||||
|
"Mock": true,
|
||||||
|
"Homeserver": "https://m.bltrucks.top",
|
||||||
|
"Username": "@bot.m.bltrucks.top",
|
||||||
|
"Token": "mvDWB96KXMF8XhOam8EC5XVdQvSEw0CDeClcSWocBcYkwZX3FPNWZ5uOnQk2EmT1cjpzfeuD7gDYPPjOuyZlI3bE9TE35UjNOlZgi0Tugm25s91iVsbIF6kMZsCIhVMSmEf6w3jxX6wQYOWvmDZ4mu6f5c8wr221EMDcOpEzQV09d1zuBSWgKLBgjqAkYHJZ5dTRIWpEDpPgujhOFZa2ld1HiAOxrJKlIrlfDBN0CUsTlGOGplujDAr4VtpFzNRS",
|
||||||
|
"Device": "TGNIOGKATZ",
|
||||||
|
"Room": "!OYZqtInrBCn1cyz90D:m.bltrucks.top"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Once": true,
|
||||||
|
"Brokers": {
|
||||||
|
"NTG": {
|
||||||
|
"JobInfo": true,
|
||||||
|
"Mock": true,
|
||||||
|
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
|
||||||
|
"LoadPageAPIURIFormat": "https://ntgvision.com/api/v1/load/LoadDetails?loadId==%d",
|
||||||
|
"Username": "noeasyrunstrucking@gmail.com",
|
||||||
|
"Password": "thumper1234"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -72,6 +72,11 @@ type Config struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
FastExact struct {
|
||||||
|
Mock bool
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
|
||||||
104
config/state.go
104
config/state.go
|
|
@ -7,56 +7,56 @@ import (
|
||||||
|
|
||||||
type State string
|
type State string
|
||||||
|
|
||||||
var States = map[string]State{
|
var States = map[State]string{
|
||||||
"99654": "AL",
|
"AL": "99654",
|
||||||
"72401": "AR",
|
"AR": "72401",
|
||||||
"85364": "AZ",
|
"AZ": "85364",
|
||||||
"90011": "CA",
|
"CA": "90011",
|
||||||
"80013": "CO",
|
"CO": "80013",
|
||||||
"06902": "CT",
|
"CT": "06902",
|
||||||
"19720": "DE",
|
"DE": "19720",
|
||||||
"33024": "FL",
|
"FL": "33024",
|
||||||
"30043": "GA",
|
"GA": "30043",
|
||||||
"96706": "HI",
|
"HI": "96706",
|
||||||
"50613": "IA",
|
"IA": "50613",
|
||||||
"83646": "ID",
|
"ID": "83646",
|
||||||
"60629": "IL",
|
"IL": "60629",
|
||||||
"47906": "IN",
|
"IN": "47906",
|
||||||
"66062": "KS",
|
"KS": "66062",
|
||||||
"40475": "KY",
|
"KY": "40475",
|
||||||
"70726": "LA",
|
"LA": "70726",
|
||||||
"02301": "MA",
|
"MA": "02301",
|
||||||
"20906": "MD",
|
"MD": "20906",
|
||||||
"04401": "ME",
|
"ME": "04401",
|
||||||
"48197": "MI",
|
"MI": "48197",
|
||||||
"55106": "MN",
|
"MN": "55106",
|
||||||
"63376": "MO",
|
"MO": "63376",
|
||||||
"39503": "MS",
|
"MS": "39503",
|
||||||
"59901": "MT",
|
"MT": "59901",
|
||||||
"27006": "NC",
|
"NC": "27006",
|
||||||
"58103": "ND",
|
"ND": "58103",
|
||||||
"68516": "NE",
|
"NE": "68516",
|
||||||
"03103": "NH",
|
"NH": "03103",
|
||||||
"08701": "NJ",
|
"NJ": "08701",
|
||||||
"87121": "NM",
|
"NM": "87121",
|
||||||
"89108": "NV",
|
"NV": "89108",
|
||||||
"11368": "NY",
|
"NY": "11368",
|
||||||
"45011": "OH",
|
"OH": "45011",
|
||||||
"73099": "OK",
|
"OK": "73099",
|
||||||
"97006": "OR",
|
"OR": "97006",
|
||||||
"19120": "PA",
|
"PA": "19120",
|
||||||
"02860": "RI",
|
"RI": "02860",
|
||||||
"29483": "SC",
|
"SC": "29483",
|
||||||
"57106": "SD",
|
"SD": "57106",
|
||||||
"37013": "TN",
|
"TN": "37013",
|
||||||
"77449": "TX",
|
"TX": "77449",
|
||||||
"84058": "UT",
|
"UT": "84058",
|
||||||
"22193": "VA",
|
"VA": "22193",
|
||||||
"05401": "VT",
|
"VT": "05401",
|
||||||
"99301": "WA",
|
"WA": "99301",
|
||||||
"53215": "WI",
|
"WI": "53215",
|
||||||
"26554": "WV",
|
"WV": "26554",
|
||||||
"82001": "WY",
|
"WY": "82001",
|
||||||
}
|
}
|
||||||
|
|
||||||
func (state *State) UnmarshalJSON(b []byte) error {
|
func (state *State) UnmarshalJSON(b []byte) error {
|
||||||
|
|
@ -65,8 +65,8 @@ func (state *State) UnmarshalJSON(b []byte) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for k := range States {
|
for k := range States {
|
||||||
if string(States[k]) == s {
|
if string(k) == s {
|
||||||
*state = States[k]
|
*state = k
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
main.go
25
main.go
|
|
@ -21,13 +21,19 @@ import (
|
||||||
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
if err := _main(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _main() error {
|
||||||
|
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if config.Get().Message.Matrix.ReceiveEnabled {
|
if config.Get().Message.Matrix.ReceiveEnabled {
|
||||||
if err := matrixrecv(); err != nil {
|
if err := matrixrecv(); err != nil {
|
||||||
logtr.SOSf("failed to recv matrix on boot: %v", err)
|
logtr.SOSf("failed to recv matrix on boot: %v", err)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lock := &sync.Mutex{}
|
lock := &sync.Mutex{}
|
||||||
|
|
@ -47,11 +53,12 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if err := _main(); err != nil {
|
if err := __main(); err != nil {
|
||||||
logtr.SOSf("failed _main: %v", err)
|
logtr.SOSf("failed __main: %v", err)
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func matrixrecv() error {
|
func matrixrecv() error {
|
||||||
|
|
@ -228,9 +235,9 @@ func parseOutStates(b []byte) []config.State {
|
||||||
return states
|
return states
|
||||||
}
|
}
|
||||||
|
|
||||||
func _main() error {
|
func __main() error {
|
||||||
for {
|
for {
|
||||||
err := _mainOne()
|
err := __mainOne()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logtr.Errorf("failed _main: %v", err)
|
logtr.Errorf("failed _main: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -247,7 +254,7 @@ func _main() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func _mainOne() error {
|
func __mainOne() error {
|
||||||
logtr.Debugf("config.refreshing...")
|
logtr.Debugf("config.refreshing...")
|
||||||
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
||||||
logtr.SOSf("bad config: %v", err)
|
logtr.SOSf("bad config: %v", err)
|
||||||
|
|
@ -258,7 +265,7 @@ func _mainOne() error {
|
||||||
logtr.SOSf("failed once(): %v", err)
|
logtr.SOSf("failed once(): %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logtr.Debugf("/_mainOne")
|
logtr.Debugf("/__mainOne")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"local/truckstop/config"
|
"local/truckstop/config"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -33,3 +34,10 @@ Content-Type: text/html; charset="UTF-8"
|
||||||
t.Fatal(want, got)
|
t.Fatal(want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMain(t *testing.T) {
|
||||||
|
os.Setenv("CONFIG", "./config.main_test.json")
|
||||||
|
if err := _main(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue