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.
This commit is contained in:
Hector 2021-08-29 12:03:44 +01:00
parent 58694047c6
commit 172971a055

View File

@ -3,9 +3,7 @@ package socket
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/kisielk/og-rek"
"github.com/nlpodyssey/gopickle/pickle" "github.com/nlpodyssey/gopickle/pickle"
"net"
) )
const ( const (
@ -15,28 +13,28 @@ const (
) )
func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) { func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) {
err := write(s.encoder, command) err := s.write(command)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return read(&s.socket) return s.read()
} }
func write(encoder *ogórek.Encoder, command []string) error { func (s *Fail2BanSocket) write(command []string) error {
err := encoder.Encode(command) err := s.encoder.Encode(command)
if err != nil { if err != nil {
return err return err
} }
err = encoder.Encode(commandTerminator) _, err = s.socket.Write([]byte(commandTerminator))
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func read(s *net.Conn) (interface{}, error) { func (s *Fail2BanSocket) read() (interface{}, error) {
buf := make([]byte, socketReadBufferSize) buf := make([]byte, socketReadBufferSize)
_, err := (*s).Read(buf) _, err := s.socket.Read(buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }