Files
kubeshark/api/pkg/inserter/main.go
nimrod-up9 b03134919e Reduce delay between tap and UI - Skip dump to file (#26)
* Pass HARs between tap and api via channel.

* Fixed make docker commad.

* Various fixes.

* Added .DS_Store to .gitignore.

* Parse flags in Mizu main instead of in tap_output.go.

* Use channel to pass HAR by default instead of files.
2021-05-03 14:50:28 +03:00

102 lines
2.5 KiB
Go

package inserter
import (
"bufio"
"encoding/json"
"fmt"
"github.com/antoniodipinto/ikisocket"
"github.com/google/martian/har"
"go.mongodb.org/mongo-driver/bson/primitive"
"mizuserver/pkg/database"
"mizuserver/pkg/models"
"mizuserver/pkg/utils"
"net/url"
"os"
"path"
"sort"
"time"
)
func StartReadingFiles(harChannel chan *har.Entry, workingDir *string) {
if workingDir != nil && *workingDir != "" {
startReadingFiles(*workingDir)
} else {
startReadingSChan(harChannel)
}
}
func startReadingFiles(workingDir string) {
err := os.MkdirAll(workingDir, os.ModePerm)
utils.CheckErr(err)
for true {
dir, _ := os.Open(workingDir)
dirFiles, _ := dir.Readdir(-1)
sort.Sort(utils.ByModTime(dirFiles))
if len(dirFiles) == 0{
fmt.Printf("Waiting for new files\n")
time.Sleep(3 * time.Second)
continue
}
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 {
time.Sleep(time.Millisecond * 250)
SaveHarToDb(*entry, "")
}
rmErr := os.Remove(inputFilePath)
utils.CheckErr(rmErr)
}
}
func startReadingSChan(harChannel chan *har.Entry) {
for entry := range harChannel {
SaveHarToDb(*entry, "")
}
}
func SaveHarToDb(entry har.Entry, source string) {
entryBytes, _ := json.Marshal(entry)
serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL)
entryId := primitive.NewObjectID().Hex()
mizuEntry := models.MizuEntry{
EntryId: entryId,
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)
baseEntry := &models.BaseEntryDetails{
Id: entryId,
Url: entry.Request.URL,
Service: serviceName,
Path: urlPath,
StatusCode: entry.Response.Status,
Method: entry.Request.Method,
Timestamp: entry.StartedDateTime.Unix(),
}
baseEntryBytes, _ := json.Marshal(&baseEntry)
ikisocket.Broadcast(baseEntryBytes)
}
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
}