func jailTestFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { var input jailFailRegexBody json.NewDecoder(req.Body).Decode(&input) regexp, err := regexp.Compile(strings.Replace(input.FailRegex, "<HOST>", "(?:::f{4,6}:)?(?P<host>\\S+)", -1)) if err != nil { writeHTTPError(res, err) return } _, _, fileList, _, _, _, err := fail2goConn.JailStatus(mux.Vars(req)["jail"]) if err != nil { writeHTTPError(res, err) return } output := make(map[string][]RegexResult) for _, fileName := range fileList { file, err := os.Open(fileName) if err != nil { writeHTTPError(res, err) return } scanner := bufio.NewScanner(file) for scanner.Scan() { output[fileName] = append(output[fileName], RegexResult{Match: regexp.MatchString(scanner.Text()), Line: scanner.Text()}) } } encodedOutput, _ := json.Marshal(output) res.Write(encodedOutput) }
func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) { 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"]) findTime, _ := fail2goConn.JailFindTime(mux.Vars(req)["jail"]) useDNS, _ := fail2goConn.JailUseDNS(mux.Vars(req)["jail"]) maxRetry, _ := fail2goConn.JailMaxRetry(mux.Vars(req)["jail"]) if IPList == nil { IPList = []string{} } if failRegexes == nil { failRegexes = []string{} } encodedOutput, _ := json.Marshal(map[string]interface{}{ "currentlyFailed": currentlyFailed, "totalFailed": totalFailed, "fileList": fileList, "currentlyBanned": currentlyBanned, "totalBanned": totalBanned, "IPList": IPList, "failRegexes": failRegexes, "findTime": findTime, "useDNS": useDNS, "maxRetry": maxRetry}) res.Write(encodedOutput) }