
Add support for sending a ping command to the fail2ban server over the socket file. This includes encoding the command data using og-rek and decoding the response using `gopickle`.
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package socket
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/kisielk/og-rek"
|
|
"github.com/nlpodyssey/gopickle/pickle"
|
|
"net"
|
|
)
|
|
|
|
const (
|
|
commandTerminator = "<F2B_END_COMMAND>"
|
|
pingCommand = "ping"
|
|
socketReadBufferSize = 10000
|
|
)
|
|
|
|
func (s *Fail2BanSocket) sendCommand(command []string) (interface{}, error) {
|
|
err := write(s.encoder, command)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return read(&s.socket)
|
|
}
|
|
|
|
func write(encoder *ogórek.Encoder, command []string) error {
|
|
err := encoder.Encode(command)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = encoder.Encode(commandTerminator)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func read(s *net.Conn) (interface{}, error) {
|
|
buf := make([]byte, socketReadBufferSize)
|
|
_, err := (*s).Read(buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
bufReader := bytes.NewReader(buf)
|
|
unpickler := pickle.NewUnpickler(bufReader)
|
|
|
|
unpickler.FindClass = func(module, name string) (interface{}, error) {
|
|
if module == "builtins" && name == "str" {
|
|
return &Py_builtins_str{}, nil
|
|
}
|
|
return nil, fmt.Errorf("class not found: " + module + " : " + name)
|
|
}
|
|
|
|
return unpickler.Load()
|
|
}
|