mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-08 22:10:50 +00:00
TRA-3234 Fetch command (#54)
* preparation to fetch command * get har as zip from server * no message * no message
This commit is contained in:
@@ -64,6 +64,65 @@ func GetEntries(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusOK).JSON(baseEntries)
|
||||
}
|
||||
|
||||
func GetHAR(c *fiber.Ctx) error {
|
||||
entriesFilter := &models.HarFetchRequestBody{}
|
||||
order := OrderDesc
|
||||
if err := c.QueryParser(entriesFilter); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(err)
|
||||
}
|
||||
err := validation.Validate(entriesFilter)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(err)
|
||||
}
|
||||
|
||||
var entries []models.MizuEntry
|
||||
database.GetEntriesTable().
|
||||
Order(fmt.Sprintf("timestamp %s", order)).
|
||||
// Where(fmt.Sprintf("timestamp %s %v", operatorSymbol, entriesFilter.Timestamp)).
|
||||
Limit(1000).
|
||||
Find(&entries)
|
||||
|
||||
if len(entries) > 0 {
|
||||
// the entries always order from oldest to newest so we should revers
|
||||
utils.ReverseSlice(entries)
|
||||
}
|
||||
|
||||
harsObject := map[string]*har.HAR{}
|
||||
|
||||
for _, entryData := range entries {
|
||||
harEntryObject := []byte(entryData.Entry)
|
||||
|
||||
var harEntry har.Entry
|
||||
_ = json.Unmarshal(harEntryObject, &harEntry)
|
||||
|
||||
sourceOfEntry := *entryData.ResolvedSource
|
||||
if harOfSource, ok := harsObject[sourceOfEntry]; ok {
|
||||
harOfSource.Log.Entries = append(harOfSource.Log.Entries, &harEntry)
|
||||
} else {
|
||||
var entriesHar []*har.Entry
|
||||
entriesHar = append(entriesHar, &harEntry)
|
||||
harsObject[sourceOfEntry] = &har.HAR{
|
||||
Log: &har.Log{
|
||||
Version: "1.2",
|
||||
Creator: &har.Creator{
|
||||
Name: "mizu",
|
||||
Version: "0.0.1",
|
||||
},
|
||||
Entries: entriesHar,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
retObj := map[string][]byte{}
|
||||
for k, v := range harsObject {
|
||||
bytesData, _ := json.Marshal(v)
|
||||
retObj[k] = bytesData
|
||||
}
|
||||
buffer := utils.ZipData(retObj)
|
||||
return c.Status(fiber.StatusOK).SendStream(buffer)
|
||||
}
|
||||
|
||||
func GetEntry(c *fiber.Ctx) error {
|
||||
var entryData models.EntryData
|
||||
database.GetEntriesTable().
|
||||
|
@@ -43,6 +43,10 @@ type EntriesFilter struct {
|
||||
Timestamp int64 `query:"timestamp" validate:"required,min=1"`
|
||||
}
|
||||
|
||||
type HarFetchRequestBody struct {
|
||||
Limit int `query:"limit" validate:"max=5000"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
type WebSocketMessageType string
|
||||
|
@@ -12,6 +12,7 @@ func EntriesRoutes(fiberApp *fiber.App) {
|
||||
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
|
||||
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry
|
||||
|
||||
routeGroup.Get("/har", controllers.GetHAR)
|
||||
routeGroup.Get("/resetDB", controllers.DeleteAllEntries) // get single (full) entry
|
||||
routeGroup.Get("/generalStats", controllers.GetGeneralStats) // get general stats about entries in DB
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"log"
|
||||
@@ -43,7 +44,6 @@ func ReverseSlice(data interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func CheckErr(e error) {
|
||||
if e != nil {
|
||||
log.Printf("%v", e)
|
||||
@@ -51,7 +51,6 @@ func CheckErr(e error) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func SetHostname(address, newHostname string) string {
|
||||
replacedUrl, err := url.Parse(address)
|
||||
if err != nil{
|
||||
@@ -81,3 +80,8 @@ func GetResolvedBaseEntry(entry models.MizuEntry) models.BaseEntryDetails {
|
||||
RequestSenderIp: entry.RequestSenderIp,
|
||||
}
|
||||
}
|
||||
|
||||
func GetBytesFromStruct(v interface{}) []byte{
|
||||
a, _ := json.Marshal(v)
|
||||
return a
|
||||
}
|
20
api/pkg/utils/zip.go
Normal file
20
api/pkg/utils/zip.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func ZipData(files map[string][]byte) *bytes.Buffer {
|
||||
// Create a buffer to write our archive to.
|
||||
buf := new(bytes.Buffer)
|
||||
// Create a new zip archive.
|
||||
zipWriter := zip.NewWriter(buf)
|
||||
defer func() { _ = zipWriter.Close() }()
|
||||
|
||||
for fileName, fileBytes := range files {
|
||||
zipFile, _ := zipWriter.Create(fileName)
|
||||
_, _ = zipFile.Write(fileBytes)
|
||||
}
|
||||
return buf
|
||||
}
|
Reference in New Issue
Block a user