forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
227 lines (200 loc) · 6.45 KB
/
config.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"strconv"
"time"
)
const (
CONFIG_FILE = "config.json"
)
var (
ErrExchangeNameEmpty = "Exchange #%d in config: Exchange name is empty."
ErrExchangeAvailablePairsEmpty = "Exchange %s: Available pairs is empty."
ErrExchangeEnabledPairsEmpty = "Exchange %s: Enabled pairs is empty."
ErrExchangeBaseCurrenciesEmpty = "Exchange %s: Base currencies is empty."
WarningExchangeAuthAPIDefaultOrEmptyValues = "WARNING -- Exchange %s: Authenticated API support disabled due to default/empty APIKey/Secret/ClientID values."
ErrExchangeNotFound = "Exchange %s: Not found."
ErrNoEnabledExchanges = "No Exchanges enabled."
ErrCryptocurrenciesEmpty = "Cryptocurrencies variable is empty."
WarningSMSGlobalDefaultOrEmptyValues = "WARNING -- SMS Support disabled due to default or empty Username/Password values."
WarningSSMSGlobalSMSContactDefaultOrEmptyValues = "WARNING -- SMS contact #%d Name/Number disabled due to default or empty values."
WarningSSMSGlobalSMSNoContacts = "WARNING -- SMS Support disabled due to no enabled contacts."
WarningWebserverCredentialValuesEmpty = "WARNING -- Webserver support disabled due to empty Username/Password values."
WarningWebserverListenAddressInvalid = "WARNING -- Webserver support disabled due to invalid listen address."
WarningWebserverRootWebFolderNotFound = "WARNING -- Webserver support disabled due to missing web folder."
)
type Webserver struct {
Enabled bool
AdminUsername string
AdminPassword string
ListenAddress string
}
type SMSGlobal struct {
Enabled bool
Username string
Password string
Contacts []struct {
Name string
Number string
Enabled bool
}
}
type ConfigPost struct {
Data Config `json:"Data"`
}
type Config struct {
Name string
Cryptocurrencies string
SMS SMSGlobal `json:"SMSGlobal"`
Webserver Webserver `json:"Webserver"`
Exchanges []Exchanges
}
type Exchanges struct {
Name string
Enabled bool
Verbose bool
Websocket bool
RESTPollingDelay time.Duration
AuthenticatedAPISupport bool
APIKey string
APISecret string
ClientID string
AvailablePairs string
EnabledPairs string
BaseCurrencies string
}
func GetEnabledExchanges() int {
counter := 0
for i := range bot.config.Exchanges {
if bot.config.Exchanges[i].Enabled {
counter++
}
}
return counter
}
func GetExchangeConfig(name string) (Exchanges, error) {
for i, _ := range bot.config.Exchanges {
if bot.config.Exchanges[i].Name == name {
return bot.config.Exchanges[i], nil
}
}
return Exchanges{}, fmt.Errorf(ErrExchangeNotFound, name)
}
func UpdateExchangeConfig(e Exchanges) error {
for i, _ := range bot.config.Exchanges {
if bot.config.Exchanges[i].Name == e.Name {
bot.config.Exchanges[i] = e
return nil
}
}
return fmt.Errorf(ErrExchangeNotFound, e.Name)
}
func CheckSMSGlobalConfigValues() error {
if bot.config.SMS.Username == "" || bot.config.SMS.Username == "Username" || bot.config.SMS.Password == "" || bot.config.SMS.Password == "Password" {
return errors.New(WarningSMSGlobalDefaultOrEmptyValues)
}
contacts := 0
for i := range bot.config.SMS.Contacts {
if bot.config.SMS.Contacts[i].Enabled {
if bot.config.SMS.Contacts[i].Name == "" || bot.config.SMS.Contacts[i].Number == "" || (bot.config.SMS.Contacts[i].Name == "Bob" && bot.config.SMS.Contacts[i].Number == "12345") {
log.Printf(WarningSSMSGlobalSMSContactDefaultOrEmptyValues, i)
continue
}
contacts++
}
}
if contacts == 0 {
return errors.New(WarningSSMSGlobalSMSNoContacts)
}
return nil
}
func CheckExchangeConfigValues() error {
if bot.config.Cryptocurrencies == "" {
return errors.New(ErrCryptocurrenciesEmpty)
}
exchanges := 0
for i, exch := range bot.config.Exchanges {
if exch.Enabled {
if exch.Name == "" {
return fmt.Errorf(ErrExchangeNameEmpty, i)
}
if exch.AvailablePairs == "" {
return fmt.Errorf(ErrExchangeAvailablePairsEmpty, exch.Name)
}
if exch.EnabledPairs == "" {
return fmt.Errorf(ErrExchangeEnabledPairsEmpty, exch.Name)
}
if exch.BaseCurrencies == "" {
return fmt.Errorf(ErrExchangeBaseCurrenciesEmpty, exch.Name)
}
if exch.AuthenticatedAPISupport { // non-fatal error
if exch.APIKey == "" || exch.APISecret == "" || exch.APIKey == "Key" || exch.APISecret == "Secret" {
bot.config.Exchanges[i].AuthenticatedAPISupport = false
log.Printf(WarningExchangeAuthAPIDefaultOrEmptyValues, exch.Name)
continue
} else if exch.Name == "ITBIT" || exch.Name == "Bitstamp" || exch.Name == "Coinbase" {
if exch.ClientID == "" || exch.ClientID == "ClientID" {
bot.config.Exchanges[i].AuthenticatedAPISupport = false
log.Printf(WarningExchangeAuthAPIDefaultOrEmptyValues, exch.Name)
continue
}
}
}
exchanges++
}
}
if exchanges == 0 {
return errors.New(ErrNoEnabledExchanges)
}
return nil
}
func CheckWebserverValues() error {
if bot.config.Webserver.AdminUsername == "" || bot.config.Webserver.AdminPassword == "" {
return errors.New(WarningWebserverCredentialValuesEmpty)
}
if !StringContains(bot.config.Webserver.ListenAddress, ":") {
return errors.New(WarningWebserverListenAddressInvalid)
}
portStr := SplitStrings(bot.config.Webserver.ListenAddress, ":")[1]
port, err := strconv.Atoi(portStr)
if err != nil {
return errors.New(WarningWebserverListenAddressInvalid)
}
if port < 1 || port > 65355 {
return errors.New(WarningWebserverListenAddressInvalid)
}
return nil
}
func ReadConfig() (Config, error) {
file, err := ioutil.ReadFile(CONFIG_FILE)
if err != nil {
return Config{}, err
}
cfg := Config{}
err = json.Unmarshal(file, &cfg)
return cfg, err
}
func SaveConfig() error {
log.Println("Saving config")
payload, err := json.MarshalIndent(bot.config, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(CONFIG_FILE, payload, 0644)
if err != nil {
return err
}
retrieved, err := ioutil.ReadFile(CONFIG_FILE)
if err != nil {
return err
}
if !bytes.Equal(retrieved, payload) {
return fmt.Errorf("file %q content doesn't match, read %s expected %s\n", CONFIG_FILE, retrieved, payload)
}
return nil
}