mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
### performance - handling of the very big packet(any size that can be buffered) - speeding up TCP sessions by using message hints: Added **proto.HasFullPayload** that helps to validate the entire HTTP request, it supports `Chunked` encoding too! Added **proto.HasRequestTitle** and **proto.HasResponseTitle** for validating the beginning of HTTP request. Those methods are used `input_raw.go` with `TCP`. - supports Keep-Alive: the above functions helps to support keep-alive ### Packaging - **capture:** engines(capture/doc.go) - **tcp:** tcp message parser (tcp/doc.go) ### benchmarking - **capture.BenchmarkPcapDump:** the benchmarks regarding dumping packets in a pcap file - **capture.BenchmarkPcapFile:** the benchmarks of reading packets from a pcap file - **capture.BenchmarkPcap:** the benchmarks of parsing packets from the loopback interface with pcap handles - **proto.BenchmarkHasFullPayload:**: benchmarking this function which validates the HTTP payload - **tcp.BenchmarkPacketParseAndSort:** benchmarks of parsing and sorting packets - **tcp.BenchmarkMessageParserWithoutHint:** benchmarks of message reasembling by using `SYN` and `FIN` flag - **tcp.BenchmarkMessageParserWithHint:** benchmarks of message reasembling by using `proto.HasRequestTitle` and `proto.HasFullPayload` flag ### issues see linked issues ### tests - fixed input raw and engine tests **Most of the changed of the files, was about using functionalities of** `tcp` **and** `capture` **in existing functionalities**
371 lines
8.7 KiB
Go
371 lines
8.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/http/httputil"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/buger/goreplay/capture"
|
|
"github.com/buger/goreplay/proto"
|
|
)
|
|
|
|
const testRawExpire = time.Millisecond * 200
|
|
|
|
func TestRAWInputIPv4(t *testing.T) {
|
|
wg := new(sync.WaitGroup)
|
|
quit := make(chan int)
|
|
|
|
listener, err := net.Listen("tcp", ":0")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
origin := &http.Server{
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ab"))
|
|
}),
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
}
|
|
go origin.Serve(listener)
|
|
defer listener.Close()
|
|
_, port, _ := net.SplitHostPort(listener.Addr().String())
|
|
|
|
var respCounter, reqCounter int64
|
|
conf := RAWInputConfig{
|
|
Engine: capture.EnginePcap,
|
|
Expire: 0,
|
|
Protocol: ProtocolHTTP,
|
|
TrackResponse: true,
|
|
RealIPHeader: "X-Real-IP",
|
|
}
|
|
input := NewRAWInput(":"+port, conf)
|
|
|
|
output := NewTestOutput(func(data []byte) {
|
|
if data[0] == '1' {
|
|
body := payloadBody(data)
|
|
if len(proto.Header(body, []byte("X-Real-IP"))) == 0 {
|
|
t.Error("Should have X-Real-IP header", string(body))
|
|
}
|
|
atomic.AddInt64(&reqCounter, 1)
|
|
} else {
|
|
atomic.AddInt64(&respCounter, 1)
|
|
}
|
|
wg.Done()
|
|
})
|
|
|
|
plugins := &InOutPlugins{
|
|
Inputs: []io.Reader{input},
|
|
Outputs: []io.Writer{output},
|
|
}
|
|
plugins.All = append(plugins.All, input, output)
|
|
|
|
client := NewHTTPClient("127.0.0.1:"+port, &HTTPClientConfig{})
|
|
|
|
emitter := NewEmitter(quit)
|
|
defer emitter.Close()
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
for i := 0; i < 10; i++ {
|
|
wg.Add(2)
|
|
_, err = client.Get("/")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
wg.Wait()
|
|
const want = 10
|
|
if reqCounter != respCounter && reqCounter != want {
|
|
t.Errorf("want %d requests and %d responses, got %d requests and %d responses", want, want, reqCounter, respCounter)
|
|
}
|
|
}
|
|
|
|
func TestRAWInputNoKeepAlive(t *testing.T) {
|
|
wg := new(sync.WaitGroup)
|
|
quit := make(chan int)
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
origin := &http.Server{
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ab"))
|
|
}),
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
}
|
|
origin.SetKeepAlivesEnabled(false)
|
|
go origin.Serve(listener)
|
|
defer listener.Close()
|
|
_, port, _ := net.SplitHostPort(listener.Addr().String())
|
|
|
|
conf := RAWInputConfig{
|
|
Engine: capture.EnginePcap,
|
|
Expire: testRawExpire,
|
|
Protocol: ProtocolHTTP,
|
|
TrackResponse: true,
|
|
}
|
|
input := NewRAWInput(":"+port, conf)
|
|
var respCounter, reqCounter int64
|
|
output := NewTestOutput(func(data []byte) {
|
|
if data[0] == '1' {
|
|
atomic.AddInt64(&reqCounter, 1)
|
|
} else {
|
|
atomic.AddInt64(&respCounter, 1)
|
|
}
|
|
wg.Done()
|
|
})
|
|
|
|
plugins := &InOutPlugins{
|
|
Inputs: []io.Reader{input},
|
|
Outputs: []io.Writer{output},
|
|
}
|
|
plugins.All = append(plugins.All, input, output)
|
|
|
|
client := NewHTTPClient("127.0.0.1:"+port, &HTTPClientConfig{})
|
|
|
|
emitter := NewEmitter(quit)
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
// request + response
|
|
wg.Add(2)
|
|
_, err = client.Get("/")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
wg.Wait()
|
|
const want = 10
|
|
if reqCounter != respCounter && reqCounter != want {
|
|
t.Errorf("want %d requests and %d responses, got %d requests and %d responses", want, want, reqCounter, respCounter)
|
|
}
|
|
emitter.Close()
|
|
}
|
|
|
|
func TestRAWInputIPv6(t *testing.T) {
|
|
wg := new(sync.WaitGroup)
|
|
quit := make(chan int)
|
|
|
|
listener, err := net.Listen("tcp", "[::1]:0")
|
|
if err != nil {
|
|
return
|
|
}
|
|
origin := &http.Server{
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ab"))
|
|
}),
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
}
|
|
go origin.Serve(listener)
|
|
defer listener.Close()
|
|
_, port, _ := net.SplitHostPort(listener.Addr().String())
|
|
originAddr := "[::1]:" + port
|
|
|
|
var respCounter, reqCounter int64
|
|
conf := RAWInputConfig{
|
|
Engine: capture.EnginePcap,
|
|
Protocol: ProtocolHTTP,
|
|
TrackResponse: true,
|
|
}
|
|
input := NewRAWInput(originAddr, conf)
|
|
|
|
output := NewTestOutput(func(data []byte) {
|
|
if data[0] == '1' {
|
|
atomic.AddInt64(&reqCounter, 1)
|
|
} else {
|
|
atomic.AddInt64(&respCounter, 1)
|
|
}
|
|
wg.Done()
|
|
})
|
|
|
|
plugins := &InOutPlugins{
|
|
Inputs: []io.Reader{input},
|
|
Outputs: []io.Writer{output},
|
|
}
|
|
|
|
client := NewHTTPClient(originAddr, &HTTPClientConfig{})
|
|
|
|
emitter := NewEmitter(quit)
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
for i := 0; i < 10; i++ {
|
|
// request + response
|
|
wg.Add(2)
|
|
_, err = client.Get("/")
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
wg.Wait()
|
|
const want = 10
|
|
if reqCounter != respCounter && reqCounter != want {
|
|
t.Errorf("want %d requests and %d responses, got %d requests and %d responses", want, want, reqCounter, respCounter)
|
|
}
|
|
emitter.Close()
|
|
}
|
|
|
|
func TestInputRAWChunkedEncoding(t *testing.T) {
|
|
wg := new(sync.WaitGroup)
|
|
quit := make(chan int)
|
|
|
|
fileContent, _ := ioutil.ReadFile("README.md")
|
|
|
|
// Origing and Replay server initialization
|
|
origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer r.Body.Close()
|
|
ioutil.ReadAll(r.Body)
|
|
|
|
wg.Done()
|
|
}))
|
|
|
|
originAddr := strings.Replace(origin.Listener.Addr().String(), "[::]", "127.0.0.1", -1)
|
|
conf := RAWInputConfig{
|
|
Engine: capture.EnginePcap,
|
|
Expire: time.Second,
|
|
Protocol: ProtocolHTTP,
|
|
TrackResponse: true,
|
|
}
|
|
input := NewRAWInput(originAddr, conf)
|
|
|
|
replay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer r.Body.Close()
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
|
|
if !bytes.Equal(body, fileContent) {
|
|
buf, _ := httputil.DumpRequest(r, true)
|
|
t.Error("Wrong POST body:", string(buf))
|
|
}
|
|
|
|
wg.Done()
|
|
}))
|
|
defer replay.Close()
|
|
|
|
httpOutput := NewHTTPOutput(replay.URL, &HTTPOutputConfig{Debug: true})
|
|
|
|
plugins := &InOutPlugins{
|
|
Inputs: []io.Reader{input},
|
|
Outputs: []io.Writer{httpOutput},
|
|
}
|
|
plugins.All = append(plugins.All, input, httpOutput)
|
|
|
|
emitter := NewEmitter(quit)
|
|
defer emitter.Close()
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
wg.Add(2)
|
|
|
|
curl := exec.Command("curl", "http://"+originAddr, "--header", "Transfer-Encoding: chunked", "--header", "Expect:", "--data-binary", "@README.md")
|
|
err := curl.Run()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func BenchmarkRAWInputWithReplay(b *testing.B) {
|
|
var respCounter, reqCounter, replayCounter, capturedBody uint64
|
|
wg := &sync.WaitGroup{}
|
|
wg.Add(b.N * 3) // reqCounter + replayCounter + respCounter
|
|
|
|
quit := make(chan int)
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
listener0, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
b.Error(err)
|
|
return
|
|
}
|
|
|
|
origin := http.Server{
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ab"))
|
|
}),
|
|
}
|
|
go origin.Serve(listener)
|
|
defer origin.Close()
|
|
originAddr := listener.Addr().String()
|
|
|
|
replay := http.Server{
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer wg.Done()
|
|
defer r.Body.Close()
|
|
w.Write([]byte("ab"))
|
|
atomic.AddUint64(&replayCounter, 1)
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
|
b.Log(err)
|
|
}
|
|
atomic.AddUint64(&capturedBody, uint64(len(data)))
|
|
}),
|
|
}
|
|
go replay.Serve(listener0)
|
|
defer replay.Close()
|
|
replayAddr := listener0.Addr().String()
|
|
|
|
conf := RAWInputConfig{
|
|
Engine: capture.EnginePcap,
|
|
Expire: testRawExpire,
|
|
Protocol: ProtocolHTTP,
|
|
TrackResponse: true,
|
|
}
|
|
input := NewRAWInput(originAddr, conf)
|
|
|
|
testOutput := NewTestOutput(func(data []byte) {
|
|
if data[0] == '1' {
|
|
atomic.AddUint64(&reqCounter, 1)
|
|
} else {
|
|
atomic.AddUint64(&respCounter, 1)
|
|
}
|
|
atomic.AddUint64(&capturedBody, uint64(len(data)))
|
|
wg.Done()
|
|
})
|
|
httpOutput := NewHTTPOutput(replayAddr, &HTTPOutputConfig{Debug: false})
|
|
|
|
plugins := &InOutPlugins{
|
|
Inputs: []io.Reader{input},
|
|
Outputs: []io.Writer{testOutput, httpOutput},
|
|
}
|
|
|
|
emitter := NewEmitter(quit)
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
now := time.Now()
|
|
var buf [1 << 20]byte
|
|
buf[1<<20-1] = 'a'
|
|
client := NewHTTPClient(originAddr, &HTTPClientConfig{ResponseBufferSize: 2 << 20, CompatibilityMode: true})
|
|
for i := 0; i < b.N; i++ {
|
|
if i&1 == 0 {
|
|
_, err = client.Get("/")
|
|
} else {
|
|
_, err = client.Post("/", buf[:])
|
|
}
|
|
if err != nil {
|
|
b.Log(err)
|
|
wg.Add(-3)
|
|
}
|
|
}
|
|
|
|
wg.Wait()
|
|
b.Logf("%d/%d Requests, %d/%d Responses, %d/%d Replayed, %d Bytes in %s\n", reqCounter, b.N, respCounter, b.N, replayCounter, b.N, capturedBody, time.Since(now))
|
|
emitter.Close()
|
|
}
|