mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
Tcp communication should use same protocol as file based
This commit is contained in:
+2
-10
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -10,7 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// TCPInput used for internal communication
|
// TCPInput used for internal communication
|
||||||
// It expected hex encoded data
|
|
||||||
type TCPInput struct {
|
type TCPInput struct {
|
||||||
data chan []byte
|
data chan []byte
|
||||||
address string
|
address string
|
||||||
@@ -62,16 +60,10 @@ func (i *TCPInput) handleConnection(conn net.Conn) {
|
|||||||
|
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
|
scanner.Split(payloadScanner)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
encodedPayload := scanner.Bytes()
|
i.data <- scanner.Bytes()
|
||||||
// Hex encoding always 2x number of bytes
|
|
||||||
decoded := make([]byte, len(encodedPayload)/2)
|
|
||||||
_, err := hex.Decode(decoded, encodedPayload)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("[TCPInput] failed to hex decode TCP payload:", err)
|
|
||||||
}
|
|
||||||
i.data <- decoded
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
|
|||||||
+3
-6
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -35,14 +34,12 @@ func TestTCPInput(t *testing.T) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := []byte("GET / HTTP/1.1\r\n\r\n")
|
msg := []byte("1 1 1\nGET / HTTP/1.1\r\n\r\n")
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
conn.Write(msg)
|
||||||
encoded := make([]byte, len(msg)*2)
|
conn.Write([]byte(payloadSeparator))
|
||||||
hex.Encode(encoded, msg)
|
|
||||||
conn.Write(append(encoded, '\n'))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
+4
-7
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -47,7 +46,9 @@ func (o *TCPOutput) worker() {
|
|||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, err := conn.Write(<-o.buf)
|
conn.Write(<-o.buf)
|
||||||
|
_, err := conn.Write([]byte(payloadSeparator))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Worker failed on write, exitings and starting new worker")
|
log.Println("Worker failed on write, exitings and starting new worker")
|
||||||
go o.worker()
|
go o.worker()
|
||||||
@@ -61,11 +62,7 @@ func (o *TCPOutput) Write(data []byte) (n int, err error) {
|
|||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hex encoding always 2x number of bytes
|
o.buf <- data
|
||||||
encoded := make([]byte, len(data)*2+1)
|
|
||||||
hex.Encode(encoded, data)
|
|
||||||
encoded[len(encoded)-1] = '\n'
|
|
||||||
o.buf <- encoded
|
|
||||||
|
|
||||||
if Settings.outputTCPStats {
|
if Settings.outputTCPStats {
|
||||||
o.bufStats.Write(len(o.buf))
|
o.bufStats.Write(len(o.buf))
|
||||||
|
|||||||
+2
-6
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@@ -50,13 +49,10 @@ func startTCP(cb func([]byte)) net.Listener {
|
|||||||
go func() {
|
go func() {
|
||||||
reader := bufio.NewReader(conn)
|
reader := bufio.NewReader(conn)
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
|
scanner.Split(payloadScanner)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
encodedPayload := scanner.Bytes()
|
cb(scanner.Bytes())
|
||||||
// Hex encoding always 2x number of bytes
|
|
||||||
decoded := make([]byte, len(encodedPayload)/2)
|
|
||||||
hex.Decode(decoded, encodedPayload)
|
|
||||||
cb(decoded)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user