https://github.com/petems/client-inspect
A library that injects an inspector to a Golang http.Client object to show traffic
https://github.com/petems/client-inspect
golang http-client inspection
Last synced: 3 months ago
JSON representation
A library that injects an inspector to a Golang http.Client object to show traffic
- Host: GitHub
- URL: https://github.com/petems/client-inspect
- Owner: petems
- License: mit
- Created: 2020-10-12T22:54:13.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-11-01T09:56:35.000Z (over 4 years ago)
- Last Synced: 2025-02-02T20:49:21.359Z (5 months ago)
- Topics: golang, http-client, inspection
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# client-inspect
[](https://godoc.org/github.com/petems/client-inspect/http) [](https://pkg.go.dev/github.com/petems/client-inspect/http) [](https://goreportcard.com/report/github.com/petems/client-inspect)Allows inspection of a `http.Client` connections
Heavily based on https://github.com/j0hnsmith/connspy
### `http` package
A `http.Client` suitable for debugging, writes all http data to stdout.
```go
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"clientInspect "github.com/petems/client-inspect/http"
)func main() {
client := clientInspect.NewClient(nil, nil)
resp, _ := client.Get("http://example.com/")
// ensure all of the body is read
ioutil.ReadAll(resp.Body)
resp.Body.Close()resp, _ = client.Get("https://example.com/")
ioutil.ReadAll(resp.Body)
resp.Body.Close()}
```
You can also specify the writer with `http.NewClientWriter`, which can be used to do things like redact certain fields:
```go
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"clientInspect "github.com/petems/client-inspect/http"
)func main() {
buf := new(bytes.Buffer)
client := http.NewClientWriter(nil, nil, buf)
resp, _ := client.Get("http://example.com/")
// ensure all of the body is read
ioutil.ReadAll(resp.Body)
resp.Body.Close()resp, _ = client.Get("https://example.com/")
ioutil.ReadAll(resp.Body)
resp.Body.Close()httpLog := buf.String()
s := strings.Split(httpLog, "\n")
for count, line := range s {
rgx := regexp.MustCompile(`^(Host: )(.+)$`)
line = rgx.ReplaceAllString(line, `$1[REDACTED]`)
s[count] = line
}fmt.Println(s)
}
```
For more complex redaction purposes, I've been having a lot of luck with creating an io.Writer with logrus, then using [redactrus](https://github.com/whuang8/redactrus) to redact certain parts of the logs. Plus you can do cool formatting!
```go
package mainimport (
"io/ioutil"
"time""github.com/petems/client-inspect/http"
"github.com/sirupsen/logrus"
"github.com/whuang8/redactrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
)func main() {
rh := &redactrus.Hook{
AcceptedLevels: logrus.AllLevels,
RedactionList: []string{"^(Host: ).+$"},
}log := logrus.New()
textFormatter := new(prefixed.TextFormatter)
textFormatter.FullTimestamp = true
textFormatter.TimestampFormat = time.RFC822log.SetFormatter(textFormatter)
log.AddHook(rh)
client := http.NewClientWriter(nil, nil, log.Writer())
resp, _ := client.Get("http://example.com/")
// ensure all of the body is read
ioutil.ReadAll(resp.Body)
resp.Body.Close()resp, _ = client.Get("https://example.com/")
ioutil.ReadAll(resp.Body)
resp.Body.Close()}
```
## Background info
[https://medium.com/@j0hnsmith/eavesdrop-on-a-golang-http-client-c4dc49af9d5e](https://medium.com/@j0hnsmith/eavesdrop-on-a-golang-http-client-c4dc49af9d5e)