styx/models/main.go
Christopher Talib 668686fbab Saving edges and node from CertStream traffic.
This work extracts fingerprints and domains from CertStream data
structure received through the stream. It builds nodes and edges and
saves them to the relevant files. It sends this data to Kafka but no
logic is implemented at the exit of the broker yet.
2020-01-29 11:02:19 +01:00

96 lines
1.9 KiB
Go

package models
import (
"encoding/json"
"io/ioutil"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"gitlab.dcso.lolcat/LABS/styx/utils"
)
// BuildNode builds a node to send to MQ instance.
func BuildNode(flag string, dataType string, data string) *Node {
t := time.Now()
rfc3339time := t.Format(time.RFC3339)
return &Node{
ID: flag + "--" + uuid.New().String(),
Type: dataType,
Data: data,
Created: rfc3339time,
Modified: rfc3339time,
}
}
// BuildEdge build a send from two nodes with a given source type.
func BuildEdge(source string, nodeOneUUID string, nodeTwoUUID string) *Edge {
t := time.Now()
rfc3339time := t.Format(time.RFC3339)
return &Edge{
ID: uuid.New().String(),
Source: source,
NodeOneID: nodeOneUUID,
NodeTwoID: nodeTwoUUID,
Timestamp: rfc3339time,
}
}
// SaveNode saves a node to a file.
func SaveNode(node *Node) {
err := utils.FileExists("nodes.json")
if err != nil {
logrus.Error(err)
}
nodeFile, err := ioutil.ReadFile("nodes.json")
if err != nil {
logrus.Error(err)
}
nodeDatas := []Node{}
if err := json.Unmarshal(nodeFile, &nodeDatas); err != nil {
logrus.Error(err)
}
nodeDatas = append(nodeDatas, *node)
nodeBytes, err := json.Marshal(nodeDatas)
if err != nil {
logrus.Error(err)
}
err = ioutil.WriteFile("nodes.json", nodeBytes, 0644)
if err != nil {
logrus.Error(err)
}
}
// SaveEdge saves an edge to a file.
func SaveEdge(edge *Edge) {
err := utils.FileExists("edges.json")
if err != nil {
logrus.Error(err)
}
edgeFile, err := ioutil.ReadFile("edges.json")
if err != nil {
logrus.Error(err)
}
edgeDatas := []Edge{}
if err := json.Unmarshal(edgeFile, &edgeDatas); err != nil {
logrus.Error(err)
}
edgeDatas = append(edgeDatas, *edge)
edgeBytes, err := json.Marshal(edgeDatas)
if err != nil {
logrus.Error(err)
}
err = ioutil.WriteFile("edges.json", edgeBytes, 0644)
if err != nil {
logrus.Error(err)
}
}