Compare commits

..

13 Commits

Author SHA1 Message Date
Bel LaPointe
ba2156133a cookie jar go 2022-01-27 11:00:05 -07:00
bel
60c88375ad wip 2022-01-27 10:38:41 -07:00
bel
a127d9fd25 no newline in log 2022-01-27 08:16:20 -07:00
bel
8c6b55301d moer logging 2022-01-27 08:13:32 -07:00
bel
3c36948269 whoops cant compile 2022-01-27 07:49:46 -07:00
bel
bc2efe928a better ntg errs 2022-01-27 07:47:55 -07:00
bel
e1b4460ebd add day to log as i can only login to ntg 30 times per week it seems 2022-01-27 07:44:30 -07:00
bel
9bb9929ff6 cant edit, but on job no more, del entry 2022-01-19 06:12:13 -07:00
bel
a6c1b8505a ew at least del image 2022-01-18 14:43:04 -07:00
bel
c755aa88fb no 2022-01-18 14:41:37 -07:00
bel
b451ed93bf origin from date lower 2022-01-18 11:27:32 -07:00
bel
76b7211d6c todo 2022-01-18 07:55:25 -07:00
bel
31a608d7f8 whoops tahts not an err 2022-01-18 07:46:24 -07:00
6 changed files with 131 additions and 8 deletions

View File

@@ -2,9 +2,15 @@ package broker
import ( import (
"context" "context"
"encoding/json"
"errors"
"local/storage"
"local/truckstop/config" "local/truckstop/config"
"local/truckstop/logtr"
"net/http" "net/http"
"net/http/cookiejar"
"strings" "strings"
"time"
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
@@ -20,9 +26,54 @@ type Broker interface {
} }
func do(r *http.Request) (*http.Response, error) { 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()) limiter.Wait(context.Background())
if strings.Contains(strings.ToLower(r.URL.Path), "login") { if strings.Contains(strings.ToLower(r.URL.Path), "login") {
authlimiter.Wait(context.Background()) 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
View 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)
}
}
}

View File

@@ -93,7 +93,7 @@ func (ntgJob *ntgVisionJob) JobInfo() (ntgVisionJobInfo, error) {
key := fmt.Sprintf("ntg_job_info_%v", ntgJob.ID) key := fmt.Sprintf("ntg_job_info_%v", ntgJob.ID)
if b, err := db.Get(key); err != nil { if b, err := db.Get(key); err != nil {
} else if err := json.Unmarshal(b, &ntgJob.jobinfo); err == nil { } else if err := json.Unmarshal(b, &ntgJob.jobinfo); err == nil {
return ntgJob.jobinfo, fmt.Errorf("failed to parse ntg job info from db: %w: %s", err, b) return ntgJob.jobinfo, nil
} }
ntg := NewNTGVision() ntg := NewNTGVision()
ji, err := ntg.SearchJob(ntgJob.ID) ji, err := ntg.SearchJob(ntgJob.ID)
@@ -228,12 +228,14 @@ func setNTGToken(token string) {
func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) { func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) {
if getNTGToken() == "" { if getNTGToken() == "" {
logtr.Debugf("NTG token is empty, refreshing ntg auth")
if err := ntg.refreshAuth(); err != nil { if err := ntg.refreshAuth(); err != nil {
return nil, err return nil, err
} }
} }
rc, err := ntg._search(states) rc, err := ntg._search(states)
if err == ErrNoAuth { if err == ErrNoAuth {
logtr.Debugf("err no auth on search, refreshing ntg auth")
if err := ntg.refreshAuth(); err != nil { if err := ntg.refreshAuth(); err != nil {
return nil, err return nil, err
} }
@@ -295,7 +297,11 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body) b, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close() 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, ErrNoAuth
} }
return nil, fmt.Errorf("bad status searching ntg: %d: %s", resp.StatusCode, b) return nil, fmt.Errorf("bad status searching ntg: %d: %s", resp.StatusCode, b)
@@ -305,7 +311,7 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) { func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) {
body, err := json.Marshal(map[string]interface{}{ 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"), "OriginToDate": time.Now().UTC().Add(time.Hour * 24 * 30).Format("2006-01-02T15:04:05.000Z"),
"DestinationFromDate": nil, "DestinationFromDate": nil,
"DestinationToDate": nil, "DestinationToDate": nil,

View File

@@ -82,15 +82,16 @@ func SetLevel(l Level) {
} }
func logf(l Level, format string, args []interface{}) { 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 cLevel := level
cAnsoser := ansoser cAnsoser := ansoser
if l >= cLevel { 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 { if l == SOS && cAnsoser != nil {
if err := cAnsoser.Send(fmt.Sprintf(format, args...)); err != nil { if err := cAnsoser.Send(logContent); err != nil {
Errorf("failed to SOS: %v", err) Errorf("failed to SOS: %v", err)
} }
} }

View File

@@ -350,9 +350,14 @@ func updateDeadJobs(jobs []broker.Job) error {
if err := json.Unmarshal(b, &recorded); err != nil { if err := json.Unmarshal(b, &recorded); err != nil {
return err return err
} }
/* // TODO this beeps on fluffychat
if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil { if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil {
return err return err
} }
*/
if err := message.NewMatrix().Remove(recorded.MatrixID); err != nil {
return err
}
if err := db.Set(listEntry, nil); err != nil { if err := db.Set(listEntry, nil); err != nil {
return err return err
} }

View File

@@ -1,4 +1,5 @@
todo: todo:
- !states emits current state
- TEST. Just like, refactor and test to shit. - TEST. Just like, refactor and test to shit.
- try search ntg by autoinc? - try search ntg by autoinc?
- test each !command callbacks to matrix - test each !command callbacks to matrix