From 6f84138eb7c36a880fcf9d60a81070cc2fb2abf0 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Sun, 22 Aug 2021 20:46:17 +0300 Subject: [PATCH] Fix the issues in summary section of details layout for HTTP/2 (gRPC) protocol --- tap/extensions/http/main.go | 27 ++++++++++++++++++--------- tap/extensions/http/structs.go | 18 ++++++++++++++++-- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/tap/extensions/http/main.go b/tap/extensions/http/main.go index 203525de6..a74db0d3f 100644 --- a/tap/extensions/http/main.go +++ b/tap/extensions/http/main.go @@ -103,7 +103,13 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolvedSource string, resolvedDestination string) *api.MizuEntry { var host, scheme, authority, path, service string - for _, header := range item.Pair.Request.Payload.(map[string]interface{})["headers"].([]interface{}) { + + request := item.Pair.Request.Payload.(map[string]interface{}) + response := item.Pair.Response.Payload.(map[string]interface{}) + reqDetails := request["details"].(map[string]interface{}) + resDetails := response["details"].(map[string]interface{}) + + for _, header := range reqDetails["headers"].([]interface{}) { h := header.(map[string]interface{}) if h["name"] == "Host" { host = h["value"].(string) @@ -118,23 +124,24 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolve path = h["value"].(string) } } - request := item.Pair.Request.Payload.(map[string]interface{}) - response := item.Pair.Response.Payload.(map[string]interface{}) - entryBytes, _ := json.Marshal(item.Pair) + if item.Protocol.Version == "2.0" { service = fmt.Sprintf("(gRPC) %s://%s", scheme, authority) } else { service = fmt.Sprintf("http://%s", host) - path = request["url"].(string) + path = reqDetails["url"].(string) } + + request["url"] = path + entryBytes, _ := json.Marshal(item.Pair) return &api.MizuEntry{ ProtocolName: protocol.Name, ProtocolVersion: item.Protocol.Version, EntryId: entryId, Entry: string(entryBytes), Url: fmt.Sprintf("%s%s", service, path), - Method: request["method"].(string), - Status: int(response["status"].(float64)), + Method: reqDetails["method"].(string), + Status: int(resDetails["status"].(float64)), RequestSenderIp: item.ConnectionInfo.ClientIP, Service: service, Timestamp: item.Timestamp, @@ -308,8 +315,10 @@ func (d dissecting) Represent(entry *api.MizuEntry) (api.Protocol, []byte, error representation := make(map[string]interface{}, 0) request := root["request"].(map[string]interface{})["payload"].(map[string]interface{}) response := root["response"].(map[string]interface{})["payload"].(map[string]interface{}) - repRequest := representRequest(request) - repResponse := representResponse(response) + reqDetails := request["details"].(map[string]interface{}) + resDetails := response["details"].(map[string]interface{}) + repRequest := representRequest(reqDetails) + repResponse := representResponse(resDetails) representation["request"] = repRequest representation["response"] = repResponse object, err := json.Marshal(representation) diff --git a/tap/extensions/http/structs.go b/tap/extensions/http/structs.go index 05be81fc8..ef0f7b6a2 100644 --- a/tap/extensions/http/structs.go +++ b/tap/extensions/http/structs.go @@ -19,6 +19,12 @@ type HTTPPayloader interface { MarshalJSON() ([]byte, error) } +type HTTPWrapper struct { + Method string `json:"method"` + Url string `json:"url"` + Details interface{} `json:"details"` +} + func (h HTTPPayload) MarshalJSON() ([]byte, error) { switch h.Type { case "http_request": @@ -27,14 +33,22 @@ func (h HTTPPayload) MarshalJSON() ([]byte, error) { rlog.Debugf("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err) return nil, errors.New("Failed converting request to HAR") } - return json.Marshal(harRequest) + return json.Marshal(&HTTPWrapper{ + Method: harRequest.Method, + Url: "", + Details: harRequest, + }) case "http_response": harResponse, err := har.NewResponse(h.Data.(*http.Response), true) if err != nil { rlog.Debugf("convert-response-to-har", "Failed converting response to HAR %s (%v,%+v)", err, err, err) return nil, errors.New("Failed converting response to HAR") } - return json.Marshal(harResponse) + return json.Marshal(&HTTPWrapper{ + Method: "", + Url: "", + Details: harResponse, + }) default: panic(fmt.Sprintf("HTTP payload cannot be marshaled: %s\n", h.Type)) }