diff --git a/cmd/calls/main.go b/cmd/calls/main.go index 9dd9e23..03ee2b0 100644 --- a/cmd/calls/main.go +++ b/cmd/calls/main.go @@ -29,8 +29,14 @@ var ( username = flag.String("user", "", "username") password = flag.String("password", "", "password") secure = flag.Bool("s", false, "secure (https/wss)") + + uaString = version.HttpString(AppName) ) +func userAgent(h http.Header) { + h.Set("User-Agent", uaString) +} + func main() { flag.Parse() log.SetFlags(0) @@ -56,7 +62,7 @@ func main() { log.Fatal(err) } loginReq.Header.Set("Content-Type", "application/x-www-form-urlencoded") - version.UserAgent(loginReq.Header, AppName) + userAgent(loginReq.Header) jar, err := cookiejar.New(nil) if err != nil { @@ -90,7 +96,7 @@ func main() { Jar: jar, } wsHdr := make(http.Header) - version.UserAgent(wsHdr, AppName) + userAgent(wsHdr) c, _, err := dialer.Dial(u.String(), wsHdr) if err != nil { log.Fatal("dial:", err) diff --git a/internal/version/version.go b/internal/version/version.go index cb680aa..52111ec 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -2,7 +2,6 @@ package version import ( "fmt" - "net/http" "runtime" ) @@ -17,6 +16,6 @@ func String() string { Version, Built, runtime.GOOS, runtime.GOARCH) } -func UserAgent(hdr http.Header, app string) { - hdr.Set("User-Agent", fmt.Sprintf("stillbox %s/%s (%s/%s)", app, Version, runtime.GOOS, runtime.GOARCH)) +func HttpString(app string) string { + return fmt.Sprintf("stillbox %s/%s (%s/%s)", app, Version, runtime.GOOS, runtime.GOARCH) } diff --git a/pkg/gordio/server/routes.go b/pkg/gordio/server/routes.go index 1481dba..1ba9d77 100644 --- a/pkg/gordio/server/routes.go +++ b/pkg/gordio/server/routes.go @@ -6,6 +6,7 @@ import ( "strings" "dynatron.me/x/stillbox/client" + "dynatron.me/x/stillbox/internal/version" "dynatron.me/x/stillbox/pkg/gordio/config" "dynatron.me/x/stillbox/pkg/gordio/database" "github.com/go-chi/chi/v5" @@ -14,6 +15,10 @@ import ( "github.com/go-chi/render" ) +const ( + serverHeader = "Server" +) + func (s *Server) setupRoutes() { clientRoot, err := fs.Sub(client.Calls, "calls") if err != nil { @@ -65,3 +70,12 @@ func (s *Server) clientRoute(r chi.Router, clientRoot fs.FS) { fs.ServeHTTP(w, r) }) } + +func ServerHeaderAdd(next http.Handler) http.Handler { + serverString := version.HttpString("gordio") + hfn := func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Server", serverString) + next.ServeHTTP(w, r) + } + return http.HandlerFunc(hfn) +} diff --git a/pkg/gordio/server/server.go b/pkg/gordio/server/server.go index 7f14f30..104f31f 100644 --- a/pkg/gordio/server/server.go +++ b/pkg/gordio/server/server.go @@ -61,6 +61,7 @@ func New(ctx context.Context, cfg *config.Config) (*Server, error) { r.Use(middleware.RequestID) r.Use(middleware.RealIP) r.Use(RequestLogger()) + r.Use(ServerHeaderAdd) r.Use(cors.Handler(cors.Options{ AllowedOrigins: srv.conf.CORS.AllowedOrigins, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, diff --git a/pkg/gordio/sources/http.go b/pkg/gordio/sources/http.go index 87f0502..2a5f13f 100644 --- a/pkg/gordio/sources/http.go +++ b/pkg/gordio/sources/http.go @@ -136,7 +136,10 @@ func (h *RdioHTTP) routeCallUpload(w http.ResponseWriter, r *http.Request) { log.Info().Int("system", cur.System).Int("tgid", cur.Talkgroup).Msg("ingested") - _, _ = w.Write([]byte("Call imported successfully.")) + written, err := w.Write([]byte("Call imported successfully.")) + if err != nil { + log.Error().Err(err).Int("written", written).Msg("upload response failed") + } } func (car *callUploadRequest) fill(r *http.Request) error {