mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-11 07:19:58 +00:00
Improve the HTTP request-response counter (still not perfect)
This commit is contained in:
@@ -14,17 +14,27 @@ import (
|
||||
"github.com/up9inc/mizu/tap/api"
|
||||
)
|
||||
|
||||
func populateCounterMap(ident string) {
|
||||
if counterMap[ident] == nil {
|
||||
counterMap[ident] = &counterPair{
|
||||
request: 0,
|
||||
response: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleHTTP2Stream(grpcAssembler *GrpcAssembler, tcpID *api.TcpID, emitter api.Emitter) error {
|
||||
streamID, messageHTTP1, err := grpcAssembler.readMessage()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
populateCounterMap(tcpID.Ident)
|
||||
|
||||
var item *api.OutputChannelItem
|
||||
|
||||
switch messageHTTP1 := messageHTTP1.(type) {
|
||||
case http.Request:
|
||||
requestCounter++
|
||||
counterMap[tcpID.Ident].request++
|
||||
ident := fmt.Sprintf(
|
||||
"%s->%s %s->%s %d",
|
||||
tcpID.SrcIP,
|
||||
@@ -44,7 +54,7 @@ func handleHTTP2Stream(grpcAssembler *GrpcAssembler, tcpID *api.TcpID, emitter a
|
||||
}
|
||||
}
|
||||
case http.Response:
|
||||
responseCounter++
|
||||
counterMap[tcpID.Ident].response++
|
||||
ident := fmt.Sprintf(
|
||||
"%s->%s %s->%s %d",
|
||||
tcpID.DstIP,
|
||||
@@ -74,13 +84,13 @@ func handleHTTP2Stream(grpcAssembler *GrpcAssembler, tcpID *api.TcpID, emitter a
|
||||
}
|
||||
|
||||
func handleHTTP1ClientStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emitter) error {
|
||||
requestCounter++
|
||||
req, err := http.ReadRequest(b)
|
||||
if err != nil {
|
||||
requestCounter--
|
||||
// log.Println("Error reading stream:", err)
|
||||
return err
|
||||
}
|
||||
populateCounterMap(tcpID.Ident)
|
||||
counterMap[tcpID.Ident].request++
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
req.Body = io.NopCloser(bytes.NewBuffer(body)) // rewind
|
||||
@@ -100,7 +110,7 @@ func handleHTTP1ClientStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emit
|
||||
tcpID.DstIP,
|
||||
tcpID.SrcPort,
|
||||
tcpID.DstPort,
|
||||
requestCounter,
|
||||
counterMap[tcpID.Ident].request,
|
||||
)
|
||||
item := reqResMatcher.registerRequest(ident, req, time.Now())
|
||||
if item != nil {
|
||||
@@ -117,13 +127,13 @@ func handleHTTP1ClientStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emit
|
||||
}
|
||||
|
||||
func handleHTTP1ServerStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emitter) error {
|
||||
responseCounter++
|
||||
res, err := http.ReadResponse(b, nil)
|
||||
if err != nil {
|
||||
responseCounter--
|
||||
// log.Println("Error reading stream:", err)
|
||||
return err
|
||||
}
|
||||
populateCounterMap(tcpID.Ident)
|
||||
counterMap[tcpID.Ident].response++
|
||||
var req string
|
||||
req = fmt.Sprintf("<no-request-seen>")
|
||||
|
||||
@@ -153,7 +163,7 @@ func handleHTTP1ServerStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emit
|
||||
tcpID.SrcIP,
|
||||
tcpID.DstPort,
|
||||
tcpID.SrcPort,
|
||||
responseCounter,
|
||||
counterMap[tcpID.Ident].response,
|
||||
)
|
||||
item := reqResMatcher.registerResponse(ident, res, time.Now())
|
||||
if item != nil {
|
||||
|
@@ -12,8 +12,12 @@ import (
|
||||
"github.com/up9inc/mizu/tap/api"
|
||||
)
|
||||
|
||||
var requestCounter uint
|
||||
var responseCounter uint
|
||||
type counterPair struct {
|
||||
request uint
|
||||
response uint
|
||||
}
|
||||
|
||||
var counterMap map[string]*counterPair
|
||||
|
||||
var protocol api.Protocol = api.Protocol{
|
||||
Name: "http",
|
||||
@@ -48,8 +52,7 @@ const (
|
||||
|
||||
func init() {
|
||||
log.Println("Initializing HTTP extension.")
|
||||
requestCounter = 0
|
||||
responseCounter = 0
|
||||
counterMap = make(map[string]*counterPair)
|
||||
}
|
||||
|
||||
type dissecting string
|
||||
|
Reference in New Issue
Block a user