Refactor the protocol payload structs

This commit is contained in:
M. Mert Yildiran
2021-08-23 17:06:35 +03:00
parent b2ffedfc2a
commit 842122b5a1
7 changed files with 11 additions and 18 deletions

View File

@@ -98,7 +98,6 @@ func emitAMQP(event interface{}, _type string, method string, connectionInfo *ap
IsRequest: true,
CaptureTime: time.Now(),
Payload: AMQPPayload{
Type: _type,
Data: &AMQPWrapper{
Method: method,
Url: "",

View File

@@ -5,9 +5,7 @@ import (
)
type AMQPPayload struct {
Type string
Method string
Data interface{}
Data interface{}
}
type AMQPPayloader interface {
@@ -16,10 +14,4 @@ type AMQPPayloader interface {
func (h AMQPPayload) MarshalJSON() ([]byte, error) {
return json.Marshal(h.Data)
// switch h.Type {
// case "amqp_request":
// return json.Marshal(h.Data)
// default:
// panic(fmt.Sprintf("AMQP payload cannot be marshaled: %s\n", h.Type))
// }
}

View File

@@ -39,6 +39,11 @@ var http2Protocol api.Protocol = api.Protocol{
Ports: []string{"80", "8080"},
}
const (
TypeHttpRequest = iota
TypeHttpResponse
)
func init() {
log.Println("Initializing HTTP extension.")
requestCounter = 0

View File

@@ -32,7 +32,7 @@ func (matcher *requestResponseMatcher) registerRequest(ident string, request *ht
IsRequest: true,
CaptureTime: captureTime,
Payload: HTTPPayload{
Type: "http_request",
Type: TypeHttpRequest,
Data: request,
},
}
@@ -61,7 +61,7 @@ func (matcher *requestResponseMatcher) registerResponse(ident string, response *
IsRequest: false,
CaptureTime: captureTime,
Payload: HTTPPayload{
Type: "http_response",
Type: TypeHttpResponse,
Data: response,
},
}

View File

@@ -11,7 +11,7 @@ import (
)
type HTTPPayload struct {
Type string
Type uint8
Data interface{}
}
@@ -27,7 +27,7 @@ type HTTPWrapper struct {
func (h HTTPPayload) MarshalJSON() ([]byte, error) {
switch h.Type {
case "http_request":
case TypeHttpRequest:
harRequest, err := har.NewRequest(h.Data.(*http.Request), true)
if err != nil {
rlog.Debugf("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err)
@@ -38,7 +38,7 @@ func (h HTTPPayload) MarshalJSON() ([]byte, error) {
Url: "",
Details: harRequest,
})
case "http_response":
case TypeHttpResponse:
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)

View File

@@ -7,7 +7,6 @@ import (
)
type KafkaPayload struct {
Type string
Data interface{}
}

View File

@@ -257,7 +257,6 @@ func ReadResponse(r io.Reader, tcpID *api.TcpID, emitter api.Emitter) (err error
IsRequest: true,
CaptureTime: time.Now(),
Payload: KafkaPayload{
Type: "kafka_request",
Data: &KafkaWrapper{
Method: apiNames[apiKey],
Url: "",
@@ -269,7 +268,6 @@ func ReadResponse(r io.Reader, tcpID *api.TcpID, emitter api.Emitter) (err error
IsRequest: false,
CaptureTime: time.Now(),
Payload: KafkaPayload{
Type: "kafka_response",
Data: &KafkaWrapper{
Method: apiNames[apiKey],
Url: "",