mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-29 09:39:54 +00:00
Remove duplication of Headers, Cookies and QueryString (#1192)
This commit is contained in:
parent
57e60073f5
commit
e2544aea12
@ -11,75 +11,30 @@ import (
|
|||||||
"github.com/up9inc/mizu/logger"
|
"github.com/up9inc/mizu/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Keep it because we might want cookies in the future
|
func BuildHeaders(rawHeaders map[string]interface{}) ([]Header, string, string, string, string, string) {
|
||||||
//func BuildCookies(rawCookies []interface{}) []har.Cookie {
|
|
||||||
// cookies := make([]har.Cookie, 0, len(rawCookies))
|
|
||||||
//
|
|
||||||
// for _, cookie := range rawCookies {
|
|
||||||
// c := cookie.(map[string]interface{})
|
|
||||||
// expiresStr := ""
|
|
||||||
// if c["expires"] != nil {
|
|
||||||
// expiresStr = c["expires"].(string)
|
|
||||||
// }
|
|
||||||
// expires, _ := time.Parse(time.RFC3339, expiresStr)
|
|
||||||
// httpOnly := false
|
|
||||||
// if c["httponly"] != nil {
|
|
||||||
// httpOnly, _ = strconv.ParseBool(c["httponly"].(string))
|
|
||||||
// }
|
|
||||||
// secure := false
|
|
||||||
// if c["secure"] != nil {
|
|
||||||
// secure, _ = strconv.ParseBool(c["secure"].(string))
|
|
||||||
// }
|
|
||||||
// path := ""
|
|
||||||
// if c["path"] != nil {
|
|
||||||
// path = c["path"].(string)
|
|
||||||
// }
|
|
||||||
// domain := ""
|
|
||||||
// if c["domain"] != nil {
|
|
||||||
// domain = c["domain"].(string)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// cookies = append(cookies, har.Cookie{
|
|
||||||
// Name: c["name"].(string),
|
|
||||||
// Value: c["value"].(string),
|
|
||||||
// Path: path,
|
|
||||||
// Domain: domain,
|
|
||||||
// HTTPOnly: httpOnly,
|
|
||||||
// Secure: secure,
|
|
||||||
// Expires: expires,
|
|
||||||
// Expires8601: expiresStr,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return cookies
|
|
||||||
//}
|
|
||||||
|
|
||||||
func BuildHeaders(rawHeaders []interface{}) ([]Header, string, string, string, string, string) {
|
|
||||||
var host, scheme, authority, path, status string
|
var host, scheme, authority, path, status string
|
||||||
headers := make([]Header, 0, len(rawHeaders))
|
headers := make([]Header, 0, len(rawHeaders))
|
||||||
|
|
||||||
for _, header := range rawHeaders {
|
for key, value := range rawHeaders {
|
||||||
h := header.(map[string]interface{})
|
|
||||||
|
|
||||||
headers = append(headers, Header{
|
headers = append(headers, Header{
|
||||||
Name: h["name"].(string),
|
Name: key,
|
||||||
Value: h["value"].(string),
|
Value: value.(string),
|
||||||
})
|
})
|
||||||
|
|
||||||
if h["name"] == "Host" {
|
if key == "Host" {
|
||||||
host = h["value"].(string)
|
host = value.(string)
|
||||||
}
|
}
|
||||||
if h["name"] == ":authority" {
|
if key == ":authority" {
|
||||||
authority = h["value"].(string)
|
authority = value.(string)
|
||||||
}
|
}
|
||||||
if h["name"] == ":scheme" {
|
if key == ":scheme" {
|
||||||
scheme = h["value"].(string)
|
scheme = value.(string)
|
||||||
}
|
}
|
||||||
if h["name"] == ":path" {
|
if key == ":path" {
|
||||||
path = h["value"].(string)
|
path = value.(string)
|
||||||
}
|
}
|
||||||
if h["name"] == ":status" {
|
if key == ":status" {
|
||||||
status = h["value"].(string)
|
status = value.(string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,8 +74,8 @@ func BuildPostParams(rawParams []interface{}) []Param {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewRequest(request map[string]interface{}) (harRequest *Request, err error) {
|
func NewRequest(request map[string]interface{}) (harRequest *Request, err error) {
|
||||||
headers, host, scheme, authority, path, _ := BuildHeaders(request["_headers"].([]interface{}))
|
headers, host, scheme, authority, path, _ := BuildHeaders(request["headers"].(map[string]interface{}))
|
||||||
cookies := make([]Cookie, 0) // BuildCookies(request["_cookies"].([]interface{}))
|
cookies := make([]Cookie, 0)
|
||||||
|
|
||||||
postData, _ := request["postData"].(map[string]interface{})
|
postData, _ := request["postData"].(map[string]interface{})
|
||||||
mimeType := postData["mimeType"]
|
mimeType := postData["mimeType"]
|
||||||
@ -134,13 +89,21 @@ func NewRequest(request map[string]interface{}) (harRequest *Request, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
queryString := make([]QueryString, 0)
|
queryString := make([]QueryString, 0)
|
||||||
for _, _qs := range request["_queryString"].([]interface{}) {
|
for key, value := range request["queryString"].(map[string]interface{}) {
|
||||||
qs := _qs.(map[string]interface{})
|
if valuesInterface, ok := value.([]interface{}); ok {
|
||||||
|
for _, valueInterface := range valuesInterface {
|
||||||
queryString = append(queryString, QueryString{
|
queryString = append(queryString, QueryString{
|
||||||
Name: qs["name"].(string),
|
Name: key,
|
||||||
Value: qs["value"].(string),
|
Value: valueInterface.(string),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
queryString = append(queryString, QueryString{
|
||||||
|
Name: key,
|
||||||
|
Value: value.(string),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
url := fmt.Sprintf("http://%s%s", host, request["url"].(string))
|
url := fmt.Sprintf("http://%s%s", host, request["url"].(string))
|
||||||
if strings.HasPrefix(mimeType.(string), "application/grpc") {
|
if strings.HasPrefix(mimeType.(string), "application/grpc") {
|
||||||
@ -172,8 +135,8 @@ func NewRequest(request map[string]interface{}) (harRequest *Request, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewResponse(response map[string]interface{}) (harResponse *Response, err error) {
|
func NewResponse(response map[string]interface{}) (harResponse *Response, err error) {
|
||||||
headers, _, _, _, _, _status := BuildHeaders(response["_headers"].([]interface{}))
|
headers, _, _, _, _, _status := BuildHeaders(response["headers"].(map[string]interface{}))
|
||||||
cookies := make([]Cookie, 0) // BuildCookies(response["_cookies"].([]interface{}))
|
cookies := make([]Cookie, 0)
|
||||||
|
|
||||||
content, _ := response["content"].(map[string]interface{})
|
content, _ := response["content"].(map[string]interface{})
|
||||||
mimeType := content["mimeType"]
|
mimeType := content["mimeType"]
|
||||||
|
@ -13,4 +13,4 @@ test-pull-bin:
|
|||||||
|
|
||||||
test-pull-expect:
|
test-pull-expect:
|
||||||
@mkdir -p expect
|
@mkdir -p expect
|
||||||
@[ "${skipexpect}" ] && echo "Skipping downloading expected JSONs" || gsutil -o 'GSUtil:parallel_process_count=5' -o 'GSUtil:parallel_thread_count=5' -m cp -r gs://static.up9.io/mizu/test-pcap/expect15/http/\* expect
|
@[ "${skipexpect}" ] && echo "Skipping downloading expected JSONs" || gsutil -o 'GSUtil:parallel_process_count=5' -o 'GSUtil:parallel_thread_count=5' -m cp -r gs://static.up9.io/mizu/test-pcap/expect16/http/\* expect
|
||||||
|
@ -6,13 +6,16 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/up9inc/mizu/tap/api"
|
"github.com/up9inc/mizu/tap/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func mapSliceRebuildAsMap(mapSlice []interface{}) (newMap map[string]interface{}) {
|
func mapSliceRebuildAsMap(mapSlice []interface{}) (newMap map[string]interface{}) {
|
||||||
newMap = make(map[string]interface{})
|
newMap = make(map[string]interface{})
|
||||||
for _, item := range mapSlice {
|
|
||||||
|
mergedMapSlice := mapSliceMergeRepeatedKeys(mapSlice)
|
||||||
|
for _, item := range mergedMapSlice {
|
||||||
h := item.(map[string]interface{})
|
h := item.(map[string]interface{})
|
||||||
newMap[h["name"].(string)] = h["value"]
|
newMap[h["name"].(string)] = h["value"]
|
||||||
}
|
}
|
||||||
@ -20,6 +23,28 @@ func mapSliceRebuildAsMap(mapSlice []interface{}) (newMap map[string]interface{}
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mapSliceRebuildAsMergedMap(mapSlice []interface{}) (newMap map[string]interface{}) {
|
||||||
|
newMap = make(map[string]interface{})
|
||||||
|
|
||||||
|
mergedMapSlice := mapSliceMergeRepeatedKeys(mapSlice)
|
||||||
|
for _, item := range mergedMapSlice {
|
||||||
|
h := item.(map[string]interface{})
|
||||||
|
|
||||||
|
if valuesInterface, ok := h["value"].([]interface{}); ok {
|
||||||
|
var values []string
|
||||||
|
for _, valueInterface := range valuesInterface {
|
||||||
|
values = append(values, valueInterface.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
newMap[h["name"].(string)] = strings.Join(values, ",")
|
||||||
|
} else {
|
||||||
|
newMap[h["name"].(string)] = h["value"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func mapSliceMergeRepeatedKeys(mapSlice []interface{}) (newMapSlice []interface{}) {
|
func mapSliceMergeRepeatedKeys(mapSlice []interface{}) (newMapSlice []interface{}) {
|
||||||
newMapSlice = make([]interface{}, 0)
|
newMapSlice = make([]interface{}, 0)
|
||||||
valuesMap := make(map[string][]interface{})
|
valuesMap := make(map[string][]interface{})
|
||||||
@ -47,6 +72,24 @@ func mapSliceMergeRepeatedKeys(mapSlice []interface{}) (newMapSlice []interface{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func representMapAsTable(mapToTable map[string]interface{}, selectorPrefix string) (representation string) {
|
||||||
|
var table []api.TableData
|
||||||
|
|
||||||
|
keys := make([]string, 0, len(mapToTable))
|
||||||
|
for k := range mapToTable {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, key := range keys {
|
||||||
|
table = append(table, createTableForKey(key, mapToTable[key], selectorPrefix)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
obj, _ := json.Marshal(table)
|
||||||
|
representation = string(obj)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func representMapSliceAsTable(mapSlice []interface{}, selectorPrefix string) (representation string) {
|
func representMapSliceAsTable(mapSlice []interface{}, selectorPrefix string) (representation string) {
|
||||||
var table []api.TableData
|
var table []api.TableData
|
||||||
for _, item := range mapSlice {
|
for _, item := range mapSlice {
|
||||||
@ -54,6 +97,17 @@ func representMapSliceAsTable(mapSlice []interface{}, selectorPrefix string) (re
|
|||||||
key := h["name"].(string)
|
key := h["name"].(string)
|
||||||
value := h["value"]
|
value := h["value"]
|
||||||
|
|
||||||
|
table = append(table, createTableForKey(key, value, selectorPrefix)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
obj, _ := json.Marshal(table)
|
||||||
|
representation = string(obj)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTableForKey(key string, value interface{}, selectorPrefix string) []api.TableData {
|
||||||
|
var table []api.TableData
|
||||||
|
|
||||||
var reflectKind reflect.Kind
|
var reflectKind reflect.Kind
|
||||||
reflectType := reflect.TypeOf(value)
|
reflectType := reflect.TypeOf(value)
|
||||||
if reflectType == nil {
|
if reflectType == nil {
|
||||||
@ -82,11 +136,8 @@ func representMapSliceAsTable(mapSlice []interface{}, selectorPrefix string) (re
|
|||||||
Selector: selector,
|
Selector: selector,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
obj, _ := json.Marshal(table)
|
return table
|
||||||
representation = string(obj)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func representSliceAsTable(slice []interface{}, selectorPrefix string) (representation string) {
|
func representSliceAsTable(slice []interface{}, selectorPrefix string) (representation string) {
|
||||||
|
@ -286,19 +286,13 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, resolvedSource string,
|
|||||||
reqDetails["pathSegments"] = strings.Split(path, "/")[1:]
|
reqDetails["pathSegments"] = strings.Split(path, "/")[1:]
|
||||||
|
|
||||||
// Rearrange the maps for the querying
|
// Rearrange the maps for the querying
|
||||||
reqDetails["_headers"] = reqDetails["headers"]
|
reqDetails["headers"] = mapSliceRebuildAsMergedMap(reqDetails["headers"].([]interface{}))
|
||||||
reqDetails["headers"] = mapSliceRebuildAsMap(reqDetails["_headers"].([]interface{}))
|
resDetails["headers"] = mapSliceRebuildAsMergedMap(resDetails["headers"].([]interface{}))
|
||||||
resDetails["_headers"] = resDetails["headers"]
|
|
||||||
resDetails["headers"] = mapSliceRebuildAsMap(resDetails["_headers"].([]interface{}))
|
|
||||||
|
|
||||||
reqDetails["_cookies"] = reqDetails["cookies"]
|
reqDetails["cookies"] = mapSliceRebuildAsMergedMap(reqDetails["cookies"].([]interface{}))
|
||||||
reqDetails["cookies"] = mapSliceRebuildAsMap(reqDetails["_cookies"].([]interface{}))
|
resDetails["cookies"] = mapSliceRebuildAsMergedMap(resDetails["cookies"].([]interface{}))
|
||||||
resDetails["_cookies"] = resDetails["cookies"]
|
|
||||||
resDetails["cookies"] = mapSliceRebuildAsMap(resDetails["_cookies"].([]interface{}))
|
|
||||||
|
|
||||||
reqDetails["_queryString"] = reqDetails["queryString"]
|
reqDetails["queryString"] = mapSliceRebuildAsMap(reqDetails["queryString"].([]interface{}))
|
||||||
reqDetails["_queryStringMerged"] = mapSliceMergeRepeatedKeys(reqDetails["_queryString"].([]interface{}))
|
|
||||||
reqDetails["queryString"] = mapSliceRebuildAsMap(reqDetails["_queryStringMerged"].([]interface{}))
|
|
||||||
|
|
||||||
elapsedTime := item.Pair.Response.CaptureTime.Sub(item.Pair.Request.CaptureTime).Round(time.Millisecond).Milliseconds()
|
elapsedTime := item.Pair.Response.CaptureTime.Sub(item.Pair.Request.CaptureTime).Round(time.Millisecond).Milliseconds()
|
||||||
if elapsedTime < 0 {
|
if elapsedTime < 0 {
|
||||||
@ -397,19 +391,19 @@ func representRequest(request map[string]interface{}) (repRequest []interface{})
|
|||||||
repRequest = append(repRequest, api.SectionData{
|
repRequest = append(repRequest, api.SectionData{
|
||||||
Type: api.TABLE,
|
Type: api.TABLE,
|
||||||
Title: "Headers",
|
Title: "Headers",
|
||||||
Data: representMapSliceAsTable(request["_headers"].([]interface{}), `request.headers`),
|
Data: representMapAsTable(request["headers"].(map[string]interface{}), `request.headers`),
|
||||||
})
|
})
|
||||||
|
|
||||||
repRequest = append(repRequest, api.SectionData{
|
repRequest = append(repRequest, api.SectionData{
|
||||||
Type: api.TABLE,
|
Type: api.TABLE,
|
||||||
Title: "Cookies",
|
Title: "Cookies",
|
||||||
Data: representMapSliceAsTable(request["_cookies"].([]interface{}), `request.cookies`),
|
Data: representMapAsTable(request["cookies"].(map[string]interface{}), `request.cookies`),
|
||||||
})
|
})
|
||||||
|
|
||||||
repRequest = append(repRequest, api.SectionData{
|
repRequest = append(repRequest, api.SectionData{
|
||||||
Type: api.TABLE,
|
Type: api.TABLE,
|
||||||
Title: "Query String",
|
Title: "Query String",
|
||||||
Data: representMapSliceAsTable(request["_queryStringMerged"].([]interface{}), `request.queryString`),
|
Data: representMapAsTable(request["queryString"].(map[string]interface{}), `request.queryString`),
|
||||||
})
|
})
|
||||||
|
|
||||||
postData, _ := request["postData"].(map[string]interface{})
|
postData, _ := request["postData"].(map[string]interface{})
|
||||||
@ -485,13 +479,13 @@ func representResponse(response map[string]interface{}) (repResponse []interface
|
|||||||
repResponse = append(repResponse, api.SectionData{
|
repResponse = append(repResponse, api.SectionData{
|
||||||
Type: api.TABLE,
|
Type: api.TABLE,
|
||||||
Title: "Headers",
|
Title: "Headers",
|
||||||
Data: representMapSliceAsTable(response["_headers"].([]interface{}), `response.headers`),
|
Data: representMapAsTable(response["headers"].(map[string]interface{}), `response.headers`),
|
||||||
})
|
})
|
||||||
|
|
||||||
repResponse = append(repResponse, api.SectionData{
|
repResponse = append(repResponse, api.SectionData{
|
||||||
Type: api.TABLE,
|
Type: api.TABLE,
|
||||||
Title: "Cookies",
|
Title: "Cookies",
|
||||||
Data: representMapSliceAsTable(response["_cookies"].([]interface{}), `response.cookies`),
|
Data: representMapAsTable(response["cookies"].(map[string]interface{}), `response.cookies`),
|
||||||
})
|
})
|
||||||
|
|
||||||
content, _ := response["content"].(map[string]interface{})
|
content, _ := response["content"].(map[string]interface{})
|
||||||
|
Loading…
Reference in New Issue
Block a user