Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ba2156133a | ||
|
|
60c88375ad | ||
|
|
a127d9fd25 | ||
|
|
8c6b55301d | ||
|
|
3c36948269 | ||
|
|
bc2efe928a | ||
|
|
e1b4460ebd | ||
|
|
9bb9929ff6 | ||
|
|
a6c1b8505a | ||
|
|
c755aa88fb | ||
|
|
b451ed93bf | ||
|
|
76b7211d6c | ||
|
|
31a608d7f8 | ||
|
|
0c3419a1fb | ||
|
|
6ea4d4700c | ||
|
|
451f741f5a | ||
|
|
ecf22c3a3d |
@@ -2,9 +2,15 @@ package broker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"local/storage"
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/logtr"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
@@ -20,9 +26,54 @@ type Broker interface {
|
||||
}
|
||||
|
||||
func do(r *http.Request) (*http.Response, error) {
|
||||
return _do(config.Get().DB(), r)
|
||||
}
|
||||
|
||||
func _do(db storage.DB, r *http.Request) (*http.Response, error) {
|
||||
limiter.Wait(context.Background())
|
||||
if strings.Contains(strings.ToLower(r.URL.Path), "login") {
|
||||
authlimiter.Wait(context.Background())
|
||||
}
|
||||
return http.DefaultClient.Do(r)
|
||||
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)
|
||||
} else {
|
||||
client.Jar.SetCookies(r.URL, cookies)
|
||||
}
|
||||
resp, err := client.Do(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := setCookies(db, cookieJarKey, client.Jar.Cookies(r.URL)); err != nil {
|
||||
logtr.Errorf("failed to set cookies: %v", err)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func getCookies(db storage.DB, host string) ([]*http.Cookie, error) {
|
||||
b, err := db.Get(host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result []*http.Cookie
|
||||
err = json.Unmarshal(b, &result)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func setCookies(db storage.DB, host string, cookies []*http.Cookie) error {
|
||||
b, err := json.Marshal(cookies)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Set(host, b)
|
||||
}
|
||||
|
||||
59
broker/broker_test.go
Normal file
59
broker/broker_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/storage"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
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") {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
t.Error("cookie not set on calls after first")
|
||||
}
|
||||
}
|
||||
}))
|
||||
defer s.Close()
|
||||
req, _ := http.NewRequest(http.MethodGet, s.URL, nil)
|
||||
|
||||
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err == nil {
|
||||
t.Fatal(err)
|
||||
} else if len(cookies) != 0 {
|
||||
t.Fatal(cookies)
|
||||
}
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
resp, err := _do(db, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
resp.Body.Close()
|
||||
|
||||
if cookies, err := getCookies(db, "cookies_"+req.URL.Host); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(cookies) == 0 {
|
||||
t.Fatal(cookies)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/logtr"
|
||||
@@ -26,6 +27,18 @@ type JobLocation struct {
|
||||
State string
|
||||
}
|
||||
|
||||
func (j Job) UID() string {
|
||||
return fmt.Sprintf(
|
||||
"%v-%s-%s-%s-%s-%v",
|
||||
j.ID,
|
||||
j.Pickup.State,
|
||||
base64.StdEncoding.EncodeToString([]byte(j.Pickup.City)),
|
||||
j.Dropoff.State,
|
||||
base64.StdEncoding.EncodeToString([]byte(j.Dropoff.City)),
|
||||
j.Pickup.Date.Unix(),
|
||||
)
|
||||
}
|
||||
|
||||
func (j *Job) Secrets() {
|
||||
if j.secrets == nil {
|
||||
return
|
||||
|
||||
@@ -109,7 +109,7 @@ func (ntgJob *ntgVisionJob) JobInfo() (ntgVisionJobInfo, error) {
|
||||
|
||||
func (ntg NTGVision) searchJob(id int64) (io.ReadCloser, error) {
|
||||
time.Sleep(config.Get().Interval.JobInfo.Get())
|
||||
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf(config.Get().Brokers.NTG.LoadPageURIFormat, id), nil)
|
||||
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf(config.Get().Brokers.NTG.LoadPageAPIURIFormat, id), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -175,8 +175,15 @@ func (ntg NTGVision) SearchJob(id int64) (ntgVisionJobInfo, error) {
|
||||
return ntgVisionJobInfo{}, err
|
||||
}
|
||||
defer rc.Close()
|
||||
b, err := ioutil.ReadAll(rc)
|
||||
if err != nil {
|
||||
return ntgVisionJobInfo{}, fmt.Errorf("failed to readall search job result: %w", err)
|
||||
}
|
||||
var result ntgVisionJobInfo
|
||||
err = json.NewDecoder(rc).Decode(&result)
|
||||
err = json.Unmarshal(b, &result)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to parse job info: %w: %s", err, b)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
@@ -221,12 +228,14 @@ func setNTGToken(token string) {
|
||||
|
||||
func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) {
|
||||
if getNTGToken() == "" {
|
||||
logtr.Debugf("NTG token is empty, refreshing ntg auth")
|
||||
if err := ntg.refreshAuth(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rc, err := ntg._search(states)
|
||||
if err == ErrNoAuth {
|
||||
logtr.Debugf("err no auth on search, refreshing ntg auth")
|
||||
if err := ntg.refreshAuth(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -288,7 +297,11 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode > 400 && resp.StatusCode < 500 && resp.StatusCode != 404 && resp.StatusCode != 410 {
|
||||
request2, _ := ntg.newRequest(states)
|
||||
requestb, _ := ioutil.ReadAll(request2.Body)
|
||||
logtr.Debugf("ntg auth bad status: url=%s, status=%v, body=%s, headers=%+v, request=%+v, requestb=%s", request.URL.String(), resp.StatusCode, b, resp.Header, request2, requestb)
|
||||
if resp.StatusCode > 400 && resp.StatusCode < 404 {
|
||||
logtr.Debugf("ntg auth bad status: err no auth")
|
||||
return nil, ErrNoAuth
|
||||
}
|
||||
return nil, fmt.Errorf("bad status searching ntg: %d: %s", resp.StatusCode, b)
|
||||
@@ -298,7 +311,7 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
|
||||
|
||||
func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) {
|
||||
body, err := json.Marshal(map[string]interface{}{
|
||||
"OriginFromDate": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
"OriginFromDate": time.Now().Add(time.Hour * -24).UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
"OriginToDate": time.Now().UTC().Add(time.Hour * 24 * 30).Format("2006-01-02T15:04:05.000Z"),
|
||||
"DestinationFromDate": nil,
|
||||
"DestinationToDate": nil,
|
||||
|
||||
@@ -75,6 +75,7 @@
|
||||
"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"
|
||||
}
|
||||
|
||||
@@ -65,11 +65,12 @@ type Config struct {
|
||||
Once bool
|
||||
Brokers struct {
|
||||
NTG struct {
|
||||
JobInfo bool
|
||||
Mock bool
|
||||
LoadPageURIFormat string
|
||||
Username string
|
||||
Password string
|
||||
JobInfo bool
|
||||
Mock bool
|
||||
LoadPageURIFormat string
|
||||
LoadPageAPIURIFormat string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
logtr/log.go
11
logtr/log.go
@@ -82,15 +82,18 @@ func SetLevel(l Level) {
|
||||
}
|
||||
|
||||
func logf(l Level, format string, args []interface{}) {
|
||||
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("15:04:05"), l.String(), strings.TrimSpace(format))
|
||||
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("01-02T15:04:05"), l.String(), strings.TrimSpace(format))
|
||||
logContent := strings.ReplaceAll(fmt.Sprintf(format, args...), "\n", "") + "\n"
|
||||
cLevel := level
|
||||
cAnsoser := ansoser
|
||||
if l >= cLevel {
|
||||
fmt.Fprintf(os.Stderr, format, args...)
|
||||
fmt.Fprint(os.Stderr, logContent)
|
||||
}
|
||||
fmt.Fprintf(logger, format, args...)
|
||||
fmt.Fprint(logger, logContent)
|
||||
if l == SOS && cAnsoser != nil {
|
||||
cAnsoser.Send(fmt.Sprintf(format, args...))
|
||||
if err := cAnsoser.Send(logContent); err != nil {
|
||||
Errorf("failed to SOS: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
12
main.go
12
main.go
@@ -9,7 +9,6 @@ import (
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/logtr"
|
||||
"local/truckstop/message"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
@@ -292,12 +291,13 @@ func once() error {
|
||||
jobs[i].Secrets()
|
||||
}
|
||||
logtr.Infof("once: sending jobs: %+v", jobs)
|
||||
db := config.Get().DB()
|
||||
for i := range jobs {
|
||||
if ok, err := sendJob(jobs[i]); err != nil {
|
||||
return err
|
||||
} else if ok {
|
||||
logtr.Debugf("sent job", jobs[i])
|
||||
if err := config.Get().DB().Set(jobs[i].ID, []byte(`sent`)); err != nil {
|
||||
if err := db.Set(jobs[i].UID(), []byte(`sent`)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -333,7 +333,6 @@ func updateDeadJobs(jobs []broker.Job) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("db.List() => %+v", list)
|
||||
for _, listEntry := range list {
|
||||
wouldBe := strings.TrimPrefix(listEntry, "sent_job_")
|
||||
found := false
|
||||
@@ -351,9 +350,14 @@ func updateDeadJobs(jobs []broker.Job) error {
|
||||
if err := json.Unmarshal(b, &recorded); err != nil {
|
||||
return err
|
||||
}
|
||||
/* // TODO this beeps on fluffychat
|
||||
if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil {
|
||||
return err
|
||||
}
|
||||
*/
|
||||
if err := message.NewMatrix().Remove(recorded.MatrixID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Set(listEntry, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -370,7 +374,7 @@ func updateDeadJobs(jobs []broker.Job) error {
|
||||
func dropStaleJobs(jobs []broker.Job) ([]broker.Job, error) {
|
||||
db := config.Get().DB()
|
||||
for i := len(jobs) - 1; i >= 0; i-- {
|
||||
if _, err := db.Get(jobs[i].ID); err == storage.ErrNotFound {
|
||||
if _, err := db.Get(jobs[i].UID()); err == storage.ErrNotFound {
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
todo:
|
||||
- mark jobs no longer avail by modifying in matrix;; save matrix ID over dummy payload
|
||||
- recv-as for clients so pa receives mas commands as writes
|
||||
- !states emits current state
|
||||
- TEST. Just like, refactor and test to shit.
|
||||
- try search ntg by autoinc?
|
||||
- test each !command callbacks to matrix
|
||||
- recv-as for clients so pa receives mas commands as writes
|
||||
- continuation is garbo, but I can still do better client side to avoid get-set high level
|
||||
- no hard code jpeg or have it in multiple places
|
||||
- test each !command callbacks to matrix
|
||||
- change matrix so I test my custom logic even if I dont fetch remote
|
||||
- warn/err/etc. on clobbering ids.matrix since clients can mess with one another
|
||||
- modify old items once no longer available; drop stale jobs good candidate but requires new matrix interaction
|
||||
@@ -14,6 +15,7 @@ todo:
|
||||
- banlist criteria like vendors, brokers, metadata
|
||||
- set up copy for caleb, broc
|
||||
done:
|
||||
- mark jobs no longer avail by modifying in matrix;; save matrix ID over dummy payload
|
||||
- TEST its falling apart
|
||||
- help() log on truckstop for stuff like perma 403
|
||||
- figure out zoom on maps;; is there like an auto-zoom I can leverage?
|
||||
|
||||
Reference in New Issue
Block a user