Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #96 short version of sensor id added in backend #100

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions backend/handlers.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package backend

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"os"

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

"github.com/julienschmidt/httprouter"
"google.golang.org/api/idtoken"
"io"
"net/http"
"os"
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
)

func (cs *server) indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Expand Down Expand Up @@ -38,6 +38,14 @@ func (cs *server) cssHandler(w http.ResponseWriter, r *http.Request, _ httproute
}
}

// Helper function which splits into two parts the sensorID by the hyphen
func extractFirst8CharactersOfSensorId(sensorID string) (string, error) {
if len(sensorID) < 8 {
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
return "", fmt.Errorf("Sensor ID must have at least 8 characters")
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
}
return sensorID[:8], nil
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
}

// publishHandler reads the request body with a limit of 8192 bytes and then publishes
// the received message.
func (cs *server) publishHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
Expand All @@ -48,9 +56,45 @@ func (cs *server) publishHandler(w http.ResponseWriter, r *http.Request, _ httpr
return
}
defer body.Close()

cs.publish(msg)

// Unmarshal the JSON message into a map
decoder := json.NewDecoder(bytes.NewReader(msg))
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
// Create a new map to store the sensorDataMap
var sensorIDMap map[string]interface{}
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
// Decode into the sensorDataMap
if err := decoder.Decode(&sensorIDMap); err != nil {
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
return
}
if sensorIDMap == nil {
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
// Handle the case where sensorMap is nil
http.Error(w, "Invalid JSON payload", http.StatusBadRequest)
return
}
// Get the sensorID from the map
sensorID, exists := sensorIDMap["sensorID"]
if !exists {
http.Error(w, "Sensor Id does not exists.", http.StatusBadRequest)
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
return
}
sensorIDStr, ok := sensorID.(string)
if !ok {
http.Error(w, "Sensor ID is not a string.", http.StatusBadRequest)
return
}
sensorID, err = extractFirst8CharactersOfSensorId(sensorIDStr)
if err != nil {
http.Error(w, "Sensor ID is less than 8 characters.", http.StatusBadRequest)
Tushar-kalsi marked this conversation as resolved.
Show resolved Hide resolved
return
}
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