mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
format file
This commit is contained in:
+12
-13
@@ -134,7 +134,7 @@ func (c *HTTPClient) isAlive() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
||||||
var payload []byte
|
var payload []byte
|
||||||
|
|
||||||
// Don't exit on panic
|
// Don't exit on panic
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -143,7 +143,7 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
|||||||
|
|
||||||
if _, ok := r.(error); ok {
|
if _, ok := r.(error); ok {
|
||||||
log.Println("[HTTPClient] Failed to send request: ", string(data))
|
log.Println("[HTTPClient] Failed to send request: ", string(data))
|
||||||
log.Println("[HTTPClient] Response: ", string(response))
|
log.Println("[HTTPClient] Response: ", string(response))
|
||||||
log.Println("PANIC: pkg:", r, string(debug.Stack()))
|
log.Println("PANIC: pkg:", r, string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,16 +202,16 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
|||||||
} else {
|
} else {
|
||||||
// If headers are finished
|
// If headers are finished
|
||||||
|
|
||||||
if bytes.Contains(c.respBuf[:readBytes], proto.EmptyLine) {
|
if bytes.Contains(c.respBuf[:readBytes], proto.EmptyLine) {
|
||||||
if bytes.Equal(proto.Header(c.respBuf[:readBytes], []byte("Transfer-Encoding")), []byte("chunked")) {
|
if bytes.Equal(proto.Header(c.respBuf[:readBytes], []byte("Transfer-Encoding")), []byte("chunked")) {
|
||||||
chunked = true
|
chunked = true
|
||||||
} else {
|
} else {
|
||||||
status, _ := strconv.Atoi(string(proto.Status(c.respBuf[:readBytes])))
|
status, _ := strconv.Atoi(string(proto.Status(c.respBuf[:readBytes])))
|
||||||
if (status >= 100 && status < 200) || status == 204 || status == 304 {
|
if (status >= 100 && status < 200) || status == 204 || status == 304 {
|
||||||
contentLength = 0
|
contentLength = 0
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
l := proto.Header(c.respBuf[:readBytes], []byte("Content-Length"))
|
l := proto.Header(c.respBuf[:readBytes], []byte("Content-Length"))
|
||||||
if len(l) > 0 {
|
if len(l) > 0 {
|
||||||
contentLength, _ = strconv.Atoi(string(l))
|
contentLength, _ = strconv.Atoi(string(l))
|
||||||
}
|
}
|
||||||
@@ -280,7 +280,6 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if readBytes >= maxResponseSize {
|
if readBytes >= maxResponseSize {
|
||||||
@@ -294,16 +293,16 @@ func (c *HTTPClient) Send(data []byte) (response []byte, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && readBytes == 0 {
|
if err != nil && readBytes == 0 {
|
||||||
Debug("[HTTPClient] Response read timeout error", err, c.conn, readBytes, string(c.respBuf[:readBytes]))
|
Debug("[HTTPClient] Response read timeout error", err, c.conn, readBytes, string(c.respBuf[:readBytes]))
|
||||||
response = errorPayload(HTTP_TIMEOUT)
|
response = errorPayload(HTTP_TIMEOUT)
|
||||||
c.Disconnect()
|
c.Disconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if readBytes < 4 || string(c.respBuf[:4]) != "HTTP" {
|
if readBytes < 4 || string(c.respBuf[:4]) != "HTTP" {
|
||||||
Debug("[HTTPClient] Response read unknown error", err, c.conn, readBytes, string(c.respBuf[:readBytes]))
|
Debug("[HTTPClient] Response read unknown error", err, c.conn, readBytes, string(c.respBuf[:readBytes]))
|
||||||
response = errorPayload(HTTP_UNKNOWN_ERROR)
|
response = errorPayload(HTTP_UNKNOWN_ERROR)
|
||||||
c.Disconnect()
|
c.Disconnect()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+48
-48
@@ -1,69 +1,69 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/buger/goreplay/proto"
|
"bytes"
|
||||||
"bytes"
|
"compress/gzip"
|
||||||
"compress/gzip"
|
"github.com/buger/goreplay/proto"
|
||||||
"strconv"
|
"io/ioutil"
|
||||||
"io/ioutil"
|
"net/http/httputil"
|
||||||
"net/http/httputil"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func prettifyHTTP(p []byte) []byte {
|
func prettifyHTTP(p []byte) []byte {
|
||||||
headSize := bytes.IndexByte(p, '\n') + 1
|
headSize := bytes.IndexByte(p, '\n') + 1
|
||||||
head := p[:headSize]
|
head := p[:headSize]
|
||||||
body := p[headSize:]
|
body := p[headSize:]
|
||||||
|
|
||||||
headersPos := proto.MIMEHeadersEndPos(body)
|
headersPos := proto.MIMEHeadersEndPos(body)
|
||||||
headers := body[:headersPos]
|
headers := body[:headersPos]
|
||||||
content := body[headersPos:]
|
content := body[headersPos:]
|
||||||
|
|
||||||
var tEnc, cEnc []byte
|
var tEnc, cEnc []byte
|
||||||
proto.ParseHeaders([][]byte{headers}, func(header, value []byte) bool {
|
proto.ParseHeaders([][]byte{headers}, func(header, value []byte) bool {
|
||||||
if proto.HeadersEqual(header, []byte("Transfer-Encoding")) {
|
if proto.HeadersEqual(header, []byte("Transfer-Encoding")) {
|
||||||
tEnc = value
|
tEnc = value
|
||||||
}
|
}
|
||||||
|
|
||||||
if proto.HeadersEqual(header, []byte("Content-Encoding")) {
|
if proto.HeadersEqual(header, []byte("Content-Encoding")) {
|
||||||
cEnc = value
|
cEnc = value
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(tEnc) == 0 && len(cEnc) == 0 {
|
if len(tEnc) == 0 && len(cEnc) == 0 {
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.Equal(tEnc, []byte("chunked")) {
|
if bytes.Equal(tEnc, []byte("chunked")) {
|
||||||
buf := bytes.NewBuffer(content)
|
buf := bytes.NewBuffer(content)
|
||||||
r := httputil.NewChunkedReader(buf)
|
r := httputil.NewChunkedReader(buf)
|
||||||
content, _ = ioutil.ReadAll(r)
|
content, _ = ioutil.ReadAll(r)
|
||||||
|
|
||||||
headers = proto.DeleteHeader(headers, []byte("Transfer-Encoding"))
|
headers = proto.DeleteHeader(headers, []byte("Transfer-Encoding"))
|
||||||
|
|
||||||
newLen := strconv.Itoa(len(content))
|
newLen := strconv.Itoa(len(content))
|
||||||
headers = proto.SetHeader(headers, []byte("Content-Length"), []byte(newLen))
|
headers = proto.SetHeader(headers, []byte("Content-Length"), []byte(newLen))
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes.Equal(cEnc, []byte("gzip")) {
|
if bytes.Equal(cEnc, []byte("gzip")) {
|
||||||
buf := bytes.NewBuffer(content)
|
buf := bytes.NewBuffer(content)
|
||||||
g, err := gzip.NewReader(buf)
|
g, err := gzip.NewReader(buf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Debug("[Prettifier] GZIP encoding error:", err)
|
Debug("[Prettifier] GZIP encoding error:", err)
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
content, _ = ioutil.ReadAll(g)
|
content, _ = ioutil.ReadAll(g)
|
||||||
|
|
||||||
headers = proto.DeleteHeader(headers, []byte("Content-Encoding"))
|
headers = proto.DeleteHeader(headers, []byte("Content-Encoding"))
|
||||||
|
|
||||||
newLen := strconv.Itoa(len(content))
|
newLen := strconv.Itoa(len(content))
|
||||||
headers = proto.SetHeader(headers, []byte("Content-Length"), []byte(newLen))
|
headers = proto.SetHeader(headers, []byte("Content-Length"), []byte(newLen))
|
||||||
}
|
}
|
||||||
|
|
||||||
newPayload := append(append(head, headers...), content...)
|
newPayload := append(append(head, headers...), content...)
|
||||||
|
|
||||||
return newPayload
|
return newPayload
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -3,8 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -46,17 +46,17 @@ func (i *TCPInput) Read(data []byte) (int, error) {
|
|||||||
|
|
||||||
func (i *TCPInput) listen(address string) {
|
func (i *TCPInput) listen(address string) {
|
||||||
if i.config.secure {
|
if i.config.secure {
|
||||||
cer, err := tls.LoadX509KeyPair(i.config.certificatePath, i.config.keyPath)
|
cer, err := tls.LoadX509KeyPair(i.config.certificatePath, i.config.keyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error while loading --input-file certificate:", err)
|
log.Fatal("Error while loading --input-file certificate:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
config := &tls.Config{Certificates: []tls.Certificate{cer}}
|
||||||
listener, err := tls.Listen("tcp", address, config)
|
listener, err := tls.Listen("tcp", address, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Can't start --input-tcp with secure connection:", err)
|
log.Fatal("Can't start --input-tcp with secure connection:", err)
|
||||||
}
|
}
|
||||||
i.listener = listener
|
i.listener = listener
|
||||||
} else {
|
} else {
|
||||||
listener, err := net.Listen("tcp", address)
|
listener, err := net.Listen("tcp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+25
-25
@@ -1,28 +1,28 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"io/ioutil"
|
|
||||||
"crypto/x509"
|
|
||||||
"crypto/rsa"
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/tls"
|
|
||||||
"encoding/pem"
|
|
||||||
"math/big"
|
|
||||||
"time"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTCPInput(t *testing.T) {
|
func TestTCPInput(t *testing.T) {
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
quit := make(chan int)
|
quit := make(chan int)
|
||||||
|
|
||||||
input := NewTCPInput("127.0.0.1:0", &TCPInputConfig{})
|
input := NewTCPInput("127.0.0.1:0", &TCPInputConfig{})
|
||||||
output := NewTestOutput(func(data []byte) {
|
output := NewTestOutput(func(data []byte) {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
@@ -90,7 +90,7 @@ func TestTCPInputSecure(t *testing.T) {
|
|||||||
serverPrivPemFile.Write(serverPrivPem)
|
serverPrivPemFile.Write(serverPrivPem)
|
||||||
serverPrivPemFile.Close()
|
serverPrivPemFile.Close()
|
||||||
|
|
||||||
defer func(){
|
defer func() {
|
||||||
os.Remove(serverPrivPemFile.Name())
|
os.Remove(serverPrivPemFile.Name())
|
||||||
os.Remove(serverCertPemFile.Name())
|
os.Remove(serverCertPemFile.Name())
|
||||||
}()
|
}()
|
||||||
@@ -99,9 +99,9 @@ func TestTCPInputSecure(t *testing.T) {
|
|||||||
quit := make(chan int)
|
quit := make(chan int)
|
||||||
|
|
||||||
input := NewTCPInput("127.0.0.1:0", &TCPInputConfig{
|
input := NewTCPInput("127.0.0.1:0", &TCPInputConfig{
|
||||||
secure: true,
|
secure: true,
|
||||||
certificatePath: serverCertPemFile.Name(),
|
certificatePath: serverCertPemFile.Name(),
|
||||||
keyPath: serverPrivPemFile.Name(),
|
keyPath: serverPrivPemFile.Name(),
|
||||||
})
|
})
|
||||||
output := NewTestOutput(func(data []byte) {
|
output := NewTestOutput(func(data []byte) {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
@@ -112,15 +112,15 @@ func TestTCPInputSecure(t *testing.T) {
|
|||||||
|
|
||||||
go Start(quit)
|
go Start(quit)
|
||||||
|
|
||||||
conf := &tls.Config{
|
conf := &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := tls.Dial("tcp", input.listener.Addr().String(), conf)
|
conn, err := tls.Dial("tcp", input.listener.Addr().String(), conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
msg := []byte("1 1 1\nGET / HTTP/1.1\r\n\r\n")
|
msg := []byte("1 1 1\nGET / HTTP/1.1\r\n\r\n")
|
||||||
|
|
||||||
@@ -133,4 +133,4 @@ func TestTCPInputSecure(t *testing.T) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
close(quit)
|
close(quit)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,8 +78,8 @@ func (t *TCPPacket) ParseBasic() {
|
|||||||
t.IsFIN = t.Raw[13]&0x01 != 0
|
t.IsFIN = t.Raw[13]&0x01 != 0
|
||||||
|
|
||||||
if len(t.Raw) >= int(t.DataOffset*4) {
|
if len(t.Raw) >= int(t.DataOffset*4) {
|
||||||
t.Data = t.Raw[t.DataOffset*4:]
|
t.Data = t.Raw[t.DataOffset*4:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TCPPacket) dump() *packet {
|
func (t *TCPPacket) dump() *packet {
|
||||||
|
|||||||
Reference in New Issue
Block a user