-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (51 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"github.com/Shopify/toxiproxy/v2"
"github.com/Shopify/toxiproxy/v2/stream"
"github.com/Shopify/toxiproxy/v2/toxics"
)
type HttpToxic struct{}
func (t *HttpToxic) ModifyResponse(resp *http.Response) {
resp.Header.Add("Location", "https://github.com/Shopify/toxiproxy")
fmt.Println("HttpToxic is modifying the response")
}
func (t *HttpToxic) Pipe(stub *toxics.ToxicStub) {
buffer := bytes.NewBuffer(make([]byte, 0, 32*1024))
writer := stream.NewChanWriter(stub.Output)
reader := stream.NewChanReader(stub.Input)
reader.SetInterrupt(stub.Interrupt)
for {
tee := io.TeeReader(reader, buffer)
resp, err := http.ReadResponse(bufio.NewReader(tee), nil)
if err == stream.ErrInterrupted {
buffer.WriteTo(writer)
return
} else if err == io.EOF {
stub.Close()
return
}
if err != nil {
buffer.WriteTo(writer)
} else {
fmt.Println("I am here")
t.ModifyResponse(resp)
resp.Write(writer)
}
buffer.Reset()
}
}
func main() {
toxics.Register("http", new(HttpToxic))
logger := zerolog.New(os.Stderr).With().Caller().Timestamp().Logger()
metrics := toxiproxy.NewMetricsContainer(prometheus.NewRegistry())
server := toxiproxy.NewServer(metrics, logger)
server.Listen("0.0.0.0:8484")
}