From 172971a055c3226a1ea9b2cf194c7d13b5d6ac7a Mon Sep 17 00:00:00 2001 From: Hector Date: Sun, 29 Aug 2021 12:03:44 +0100 Subject: [PATCH] fix command writing code Fix the code writing commands to the fail2ban socket to correctly encode the command terminator string. The terminator string should be encoded as a simple byte array, not using the pickle format. Using the pickle format allowed the first command to succeed, but would "break" the socket in the sense that all following commands would fail with an underflow exception. --- src/socket/protocol.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/socket/protocol.go b/src/socket/protocol.go index 905cae1..9dc10f5 100644 --- a/src/socket/protocol.go +++ b/src/socket/protocol.go @@ -3,9 +3,7 @@ package socket import ( "bytes" "fmt" - "github.com/kisielk/og-rek" "github.com/nlpodyssey/gopickle/pickle" - "net" ) const ( @@ -15,28 +13,28 @@ const ( ) func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) { - err := write(s.encoder, command) + err := s.write(command) if err != nil { return nil, err } - return read(&s.socket) + return s.read() } -func write(encoder *ogórek.Encoder, command []string) error { - err := encoder.Encode(command) +func (s *Fail2BanSocket) write(command []string) error { + err := s.encoder.Encode(command) if err != nil { return err } - err = encoder.Encode(commandTerminator) + _, err = s.socket.Write([]byte(commandTerminator)) if err != nil { return err } return nil } -func read(s *net.Conn) (interface{}, error) { +func (s *Fail2BanSocket) read() (interface{}, error) { buf := make([]byte, socketReadBufferSize) - _, err := (*s).Read(buf) + _, err := s.socket.Read(buf) if err != nil { return nil, err }