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**
138 lines
2.8 KiB
Go
138 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// TCPOutput used for sending raw tcp payloads
|
|
// Currently used for internal communication between listener and replay server
|
|
// Can be used for transfering binary payloads like protocol buffers
|
|
type TCPOutput struct {
|
|
address string
|
|
limit int
|
|
buf []chan []byte
|
|
bufStats *GorStat
|
|
config *TCPOutputConfig
|
|
}
|
|
|
|
// TCPOutputConfig tcp output configuration
|
|
type TCPOutputConfig struct {
|
|
Secure bool `json:"output-tcp-secure"`
|
|
Sticky bool `json:"output-tcp-sticky"`
|
|
}
|
|
|
|
// NewTCPOutput constructor for TCPOutput
|
|
// Initialize 10 workers which hold keep-alive connection
|
|
func NewTCPOutput(address string, config *TCPOutputConfig) io.Writer {
|
|
o := new(TCPOutput)
|
|
|
|
o.address = address
|
|
o.config = config
|
|
|
|
if Settings.OutputTCPStats {
|
|
o.bufStats = NewGorStat("output_tcp", 5000)
|
|
}
|
|
|
|
if o.config.Sticky {
|
|
// create 10 buffers and send the buffer index to the worker
|
|
o.buf = make([]chan []byte, 10)
|
|
for i := 0; i < 10; i++ {
|
|
o.buf[i] = make(chan []byte, 100)
|
|
go o.worker(i)
|
|
}
|
|
} else {
|
|
// create 1 buffer and send its index (0) to all workers
|
|
o.buf = make([]chan []byte, 1)
|
|
o.buf[0] = make(chan []byte, 1000)
|
|
for i := 0; i < 10; i++ {
|
|
go o.worker(0)
|
|
}
|
|
}
|
|
|
|
return o
|
|
}
|
|
|
|
func (o *TCPOutput) worker(bufferIndex int) {
|
|
retries := 0
|
|
conn, err := o.connect(o.address)
|
|
for {
|
|
if err == nil {
|
|
break
|
|
}
|
|
|
|
log.Println("Can't connect to aggregator instance, reconnecting in 1 second. Retries:", retries)
|
|
time.Sleep(1 * time.Second)
|
|
|
|
conn, err = o.connect(o.address)
|
|
retries++
|
|
}
|
|
|
|
if retries > 0 {
|
|
log.Println("Connected to aggregator instance after ", retries, " retries")
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
data := <-o.buf[bufferIndex]
|
|
conn.Write(data)
|
|
_, err := conn.Write([]byte(payloadSeparator))
|
|
|
|
if err != nil {
|
|
log.Println("INFO: TCP output connection closed, reconnecting")
|
|
o.buf[bufferIndex] <- data
|
|
go o.worker(bufferIndex)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (o *TCPOutput) getBufferIndex(data []byte) int {
|
|
if !o.config.Sticky {
|
|
return 0
|
|
}
|
|
|
|
hasher := fnv.New32a()
|
|
hasher.Write(payloadMeta(data)[1])
|
|
return int(hasher.Sum32()) % 10
|
|
}
|
|
|
|
func (o *TCPOutput) Write(data []byte) (n int, err error) {
|
|
if !isOriginPayload(data) {
|
|
return len(data), nil
|
|
}
|
|
|
|
// We have to copy, because sending data in multiple threads
|
|
newBuf := make([]byte, len(data))
|
|
copy(newBuf, data)
|
|
|
|
bufferIndex := o.getBufferIndex(data)
|
|
o.buf[bufferIndex] <- newBuf
|
|
|
|
if Settings.OutputTCPStats {
|
|
o.bufStats.Write(len(o.buf[bufferIndex]))
|
|
}
|
|
|
|
return len(data), nil
|
|
}
|
|
|
|
func (o *TCPOutput) connect(address string) (conn net.Conn, err error) {
|
|
if o.config.Secure {
|
|
conn, err = tls.Dial("tcp", address, &tls.Config{})
|
|
} else {
|
|
conn, err = net.Dial("tcp", address)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (o *TCPOutput) String() string {
|
|
return fmt.Sprintf("TCP output %s, limit: %d", o.address, o.limit)
|
|
}
|