2014-06-16 05:38:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/Sean-Der/fail2go"
|
2014-06-16 10:15:26 +02:00
|
|
|
"github.com/gorilla/mux"
|
2014-06-16 05:38:10 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
2014-06-20 15:58:57 +02:00
|
|
|
currentlyFailed, totalFailed, fileList, currentlyBanned, totalBanned, IPList, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"])
|
|
|
|
failRegexes, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"])
|
|
|
|
|
|
|
|
encodedOutput, err := json.Marshal(map[string]interface{}{
|
|
|
|
"currentlyFailed": currentlyFailed,
|
|
|
|
"totalFailed": totalFailed,
|
|
|
|
"fileList": fileList,
|
|
|
|
"currentlyBanned": currentlyBanned,
|
|
|
|
"totalBanned": totalBanned,
|
|
|
|
"IPList": IPList,
|
2014-06-21 04:48:40 +02:00
|
|
|
"failRegexes": failRegexes})
|
2014-06-16 05:38:10 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-17 08:02:54 +02:00
|
|
|
type jailBanIPBody struct {
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
2014-06-17 08:02:54 +02:00
|
|
|
var input jailBanIPBody
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
output, _ := fail2goConn.JailBanIP(mux.Vars(req)["jail"], input.IP)
|
2014-06-17 08:02:54 +02:00
|
|
|
|
2014-06-20 15:58:57 +02:00
|
|
|
encodedOutput, err := json.Marshal(map[string]interface{}{"bannedIP": output})
|
2014-06-17 08:02:54 +02:00
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
2014-06-17 08:02:54 +02:00
|
|
|
var input jailBanIPBody
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
output, _ := fail2goConn.JailUnbanIP(mux.Vars(req)["jail"], input.IP)
|
2014-06-17 08:02:54 +02:00
|
|
|
|
2014-06-20 15:58:57 +02:00
|
|
|
encodedOutput, err := json.Marshal(map[string]interface{}{"unBannedIP": output})
|
2014-06-17 08:02:54 +02:00
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(encodedOutput)
|
2014-06-21 04:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type jailFailRegexBody struct {
|
|
|
|
FailRegex string
|
|
|
|
}
|
|
|
|
|
|
|
|
func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
|
|
|
var input jailFailRegexBody
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
output, _ := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
|
|
|
|
|
|
|
encodedOutput, err := json.Marshal(map[string]interface{}{"FailRegex": output})
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
2014-06-17 08:02:54 +02:00
|
|
|
|
2014-06-21 04:48:40 +02:00
|
|
|
func jailDeleteFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
|
|
|
var input jailFailRegexBody
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
output, _ := fail2goConn.JailDeleteFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
|
|
|
|
|
|
|
encodedOutput, err := json.Marshal(map[string]interface{}{"FailRegex": output})
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Write(encodedOutput)
|
2014-06-17 08:02:54 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
func jailHandler(jailRouter *mux.Router, fail2goConn *fail2go.Fail2goConn) {
|
2014-06-20 08:30:18 +02:00
|
|
|
|
2014-06-21 04:48:40 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}/bannedip", func(res http.ResponseWriter, req *http.Request) {
|
2014-06-18 07:53:54 +02:00
|
|
|
jailBanIPHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
2014-06-21 04:48:40 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}/bannedip", func(res http.ResponseWriter, req *http.Request) {
|
2014-06-18 07:53:54 +02:00
|
|
|
jailUnbanIPHandler(res, req, fail2goConn)
|
2014-06-20 08:30:18 +02:00
|
|
|
}).Methods("DELETE")
|
|
|
|
|
2014-06-21 04:48:40 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}/failregex", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailAddFailRegexHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
|
|
|
jailRouter.HandleFunc("/{jail}/failregex", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailDeleteFailRegexHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("DELETE")
|
|
|
|
|
2014-06-18 07:53:54 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailGetHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("GET")
|
2014-06-16 05:38:10 +02:00
|
|
|
}
|