All fail2go communication now has error handling, JSON casting still has no error handling

This commit is contained in:
Sean DuBois 2014-07-08 05:28:30 +00:00
parent 36cfd63495
commit c59cb07298
2 changed files with 60 additions and 91 deletions

View file

@ -9,30 +9,25 @@ import (
func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func globalStatusHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
globalStatus, err := fail2goConn.GlobalStatus() globalStatus, err := fail2goConn.GlobalStatus()
if err != nil {
writeHTTPError(res, err)
return
}
encodedOutput, err := json.Marshal(globalStatus)
if err != nil { if err != nil {
writeHTTPError(res, err) writeHTTPError(res, err)
return return
} }
encodedOutput, _ := json.Marshal(globalStatus)
res.Write(encodedOutput) res.Write(encodedOutput)
} }
func globalPingHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func globalPingHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
globalPing, _ := fail2goConn.GlobalPing() globalPing, err := fail2goConn.GlobalPing()
encodedOutput, err := json.Marshal(globalPing)
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
res.Write(encodedOutput)
encodedOutput, _ := json.Marshal(globalPing)
res.Write(encodedOutput)
} }
func globalHandler(globalRouter *mux.Router, fail2goConn *fail2go.Conn) { func globalHandler(globalRouter *mux.Router, fail2goConn *fail2go.Conn) {

134
jail.go
View file

@ -12,7 +12,12 @@ import (
) )
func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
currentlyFailed, totalFailed, fileList, currentlyBanned, totalBanned, IPList, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"]) currentlyFailed, totalFailed, fileList, currentlyBanned, totalBanned, IPList, err := fail2goConn.JailStatus(mux.Vars(req)["jail"])
if err != nil {
writeHTTPError(res, err)
return
}
failRegexes, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"]) failRegexes, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"])
findTime, _ := fail2goConn.JailFindTime(mux.Vars(req)["jail"]) findTime, _ := fail2goConn.JailFindTime(mux.Vars(req)["jail"])
useDNS, _ := fail2goConn.JailUseDNS(mux.Vars(req)["jail"]) useDNS, _ := fail2goConn.JailUseDNS(mux.Vars(req)["jail"])
@ -25,7 +30,7 @@ func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fai
failRegexes = []string{} failRegexes = []string{}
} }
encodedOutput, err := json.Marshal(map[string]interface{}{ encodedOutput, _ := json.Marshal(map[string]interface{}{
"currentlyFailed": currentlyFailed, "currentlyFailed": currentlyFailed,
"totalFailed": totalFailed, "totalFailed": totalFailed,
"fileList": fileList, "fileList": fileList,
@ -36,10 +41,6 @@ func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fai
"findTime": findTime, "findTime": findTime,
"useDNS": useDNS, "useDNS": useDNS,
"maxRetry": maxRetry}) "maxRetry": maxRetry})
if err != nil {
}
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -49,31 +50,28 @@ type jailBanIPBody struct {
func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailBanIPBody var input jailBanIPBody
err := json.NewDecoder(req.Body).Decode(&input) json.NewDecoder(req.Body).Decode(&input)
if err != nil {
} output, err := fail2goConn.JailBanIP(mux.Vars(req)["jail"], input.IP)
output, _ := fail2goConn.JailBanIP(mux.Vars(req)["jail"], input.IP)
encodedOutput, err := json.Marshal(map[string]interface{}{"bannedIP": output})
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, _ := json.Marshal(map[string]interface{}{"bannedIP": output})
res.Write(encodedOutput) res.Write(encodedOutput)
} }
func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailBanIPBody var input jailBanIPBody
err := json.NewDecoder(req.Body).Decode(&input) json.NewDecoder(req.Body).Decode(&input)
if err != nil { output, err := fail2goConn.JailUnbanIP(mux.Vars(req)["jail"], input.IP)
}
output, _ := fail2goConn.JailUnbanIP(mux.Vars(req)["jail"], input.IP)
encodedOutput, err := json.Marshal(map[string]interface{}{"unBannedIP": output})
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, _ := json.Marshal(map[string]interface{}{"unBannedIP": output})
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -83,39 +81,29 @@ type jailFailRegexBody struct {
func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailFailRegexBody var input jailFailRegexBody
var encodedOutput []byte json.NewDecoder(req.Body).Decode(&input)
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
output, err := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex) output, err := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
if err != nil { if err != nil {
res.WriteHeader(400) writeHTTPError(res, err)
encodedOutput, err = json.Marshal(ErrorBody{Error: err.Error()}) return
} else {
encodedOutput, err = json.Marshal(map[string]interface{}{"FailRegex": output})
}
if err != nil {
} }
encodedOutput, _ := json.Marshal(map[string]interface{}{"FailRegex": output})
res.Write(encodedOutput) res.Write(encodedOutput)
} }
func jailDeleteFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailDeleteFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailFailRegexBody var input jailFailRegexBody
err := json.NewDecoder(req.Body).Decode(&input) json.NewDecoder(req.Body).Decode(&input)
if err != nil {
} output, err := fail2goConn.JailDeleteFailRegex(mux.Vars(req)["jail"], input.FailRegex)
output, _ := fail2goConn.JailDeleteFailRegex(mux.Vars(req)["jail"], input.FailRegex)
encodedOutput, err := json.Marshal(map[string]interface{}{"FailRegex": output})
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, _ := json.Marshal(map[string]interface{}{"FailRegex": output})
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -126,34 +114,35 @@ type RegexResult struct {
func jailTestFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailTestFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailFailRegexBody var input jailFailRegexBody
err := json.NewDecoder(req.Body).Decode(&input) json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
regexp, err := regexp.Compile(strings.Replace(input.FailRegex, "<HOST>", "(?:::f{4,6}:)?(?P<host>\\S+)", -1)) regexp, err := regexp.Compile(strings.Replace(input.FailRegex, "<HOST>", "(?:::f{4,6}:)?(?P<host>\\S+)", -1))
if err != nil { if err != nil {
res.WriteHeader(400) writeHTTPError(res, err)
errOutput, _ := json.Marshal(ErrorBody{Error: err.Error()}) return
res.Write(errOutput) }
_, _, fileList, _, _, _, err := fail2goConn.JailStatus(mux.Vars(req)["jail"])
if err != nil {
writeHTTPError(res, err)
return return
} }
_, _, fileList, _, _, _, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"])
output := make(map[string][]RegexResult) output := make(map[string][]RegexResult)
for _, fileName := range fileList { for _, fileName := range fileList {
file, _ := os.Open(fileName) file, err := os.Open(fileName)
if err != nil {
writeHTTPError(res, err)
return
}
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
for scanner.Scan() { for scanner.Scan() {
output[fileName] = append(output[fileName], RegexResult{Match: regexp.MatchString(scanner.Text()), Line: scanner.Text()}) output[fileName] = append(output[fileName], RegexResult{Match: regexp.MatchString(scanner.Text()), Line: scanner.Text()})
} }
} }
encodedOutput, err := json.Marshal(output) encodedOutput, _ := json.Marshal(output)
if err != nil {
}
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -163,20 +152,15 @@ type jailFindTimeBody struct {
func jailSetFindTimeHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailSetFindTimeHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailFindTimeBody var input jailFindTimeBody
json.NewDecoder(req.Body).Decode(&input)
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
output, err := fail2goConn.JailSetFindTime(mux.Vars(req)["jail"], input.FindTime) output, err := fail2goConn.JailSetFindTime(mux.Vars(req)["jail"], input.FindTime)
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, err := json.Marshal(map[string]interface{}{"FindTime": output}) encodedOutput, _ := json.Marshal(map[string]interface{}{"FindTime": output})
if err != nil {
}
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -186,20 +170,15 @@ type jailUseDNSBody struct {
func jailSetUseDNSHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailSetUseDNSHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailUseDNSBody var input jailUseDNSBody
json.NewDecoder(req.Body).Decode(&input)
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
output, err := fail2goConn.JailSetUseDNS(mux.Vars(req)["jail"], input.UseDNS) output, err := fail2goConn.JailSetUseDNS(mux.Vars(req)["jail"], input.UseDNS)
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, err := json.Marshal(map[string]interface{}{"useDNS": output}) encodedOutput, _ := json.Marshal(map[string]interface{}{"useDNS": output})
if err != nil {
}
res.Write(encodedOutput) res.Write(encodedOutput)
} }
@ -209,20 +188,15 @@ type jailMaxRetryBody struct {
func jailSetMaxRetryHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { func jailSetMaxRetryHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailMaxRetryBody var input jailMaxRetryBody
json.NewDecoder(req.Body).Decode(&input)
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
output, err := fail2goConn.JailSetMaxRetry(mux.Vars(req)["jail"], input.MaxRetry) output, err := fail2goConn.JailSetMaxRetry(mux.Vars(req)["jail"], input.MaxRetry)
if err != nil { if err != nil {
writeHTTPError(res, err)
return
} }
encodedOutput, err := json.Marshal(map[string]interface{}{"maxRetry": output}) encodedOutput, _ := json.Marshal(map[string]interface{}{"maxRetry": output})
if err != nil {
}
res.Write(encodedOutput) res.Write(encodedOutput)
} }