forked from EOSIO/patroneos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fail2ban-relay.go
48 lines (39 loc) · 1.15 KB
/
fail2ban-relay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
)
// Log defines the fields needed for the Fail2Ban logs
type Log struct {
Host string `json:"host"`
Success bool `json:"success"`
Message string `json:"message"`
}
var logFile *os.File
var logger *log.Logger
// listenForLogs listens to the middleware for success/failure logs
// and logs them to the correct file for Fail2Ban
func listenForLogs(w http.ResponseWriter, r *http.Request) {
var logEntry Log
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &logEntry)
if err != nil {
log.Printf("Error unmarshalling logs %s", err)
return
}
// Print to file and stderr for now
logger.Printf("%s %t %s", logEntry.Host, logEntry.Success, logEntry.Message)
log.Printf("%s %t %s", logEntry.Host, logEntry.Success, logEntry.Message)
}
func addLogHandlers(mux *http.ServeMux) {
var err error
logFile, err = os.OpenFile(appConfig.LogFileLocation, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Error opening log file %s", err)
}
logger = log.New(logFile, "", log.LstdFlags)
mux.HandleFunc("/patroneos/fail2ban-relay", listenForLogs)
}