mirror of
https://github.com/Sean-Der/fail2rest.git
synced 2024-12-22 13:42:17 +01:00
Move fail2ban socket communication into its own project called fail2go
This commit is contained in:
parent
59f9d9b0a2
commit
405d61e729
7 changed files with 64 additions and 138 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
##Overview
|
##Overview
|
||||||
|
|
||||||
fail2rest is a small REST server that aims to replicate the failban-client UI
|
fail2rest is a small REST server that aims to allow full administration of a fail2ban server via HTTP
|
||||||
|
|
||||||
fail2rest will eventually be used as a backend to a small web app to make fail2ban
|
fail2rest will eventually be used as a backend to a small web app to make fail2ban
|
||||||
administration and reporting easier.
|
administration and reporting easier.
|
||||||
|
@ -13,12 +13,9 @@ Every PR will be merged! Feel free to open up PRs that aren't fully done, I will
|
||||||
my best to finish them for you. I will make sure to review everything I can. If
|
my best to finish them for you. I will make sure to review everything I can. If
|
||||||
you are interested in working on fail2rest, but don't know where to start here are some ideas.
|
you are interested in working on fail2rest, but don't know where to start here are some ideas.
|
||||||
|
|
||||||
* Find unimplemented fail2ban-client commands
|
|
||||||
* Improve data assertions before json.marshall (this is really important!)
|
|
||||||
* Document current API calls (and examples with cURL), small static website for this info
|
* Document current API calls (and examples with cURL), small static website for this info
|
||||||
* Start on fail2web, I would like to write this in angular, angular-ui, browserify and d3.js (Open to suggestions just most comfortable with this)
|
* Start on fail2web, I would like to write this in angular, angular-ui, browserify and d3.js (Open to suggestions just most comfortable with this)
|
||||||
* Write tests, and implement some post-commit system for running tests
|
* Write tests, and implement some post-commit system for running tests
|
||||||
* Expand fail2ban-server so that we can perform more operations via socket. I would like to avoid editing files as long as possible
|
|
||||||
|
|
||||||
##License
|
##License
|
||||||
iThe MIT License (MIT)
|
iThe MIT License (MIT)
|
||||||
|
|
50
basic.go
50
basic.go
|
@ -1,50 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func BasicStatusHandler(res http.ResponseWriter, req *http.Request) {
|
|
||||||
fail2banInput := make([]string, 1)
|
|
||||||
fail2banInput[0] = "status"
|
|
||||||
|
|
||||||
output, err := fail2banRequest(fail2banInput)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO use reflection to assert data structures and give proper errors
|
|
||||||
jails := output.([]interface{})[1].([]interface{})[1].([]interface{})[1]
|
|
||||||
jails = strings.Split(jails.(string), ",")
|
|
||||||
|
|
||||||
encodedOutput, err := json.Marshal(jails)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
res.Write(encodedOutput)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BasicPingHandler(res http.ResponseWriter, req *http.Request) {
|
|
||||||
fail2banInput := make([]string, 1)
|
|
||||||
fail2banInput[0] = "ping"
|
|
||||||
|
|
||||||
output, err := fail2banRequest(fail2banInput)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO use reflection to assert data structures and give proper errors
|
|
||||||
output = output.([]interface{})[1]
|
|
||||||
|
|
||||||
encodedOutput, err := json.Marshal(output)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
res.Write(encodedOutput)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BasicHandler(basicRouter *mux.Router) {
|
|
||||||
basicRouter.HandleFunc("/status", BasicStatusHandler).Methods("GET")
|
|
||||||
basicRouter.HandleFunc("/ping", BasicPingHandler).Methods("GET")
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"github.com/kisielk/og-rek"
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
func fail2banRequest(input []string) (interface{}, error) {
|
|
||||||
c, err := net.Dial("unix", "/var/run/fail2ban/fail2ban.sock")
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("Failed to contact fail2ban socket")
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &bytes.Buffer{}
|
|
||||||
ogórek.NewEncoder(p).Encode(input)
|
|
||||||
c.Write(p.Bytes())
|
|
||||||
c.Write([]byte("<F2B_END_COMMAND>"))
|
|
||||||
|
|
||||||
buf := make([]byte, 0)
|
|
||||||
tmpBuf := make([]byte, 1)
|
|
||||||
for {
|
|
||||||
bufRead, _ := c.Read(tmpBuf)
|
|
||||||
|
|
||||||
if bufRead != 0 {
|
|
||||||
buf = append(buf, tmpBuf...)
|
|
||||||
} else {
|
|
||||||
buf = buf[:len(buf)-17]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
dec := ogórek.NewDecoder(bytes.NewBuffer(buf))
|
|
||||||
v, err := dec.Decode()
|
|
||||||
return v, err
|
|
||||||
}
|
|
|
@ -29,8 +29,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
BasicHandler(r.PathPrefix("/basic").Subrouter())
|
globalHandler(r.PathPrefix("/global").Subrouter())
|
||||||
JailControlHandler(r.PathPrefix("/jailControl").Subrouter())
|
jailHandler(r.PathPrefix("/jail").Subrouter())
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
http.ListenAndServe(configuration.Addr, nil)
|
http.ListenAndServe(configuration.Addr, nil)
|
||||||
}
|
}
|
34
global.go
Normal file
34
global.go
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/Sean-Der/fail2go"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func globalStatusHandler(res http.ResponseWriter, req *http.Request) {
|
||||||
|
globalStatus, _ := fail2go.GlobalStatus()
|
||||||
|
|
||||||
|
encodedOutput, err := json.Marshal(globalStatus)
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Write(encodedOutput)
|
||||||
|
}
|
||||||
|
|
||||||
|
func globalPingHandler(res http.ResponseWriter, req *http.Request) {
|
||||||
|
globalPing, _ := fail2go.GlobalPing()
|
||||||
|
|
||||||
|
encodedOutput, err := json.Marshal(globalPing)
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Write(encodedOutput)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func globalHandler(globalRouter *mux.Router) {
|
||||||
|
globalRouter.HandleFunc("/status", globalStatusHandler).Methods("GET")
|
||||||
|
globalRouter.HandleFunc("/ping", globalPingHandler).Methods("GET")
|
||||||
|
}
|
27
jail.go
Normal file
27
jail.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/Sean-Der/fail2go"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func jailGetHandler(res http.ResponseWriter, req *http.Request) {
|
||||||
|
jailStatus, _ := fail2go.JailStatus(mux.Vars(req)["jail"])
|
||||||
|
|
||||||
|
output := make(map[string]interface{})
|
||||||
|
|
||||||
|
for key, value := range jailStatus {
|
||||||
|
output[key] = value
|
||||||
|
}
|
||||||
|
encodedOutput, err := json.Marshal(output)
|
||||||
|
if err != nil {
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Write(encodedOutput)
|
||||||
|
}
|
||||||
|
|
||||||
|
func jailHandler(jailRouter *mux.Router) {
|
||||||
|
jailRouter.HandleFunc("/{jail}", jailGetHandler).Methods("GET")
|
||||||
|
}
|
|
@ -1,43 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func JailControlStatusHandler(res http.ResponseWriter, req *http.Request) {
|
|
||||||
fail2banInput := make([]string, 2)
|
|
||||||
fail2banInput[0] = "status"
|
|
||||||
fail2banInput[1] = mux.Vars(req)["jail"]
|
|
||||||
|
|
||||||
fail2banOutput, err := fail2banRequest(fail2banInput)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO use reflection to assert data structures and give proper errors
|
|
||||||
action := fail2banOutput.([]interface{})[1].([]interface{})[1].([]interface{})[1]
|
|
||||||
filter := fail2banOutput.([]interface{})[1].([]interface{})[0].([]interface{})[1]
|
|
||||||
|
|
||||||
output := make(map[string]map[string]interface{})
|
|
||||||
output["action"] = make(map[string]interface{})
|
|
||||||
output["filter"] = make(map[string]interface{})
|
|
||||||
|
|
||||||
output["filter"]["currentlyFailed"] = filter.([]interface{})[0].([]interface{})[1]
|
|
||||||
output["filter"]["totalFailed"] = filter.([]interface{})[1].([]interface{})[1]
|
|
||||||
output["filter"]["fileList"] = filter.([]interface{})[2].([]interface{})[1]
|
|
||||||
|
|
||||||
output["action"]["currentlyBanned"] = action.([]interface{})[0].([]interface{})[1]
|
|
||||||
output["action"]["totalBanned"] = action.([]interface{})[1].([]interface{})[1]
|
|
||||||
output["action"]["ipList"] = action.([]interface{})[2].([]interface{})[1]
|
|
||||||
|
|
||||||
encodedOutput, err := json.Marshal(output)
|
|
||||||
if err != nil {
|
|
||||||
}
|
|
||||||
|
|
||||||
res.Write(encodedOutput)
|
|
||||||
}
|
|
||||||
|
|
||||||
func JailControlHandler(basicRouter *mux.Router) {
|
|
||||||
basicRouter.HandleFunc("/status/{jail}", JailControlStatusHandler).Methods("GET")
|
|
||||||
}
|
|
Loading…
Reference in a new issue