diff --git a/input_tcp.go b/input_tcp.go index 47d2c3d..15dffa5 100644 --- a/input_tcp.go +++ b/input_tcp.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "encoding/hex" "fmt" "log" "net" @@ -10,7 +9,6 @@ import ( ) // TCPInput used for internal communication -// It expected hex encoded data type TCPInput struct { data chan []byte address string @@ -62,16 +60,10 @@ func (i *TCPInput) handleConnection(conn net.Conn) { reader := bufio.NewReader(conn) scanner := bufio.NewScanner(reader) + scanner.Split(payloadScanner) for scanner.Scan() { - encodedPayload := 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 + i.data <- scanner.Bytes() } if err := scanner.Err(); err != nil { diff --git a/input_tcp_test.go b/input_tcp_test.go index f440c57..d844023 100644 --- a/input_tcp_test.go +++ b/input_tcp_test.go @@ -1,7 +1,6 @@ package main import ( - "encoding/hex" "io" "log" "net" @@ -35,14 +34,12 @@ func TestTCPInput(t *testing.T) { 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++ { wg.Add(1) - - encoded := make([]byte, len(msg)*2) - hex.Encode(encoded, msg) - conn.Write(append(encoded, '\n')) + conn.Write(msg) + conn.Write([]byte(payloadSeparator)) } wg.Wait() diff --git a/output_tcp.go b/output_tcp.go index fdbe579..96295dc 100644 --- a/output_tcp.go +++ b/output_tcp.go @@ -1,7 +1,6 @@ package main import ( - "encoding/hex" "fmt" "io" "log" @@ -47,7 +46,9 @@ func (o *TCPOutput) worker() { defer conn.Close() for { - _, err := conn.Write(<-o.buf) + conn.Write(<-o.buf) + _, err := conn.Write([]byte(payloadSeparator)) + if err != nil { log.Println("Worker failed on write, exitings and starting new worker") go o.worker() @@ -61,11 +62,7 @@ func (o *TCPOutput) Write(data []byte) (n int, err error) { return len(data), nil } - // Hex encoding always 2x number of bytes - encoded := make([]byte, len(data)*2+1) - hex.Encode(encoded, data) - encoded[len(encoded)-1] = '\n' - o.buf <- encoded + o.buf <- data if Settings.outputTCPStats { o.bufStats.Write(len(o.buf)) diff --git a/output_tcp_test.go b/output_tcp_test.go index 84c66b8..b295d7a 100644 --- a/output_tcp_test.go +++ b/output_tcp_test.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "encoding/hex" "io" "log" "net" @@ -50,13 +49,10 @@ func startTCP(cb func([]byte)) net.Listener { go func() { reader := bufio.NewReader(conn) scanner := bufio.NewScanner(reader) + scanner.Split(payloadScanner) for scanner.Scan() { - encodedPayload := scanner.Bytes() - // Hex encoding always 2x number of bytes - decoded := make([]byte, len(encodedPayload)/2) - hex.Decode(decoded, encodedPayload) - cb(decoded) + cb(scanner.Bytes()) } }() }