From c9ba89e7253e027f14124b0e95ab19f7b0b12ab0 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Wed, 18 Jun 2014 05:53:54 +0000 Subject: [PATCH] Fail2bansocket location is now a configuration option, refactor project since fail2go does method dispatch off of Fail2goConn struct --- config.json | 3 ++- fail2rest.go | 13 ++++++++++--- global.go | 18 +++++++++++------- jail.go | 30 +++++++++++++++++------------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/config.json b/config.json index ee3aedf..72d38b9 100644 --- a/config.json +++ b/config.json @@ -1,3 +1,4 @@ { - "Addr": "0.0.0.0:5000" + "Addr": "0.0.0.0:5000", + "Fail2banSocket": "/var/run/fail2ban/fail2ban.sock" } diff --git a/fail2rest.go b/fail2rest.go index 4cf9f91..331677c 100644 --- a/fail2rest.go +++ b/fail2rest.go @@ -3,15 +3,19 @@ package main import ( "encoding/json" "fmt" + "github.com/Sean-Der/fail2go" "github.com/gorilla/mux" "net/http" "os" ) type Configuration struct { - Addr string + Addr string + Fail2banSocket string } +var fail2goConn *fail2go.Fail2goConn + func main() { file, fileErr := os.Open("config.json") @@ -28,9 +32,12 @@ func main() { os.Exit(1) } + fail2goConn := fail2go.Newfail2goConn(configuration.Fail2banSocket) r := mux.NewRouter() - globalHandler(r.PathPrefix("/global").Subrouter()) - jailHandler(r.PathPrefix("/jail").Subrouter()) + + globalHandler(r.PathPrefix("/global").Subrouter(), fail2goConn) + jailHandler(r.PathPrefix("/jail").Subrouter(), fail2goConn) + http.Handle("/", r) http.ListenAndServe(configuration.Addr, nil) } diff --git a/global.go b/global.go index a3279d8..56c6756 100644 --- a/global.go +++ b/global.go @@ -7,8 +7,8 @@ import ( "net/http" ) -func globalStatusHandler(res http.ResponseWriter, req *http.Request) { - globalStatus, _ := fail2go.GlobalStatus() +func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) { + globalStatus, _ := fail2goConn.GlobalStatus() encodedOutput, err := json.Marshal(globalStatus) if err != nil { @@ -17,8 +17,8 @@ func globalStatusHandler(res http.ResponseWriter, req *http.Request) { res.Write(encodedOutput) } -func globalPingHandler(res http.ResponseWriter, req *http.Request) { - globalPing, _ := fail2go.GlobalPing() +func globalPingHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) { + globalPing, _ := fail2goConn.GlobalPing() encodedOutput, err := json.Marshal(globalPing) if err != nil { @@ -28,7 +28,11 @@ func globalPingHandler(res http.ResponseWriter, req *http.Request) { } -func globalHandler(globalRouter *mux.Router) { - globalRouter.HandleFunc("/status", globalStatusHandler).Methods("GET") - globalRouter.HandleFunc("/ping", globalPingHandler).Methods("GET") +func globalHandler(globalRouter *mux.Router, fail2goConn *fail2go.Fail2goConn) { + globalRouter.HandleFunc("/status", func(res http.ResponseWriter, req *http.Request) { + globalStatusHandler(res, req, fail2goConn) + }).Methods("GET") + globalRouter.HandleFunc("/ping", func(res http.ResponseWriter, req *http.Request) { + globalPingHandler(res, req, fail2goConn) + }).Methods("GET") } diff --git a/jail.go b/jail.go index 0cac162..e3dcd00 100644 --- a/jail.go +++ b/jail.go @@ -7,9 +7,9 @@ import ( "net/http" ) -func jailGetHandler(res http.ResponseWriter, req *http.Request) { - jailStatus, _ := fail2go.JailStatus(mux.Vars(req)["jail"]) - jailFailRegex, _ := fail2go.JailFailRegex(mux.Vars(req)["jail"]) +func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) { + jailStatus, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"]) + jailFailRegex, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"]) output := make(map[string]interface{}) @@ -31,13 +31,13 @@ type jailBanIPBody struct { IP string } -func jailBanIPHandler(res http.ResponseWriter, req *http.Request) { +func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) { var input jailBanIPBody err := json.NewDecoder(req.Body).Decode(&input) if err != nil { } - output, _ := fail2go.JailBanIP(mux.Vars(req)["jail"], input.IP) + output, _ := fail2goConn.JailBanIP(mux.Vars(req)["jail"], input.IP) encodedOutput, err := json.Marshal(output) if err != nil { @@ -46,13 +46,13 @@ func jailBanIPHandler(res http.ResponseWriter, req *http.Request) { res.Write(encodedOutput) } -func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request) { +func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) { var input jailBanIPBody err := json.NewDecoder(req.Body).Decode(&input) if err != nil { } - output, _ := fail2go.JailUnbanIP(mux.Vars(req)["jail"], input.IP) + output, _ := fail2goConn.JailUnbanIP(mux.Vars(req)["jail"], input.IP) encodedOutput, err := json.Marshal(output) if err != nil { @@ -62,10 +62,14 @@ func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request) { } -func jailHandler(jailRouter *mux.Router) { - jailRouter.HandleFunc("/{jail}/banip", jailBanIPHandler).Methods("POST") - jailRouter.HandleFunc("/{jail}/unbanip", jailUnbanIPHandler).Methods("POST") - - jailRouter.HandleFunc("/{jail}", jailGetHandler).Methods("GET") - +func jailHandler(jailRouter *mux.Router, fail2goConn *fail2go.Fail2goConn) { + jailRouter.HandleFunc("/{jail}/banip", func(res http.ResponseWriter, req *http.Request) { + jailBanIPHandler(res, req, fail2goConn) + }).Methods("POST") + jailRouter.HandleFunc("/{jail}/unbanip", func(res http.ResponseWriter, req *http.Request) { + jailUnbanIPHandler(res, req, fail2goConn) + }).Methods("POST") + jailRouter.HandleFunc("/{jail}", func(res http.ResponseWriter, req *http.Request) { + jailGetHandler(res, req, fail2goConn) + }).Methods("GET") }