Introduce HTTPPayload struct and HTTPPayloader interface to MarshalJSON() all the data structures that are returned by the HTTP protocol

This commit is contained in:
M. Mert Yildiran
2021-08-19 21:38:18 +03:00
parent a7cd840522
commit 061cb098e9
4 changed files with 37 additions and 7 deletions

View File

@@ -2,12 +2,13 @@ package main
import ( import (
"fmt" "fmt"
"github.com/romana/rlog"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/romana/rlog"
"github.com/up9inc/mizu/tap/api" "github.com/up9inc/mizu/tap/api"
) )
@@ -31,7 +32,10 @@ func (matcher *requestResponseMatcher) registerRequest(ident string, request *ht
requestHTTPMessage := api.GenericMessage{ requestHTTPMessage := api.GenericMessage{
IsRequest: true, IsRequest: true,
CaptureTime: captureTime, CaptureTime: captureTime,
Orig: request, Orig: HTTPPayload{
Type: "http_request",
Data: request,
},
} }
if response, found := matcher.openMessagesMap.LoadAndDelete(key); found { if response, found := matcher.openMessagesMap.LoadAndDelete(key); found {
@@ -58,7 +62,10 @@ func (matcher *requestResponseMatcher) registerResponse(ident string, response *
responseHTTPMessage := api.GenericMessage{ responseHTTPMessage := api.GenericMessage{
IsRequest: false, IsRequest: false,
CaptureTime: captureTime, CaptureTime: captureTime,
Orig: response, Orig: HTTPPayload{
Type: "http_response",
Data: response,
},
} }
if request, found := matcher.openMessagesMap.LoadAndDelete(key); found { if request, found := matcher.openMessagesMap.LoadAndDelete(key); found {

View File

@@ -0,0 +1,25 @@
package main
import (
"fmt"
)
type HTTPPayload struct {
Type string
Data interface{}
}
type HTTPPayloader interface {
MarshalJSON() ([]byte, error)
}
func (h HTTPPayload) MarshalJSON() ([]byte, error) {
switch h.Type {
case "http_request":
return []byte("{\"val\": \"" + h.Type + "\"}"), nil
case "http_response":
return []byte("{\"val\": \"" + h.Type + "\"}"), nil
default:
panic(fmt.Sprintf("HTTP payload cannot be marshaled: %s\n", h.Type))
}
}

View File

@@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"log"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@@ -103,7 +102,7 @@ func containsPort(ports []string, port string) bool {
func (h *tcpReader) run(wg *sync.WaitGroup) { func (h *tcpReader) run(wg *sync.WaitGroup) {
defer wg.Done() defer wg.Done()
log.Printf("Called run h.isClient: %v\n", h.isClient) // log.Printf("Called run h.isClient: %v\n", h.isClient)
b := bufio.NewReader(h) b := bufio.NewReader(h)
if h.isClient { if h.isClient {
extensions[1].Dissector.Dissect(b, h.isClient, h.tcpID, h.Emitter) extensions[1].Dissector.Dissect(b, h.isClient, h.tcpID, h.Emitter)

View File

@@ -2,7 +2,6 @@ package tap
import ( import (
"fmt" "fmt"
"log"
"sync" "sync"
"github.com/romana/rlog" "github.com/romana/rlog"
@@ -26,7 +25,7 @@ type tcpStreamFactory struct {
} }
func (factory *tcpStreamFactory) New(net, transport gopacket.Flow, tcp *layers.TCP, ac reassembly.AssemblerContext) reassembly.Stream { func (factory *tcpStreamFactory) New(net, transport gopacket.Flow, tcp *layers.TCP, ac reassembly.AssemblerContext) reassembly.Stream {
log.Printf("* NEW: %s %s", net, transport) rlog.Debugf("* NEW: %s %s", net, transport)
fsmOptions := reassembly.TCPSimpleFSMOptions{ fsmOptions := reassembly.TCPSimpleFSMOptions{
SupportMissingEstablishment: *allowmissinginit, SupportMissingEstablishment: *allowmissinginit,
} }