Introduce Version field to Protocol struct for dynamically switching the HTTP protocol to HTTP/2

This commit is contained in:
M. Mert Yildiran 2021-08-22 18:35:17 +03:00
parent 5900d59567
commit 820dc09976
No known key found for this signature in database
GPG Key ID: D42ADB236521BF7A
6 changed files with 42 additions and 22 deletions

View File

@ -237,9 +237,9 @@ func GetEntry(c *gin.Context) {
// }) // })
// } // }
extension := extensionsMap[entryData.ProtocolName] extension := extensionsMap[entryData.ProtocolName]
representation, _ := extension.Dissector.Represent(entryData.Entry) protocol, representation, _ := extension.Dissector.Represent(&entryData)
c.JSON(http.StatusOK, tapApi.MizuEntryWrapper{ c.JSON(http.StatusOK, tapApi.MizuEntryWrapper{
Protocol: extension.Protocol, Protocol: protocol,
Representation: string(representation), Representation: string(representation),
Data: entryData, Data: entryData,
}) })

View File

@ -10,11 +10,12 @@ type Protocol struct {
Name string `json:"name"` Name string `json:"name"`
LongName string `json:"long_name"` LongName string `json:"long_name"`
Abbreviation string `json:"abbreviation"` Abbreviation string `json:"abbreviation"`
Version string `json:"version"`
BackgroundColor string `json:"background_color"` BackgroundColor string `json:"background_color"`
ForegroundColor string `json:"foreground_color"` ForegroundColor string `json:"foreground_color"`
FontSize int8 `json:"font_size"` FontSize int8 `json:"font_size"`
ReferenceLink string `json:"reference_link"` ReferenceLink string `json:"reference_link"`
Ports []string `json:"outbound_ports"` Ports []string `json:"ports"`
} }
type Extension struct { type Extension struct {
@ -64,7 +65,7 @@ type Dissector interface {
Dissect(b *bufio.Reader, isClient bool, tcpID *TcpID, emitter Emitter) Dissect(b *bufio.Reader, isClient bool, tcpID *TcpID, emitter Emitter)
Analyze(item *OutputChannelItem, entryId string, resolvedSource string, resolvedDestination string) *MizuEntry Analyze(item *OutputChannelItem, entryId string, resolvedSource string, resolvedDestination string) *MizuEntry
Summarize(entry *MizuEntry) *BaseEntryDetails Summarize(entry *MizuEntry) *BaseEntryDetails
Represent(entry string) ([]byte, error) Represent(entry *MizuEntry) (Protocol, []byte, error)
} }
type Emitting struct { type Emitting struct {
@ -84,6 +85,7 @@ type MizuEntry struct {
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
ProtocolName string `json:"protocol_key" gorm:"column:protocolKey"` ProtocolName string `json:"protocol_key" gorm:"column:protocolKey"`
ProtocolVersion string `json:"protocol_version" gorm:"column:protocolVersion"`
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"`

View File

@ -15,6 +15,7 @@ var protocol api.Protocol = api.Protocol{
Name: "amqp", Name: "amqp",
LongName: "Advanced Message Queuing Protocol 0-9-1", LongName: "Advanced Message Queuing Protocol 0-9-1",
Abbreviation: "AMQP", Abbreviation: "AMQP",
Version: "0-9-1",
BackgroundColor: "#ff6600", BackgroundColor: "#ff6600",
ForegroundColor: "#ffffff", ForegroundColor: "#ffffff",
FontSize: 12, FontSize: 12,
@ -246,6 +247,7 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolve
entryBytes, _ := json.Marshal(item.Pair) entryBytes, _ := json.Marshal(item.Pair)
return &api.MizuEntry{ return &api.MizuEntry{
ProtocolName: protocol.Name, ProtocolName: protocol.Name,
ProtocolVersion: protocol.Version,
EntryId: entryId, EntryId: entryId,
Entry: string(entryBytes), Entry: string(entryBytes),
Url: fmt.Sprintf("%s%s", service, summary), Url: fmt.Sprintf("%s%s", service, summary),
@ -290,9 +292,9 @@ func (d dissecting) Summarize(entry *api.MizuEntry) *api.BaseEntryDetails {
} }
} }
func (d dissecting) Represent(entry string) ([]byte, error) { func (d dissecting) Represent(entry *api.MizuEntry) (api.Protocol, []byte, error) {
var root map[string]interface{} var root map[string]interface{}
json.Unmarshal([]byte(entry), &root) json.Unmarshal([]byte(entry.Entry), &root)
representation := make(map[string]interface{}, 0) representation := make(map[string]interface{}, 0)
request := root["request"].(map[string]interface{})["payload"].(map[string]interface{}) request := root["request"].(map[string]interface{})["payload"].(map[string]interface{})
var repRequest []interface{} var repRequest []interface{}
@ -323,12 +325,9 @@ func (d dissecting) Represent(entry string) ([]byte, error) {
repRequest = representBasicConsume(details) repRequest = representBasicConsume(details)
break break
} }
// response := root["response"].(map[string]interface{})["payload"].(map[string]interface{})
// repRequest := representRequest(request)
// repResponse := representResponse(response)
representation["request"] = repRequest representation["request"] = repRequest
// representation["response"] = repResponse object, err := json.Marshal(representation)
return json.Marshal(representation) return protocol, object, err
} }
var Dissector dissecting var Dissector dissecting

View File

@ -19,20 +19,22 @@ var protocol api.Protocol = api.Protocol{
Name: "http", Name: "http",
LongName: "Hypertext Transfer Protocol -- HTTP/1.1", LongName: "Hypertext Transfer Protocol -- HTTP/1.1",
Abbreviation: "HTTP", Abbreviation: "HTTP",
Version: "1.1",
BackgroundColor: "#205cf5", BackgroundColor: "#205cf5",
ForegroundColor: "#ffffff", ForegroundColor: "#ffffff",
FontSize: 12, FontSize: 12,
ReferenceLink: "https://datatracker.ietf.org/doc/html/rfc2616", ReferenceLink: "https://datatracker.ietf.org/doc/html/rfc2616",
Ports: []string{"80", "8080"}, Ports: []string{"80", "8080", "50051"},
} }
var http2Protocol api.Protocol = api.Protocol{ var http2Protocol api.Protocol = api.Protocol{
Name: "http", Name: "http",
LongName: "Hypertext Transfer Protocol Version 2 (HTTP/2) (gRPC)", LongName: "Hypertext Transfer Protocol Version 2 (HTTP/2) (gRPC)",
Abbreviation: "HTTP/2", Abbreviation: "HTTP/2",
Version: "2.0",
BackgroundColor: "#244c5a", BackgroundColor: "#244c5a",
ForegroundColor: "#ffffff", ForegroundColor: "#ffffff",
FontSize: 12, FontSize: 11,
ReferenceLink: "https://datatracker.ietf.org/doc/html/rfc7540", ReferenceLink: "https://datatracker.ietf.org/doc/html/rfc7540",
Ports: []string{"80", "8080"}, Ports: []string{"80", "8080"},
} }
@ -113,6 +115,7 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolve
service := fmt.Sprintf("http://%s", host) service := fmt.Sprintf("http://%s", host)
return &api.MizuEntry{ return &api.MizuEntry{
ProtocolName: protocol.Name, ProtocolName: protocol.Name,
ProtocolVersion: item.Protocol.Version,
EntryId: entryId, EntryId: entryId,
Entry: string(entryBytes), Entry: string(entryBytes),
Url: fmt.Sprintf("%s%s", service, request["url"].(string)), Url: fmt.Sprintf("%s%s", service, request["url"].(string)),
@ -133,9 +136,15 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolve
} }
func (d dissecting) Summarize(entry *api.MizuEntry) *api.BaseEntryDetails { func (d dissecting) Summarize(entry *api.MizuEntry) *api.BaseEntryDetails {
var p api.Protocol
if entry.ProtocolVersion == "2.0" {
p = http2Protocol
} else {
p = protocol
}
return &api.BaseEntryDetails{ return &api.BaseEntryDetails{
Id: entry.EntryId, Id: entry.EntryId,
Protocol: protocol, Protocol: p,
Url: entry.Url, Url: entry.Url,
RequestSenderIp: entry.RequestSenderIp, RequestSenderIp: entry.RequestSenderIp,
Service: entry.Service, Service: entry.Service,
@ -273,9 +282,15 @@ func representResponse(response map[string]interface{}) []interface{} {
return repResponse return repResponse
} }
func (d dissecting) Represent(entry string) ([]byte, error) { func (d dissecting) Represent(entry *api.MizuEntry) (api.Protocol, []byte, error) {
var p api.Protocol
if entry.ProtocolVersion == "2.0" {
p = http2Protocol
} else {
p = protocol
}
var root map[string]interface{} var root map[string]interface{}
json.Unmarshal([]byte(entry), &root) json.Unmarshal([]byte(entry.Entry), &root)
representation := make(map[string]interface{}, 0) representation := make(map[string]interface{}, 0)
request := root["request"].(map[string]interface{})["payload"].(map[string]interface{}) request := root["request"].(map[string]interface{})["payload"].(map[string]interface{})
response := root["response"].(map[string]interface{})["payload"].(map[string]interface{}) response := root["response"].(map[string]interface{})["payload"].(map[string]interface{})
@ -283,7 +298,8 @@ func (d dissecting) Represent(entry string) ([]byte, error) {
repResponse := representResponse(response) repResponse := representResponse(response)
representation["request"] = repRequest representation["request"] = repRequest
representation["response"] = repResponse representation["response"] = repResponse
return json.Marshal(representation) object, err := json.Marshal(representation)
return p, object, err
} }
var Dissector dissecting var Dissector dissecting

View File

@ -13,6 +13,7 @@ var _protocol api.Protocol = api.Protocol{
Name: "kafka", Name: "kafka",
LongName: "Apache Kafka Protocol", LongName: "Apache Kafka Protocol",
Abbreviation: "KAFKA", Abbreviation: "KAFKA",
Version: "12",
BackgroundColor: "#000000", BackgroundColor: "#000000",
ForegroundColor: "#ffffff", ForegroundColor: "#ffffff",
FontSize: 11, FontSize: 11,
@ -116,6 +117,7 @@ func (d dissecting) Analyze(item *api.OutputChannelItem, entryId string, resolve
entryBytes, _ := json.Marshal(item.Pair) entryBytes, _ := json.Marshal(item.Pair)
return &api.MizuEntry{ return &api.MizuEntry{
ProtocolName: _protocol.Name, ProtocolName: _protocol.Name,
ProtocolVersion: _protocol.Version,
EntryId: entryId, EntryId: entryId,
Entry: string(entryBytes), Entry: string(entryBytes),
Url: fmt.Sprintf("%s%s", service, summary), Url: fmt.Sprintf("%s%s", service, summary),
@ -159,9 +161,9 @@ func (d dissecting) Summarize(entry *api.MizuEntry) *api.BaseEntryDetails {
} }
} }
func (d dissecting) Represent(entry string) ([]byte, error) { func (d dissecting) Represent(entry *api.MizuEntry) (api.Protocol, []byte, error) {
var root map[string]interface{} var root map[string]interface{}
json.Unmarshal([]byte(entry), &root) json.Unmarshal([]byte(entry.Entry), &root)
representation := make(map[string]interface{}, 0) representation := make(map[string]interface{}, 0)
request := root["request"].(map[string]interface{})["payload"].(map[string]interface{}) request := root["request"].(map[string]interface{})["payload"].(map[string]interface{})
response := root["response"].(map[string]interface{})["payload"].(map[string]interface{}) response := root["response"].(map[string]interface{})["payload"].(map[string]interface{})
@ -205,7 +207,8 @@ func (d dissecting) Represent(entry string) ([]byte, error) {
representation["request"] = repRequest representation["request"] = repRequest
representation["response"] = repResponse representation["response"] = repResponse
return json.Marshal(representation) object, err := json.Marshal(representation)
return _protocol, object, err
} }
var Dissector dissecting var Dissector dissecting

View File

@ -9,7 +9,7 @@ export interface ProtocolInterface {
foreground_color: string foreground_color: string
font_size: number font_size: number
reference_link: string reference_link: string
outbound_ports: string[] ports: string[]
inbound_ports: string inbound_ports: string
} }
@ -26,7 +26,7 @@ const Protocol: React.FC<ProtocolProps> = ({protocol, horizontal}) => {
style={{ style={{
backgroundColor: protocol.background_color, backgroundColor: protocol.background_color,
color: protocol.foreground_color, color: protocol.foreground_color,
fontSize: protocol.font_size * 1.1, fontSize: 13,
}} }}
title={protocol.abbreviation} title={protocol.abbreviation}
> >