Skip to content

Commit

Permalink
Fix #96 short version of sensor id added in backend (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tushar-kalsi authored Oct 26, 2023
1 parent d036561 commit f7f4b08
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 31 additions & 6 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"

"github.com/honeynet/ochi/backend/entities"

"github.com/julienschmidt/httprouter"
"google.golang.org/api/idtoken"
)
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/components/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</script>

<p on:click={click} on:keypress={click} bind:this={element}>
{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}
<u>Details</u>
Expand Down

0 comments on commit f7f4b08

Please sign in to comment.