From 55806495242672ccf18e6bd96a73956bce61366f Mon Sep 17 00:00:00 2001 From: therealbobo Date: Fri, 11 Sep 2020 18:00:19 +0200 Subject: added support to http response body decoding --- connection_streams_controller.go | 8 +++++++- utils.go | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/connection_streams_controller.go b/connection_streams_controller.go index 251e842..000e3d4 100644 --- a/connection_streams_controller.go +++ b/connection_streams_controller.go @@ -110,9 +110,15 @@ func (csc ConnectionStreamsController) GetConnectionPayload(c context.Context, c } size := uint64(end - start) + content := DecodeBytes(serverStream.Payload[start:end], format.Format) + // check if is encoded + if format.Format == "default" { + content = DecodeHttpResponse(content) + } + payload = Payload{ FromClient: false, - Content: DecodeBytes(serverStream.Payload[start:end], format.Format), + Content: content, Index: start, Timestamp: serverStream.BlocksTimestamps[serverBlocksIndex], IsRetransmitted: serverStream.BlocksLoss[serverBlocksIndex], diff --git a/utils.go b/utils.go index a14fdca..e222b17 100644 --- a/utils.go +++ b/utils.go @@ -13,6 +13,12 @@ import ( "net" "os" "time" + //"net/textproto" + "net/http" + "bufio" + "strings" + "io/ioutil" + "compress/gzip" ) func Sha256Sum(fileName string) (string, error) { @@ -108,6 +114,41 @@ func DecodeBytes(buffer []byte, format string) string { } } +func DecodeHttpResponse(raw string) string { + var header string + trailer := "\n" + reader := bufio.NewReader(strings.NewReader(raw)) + resp,err := http.ReadResponse(reader, &http.Request{}) + if err != nil{ + log.Info("Reading response: ",resp) + return raw + trailer + } + + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + var bodyReader io.ReadCloser + switch resp.Header.Get("Content-Encoding") { + case "gzip": + bodyReader, err = gzip.NewReader(resp.Body) + if err != nil { + log.Error("Gunzipping body: ",err) + } + header = "\n[==== GUNZIPPED ====]\n" + trailer = "\n[===================]\n" + defer bodyReader.Close() + default: + bodyReader = resp.Body + } + body, err := ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading body: ",err) + } + return raw + header + string(body) + trailer + } + return raw + trailer +} + func CopyFile(dst, src string) error { in, err := os.Open(src) if err != nil { -- cgit v1.2.3-70-g09d2 From d67d6c7b59c0454e0fdfdc3adcfd2064c9b1810e Mon Sep 17 00:00:00 2001 From: therealbobo Date: Sat, 12 Sep 2020 17:17:12 +0200 Subject: fixed scrollbar in connection-content class --- frontend/src/components/ConnectionContent.scss | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/ConnectionContent.scss b/frontend/src/components/ConnectionContent.scss index a1b4afd..5a17066 100644 --- a/frontend/src/components/ConnectionContent.scss +++ b/frontend/src/components/ConnectionContent.scss @@ -3,7 +3,7 @@ .connection-content { background-color: $color-primary-3; height: 100%; - overflow: auto; + overflow: fixed; pre { background-color: $color-primary-0; @@ -11,7 +11,7 @@ word-break: break-word; max-width: 100%; white-space: pre-wrap; - height: 100%; + height: 95%; } .from-client { -- cgit v1.2.3-70-g09d2 From 523ed67dc2d0f800efc68d414b37bb001535d3ee Mon Sep 17 00:00:00 2001 From: therealbobo Date: Sat, 12 Sep 2020 17:40:02 +0200 Subject: added decoded_content field --- connection_streams_controller.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/connection_streams_controller.go b/connection_streams_controller.go index 000e3d4..096210e 100644 --- a/connection_streams_controller.go +++ b/connection_streams_controller.go @@ -27,6 +27,7 @@ type PatternSlice [2]uint64 type Payload struct { FromClient bool `json:"from_client"` Content string `json:"content"` + DecodedContent string `json:"decoded_content"` Index int `json:"index"` Timestamp time.Time `json:"timestamp"` IsRetransmitted bool `json:"is_retransmitted"` @@ -92,6 +93,7 @@ func (csc ConnectionStreamsController) GetConnectionPayload(c context.Context, c payload = Payload{ FromClient: true, Content: DecodeBytes(clientStream.Payload[start:end], format.Format), + //Request: ReadRequest(content), Index: start, Timestamp: clientStream.BlocksTimestamps[clientBlocksIndex], IsRetransmitted: clientStream.BlocksLoss[clientBlocksIndex], @@ -111,14 +113,14 @@ func (csc ConnectionStreamsController) GetConnectionPayload(c context.Context, c size := uint64(end - start) content := DecodeBytes(serverStream.Payload[start:end], format.Format) - // check if is encoded - if format.Format == "default" { - content = DecodeHttpResponse(content) - } + + plainContent := DecodeBytes(serverStream.Payload[start:end], "default") + decodedContent := DecodeBytes([]byte(DecodeHttpResponse(plainContent)), format.Format) payload = Payload{ FromClient: false, Content: content, + DecodedContent: decodedContent, Index: start, Timestamp: serverStream.BlocksTimestamps[serverBlocksIndex], IsRetransmitted: serverStream.BlocksLoss[serverBlocksIndex], -- cgit v1.2.3-70-g09d2 From ad7e9226bac7524462755f5916b076408ebd86cb Mon Sep 17 00:00:00 2001 From: therealbobo Date: Sat, 12 Sep 2020 17:40:45 +0200 Subject: better decode handling --- utils.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/utils.go b/utils.go index e222b17..b07244d 100644 --- a/utils.go +++ b/utils.go @@ -13,7 +13,6 @@ import ( "net" "os" "time" - //"net/textproto" "net/http" "bufio" "strings" @@ -114,19 +113,52 @@ func DecodeBytes(buffer []byte, format string) string { } } +func ReadRequest(raw string) http.Request { + reader := bufio.NewReader(strings.NewReader(raw)) + req,err := http.ReadRequest(reader) + if err != nil{ + log.Info("Reading request: ",req) + return http.Request{} + } + return *req +} + +func GetHeader(raw string) string{ + tmp := strings.Split(raw,"\r\n") + end := len(tmp) + for i, line := range tmp{ + if line == ""{ + end = i + break + } + } + return strings.Join(tmp[:end],"\r\n") +} + +func GetBody(raw string) string{ + tmp := strings.Split(raw,"\r\n") + start := 0 + for i, line := range tmp{ + if line == ""{ + start = i + 2 + break + } + } + return strings.Join(tmp[start:],"\r\n") +} + func DecodeHttpResponse(raw string) string { - var header string - trailer := "\n" + body := []byte{} reader := bufio.NewReader(strings.NewReader(raw)) resp,err := http.ReadResponse(reader, &http.Request{}) if err != nil{ log.Info("Reading response: ",resp) - return raw + trailer + return "" } defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { + if resp.StatusCode >= 200 && resp.StatusCode < 300 { var bodyReader io.ReadCloser switch resp.Header.Get("Content-Encoding") { case "gzip": @@ -134,19 +166,28 @@ func DecodeHttpResponse(raw string) string { if err != nil { log.Error("Gunzipping body: ",err) } - header = "\n[==== GUNZIPPED ====]\n" - trailer = "\n[===================]\n" defer bodyReader.Close() + body, err = ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading gzipped body: ",err) + // if the response is malformed + // or the connection is closed + fallbackReader, _ := gzip.NewReader(strings.NewReader(GetBody(raw))) + body, err = ioutil.ReadAll(fallbackReader) + if err != nil{ + log.Error(string(body)) + } + } default: bodyReader = resp.Body + body, err = ioutil.ReadAll(bodyReader) + if err != nil{ + log.Error("Reading body: ",err) + body = []byte(GetBody(raw)) + } } - body, err := ioutil.ReadAll(bodyReader) - if err != nil{ - log.Error("Reading body: ",err) - } - return raw + header + string(body) + trailer } - return raw + trailer + return GetHeader(raw) + "\r\n\r\n"+ string(body) } func CopyFile(dst, src string) error { -- cgit v1.2.3-70-g09d2 From e7c2428e24d0fc6aebf524130531800a89332829 Mon Sep 17 00:00:00 2001 From: therealbobo Date: Sat, 12 Sep 2020 17:41:03 +0200 Subject: initial decoding support --- frontend/src/components/ConnectionContent.js | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/frontend/src/components/ConnectionContent.js b/frontend/src/components/ConnectionContent.js index 905a56d..2100a68 100644 --- a/frontend/src/components/ConnectionContent.js +++ b/frontend/src/components/ConnectionContent.js @@ -1,6 +1,6 @@ import React, {Component} from 'react'; import './ConnectionContent.scss'; -import {Dropdown} from 'react-bootstrap'; +import {Dropdown, Button} from 'react-bootstrap'; import axios from 'axios'; class ConnectionContent extends Component { @@ -10,7 +10,8 @@ class ConnectionContent extends Component { this.state = { loading: false, connectionContent: null, - format: "default" + format: "default", + decoded: false, }; this.validFormats = ["default", "hex", "hexdump", "base32", "base64", "ascii", "binary", "decimal", "octal"]; @@ -37,6 +38,10 @@ class ConnectionContent extends Component { } } + toggleDecoded() { + this.setState({decoded: !this.state.decoded}); + } + render() { let content = this.state.connectionContent; @@ -46,7 +51,22 @@ class ConnectionContent extends Component { let payload = content.map((c, i) => - {c.content} + {c.from_client + ? +
{c.content}
+ : + <> + {c.decoded_content + ? + <> +
{c.content}
+
{c.decoded_content}
+ + : +
{c.content}
+ } + + }
); @@ -69,6 +89,9 @@ class ConnectionContent extends Component { decimal octal + + + -- cgit v1.2.3-70-g09d2