Add endpoint to do basic regex testing against all the logfiles for a jail

This commit is contained in:
Sean DuBois 2014-07-05 06:03:53 +00:00
parent 598eea73e3
commit 17c57fa7f1
1 changed files with 44 additions and 0 deletions

44
jail.go
View File

@ -1,10 +1,13 @@
package main
import (
"bufio"
"encoding/json"
"github.com/Sean-Der/fail2go"
"github.com/gorilla/mux"
"net/http"
"os"
"regexp"
)
func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
@ -115,6 +118,43 @@ func jailDeleteFailRegexHandler(res http.ResponseWriter, req *http.Request, fail
res.Write(encodedOutput)
}
type RegexResult struct {
Line string
Match bool
}
func jailTestFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
var input jailFailRegexBody
err := json.NewDecoder(req.Body).Decode(&input)
if err != nil {
}
regexp, err := regexp.Compile(input.FailRegex)
if err != nil {
res.WriteHeader(400)
errOutput, _ := json.Marshal(ErrorBody{Error: err.Error()})
res.Write(errOutput)
return
}
_, _, fileList, _, _, _, _ := fail2goConn.JailStatus(mux.Vars(req)["jail"])
output := make(map[string][]RegexResult)
for _, fileName := range fileList {
file, _ := os.Open(fileName)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
output[fileName] = append(output[fileName], RegexResult{Match: regexp.MatchString(scanner.Text()), Line: scanner.Text()})
}
}
encodedOutput, err := json.Marshal(output)
if err != nil {
}
res.Write(encodedOutput)
}
type jailFindTimeBody struct {
FindTime int
}
@ -200,6 +240,10 @@ func jailHandler(jailRouter *mux.Router, fail2goConn *fail2go.Conn) {
jailDeleteFailRegexHandler(res, req, fail2goConn)
}).Methods("DELETE")
jailRouter.HandleFunc("/{jail}/testfailregex", func(res http.ResponseWriter, req *http.Request) {
jailTestFailRegexHandler(res, req, fail2goConn)
}).Methods("POST")
jailRouter.HandleFunc("/{jail}/findtime", func(res http.ResponseWriter, req *http.Request) {
jailSetFindTimeHandler(res, req, fail2goConn)
}).Methods("POST")