mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-28 22:08:34 +00:00
no message
This commit is contained in:
59
api/testing_from_file.go
Normal file
59
api/testing_from_file.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/google/martian/har"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"mizuserver/src/pkg/database"
|
||||
"mizuserver/src/pkg/models"
|
||||
"mizuserver/src/pkg/utils"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func TestHarSavingFromFolder(inputDir string) {
|
||||
dir, _ := os.Open(inputDir)
|
||||
dirFiles, _ := dir.Readdir(-1)
|
||||
sort.Sort(utils.ByModTime(dirFiles))
|
||||
|
||||
for _, fileInfo := range dirFiles {
|
||||
inputFilePath := path.Join(inputDir, 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 {
|
||||
SaveHarToDb(*entry, "source")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
ServiceName: serviceName,
|
||||
Url: entry.Request.URL,
|
||||
Path: urlPath,
|
||||
Method: entry.Request.Method,
|
||||
Status: entry.Response.Status,
|
||||
Source: source,
|
||||
Timestamp: entry.StartedDateTime.Unix(),
|
||||
}
|
||||
database.EntriesCollection.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
|
||||
}
|
Reference in New Issue
Block a user