Skip to content

Commit

Permalink
Merge pull request #21 from EOSIO/fix-config-issues
Browse files Browse the repository at this point in the history
Minor bug fixes.
  • Loading branch information
jeffreyssmith2nd authored May 30, 2018
2 parents 4593a57 + b27cf88 commit 32f22d6
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rpc-checkpoint
patroneos
main

# Binaries for programs and plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"maxSignatures": 10,
"maxTransactionSize": 1000000,

"logEndpoints": ["http://localhost:8080"],
"logEndpoints": [],
"filterEndpoints": ["http://localhost:8081"],

"logFileLocation": "./fail2ban.log"
Expand Down
18 changes: 18 additions & 0 deletions example-configs/advanced/filter-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"listenPort": "8081",

"nodeosProtocol": "http",
"nodeosUrl": "localhost",
"nodeosPort": "8888",

"contractBlackList": {
"currency": true
},
"maxSignatures": 10,
"maxTransactionSize": 1000000,

"logEndpoints": ["http://localhost:8080"],
"filterEndpoints": [],

"logFileLocation": "./fail2ban.log"
}
13 changes: 13 additions & 0 deletions example-configs/simple/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"listenPort": "8080",

"nodeosProtocol": "http",
"nodeosUrl": "localhost",
"nodeosPort": "8888",

"contractBlackList": {
"currency": true
},
"maxSignatures": 10,
"maxTransactionSize": 1000000
}
2 changes: 1 addition & 1 deletion fail2ban-relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ func addLogHandlers(mux *http.ServeMux) {
}

logger = log.New(logFile, "", log.LstdFlags)
mux.HandleFunc("/", listenForLogs)
mux.HandleFunc("/patroneos/fail2ban-relay", listenForLogs)
}
36 changes: 31 additions & 5 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strings"
)

// Middleware returns a handler that can perform various operations
Expand Down Expand Up @@ -58,6 +59,9 @@ func getHost(r *http.Request) string {
func logFailure(message string, w http.ResponseWriter, r *http.Request) {
remoteHost := getHost(r)
for _, logAgent := range appConfig.LogEndpoints {
if !strings.Contains(logAgent, "/patroneos/fail2ban-relay") {
logAgent += "/patroneos/fail2ban-relay"
}
logEvent := Log{
Host: remoteHost,
Success: false,
Expand All @@ -67,7 +71,7 @@ func logFailure(message string, w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("Error marshalling failure message %s", err)
}
client.Post(logAgent, "application/json", bytes.NewBuffer(body))
_, err = client.Post(logAgent, "application/json", bytes.NewBuffer(body))
if err != nil {
log.Print(err)
}
Expand All @@ -78,14 +82,20 @@ func logFailure(message string, w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-REJECTED-BY", "patroneos")
w.Header().Add("CONTENT-TYPE", "application/json")
w.WriteHeader(400)
w.Write(errorBody)
_, err := w.Write(errorBody)
if err != nil {
log.Printf("Error writing response body %s", err)
}
}
}

// logSuccess logs a success to the Fail2Ban server
func logSuccess(message string, r *http.Request) {
remoteHost := getHost(r)
for _, logAgent := range appConfig.LogEndpoints {
if !strings.Contains(logAgent, "/patroneos/fail2ban-relay") {
logAgent += "/patroneos/fail2ban-relay"
}
logEvent := Log{
Host: remoteHost,
Success: true,
Expand All @@ -95,7 +105,7 @@ func logSuccess(message string, r *http.Request) {
if err != nil {
log.Printf("Error marshalling success message %s", err)
}
client.Post(logAgent, "application/json", bytes.NewBuffer(body))
_, err = client.Post(logAgent, "application/json", bytes.NewBuffer(body))
if err != nil {
log.Print(err)
}
Expand Down Expand Up @@ -221,8 +231,6 @@ func copyHeaders(response http.Header, request http.Header) {
// If the request passes all middleware validations
// we forward it to the node to be processed.
func forwardCallToNodeos(w http.ResponseWriter, r *http.Request) {
log.Println("forward calls to nodeos")

nodeosHost := fmt.Sprintf("%s://%s:%s", appConfig.NodeosProtocol, appConfig.NodeosURL, appConfig.NodeosPort)
url := nodeosHost + r.URL.String()
method := r.Method
Expand All @@ -242,6 +250,8 @@ func forwardCallToNodeos(w http.ResponseWriter, r *http.Request) {
return
}

defer res.Body.Close()

body, _ = ioutil.ReadAll(res.Body)

if res.StatusCode == 200 {
Expand All @@ -259,6 +269,21 @@ func forwardCallToNodeos(w http.ResponseWriter, r *http.Request) {
}
}

func relay(w http.ResponseWriter, r *http.Request) {
message := "Patroneos cannot receive fail2ban relay requests when running in filter mode. Please check your config."
log.Printf("%s", message)

errorBody, _ := json.Marshal(ErrorMessage{Message: message, Code: 403})

w.WriteHeader(http.StatusForbidden)
_, err := w.Write(errorBody)

if err != nil {
log.Printf("Error writing response body %s", err)
return
}
}

func addFilterHandlers(mux *http.ServeMux) {
// Middleware are executed in the order that they are passed to chainMiddleware.
middlewareChain := chainMiddleware(
Expand All @@ -269,4 +294,5 @@ func addFilterHandlers(mux *http.ServeMux) {
)

mux.HandleFunc("/", middlewareChain(forwardCallToNodeos))
mux.HandleFunc("/patroneos/fail2ban-relay", relay)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {
parseConfigFile()

mux := http.NewServeMux()
mux.HandleFunc("/config", updateConfig)
mux.HandleFunc("/patroneos/config", updateConfig)

if operatingMode == "filter" {
addFilterHandlers(mux)
Expand Down

0 comments on commit 32f22d6

Please sign in to comment.