mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-22 18:36:16 +00:00
remove some unwanted code
This commit is contained in:
parent
54f5a3432b
commit
f078aa6c36
@ -3,14 +3,10 @@ module github.com/up9inc/mizu/tap/extensions/http
|
|||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/martian v2.1.0+incompatible
|
|
||||||
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7
|
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7
|
||||||
github.com/up9inc/mizu/tap/api v0.0.0
|
github.com/up9inc/mizu/tap/api v0.0.0
|
||||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7
|
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7
|
||||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073
|
golang.org/x/text v0.3.5 // indirect
|
||||||
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
|
|
||||||
golang.org/x/text v0.3.5
|
|
||||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a
|
|
||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/up9inc/mizu/tap/api v0.0.0 => ../../api
|
replace github.com/up9inc/mizu/tap/api v0.0.0 => ../../api
|
||||||
|
@ -1,39 +1,10 @@
|
|||||||
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
|
|
||||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
|
||||||
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7 h1:jkvpcEatpwuMF5O5LVxTnehj6YZ/aEZN4NWD/Xml4pI=
|
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7 h1:jkvpcEatpwuMF5O5LVxTnehj6YZ/aEZN4NWD/Xml4pI=
|
||||||
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7/go.mod h1:KTrHyWpO1sevuXPZwyeZc72ddWRFqNSKDFl7uVWKpg0=
|
github.com/romana/rlog v0.0.0-20171115192701-f018bc92e7d7/go.mod h1:KTrHyWpO1sevuXPZwyeZc72ddWRFqNSKDFl7uVWKpg0=
|
||||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
|
||||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
|
||||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 h1:OgUuv8lsRpBibGNbSizVwKWlysjaNzmC9gYMhPVfqFM=
|
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7 h1:OgUuv8lsRpBibGNbSizVwKWlysjaNzmC9gYMhPVfqFM=
|
||||||
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210224082022-3d97a244fca7/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073 h1:8qxJSnu+7dRq6upnbntrmriWByIakBuct5OM/MdQC1M=
|
|
||||||
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
|
|
||||||
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
|
|
||||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
|
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
|
||||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a h1:CB3a9Nez8M13wwlr/E2YtwoU+qYHKfC+JrDa45RXXoQ=
|
|
||||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
|
@ -25,7 +25,7 @@ const protoHTTP2 = "HTTP/2.0"
|
|||||||
const protoMajorHTTP2 = 2
|
const protoMajorHTTP2 = 2
|
||||||
const protoMinorHTTP2 = 0
|
const protoMinorHTTP2 = 0
|
||||||
|
|
||||||
var maxHTTP2DataLen int = 1 * 1024 * 1024 // 1MB
|
var maxHTTP2DataLen = 1 * 1024 * 1024 // 1MB
|
||||||
|
|
||||||
type messageFragment struct {
|
type messageFragment struct {
|
||||||
headers []hpack.HeaderField
|
headers []hpack.HeaderField
|
||||||
@ -139,7 +139,7 @@ func (ga *GrpcAssembler) readMessage() (uint32, interface{}, error) {
|
|||||||
ContentLength: int64(len(dataString)),
|
ContentLength: int64(len(dataString)),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return 0, nil, errors.New("Failed to assemble stream: neither a request nor a message")
|
return 0, nil, errors.New("failed to assemble stream: neither a request nor a message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return streamID, messageHTTP1, nil
|
return streamID, messageHTTP1, nil
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/romana/rlog"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@ -127,10 +128,10 @@ func handleHTTP1ServerStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emit
|
|||||||
res.Body = io.NopCloser(bytes.NewBuffer(body)) // rewind
|
res.Body = io.NopCloser(bytes.NewBuffer(body)) // rewind
|
||||||
s := len(body)
|
s := len(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SilentError("HTTP-response-body", "HTTP/%s: failed to get body(parsed len:%d): %s", tcpID.Ident, s, err)
|
rlog.Debugf("[HTTP-response-body] HTTP/%s: failed to get body(parsed len:%d): %s", tcpID.Ident, s, err)
|
||||||
}
|
}
|
||||||
if err := res.Body.Close(); err != nil {
|
if err := res.Body.Close(); err != nil {
|
||||||
SilentError("HTTP-response-body-close", "HTTP/%s: failed to close body(parsed len:%d): %s", tcpID.Ident, s, err)
|
rlog.Debugf("[HTTP-response-body-close] HTTP/%s: failed to close body(parsed len:%d): %s", tcpID.Ident, s, err)
|
||||||
}
|
}
|
||||||
sym := ","
|
sym := ","
|
||||||
if res.ContentLength > 0 && res.ContentLength != int64(s) {
|
if res.ContentLength > 0 && res.ContentLength != int64(s) {
|
||||||
@ -141,7 +142,7 @@ func handleHTTP1ServerStream(b *bufio.Reader, tcpID *api.TcpID, emitter api.Emit
|
|||||||
contentType = []string{http.DetectContentType(body)}
|
contentType = []string{http.DetectContentType(body)}
|
||||||
}
|
}
|
||||||
encoding := res.Header["Content-Encoding"]
|
encoding := res.Header["Content-Encoding"]
|
||||||
Debug("HTTP/1 Response: %s %s URL:%s (%d%s%d%s) -> %s", tcpID.Ident, res.Status, req, res.ContentLength, sym, s, contentType, encoding)
|
rlog.Tracef(1, "HTTP/1 Response: %s %s URL:%s (%d%s%d%s) -> %s", tcpID.Ident, res.Status, req, res.ContentLength, sym, s, contentType, encoding)
|
||||||
|
|
||||||
ident := fmt.Sprintf(
|
ident := fmt.Sprintf(
|
||||||
"%s->%s %s->%s %d",
|
"%s->%s %s->%s %d",
|
||||||
|
@ -1,316 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/martian/har"
|
|
||||||
"github.com/romana/rlog"
|
|
||||||
)
|
|
||||||
|
|
||||||
var outputLevel int
|
|
||||||
var errorsMap map[string]uint
|
|
||||||
var errorsMapMutex sync.Mutex
|
|
||||||
var nErrors uint
|
|
||||||
var ownIps []string // global
|
|
||||||
var hostMode bool // global
|
|
||||||
|
|
||||||
func logError(minOutputLevel int, t string, s string, a ...interface{}) {
|
|
||||||
errorsMapMutex.Lock()
|
|
||||||
nErrors++
|
|
||||||
nb, _ := errorsMap[t]
|
|
||||||
errorsMap[t] = nb + 1
|
|
||||||
errorsMapMutex.Unlock()
|
|
||||||
|
|
||||||
if outputLevel >= minOutputLevel {
|
|
||||||
formatStr := fmt.Sprintf("%s: %s", t, s)
|
|
||||||
rlog.Errorf(formatStr, a...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func Error(t string, s string, a ...interface{}) {
|
|
||||||
logError(0, t, s, a...)
|
|
||||||
}
|
|
||||||
func SilentError(t string, s string, a ...interface{}) {
|
|
||||||
logError(2, t, s, a...)
|
|
||||||
}
|
|
||||||
func Debug(s string, a ...interface{}) {
|
|
||||||
rlog.Debugf(s, a...)
|
|
||||||
}
|
|
||||||
func Trace(s string, a ...interface{}) {
|
|
||||||
rlog.Tracef(1, s, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
const readPermission = 0644
|
|
||||||
const harFilenameSuffix = ".har"
|
|
||||||
const tempFilenameSuffix = ".har.tmp"
|
|
||||||
|
|
||||||
type PairChanItem struct {
|
|
||||||
Request *http.Request
|
|
||||||
RequestTime time.Time
|
|
||||||
Response *http.Response
|
|
||||||
ResponseTime time.Time
|
|
||||||
RequestSenderIp string
|
|
||||||
ConnectionInfo *ConnectionInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func openNewHarFile(filename string) *HarFile {
|
|
||||||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, readPermission)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Failed to open output file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
harFile := HarFile{file: file, entryCount: 0}
|
|
||||||
harFile.writeHeader()
|
|
||||||
|
|
||||||
return &harFile
|
|
||||||
}
|
|
||||||
|
|
||||||
type HarFile struct {
|
|
||||||
file *os.File
|
|
||||||
entryCount int
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEntry(request *http.Request, requestTime time.Time, response *http.Response, responseTime time.Time) (*har.Entry, error) {
|
|
||||||
harRequest, err := har.NewRequest(request, false)
|
|
||||||
if err != nil {
|
|
||||||
SilentError("convert-request-to-har", "Failed converting request to HAR %s (%v,%+v)", err, err, err)
|
|
||||||
return nil, errors.New("Failed converting request to HAR")
|
|
||||||
}
|
|
||||||
|
|
||||||
// For requests with multipart/form-data or application/x-www-form-urlencoded Content-Type,
|
|
||||||
// martian/har will parse the request body and place the parameters in harRequest.PostData.Params
|
|
||||||
// instead of harRequest.PostData.Text (as the HAR spec requires it).
|
|
||||||
// Mizu currently only looks at PostData.Text. Therefore, instead of letting martian/har set the content of
|
|
||||||
// PostData, always copy the request body to PostData.Text.
|
|
||||||
if request.ContentLength > 0 {
|
|
||||||
reqBody, err := ioutil.ReadAll(request.Body)
|
|
||||||
if err != nil {
|
|
||||||
SilentError("read-request-body", "Failed converting request to HAR %s (%v,%+v)", err, err, err)
|
|
||||||
return nil, errors.New("Failed reading request body")
|
|
||||||
}
|
|
||||||
request.Body = ioutil.NopCloser(bytes.NewReader(reqBody))
|
|
||||||
harRequest.PostData.Text = string(reqBody)
|
|
||||||
}
|
|
||||||
|
|
||||||
harResponse, err := har.NewResponse(response, true)
|
|
||||||
if err != nil {
|
|
||||||
SilentError("convert-response-to-har", "Failed converting response to HAR %s (%v,%+v)", err, err, err)
|
|
||||||
return nil, errors.New("Failed converting response to HAR")
|
|
||||||
}
|
|
||||||
|
|
||||||
if harRequest.PostData != nil && strings.HasPrefix(harRequest.PostData.MimeType, "application/grpc") {
|
|
||||||
// Force HTTP/2 gRPC into HAR template
|
|
||||||
|
|
||||||
harRequest.URL = fmt.Sprintf("%s://%s%s", request.Header.Get(":scheme"), request.Header.Get(":authority"), request.Header.Get(":path"))
|
|
||||||
|
|
||||||
status, err := strconv.Atoi(response.Header.Get(":status"))
|
|
||||||
if err != nil {
|
|
||||||
SilentError("convert-response-status-for-har", "Failed converting status to int %s (%v,%+v)", err, err, err)
|
|
||||||
return nil, errors.New("Failed converting response status to int for HAR")
|
|
||||||
}
|
|
||||||
harResponse.Status = status
|
|
||||||
} else {
|
|
||||||
// Martian copies http.Request.URL.String() to har.Request.URL, which usually contains the path.
|
|
||||||
// However, according to the HAR spec, the URL field needs to be the absolute URL.
|
|
||||||
var scheme string
|
|
||||||
if request.URL.Scheme != "" {
|
|
||||||
scheme = request.URL.Scheme
|
|
||||||
} else {
|
|
||||||
scheme = "http"
|
|
||||||
}
|
|
||||||
harRequest.URL = fmt.Sprintf("%s://%s%s", scheme, request.Host, request.URL)
|
|
||||||
}
|
|
||||||
|
|
||||||
totalTime := responseTime.Sub(requestTime).Round(time.Millisecond).Milliseconds()
|
|
||||||
if totalTime < 1 {
|
|
||||||
totalTime = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
harEntry := har.Entry{
|
|
||||||
StartedDateTime: time.Now().UTC(),
|
|
||||||
Time: totalTime,
|
|
||||||
Request: harRequest,
|
|
||||||
Response: harResponse,
|
|
||||||
Cache: &har.Cache{},
|
|
||||||
Timings: &har.Timings{
|
|
||||||
Send: -1,
|
|
||||||
Wait: -1,
|
|
||||||
Receive: totalTime,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return &harEntry, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *HarFile) WriteEntry(harEntry *har.Entry) {
|
|
||||||
harEntryJson, err := json.Marshal(harEntry)
|
|
||||||
if err != nil {
|
|
||||||
SilentError("har-entry-marshal", "Failed converting har entry object to JSON%s (%v,%+v)", err, err, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var separator string
|
|
||||||
if f.GetEntryCount() > 0 {
|
|
||||||
separator = ","
|
|
||||||
} else {
|
|
||||||
separator = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
harEntryString := append([]byte(separator), harEntryJson...)
|
|
||||||
|
|
||||||
if _, err := f.file.Write(harEntryString); err != nil {
|
|
||||||
log.Panicf("Failed to write to output file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.entryCount++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *HarFile) GetEntryCount() int {
|
|
||||||
return f.entryCount
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *HarFile) Close() {
|
|
||||||
f.writeTrailer()
|
|
||||||
|
|
||||||
err := f.file.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("Failed to close output file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *HarFile) writeHeader() {
|
|
||||||
header := []byte(`{"log": {"version": "1.2", "creator": {"name": "Mizu", "version": "0.0.1"}, "entries": [`)
|
|
||||||
if _, err := f.file.Write(header); err != nil {
|
|
||||||
log.Panicf("Failed to write header to output file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *HarFile) writeTrailer() {
|
|
||||||
trailer := []byte("]}}")
|
|
||||||
if _, err := f.file.Write(trailer); err != nil {
|
|
||||||
log.Panicf("Failed to write trailer to output file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHarWriter(outputDir string, maxEntries int) *HarWriter {
|
|
||||||
return &HarWriter{
|
|
||||||
OutputDirPath: outputDir,
|
|
||||||
MaxEntries: maxEntries,
|
|
||||||
PairChan: make(chan *PairChanItem),
|
|
||||||
OutChan: make(chan *OutputChannelItem, 1000),
|
|
||||||
currentFile: nil,
|
|
||||||
done: make(chan bool),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type ConnectionInfo struct {
|
|
||||||
ClientIP string
|
|
||||||
ClientPort string
|
|
||||||
ServerIP string
|
|
||||||
ServerPort string
|
|
||||||
IsOutgoing bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type OutputChannelItem struct {
|
|
||||||
HarEntry *har.Entry
|
|
||||||
ConnectionInfo *ConnectionInfo
|
|
||||||
ValidationRulesChecker string
|
|
||||||
}
|
|
||||||
|
|
||||||
type HarWriter struct {
|
|
||||||
OutputDirPath string
|
|
||||||
MaxEntries int
|
|
||||||
PairChan chan *PairChanItem
|
|
||||||
OutChan chan *OutputChannelItem
|
|
||||||
currentFile *HarFile
|
|
||||||
done chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *HarWriter) WritePair(request *http.Request, requestTime time.Time, response *http.Response, responseTime time.Time, connectionInfo *ConnectionInfo) {
|
|
||||||
hw.PairChan <- &PairChanItem{
|
|
||||||
Request: request,
|
|
||||||
RequestTime: requestTime,
|
|
||||||
Response: response,
|
|
||||||
ResponseTime: responseTime,
|
|
||||||
ConnectionInfo: connectionInfo,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *HarWriter) Start() {
|
|
||||||
if hw.OutputDirPath != "" {
|
|
||||||
if err := os.MkdirAll(hw.OutputDirPath, os.ModePerm); err != nil {
|
|
||||||
log.Panicf("Failed to create output directory: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for pair := range hw.PairChan {
|
|
||||||
harEntry, err := NewEntry(pair.Request, pair.RequestTime, pair.Response, pair.ResponseTime)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if hw.OutputDirPath != "" {
|
|
||||||
if hw.currentFile == nil {
|
|
||||||
hw.openNewFile()
|
|
||||||
}
|
|
||||||
|
|
||||||
hw.currentFile.WriteEntry(harEntry)
|
|
||||||
|
|
||||||
if hw.currentFile.GetEntryCount() >= hw.MaxEntries {
|
|
||||||
hw.closeFile()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
hw.OutChan <- &OutputChannelItem{
|
|
||||||
HarEntry: harEntry,
|
|
||||||
ConnectionInfo: pair.ConnectionInfo,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if hw.currentFile != nil {
|
|
||||||
hw.closeFile()
|
|
||||||
}
|
|
||||||
hw.done <- true
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *HarWriter) Stop() {
|
|
||||||
close(hw.PairChan)
|
|
||||||
<-hw.done
|
|
||||||
close(hw.OutChan)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *HarWriter) openNewFile() {
|
|
||||||
filename := buildFilename(hw.OutputDirPath, time.Now(), tempFilenameSuffix)
|
|
||||||
hw.currentFile = openNewHarFile(filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hw *HarWriter) closeFile() {
|
|
||||||
hw.currentFile.Close()
|
|
||||||
tmpFilename := hw.currentFile.file.Name()
|
|
||||||
hw.currentFile = nil
|
|
||||||
|
|
||||||
filename := buildFilename(hw.OutputDirPath, time.Now(), harFilenameSuffix)
|
|
||||||
err := os.Rename(tmpFilename, filename)
|
|
||||||
if err != nil {
|
|
||||||
SilentError("Rename-file", "cannot rename file: %s (%v,%+v)", err, err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildFilename(dir string, t time.Time, suffix string) string {
|
|
||||||
// (epoch time in nanoseconds)__(YYYY_Month_DD__hh-mm-ss).har
|
|
||||||
filename := fmt.Sprintf("%d__%s%s", t.UnixNano(), t.Format("2006_Jan_02__15-04-05"), suffix)
|
|
||||||
return filepath.Join(dir, filename)
|
|
||||||
}
|
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/romana/rlog"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@ -36,7 +37,7 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
|
|||||||
ident := fmt.Sprintf("%s->%s:%s->%s", tcpID.SrcIP, tcpID.DstIP, tcpID.SrcPort, tcpID.DstPort)
|
ident := fmt.Sprintf("%s->%s:%s->%s", tcpID.SrcIP, tcpID.DstIP, tcpID.SrcPort, tcpID.DstPort)
|
||||||
isHTTP2, err := checkIsHTTP2Connection(b, isClient)
|
isHTTP2, err := checkIsHTTP2Connection(b, isClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SilentError("HTTP/2-Prepare-Connection", "stream %s Failed to check if client is HTTP/2: %s (%v,%+v)", ident, err, err, err)
|
rlog.Debugf("[HTTP/2-Prepare-Connection] stream %s Failed to check if client is HTTP/2: %s (%v,%+v)", ident, err, err, err)
|
||||||
// Do something?
|
// Do something?
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +45,7 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
|
|||||||
if isHTTP2 {
|
if isHTTP2 {
|
||||||
err := prepareHTTP2Connection(b, isClient)
|
err := prepareHTTP2Connection(b, isClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SilentError("HTTP/2-Prepare-Connection-After-Check", "stream %s error: %s (%v,%+v)", ident, err, err, err)
|
rlog.Debugf("[HTTP/2-Prepare-Connection-After-Check] stream %s error: %s (%v,%+v)", ident, err, err, err)
|
||||||
}
|
}
|
||||||
grpcAssembler = createGrpcAssembler(b)
|
grpcAssembler = createGrpcAssembler(b)
|
||||||
}
|
}
|
||||||
@ -55,7 +56,7 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
|
|||||||
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
SilentError("HTTP/2", "stream %s error: %s (%v,%+v)", ident, err, err, err)
|
rlog.Debugf("[HTTP/2] stream %s error: %s (%v,%+v)", ident, err, err, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else if isClient {
|
} else if isClient {
|
||||||
@ -63,7 +64,7 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
|
|||||||
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
SilentError("HTTP-request", "stream %s Request error: %s (%v,%+v)", ident, err, err, err)
|
rlog.Debugf("[HTTP-request] stream %s Request error: %s (%v,%+v)", ident, err, err, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -71,7 +72,7 @@ func (d dissecting) Dissect(b *bufio.Reader, isClient bool, tcpID *api.TcpID, em
|
|||||||
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
SilentError("HTTP-response", "stream %s Response error: %s (%v,%+v)", ident, err, err, err)
|
rlog.Debugf("[HTTP-response], stream %s Response error: %s (%v,%+v)", ident, err, err, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/romana/rlog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -37,15 +38,15 @@ func (matcher *requestResponseMatcher) registerRequest(ident string, request *ht
|
|||||||
// Type assertion always succeeds because all of the map's values are of api.GenericMessage type
|
// Type assertion always succeeds because all of the map's values are of api.GenericMessage type
|
||||||
responseHTTPMessage := response.(*api.GenericMessage)
|
responseHTTPMessage := response.(*api.GenericMessage)
|
||||||
if responseHTTPMessage.IsRequest {
|
if responseHTTPMessage.IsRequest {
|
||||||
SilentError("Request-Duplicate", "Got duplicate request with same identifier")
|
rlog.Debugf("[Request-Duplicate] Got duplicate request with same identifier")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Trace("Matched open Response for %s", key)
|
rlog.Tracef(1, "Matched open Response for %s", key)
|
||||||
return matcher.preparePair(&requestHTTPMessage, responseHTTPMessage)
|
return matcher.preparePair(&requestHTTPMessage, responseHTTPMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.openMessagesMap.Store(key, &requestHTTPMessage)
|
matcher.openMessagesMap.Store(key, &requestHTTPMessage)
|
||||||
Trace("Registered open Request for %s", key)
|
rlog.Tracef(1, "Registered open Request for %s", key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,15 +65,15 @@ func (matcher *requestResponseMatcher) registerResponse(ident string, response *
|
|||||||
// Type assertion always succeeds because all of the map's values are of api.GenericMessage type
|
// Type assertion always succeeds because all of the map's values are of api.GenericMessage type
|
||||||
requestHTTPMessage := request.(*api.GenericMessage)
|
requestHTTPMessage := request.(*api.GenericMessage)
|
||||||
if !requestHTTPMessage.IsRequest {
|
if !requestHTTPMessage.IsRequest {
|
||||||
SilentError("Response-Duplicate", "Got duplicate response with same identifier")
|
rlog.Debugf("[Response-Duplicate] Got duplicate response with same identifier")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Trace("Matched open Request for %s", key)
|
rlog.Tracef(1, "Matched open Request for %s", key)
|
||||||
return matcher.preparePair(requestHTTPMessage, &responseHTTPMessage)
|
return matcher.preparePair(requestHTTPMessage, &responseHTTPMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
matcher.openMessagesMap.Store(key, &responseHTTPMessage)
|
matcher.openMessagesMap.Store(key, &responseHTTPMessage)
|
||||||
Trace("Registered open Response for %s", key)
|
rlog.Tracef(1, "Registered open Response for %s", key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,10 +3,9 @@ package tap
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/romana/rlog"
|
"github.com/romana/rlog"
|
||||||
"github.com/up9inc/mizu/tap/api"
|
"github.com/up9inc/mizu/tap/api"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/google/gopacket" // pulls in all layers decoders
|
"github.com/google/gopacket" // pulls in all layers decoders
|
||||||
"github.com/google/gopacket/tcpassembly"
|
"github.com/google/gopacket/tcpassembly"
|
||||||
@ -18,8 +17,6 @@ type tcpStreamFactory struct {
|
|||||||
Emitter api.Emitter
|
Emitter api.Emitter
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkTLSPacketAmount = 100
|
|
||||||
|
|
||||||
func containsPort(ports []string, port string) bool {
|
func containsPort(ports []string, port string) bool {
|
||||||
for _, x := range ports {
|
for _, x := range ports {
|
||||||
if x == port {
|
if x == port {
|
||||||
@ -29,6 +26,7 @@ func containsPort(ports []string, port string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remove?
|
||||||
func Emit(item *api.OutputChannelItem) {
|
func Emit(item *api.OutputChannelItem) {
|
||||||
log.Printf("Emit item: %+v\n", item)
|
log.Printf("Emit item: %+v\n", item)
|
||||||
}
|
}
|
||||||
@ -67,6 +65,15 @@ func (h *tcpStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream
|
|||||||
DstPort: transport.Dst().String(),
|
DstPort: transport.Dst().String(),
|
||||||
Ident: fmt.Sprintf("%s:%s", net, transport),
|
Ident: fmt.Sprintf("%s:%s", net, transport),
|
||||||
}
|
}
|
||||||
|
//dstPort, _ := strconv.Atoi(transport.Dst().String())
|
||||||
|
//streamProps := h.getStreamProps(net.Src().String(), net.Dst().String(), dstPort)
|
||||||
|
//if streamProps.isTapTarget {
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
//if h.shouldNotifyOnOutboundLink(net.Dst().String(), dstPort) {
|
||||||
|
// h.outbountLinkWriter.WriteOutboundLink(net.Src().String(), net.Dst().String(), dstPort, "", "")
|
||||||
|
//}
|
||||||
|
|
||||||
if containsPort(allOutboundPorts, transport.Dst().String()) {
|
if containsPort(allOutboundPorts, transport.Dst().String()) {
|
||||||
go stream.clientRun(tcpID, h.Emitter)
|
go stream.clientRun(tcpID, h.Emitter)
|
||||||
} else if containsPort(allOutboundPorts, transport.Src().String()) {
|
} else if containsPort(allOutboundPorts, transport.Src().String()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user