mirror of
https://github.com/Sean-Der/fail2rest.git
synced 2024-12-22 13:42:17 +01:00
Add ErrorBody struct so all errors will have the same JSON format. If a failregex is malformed send a 400 error code with the fail message
This commit is contained in:
parent
66961603d6
commit
b46fd86c8a
2 changed files with 15 additions and 3 deletions
|
@ -14,6 +14,10 @@ type Configuration struct {
|
|||
Fail2banSocket string
|
||||
}
|
||||
|
||||
type ErrorBody struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
var fail2goConn *fail2go.Fail2goConn
|
||||
|
||||
func main() {
|
||||
|
|
14
jail.go
14
jail.go
|
@ -18,7 +18,7 @@ func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fai
|
|||
"currentlyBanned": currentlyBanned,
|
||||
"totalBanned": totalBanned,
|
||||
"IPList": IPList,
|
||||
"failRegexes": failRegexes})
|
||||
"failRegexes": failRegexes})
|
||||
|
||||
if err != nil {
|
||||
}
|
||||
|
@ -66,13 +66,21 @@ type jailFailRegexBody struct {
|
|||
|
||||
func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Fail2goConn) {
|
||||
var input jailFailRegexBody
|
||||
var encodedOutput []byte
|
||||
|
||||
err := json.NewDecoder(req.Body).Decode(&input)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
|
||||
output, _ := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
||||
output, err := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
||||
if err != nil {
|
||||
res.WriteHeader(400)
|
||||
encodedOutput, err = json.Marshal(ErrorBody{Error: "Invalid Regex"})
|
||||
} else {
|
||||
encodedOutput, err = json.Marshal(map[string]interface{}{"FailRegex": output})
|
||||
}
|
||||
|
||||
encodedOutput, err := json.Marshal(map[string]interface{}{"FailRegex": output})
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue