From f7f4b08cb9afc4828c21cbf3e96f00369cb06db9 Mon Sep 17 00:00:00 2001 From: Tushar kalsi <55044345+Tushar-kalsi@users.noreply.github.com> Date: Thu, 26 Oct 2023 19:24:54 +0530 Subject: [PATCH] Fix #96 short version of sensor id added in backend (#100) --- backend/handlers.go | 37 +++++++++++++++++++++++++----- frontend/components/Message.svelte | 2 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/backend/handlers.go b/backend/handlers.go index a3414d0..3c57a6e 100644 --- a/backend/handlers.go +++ b/backend/handlers.go @@ -8,7 +8,6 @@ import ( "os" "github.com/honeynet/ochi/backend/entities" - "github.com/julienschmidt/httprouter" "google.golang.org/api/idtoken" ) @@ -42,15 +41,41 @@ func (cs *server) cssHandler(w http.ResponseWriter, r *http.Request, _ httproute // the received message. func (cs *server) publishHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { body := http.MaxBytesReader(w, r.Body, 8192) - msg, err := io.ReadAll(body) - if err != nil { - http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge) + + // Unmarshal the JSON message into a map + decoder := json.NewDecoder(body) + + // Create a new map to store the sensorDataMap + var sensorIDMap map[string]string + // Decode into the sensorDataMap + if err := decoder.Decode(&sensorIDMap); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) return } - defer body.Close() - cs.publish(msg) + // Get the sensorID from the map + sensorID, exists := sensorIDMap["sensorID"] + if !exists { + http.Error(w, "sensor id does not exists", http.StatusBadRequest) + return + } + if len(sensorID) < 8 { + http.Error(w, "sensor id must have at least 8 characters", http.StatusBadRequest) + return + } + + sensorID = sensorID[:8] + + sensorIDMap["sensorID"] = sensorID + // Convert the sensorID back to a JSON message + alteredMsg, err := json.Marshal(sensorIDMap) + if err != nil { + http.Error(w, "Error processing JSON", http.StatusInternalServerError) + return + } + // Publish the altered JSON message + cs.publish(alteredMsg) w.WriteHeader(http.StatusAccepted) } diff --git a/frontend/components/Message.svelte b/frontend/components/Message.svelte index 730a649..45fbce8 100644 --- a/frontend/components/Message.svelte +++ b/frontend/components/Message.svelte @@ -16,7 +16,7 @@

- {message.sensorID.split('-')[0]} | {message.srcHost}:{message.srcPort} -> {message.dstPort}: + {message.sensorID} | {message.srcHost}:{message.srcPort} -> {message.dstPort}: {#if message.handler}{message.handler}{:else}{message.rule}{/if} {#if message.scanner}"{message.scanner}"{/if} Details