forked from EOSIO/patroneos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
146 lines (122 loc) · 3.78 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)
// Config defines the application configuration
type Config struct {
ListenIP string `json:"listenIP"`
ListenPort string `json:"listenPort"`
NodeosProtocol string `json:"nodeosProtocol"`
NodeosURL string `json:"nodeosUrl"`
NodeosPort string `json:"nodeosPort"`
ContractBlackList map[string]bool `json:"contractBlackList"`
MaxSignatures int `json:"maxSignatures"`
MaxTransactionSize int `json:"maxTransactionSize"`
MaxTransactions int `json:"maxTransactions"`
LogEndpoints []string `json:"logEndpoints"`
FilterEndpoints []string `json:"filterEndpoints"`
LogFileLocation string `json:"logFileLocation"`
Headers map[string]string `json:"headers"`
}
var (
configFile string // path to config.json
operatingMode string // operating mode (filter or relay)
version string // application version
commit string // sha1 commit hash used to build application
buildDate string // compilation date
appConfig Config // configuration fields
)
// updateConfig allows the configuration to be updated via POST requests.
func updateConfig(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
responseBody, err := json.MarshalIndent(appConfig, "", " ")
if err != nil {
log.Printf("Failed to marshal config %s", err)
return
}
_, err = w.Write(responseBody)
if err != nil {
log.Printf("Error writing response body %s", err)
return
}
} else if r.Method == "POST" {
body, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(body, &appConfig)
if err != nil {
log.Printf("Error unmarshalling updated config %s", err)
return
}
err = ioutil.WriteFile(configFile, body, 0644)
if err != nil {
log.Printf("Error writing new configuration to file %s", err)
return
}
}
}
func parseArgs() {
const (
defaultConfigLocation = "./config.json"
defaultOperatingMode = "filter"
defaultShowHelp = false
defaultShowVersion = false
)
var (
showHelp bool
showVersion bool
)
flag.BoolVar(&showHelp, "h", defaultShowHelp, "shows application help")
flag.BoolVar(&showVersion, "v", defaultShowVersion, "show application version")
flag.StringVar(&configFile, "configFile", defaultConfigLocation, "location of the file used for application configuration")
flag.StringVar(&operatingMode, "mode", defaultOperatingMode, "mode in which the application will run")
flag.Parse()
if showHelp {
flag.Usage()
os.Exit(0)
}
if showVersion {
var buildDateTime string
date, err := time.Parse("2006-01-02T15:04:05Z-0700", buildDate)
if err != nil {
log.Printf("Error parsing build date: %v", err)
buildDateTime = ""
} else {
buildDateTime = date.In(time.Local).String()
}
fmt.Printf("Version: %v\nGit Commit: %v\nBuilt on: %v\n", version, commit, buildDateTime)
os.Exit(0)
}
}
func parseConfigFile() {
fileBody, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("Error reading configuration file.")
}
err = json.Unmarshal(fileBody, &appConfig)
if err != nil {
log.Fatalf("Error unmarshalling configuration file.")
}
}
func main() {
parseArgs()
parseConfigFile()
mux := http.NewServeMux()
mux.HandleFunc("/patroneos/config", updateConfig)
if operatingMode == "filter" {
addFilterHandlers(mux)
fmt.Println("Filtering node requests...")
} else if operatingMode == "fail2ban-relay" {
addLogHandlers(mux)
fmt.Println("Relaying log events to fail2ban...")
} else {
fmt.Printf("This mode is not supported.")
os.Exit(1)
}
log.Fatal(http.ListenAndServe(appConfig.ListenIP+":"+appConfig.ListenPort, mux))
}