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, IsRequest: true,
CaptureTime: time.Now(), CaptureTime: time.Now(),
Payload: AMQPPayload{ Payload: AMQPPayload{
Type: _type,
Data: &AMQPWrapper{ Data: &AMQPWrapper{
Method: method, Method: method,
Url: "", Url: "",

View File

@@ -5,8 +5,6 @@ import (
) )
type AMQPPayload struct { type AMQPPayload struct {
Type string
Method string
Data interface{} Data interface{}
} }
@@ -16,10 +14,4 @@ type AMQPPayloader interface {
func (h AMQPPayload) MarshalJSON() ([]byte, error) { func (h AMQPPayload) MarshalJSON() ([]byte, error) {
return json.Marshal(h.Data) 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"}, Ports: []string{"80", "8080"},
} }
const (
TypeHttpRequest = iota
TypeHttpResponse
)
func init() { func init() {
log.Println("Initializing HTTP extension.") log.Println("Initializing HTTP extension.")
requestCounter = 0 requestCounter = 0

View File

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

View File

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

View File

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