diff --git a/api/endpoints.go b/api/endpoints.go index 9178bde..2742f78 100644 --- a/api/endpoints.go +++ b/api/endpoints.go @@ -305,6 +305,9 @@ func (e *Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { case e.ActionFunc != nil: var msg string msg, err = e.ActionFunc(apiRequest) + if !strings.HasSuffix(msg, "\n") { + msg += "\n" + } if err == nil { responseData = []byte(msg) } diff --git a/formats/dsd/dsd.go b/formats/dsd/dsd.go index 753aa7e..fce2473 100644 --- a/formats/dsd/dsd.go +++ b/formats/dsd/dsd.go @@ -8,34 +8,36 @@ import ( "errors" "fmt" - // "github.com/pkg/bson" - + "github.com/fxamacker/cbor/v2" "github.com/safing/portbase/formats/varint" "github.com/safing/portbase/utils" ) -// define types +// Types. const ( AUTO = 0 NONE = 1 - // special + // Special types. LIST = 76 // L - // serialization + // Serialization types. + BSON = 66 // B + CBOR = 67 // C + GenCode = 71 // G + JSON = 74 // J STRING = 83 // S BYTES = 88 // X - JSON = 74 // J - BSON = 66 // B - GenCode = 71 // G - // compression + // Compression types. GZIP = 90 // Z ) -// define errors -var errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type") -var errNotImplemented = errors.New("dsd: this type is not yet implemented") +// Errors. +var ( + errNoMoreSpace = errors.New("dsd: no more space left after reading dsd type") + errNotImplemented = errors.New("dsd: this type is not yet implemented") +) // Load loads an dsd structured data blob into the given interface. func Load(data []byte, t interface{}) (interface{}, error) { @@ -75,6 +77,12 @@ func LoadAsFormat(data []byte, format uint8, t interface{}) (interface{}, error) // return nil, err // } // return t, nil + case CBOR: + err := cbor.Unmarshal(data, t) + if err != nil { + return nil, fmt.Errorf("dsd: failed to unpack cbor: %s, data: %s", err, utils.SafeFirst16Bytes(data)) + } + return t, nil case GenCode: genCodeStruct, ok := t.(GenCodeCompatible) if !ok { @@ -132,6 +140,11 @@ func DumpIndent(t interface{}, format uint8, indent string) ([]byte, error) { // if err != nil { // return nil, err // } + case CBOR: + data, err = cbor.Marshal(t) + if err != nil { + return nil, err + } case GenCode: genCodeStruct, ok := t.(GenCodeCompatible) if !ok { diff --git a/formats/dsd/dsd_test.go b/formats/dsd/dsd_test.go index f93c896..fbf9d02 100644 --- a/formats/dsd/dsd_test.go +++ b/formats/dsd/dsd_test.go @@ -3,12 +3,11 @@ package dsd import ( "bytes" + "math/big" "reflect" "testing" ) -//go:generate msgp - // SimpleTestStruct is used for testing. type SimpleTestStruct struct { S string @@ -26,6 +25,7 @@ type ComplexTestStruct struct { UI16 uint16 UI32 uint32 UI64 uint64 + BI *big.Int S string Sp *string Sa []string @@ -113,6 +113,7 @@ func TestConversion(t *testing.T) { 3, 4, 5, + big.NewInt(6), "a", &bString, []string{"c", "d", "e"}, @@ -153,7 +154,7 @@ func TestConversion(t *testing.T) { } // test all formats (complex) - formats := []uint8{JSON} + formats := []uint8{JSON, CBOR} for _, format := range formats { @@ -217,6 +218,9 @@ func TestConversion(t *testing.T) { if complexSubject.UI64 != co.UI64 { t.Errorf("Load (complex struct): struct.UI64 is not equal (%v != %v)", complexSubject.UI64, co.UI64) } + if complexSubject.BI.Cmp(co.BI) != 0 { + t.Errorf("Load (complex struct): struct.BI is not equal (%v != %v)", complexSubject.BI, co.BI) + } if complexSubject.S != co.S { t.Errorf("Load (complex struct): struct.S is not equal (%v != %v)", complexSubject.S, co.S) } @@ -251,7 +255,7 @@ func TestConversion(t *testing.T) { } // test all formats - formats = []uint8{JSON, GenCode} + formats = []uint8{JSON, CBOR, GenCode} for _, format := range formats { // simple diff --git a/go.mod b/go.mod index b734047..8035220 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/dgraph-io/badger v1.6.2 github.com/dgraph-io/ristretto v0.1.0 // indirect + github.com/fxamacker/cbor/v2 v2.3.0 github.com/go-ole/go-ole v1.2.5 // indirect github.com/gofrs/uuid v4.0.0+incompatible github.com/golang/glog v1.0.0 // indirect diff --git a/go.sum b/go.sum index e0b3828..f9b49c1 100644 --- a/go.sum +++ b/go.sum @@ -79,6 +79,9 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fxamacker/cbor v1.5.1 h1:XjQWBgdmQyqimslUh5r4tUGmoqzHmBFQOImkWGi2awg= +github.com/fxamacker/cbor/v2 v2.3.0 h1:aM45YGMctNakddNNAezPxDUpv38j44Abh+hifNuqXik= +github.com/fxamacker/cbor/v2 v2.3.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= diff --git a/modules/error.go b/modules/error.go index 6e5a957..d244298 100644 --- a/modules/error.go +++ b/modules/error.go @@ -43,6 +43,7 @@ func (m *Module) NewErrorMessage(taskName string, err error) *ModuleError { return &ModuleError{ Message: err.Error(), ModuleName: m.Name, + TaskName: taskName, Severity: "error", StackTrace: string(debug.Stack()), } diff --git a/modules/start.go b/modules/start.go index 8ee35a0..ef4af73 100644 --- a/modules/start.go +++ b/modules/start.go @@ -151,6 +151,7 @@ func prepareModules() error { if rep.err == ErrCleanExit { return rep.err } + rep.module.NewErrorMessage("prep module", rep.err).Report() return fmt.Errorf("failed to prep module %s: %s", rep.module.Name, rep.err) } reportCnt++ @@ -198,6 +199,7 @@ func startModules() error { // wait for reports rep = <-reports if rep.err != nil { + rep.module.NewErrorMessage("start module", rep.err).Report() return fmt.Errorf("modules: could not start module %s: %s", rep.module.Name, rep.err) } reportCnt++ diff --git a/modules/stop.go b/modules/stop.go index 0ad30cd..bff6cc8 100644 --- a/modules/stop.go +++ b/modules/stop.go @@ -106,6 +106,7 @@ func stopModules() error { rep = <-reports if rep.err != nil { lastErr = rep.err + rep.module.NewErrorMessage("stop module", rep.err).Report() log.Warningf("modules: could not stop module %s: %s", rep.module.Name, rep.err) } reportCnt++ diff --git a/utils/debug/debug.go b/utils/debug/debug.go index 0c1c420..514bef5 100644 --- a/utils/debug/debug.go +++ b/utils/debug/debug.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "runtime/pprof" + "strings" "time" "github.com/safing/portbase/info" @@ -155,7 +156,7 @@ func (di *Info) AddLastReportedModuleError() { } di.AddSection( - "Module Error", + fmt.Sprintf("%s Module Error", strings.Title(me.ModuleName)), UseCodeSection, me.Format(), )