Implement #'fail2banRequest, allowing us to send a slice of commands to the fail2ban socket and using ogrek for serialize/deserialization to pickle

This commit is contained in:
Sean DuBois 2014-06-13 04:06:27 +00:00
commit 28316de576
2 changed files with 52 additions and 0 deletions

39
fail2banClient.go Normal file
View file

@ -0,0 +1,39 @@
package main
import (
"bytes"
"errors"
"github.com/kisielk/og-rek"
"net"
)
func fail2banRequest(input []string) (interface{}, error) {
c, err := net.Dial("unix", "/var/run/fail2ban/fail2ban.sock")
if err != nil {
return nil, errors.New("Failed to contact fail2ban socket")
}
p := &bytes.Buffer{}
ogórek.NewEncoder(p).Encode(input)
c.Write(p.Bytes())
c.Write([]byte("<F2B_END_COMMAND>"))
buf := make([]byte, 0)
tmpBuf := make([]byte, 1)
for {
bufRead, _ := c.Read(tmpBuf)
if bufRead != 0 {
buf = append(buf, tmpBuf...)
} else {
buf = buf[:len(buf)-17]
break
}
}
dec := ogórek.NewDecoder(bytes.NewBuffer(buf))
v, err := dec.Decode()
return v, err
}

13
main.go Normal file
View file

@ -0,0 +1,13 @@
package main
import ("fmt")
func main() {
x := make([]string, 0)
x = append(x, "status")
output, err := fail2banRequest(x)
fmt.Println(output)
fmt.Println(err)
}