mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-23 14:58:44 +00:00
no message
This commit is contained in:
parent
1666d578f1
commit
6c698d15c7
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
api "mizuserver"
|
"mizuserver/src/pkg/inserter"
|
||||||
"mizuserver/src/pkg/middleware"
|
"mizuserver/src/pkg/middleware"
|
||||||
"mizuserver/src/pkg/routes"
|
"mizuserver/src/pkg/routes"
|
||||||
"mizuserver/src/pkg/utils"
|
"mizuserver/src/pkg/utils"
|
||||||
@ -10,9 +10,9 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// TODO: to generate data
|
// TODO: to generate data
|
||||||
path := "/Users/roeegadot/Downloads/output2"
|
//path := "/Users/roeegadot/Downloads/output2"
|
||||||
api.TestHarSavingFromFolder(path)
|
//api.TestHarSavingFromFolder(path)
|
||||||
|
go inserter.StartReadingFiles("/var/up9hars")
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
middleware.FiberMiddleware(app) // Register Fiber's middleware for app.
|
middleware.FiberMiddleware(app) // Register Fiber's middleware for app.
|
||||||
|
@ -50,3 +50,14 @@ func GetEntry(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(fullEntry)
|
return c.Status(fiber.StatusOK).JSON(fullEntry)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func DeleteAllEntries(c *fiber.Ctx) error {
|
||||||
|
database.GetEntriesTable().
|
||||||
|
Where("1 = 1").
|
||||||
|
Delete(&models.MizuEntry{})
|
||||||
|
|
||||||
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
|
"msg": "Success",
|
||||||
|
})
|
||||||
|
}
|
@ -11,10 +11,10 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DB = initDataBase(DBPath)
|
DB = initDataBase(DBPath)
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetEntriesTable() *gorm.DB{
|
func GetEntriesTable() *gorm.DB {
|
||||||
return DB.Table("mizu_entries")
|
return DB.Table("mizu_entries")
|
||||||
}
|
}
|
||||||
|
|
91
api/src/pkg/inserter/main.go
Normal file
91
api/src/pkg/inserter/main.go
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package inserter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/google/martian/har"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
|
"mizuserver/src/pkg/database"
|
||||||
|
"mizuserver/src/pkg/models"
|
||||||
|
"mizuserver/src/pkg/utils"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func IsEmpty(name string) (bool) {
|
||||||
|
f, err := os.Open(name)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.Readdirnames(1) // Or f.Readdir(1)
|
||||||
|
if err == io.EOF {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false // Either not empty or error, suits both cases
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartReadingFiles(workingDir string) {
|
||||||
|
err := os.MkdirAll(workingDir, fs.ModeDir)
|
||||||
|
utils.CheckErr(err)
|
||||||
|
|
||||||
|
for true {
|
||||||
|
if IsEmpty(workingDir) {
|
||||||
|
fmt.Printf("Waiting for new files\n")
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, _ := os.Open(workingDir)
|
||||||
|
dirFiles, _ := dir.Readdir(-1)
|
||||||
|
sort.Sort(utils.ByModTime(dirFiles))
|
||||||
|
|
||||||
|
fileInfo := dirFiles[0]
|
||||||
|
inputFilePath := path.Join(workingDir, fileInfo.Name())
|
||||||
|
file, err := os.Open(inputFilePath)
|
||||||
|
utils.CheckErr(err)
|
||||||
|
|
||||||
|
var inputHar har.HAR
|
||||||
|
decErr := json.NewDecoder(bufio.NewReader(file)).Decode(&inputHar)
|
||||||
|
utils.CheckErr(decErr)
|
||||||
|
|
||||||
|
for _, entry := range inputHar.Log.Entries {
|
||||||
|
fmt.Printf("Entry inserted")
|
||||||
|
SaveHarToDb(*entry, "")
|
||||||
|
}
|
||||||
|
rmErr := os.Remove(inputFilePath)
|
||||||
|
utils.CheckErr(rmErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func SaveHarToDb(entry har.Entry, source string) {
|
||||||
|
entryBytes, _ := json.Marshal(entry)
|
||||||
|
serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL)
|
||||||
|
mizuEntry := models.MizuEntry{
|
||||||
|
EntryId: primitive.NewObjectID().Hex(),
|
||||||
|
Entry: string(entryBytes), // simple way to store it and not convert to bytes
|
||||||
|
Service: serviceName,
|
||||||
|
Url: entry.Request.URL,
|
||||||
|
Path: urlPath,
|
||||||
|
Method: entry.Request.Method,
|
||||||
|
Status: entry.Response.Status,
|
||||||
|
Source: source,
|
||||||
|
Timestamp: entry.StartedDateTime.Unix(),
|
||||||
|
}
|
||||||
|
database.GetEntriesTable().Create(&mizuEntry)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getServiceNameFromUrl(inputUrl string) (string, string) {
|
||||||
|
parsed, err := url.Parse(inputUrl)
|
||||||
|
utils.CheckErr(err)
|
||||||
|
return fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host), parsed.Path
|
||||||
|
}
|
@ -1,11 +1,11 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MizuEntry struct {
|
type MizuEntry struct {
|
||||||
gorm.Model
|
ID uint `gorm:"primarykey"`
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
Entry string `json:"entry,omitempty" gorm:"column:entry"`
|
Entry string `json:"entry,omitempty" gorm:"column:entry"`
|
||||||
EntryId string `json:"entryId" gorm:"column:entryId"`
|
EntryId string `json:"entryId" gorm:"column:entryId"`
|
||||||
Url string `json:"url" gorm:"column:url"`
|
Url string `json:"url" gorm:"column:url"`
|
||||||
|
@ -11,4 +11,7 @@ func PublicRoutes(fiberApp *fiber.App) {
|
|||||||
|
|
||||||
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
|
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
|
||||||
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry
|
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry
|
||||||
|
|
||||||
|
routeGroup.Get("/resetDB", controllers.DeleteAllEntries) // get single (full) entry
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ func TestHarSavingFromFolder(inputDir string) {
|
|||||||
utils.CheckErr(decErr)
|
utils.CheckErr(decErr)
|
||||||
|
|
||||||
for _, entry := range inputHar.Log.Entries {
|
for _, entry := range inputHar.Log.Entries {
|
||||||
SaveHarToDb(*entry, "source")
|
SaveHarToDb(*entry, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user