2014-06-16 05:38:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-05 08:03:53 +02:00
|
|
|
"bufio"
|
2014-06-16 05:38:10 +02:00
|
|
|
"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-07-05 08:03:53 +02:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2014-07-06 15:31:25 +02:00
|
|
|
"strings"
|
2014-06-16 05:38:10 +02:00
|
|
|
)
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailGetHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-07-08 07:28:30 +02:00
|
|
|
currentlyFailed, totalFailed, fileList, currentlyBanned, totalBanned, IPList, err := fail2goConn.JailStatus(mux.Vars(req)["jail"])
|
|
|
|
if err != nil {
|
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-20 15:58:57 +02:00
|
|
|
failRegexes, _ := fail2goConn.JailFailRegex(mux.Vars(req)["jail"])
|
2014-06-25 07:23:33 +02:00
|
|
|
findTime, _ := fail2goConn.JailFindTime(mux.Vars(req)["jail"])
|
|
|
|
useDNS, _ := fail2goConn.JailUseDNS(mux.Vars(req)["jail"])
|
|
|
|
maxRetry, _ := fail2goConn.JailMaxRetry(mux.Vars(req)["jail"])
|
2014-06-20 15:58:57 +02:00
|
|
|
|
2014-07-03 17:09:16 +02:00
|
|
|
if IPList == nil {
|
|
|
|
IPList = []string{}
|
|
|
|
}
|
2014-07-04 03:50:39 +02:00
|
|
|
if failRegexes == nil {
|
2014-07-03 17:58:18 +02:00
|
|
|
failRegexes = []string{}
|
|
|
|
}
|
2014-07-03 17:09:16 +02:00
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{
|
2014-06-20 15:58:57 +02:00
|
|
|
"currentlyFailed": currentlyFailed,
|
|
|
|
"totalFailed": totalFailed,
|
|
|
|
"fileList": fileList,
|
|
|
|
"currentlyBanned": currentlyBanned,
|
|
|
|
"totalBanned": totalBanned,
|
|
|
|
"IPList": IPList,
|
2014-06-25 07:23:33 +02:00
|
|
|
"failRegexes": failRegexes,
|
|
|
|
"findTime": findTime,
|
|
|
|
"useDNS": useDNS,
|
|
|
|
"maxRetry": maxRetry})
|
2014-06-16 05:38:10 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-17 08:02:54 +02:00
|
|
|
type jailBanIPBody struct {
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailBanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-17 08:02:54 +02:00
|
|
|
var input jailBanIPBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-17 08:02:54 +02:00
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
output, err := fail2goConn.JailBanIP(mux.Vars(req)["jail"], input.IP)
|
2014-06-17 08:02:54 +02:00
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-17 08:02:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"bannedIP": output})
|
2014-06-17 08:02:54 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailUnbanIPHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-17 08:02:54 +02:00
|
|
|
var input jailBanIPBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
output, err := fail2goConn.JailUnbanIP(mux.Vars(req)["jail"], input.IP)
|
2014-06-17 08:02:54 +02:00
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-17 08:02:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"unBannedIP": output})
|
2014-06-17 08:02:54 +02:00
|
|
|
res.Write(encodedOutput)
|
2014-06-21 04:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type jailFailRegexBody struct {
|
|
|
|
FailRegex string
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailAddFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-21 04:48:40 +02:00
|
|
|
var input jailFailRegexBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-21 04:48:40 +02:00
|
|
|
|
2014-06-23 08:53:42 +02:00
|
|
|
output, err := fail2goConn.JailAddFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-21 04:48:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"FailRegex": output})
|
2014-06-21 04:48:40 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
2014-06-17 08:02:54 +02:00
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailDeleteFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-21 04:48:40 +02:00
|
|
|
var input jailFailRegexBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-21 04:48:40 +02:00
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
output, err := fail2goConn.JailDeleteFailRegex(mux.Vars(req)["jail"], input.FailRegex)
|
2014-06-21 04:48:40 +02:00
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-21 04:48:40 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"FailRegex": output})
|
2014-06-21 04:48:40 +02:00
|
|
|
res.Write(encodedOutput)
|
2014-06-17 08:02:54 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 08:03:53 +02:00
|
|
|
type RegexResult struct {
|
|
|
|
Line string
|
|
|
|
Match bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func jailTestFailRegexHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
|
|
|
var input jailFailRegexBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
|
|
|
|
|
|
|
regexp, err := regexp.Compile(strings.Replace(input.FailRegex, "<HOST>", "(?:::f{4,6}:)?(?P<host>\\S+)", -1))
|
2014-07-06 15:31:25 +02:00
|
|
|
|
2014-07-05 08:03:53 +02:00
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-07-05 08:03:53 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
_, _, fileList, _, _, _, err := fail2goConn.JailStatus(mux.Vars(req)["jail"])
|
2014-07-05 08:03:53 +02:00
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
2014-07-05 08:03:53 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output := make(map[string][]RegexResult)
|
|
|
|
for _, fileName := range fileList {
|
2014-07-08 07:28:30 +02:00
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-05 08:03:53 +02:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
output[fileName] = append(output[fileName], RegexResult{Match: regexp.MatchString(scanner.Text()), Line: scanner.Text()})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(output)
|
2014-07-05 08:03:53 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-25 09:00:44 +02:00
|
|
|
type jailFindTimeBody struct {
|
|
|
|
FindTime int
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailSetFindTimeHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-25 09:00:44 +02:00
|
|
|
var input jailFindTimeBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-25 09:00:44 +02:00
|
|
|
|
|
|
|
output, err := fail2goConn.JailSetFindTime(mux.Vars(req)["jail"], input.FindTime)
|
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-25 09:00:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"FindTime": output})
|
2014-06-25 09:00:44 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
type jailUseDNSBody struct {
|
|
|
|
UseDNS string
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailSetUseDNSHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-25 09:00:44 +02:00
|
|
|
var input jailUseDNSBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-25 09:00:44 +02:00
|
|
|
|
|
|
|
output, err := fail2goConn.JailSetUseDNS(mux.Vars(req)["jail"], input.UseDNS)
|
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-25 09:00:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"useDNS": output})
|
2014-06-25 09:00:44 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
type jailMaxRetryBody struct {
|
|
|
|
MaxRetry int
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailSetMaxRetryHandler(res http.ResponseWriter, req *http.Request, fail2goConn *fail2go.Conn) {
|
2014-06-25 09:00:44 +02:00
|
|
|
var input jailMaxRetryBody
|
2014-07-08 07:28:30 +02:00
|
|
|
json.NewDecoder(req.Body).Decode(&input)
|
2014-06-25 09:00:44 +02:00
|
|
|
|
|
|
|
output, err := fail2goConn.JailSetMaxRetry(mux.Vars(req)["jail"], input.MaxRetry)
|
|
|
|
if err != nil {
|
2014-07-08 07:28:30 +02:00
|
|
|
writeHTTPError(res, err)
|
|
|
|
return
|
2014-06-25 09:00:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 07:28:30 +02:00
|
|
|
encodedOutput, _ := json.Marshal(map[string]interface{}{"maxRetry": output})
|
2014-06-25 09:00:44 +02:00
|
|
|
res.Write(encodedOutput)
|
|
|
|
}
|
|
|
|
|
2014-06-27 08:58:35 +02:00
|
|
|
func jailHandler(jailRouter *mux.Router, fail2goConn *fail2go.Conn) {
|
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-07-05 08:03:53 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}/testfailregex", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailTestFailRegexHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
2014-06-25 09:00:44 +02:00
|
|
|
jailRouter.HandleFunc("/{jail}/findtime", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailSetFindTimeHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
jailRouter.HandleFunc("/{jail}/usedns", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailSetUseDNSHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
|
|
|
jailRouter.HandleFunc("/{jail}/maxretry", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
jailSetMaxRetryHandler(res, req, fail2goConn)
|
|
|
|
}).Methods("POST")
|
|
|
|
|
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
|
|
|
}
|