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,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)
}

View file

@ -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")
}

30
jail.go
View file

@ -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")
}