From 2a1e4b97a5188e20082b07df344596fe1394c3f8 Mon Sep 17 00:00:00 2001 From: Johan Stenstam Date: Mon, 27 May 2024 12:51:06 +0200 Subject: [PATCH] foo --- Makefile | 6 ++-- apihandler.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++----- bootstrap.go | 3 +- go.mod | 6 ++-- go.sum | 9 ++++++ 5 files changed, 92 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 2020680..9ba9a73 100644 --- a/Makefile +++ b/Makefile @@ -13,13 +13,15 @@ default: ${PROG} ${PROG}: build - version.go: /bin/sh make-version.sh $(VERSION)-$(COMMIT) $(APPDATE) $(PROG) -build: version.go +build: version.go # ../tapir/tapir.pb.go $(GO) build $(GOFLAGS) -o ${PROG} +# ../tapir/tapir.pb.go: ../tapir/tapir.proto +# make -C ../tapir tapir.pb.go + linux: /bin/sh make-version.sh $(VERSION)-$(COMMIT) $(APPDATE) $(PROG) GOOS=linux GOARCH=amd64 go build $(GOFLAGS) -o ${PROG}.linux diff --git a/apihandler.go b/apihandler.go index a1393d0..41dd71d 100644 --- a/apihandler.go +++ b/apihandler.go @@ -1,5 +1,5 @@ /* - * Johan Stenstam, johani@johani.org + * Johan Stenstam, johan.stenstam@internetstiftelsen.se */ package main @@ -233,17 +233,83 @@ func APIbootstrap(conf *Config) func(w http.ResponseWriter, r *http.Request) { } log.Printf("Found %s greylist containing %d names", bp.ListName, len(greylist.Names)) - w.Header().Set("Content-Type", "application/octet-stream") - w.Header().Set("Content-Disposition", "attachment; filename=greylist-dns-tapir.gob") + switch bp.Encoding { + case "gob": + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=greylist-%s.gob", bp.ListName)) + + encoder := gob.NewEncoder(w) + err := encoder.Encode(greylist) + if err != nil { + log.Printf("Error encoding greylist: %v", err) + resp.Error = true + resp.ErrorMsg = err.Error() + return + } - encoder := gob.NewEncoder(w) - err := encoder.Encode(greylist) - if err != nil { - log.Printf("Error encoding greylist: %v", err) + // case "protobuf": + // w.Header().Set("Content-Type", "application/octet-stream") + // w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=greylist-%s.protobuf", bp.ListName)) + // + // data, err := proto.Marshal(greylist) + // if err != nil { + // log.Printf("Error encoding greylist to protobuf: %v", err) + // resp.Error = true + // resp.ErrorMsg = err.Error() + // return + // } + + // _, err = w.Write(data) + // if err != nil { + // log.Printf("Error writing protobuf data to response: %v", err) + // resp.Error = true + // resp.ErrorMsg = err.Error() + // return + // } + + // case "flatbuffer": + // w.Header().Set("Content-Type", "application/octet-stream") + // w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=greylist-%s.flatbuffer", bp.ListName)) + + // builder := flatbuffers.NewBuilder(0) + // names := make([]flatbuffers.UOffsetT, len(greylist.Names)) + + // i := 0 + // for name := range greylist.Names { + // nameOffset := builder.CreateString(name) + // tapir.NameStart(builder) + // tapir.NameAddName(builder, nameOffset) + // names[i] = tapir.NameEnd(builder) + // i++ + // } + + // tapir.GreylistStartNamesVector(builder, len(names)) + // for j := len(names) - 1; j >= 0; j-- { + // builder.PrependUOffsetT(names[j]) + // } + // namesVector := builder.EndVector(len(names)) + + // tapir.GreylistStart(builder) + // tapir.GreylistAddNames(builder, namesVector) + // greylistOffset := tapir.GreylistEnd(builder) + + // builder.Finish(greylistOffset) + // buf := builder.FinishedBytes() + + // _, err := w.Write(buf) + // if err != nil { + // log.Printf("Error writing flatbuffer data to response: %v", err) + // resp.Error = true + // resp.ErrorMsg = err.Error() + // return + // } + + default: resp.Error = true - resp.ErrorMsg = err.Error() + resp.ErrorMsg = fmt.Sprintf("Unknown encoding: %s", bp.Encoding) return } + default: resp.ErrorMsg = fmt.Sprintf("Unknown command: %s", bp.Command) resp.Error = true diff --git a/bootstrap.go b/bootstrap.go index e8eb204..4ba7262 100644 --- a/bootstrap.go +++ b/bootstrap.go @@ -59,16 +59,15 @@ func (td *TemData) BootstrapMqttSource(s *tapir.WBGlist, src SourceConf) (*tapir status, buf, err := api.RequestNG(http.MethodPost, "/bootstrap", tapir.BootstrapPost{ Command: "export-greylist", ListName: src.Name, + Encoding: "gob", // XXX: This is our default, but we'll test other encodings later }, true) if err != nil { fmt.Printf("Error from RequestNG: %v\n", err) - // return nil, fmt.Errorf("Error from RequestNG: %v", err) continue } if status != http.StatusOK { fmt.Printf("HTTP Error: %s\n", buf) - // return nil, fmt.Errorf("HTTP Error: %s", buf) continue } diff --git a/go.mod b/go.mod index e764e76..41273ab 100644 --- a/go.mod +++ b/go.mod @@ -7,11 +7,15 @@ replace github.com/dnstapir/tapir => ../tapir require ( github.com/dnstapir/tapir v0.0.0-00010101000000-000000000000 github.com/go-playground/validator/v10 v10.20.0 + github.com/golang/protobuf v1.5.3 github.com/gorilla/mux v1.8.1 github.com/mattn/go-sqlite3 v1.14.22 github.com/miekg/dns v1.1.59 + github.com/ryanuber/columnize v2.1.2+incompatible github.com/smhanov/dawg v0.0.0-20220118194912-66057bdbf2e3 github.com/spf13/viper v1.18.2 + google.golang.org/protobuf v1.31.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 gopkg.in/yaml.v3 v3.0.1 ) @@ -35,7 +39,6 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect - github.com/ryanuber/columnize v2.1.2+incompatible // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/segmentio/asm v1.2.0 // indirect @@ -56,5 +59,4 @@ require ( golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.19.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect ) diff --git a/go.sum b/go.sum index 2d2cd89..3652cf2 100644 --- a/go.sum +++ b/go.sum @@ -31,6 +31,10 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= @@ -163,6 +167,11 @@ golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=