Fail2bansocket location is now a configuration option, refactor project since fail2go does method dispatch off of Fail2goConn struct

This commit is contained in:
Sean DuBois 2014-06-18 05:53:54 +00:00
parent cf7967390d
commit c9ba89e725
4 changed files with 40 additions and 24 deletions

View file

@ -1,3 +1,4 @@
{ {
"Addr": "0.0.0.0:5000" "Addr": "0.0.0.0:5000",
"Fail2banSocket": "/var/run/fail2ban/fail2ban.sock"
} }

View file

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Sean-Der/fail2go"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"net/http" "net/http"
"os" "os"
@ -10,8 +11,11 @@ import (
type Configuration struct { type Configuration struct {
Addr string Addr string
Fail2banSocket string
} }
var fail2goConn *fail2go.Fail2goConn
func main() { func main() {
file, fileErr := os.Open("config.json") file, fileErr := os.Open("config.json")
@ -28,9 +32,12 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fail2goConn := fail2go.Newfail2goConn(configuration.Fail2banSocket)
r := mux.NewRouter() 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.Handle("/", r)
http.ListenAndServe(configuration.Addr, nil) http.ListenAndServe(configuration.Addr, nil)
} }

View file

@ -7,8 +7,8 @@ import (
"net/http" "net/http"
) )
func globalStatusHandler(res http.ResponseWriter, req *http.Request) { func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
globalStatus, _ := fail2go.GlobalStatus() globalStatus, _ := fail2goConn.GlobalStatus()
encodedOutput, err := json.Marshal(globalStatus) encodedOutput, err := json.Marshal(globalStatus)
if err != nil { if err != nil {
@ -17,8 +17,8 @@ func globalStatusHandler(res http.ResponseWriter, req *http.Request) {
res.Write(encodedOutput) res.Write(encodedOutput)
} }
func globalPingHandler(res http.ResponseWriter, req *http.Request) { func globalPingHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
globalPing, _ := fail2go.GlobalPing() globalPing, _ := fail2goConn.GlobalPing()
encodedOutput, err := json.Marshal(globalPing) encodedOutput, err := json.Marshal(globalPing)
if err != nil { if err != nil {
@ -28,7 +28,11 @@ func globalPingHandler(res http.ResponseWriter, req *http.Request) {
} }
func globalHandler(globalRouter *mux.Router) { func globalHandler(globalRouter *mux.Router, fail2goConn *fail2go.Fail2goConn) {
globalRouter.HandleFunc("/status", globalStatusHandler).Methods("GET") globalRouter.HandleFunc("/status", func(res http.ResponseWriter, req *http.Request) {
globalRouter.HandleFunc("/ping", globalPingHandler).Methods("GET") globalStatusHandler(res, req, fail2goConn)
}).Methods("GET")
globalRouter.HandleFunc("/ping", func(res http.ResponseWriter, req *http.Request) {
globalPingHandler(res, req, fail2goConn)
}).Methods("GET")
} }

30
jail.go
View file

@ -7,9 +7,9 @@ import (
"net/http" "net/http"
) )
func jailGetHandler(res http.ResponseWriter, req *http.Request) { func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
jailStatus, _ := fail2go.JailStatus(mux.Vars(req)["jail"]) jailStatus, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"])
jailFailRegex, _ := fail2go.JailFailRegex(mux.Vars(req)["jail"]) jailFailRegex, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"])
output := make(map[string]interface{}) output := make(map[string]interface{})
@ -31,13 +31,13 @@ type jailBanIPBody struct {
IP string IP string
} }
func jailBanIPHandler(res http.ResponseWriter, req *http.Request) { func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
var input jailBanIPBody var input jailBanIPBody
err := json.NewDecoder(req.Body).Decode(&input) err := json.NewDecoder(req.Body).Decode(&input)
if err != nil { 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) encodedOutput, err := json.Marshal(output)
if err != nil { if err != nil {
@ -46,13 +46,13 @@ func jailBanIPHandler(res http.ResponseWriter, req *http.Request) {
res.Write(encodedOutput) 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 var input jailBanIPBody
err := json.NewDecoder(req.Body).Decode(&input) err := json.NewDecoder(req.Body).Decode(&input)
if err != nil { 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) encodedOutput, err := json.Marshal(output)
if err != nil { if err != nil {
@ -62,10 +62,14 @@ func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request) {
} }
func jailHandler(jailRouter *mux.Router) { func jailHandler(jailRouter *mux.Router, fail2goConn *fail2go.Fail2goConn) {
jailRouter.HandleFunc("/{jail}/banip", jailBanIPHandler).Methods("POST") jailRouter.HandleFunc("/{jail}/banip", func(res http.ResponseWriter, req *http.Request) {
jailRouter.HandleFunc("/{jail}/unbanip", jailUnbanIPHandler).Methods("POST") jailBanIPHandler(res, req, fail2goConn)
}).Methods("POST")
jailRouter.HandleFunc("/{jail}", jailGetHandler).Methods("GET") 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")
} }