From 28316de576c625629836579a342defdfb30f1054 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Fri, 13 Jun 2014 04:06:27 +0000 Subject: [PATCH] Implement #'fail2banRequest, allowing us to send a slice of commands to the fail2ban socket and using ogrek for serialize/deserialization to pickle --- fail2banClient.go | 39 +++++++++++++++++++++++++++++++++++++++ main.go | 13 +++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 fail2banClient.go create mode 100644 main.go diff --git a/fail2banClient.go b/fail2banClient.go new file mode 100644 index 0000000..6a42a84 --- /dev/null +++ b/fail2banClient.go @@ -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("")) + + 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 +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..6799d90 --- /dev/null +++ b/main.go @@ -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) + +}