This repository was archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
262 lines (241 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"errors"
"fmt"
"io/ioutil"
"net"
"time"
"strconv"
"strings"
"hash/crc32"
hjson "github.com/hjson/hjson-go"
)
type Peer struct {
Name *string
Address *string
Interval *int
Timeout *int
ID *int64
ip net.IP
}
type Config struct {
Peers []Peer
Interval *int
Timeout *int
ListenAddress *string
DataBase *string
KeepHistoryFor time.Duration
}
func readInt(amap map[string]interface{}, name string) (*int, error) {
for key, value := range amap {
if strings.EqualFold(key, name) {
switch value.(type) {
case int:
ret := new(int)
*ret = value.(int)
return ret, nil
case int64:
ret := new(int)
*ret = int(value.(int64))
return ret, nil
case string:
var err error
ret := new(int)
*ret, err = strconv.Atoi(value.(string))
return ret, err
case float32:
ret := new(int)
*ret = int(value.(float32))
return ret, nil
case float64:
ret := new(int)
*ret = int(value.(float64))
return ret, nil
case bool:
ret := new(int)
if value.(bool) {
*ret = 1
} else {
*ret = 0
}
return ret, nil
}
return nil, errors.New("invalid format")
}
}
return nil, errors.New("not found")
}
func readString(amap map[string]interface{}, name string) (*string, error) {
for key, value := range amap {
if strings.EqualFold(key, name) {
switch value.(type) {
case int:
ret := new(string)
*ret = string(value.(int))
return ret, nil
case int64:
ret := new(string)
*ret = string(value.(int64))
return ret, nil
case string:
ret := new(string)
*ret = value.(string)
return ret, nil
case float32:
ret := new(string)
*ret = strconv.FormatFloat(float64(value.(float32)), 'E', -1, 32)
return ret, nil
case float64:
ret := new(string)
*ret = strconv.FormatFloat(value.(float64), 'E', -1, 64)
return ret, nil
case bool:
ret := new(string)
if value.(bool) {
*ret = "true"
} else {
*ret = "false"
}
return ret, nil
}
return nil, errors.New("invalid format")
}
}
return nil, errors.New("not found")
}
func readPeers(amap map[string]interface{}, name string) (peers []Peer, err error) {
for key, value := range amap {
if strings.EqualFold(key, name) {
switch value.(type) {
case string:
str := new(string)
*str = value.(string)
return []Peer{Peer{Address: str}}, nil
case []interface{}:
for _, value := range value.([]interface{}) {
switch value.(type) {
case string:
str := new(string)
*str = value.(string)
peers = append(peers, Peer{Address: str})
case map[string]interface{}:
var peer Peer
var id *int
id, _ = readInt(value.(map[string]interface{}), "ID")
if id != nil {
peer.ID = new(int64)
*peer.ID = int64(*id)
}
peer.Interval, _ = readInt(value.(map[string]interface{}), "Interval")
peer.Timeout, _ = readInt(value.(map[string]interface{}), "Timeout")
peer.Address, err = readString(value.(map[string]interface{}), "Address")
if err != nil {
if err.Error() == "invalid format" {
return peers, errors.New("'Address' has an invalid format")
} else if err.Error() == "not found" {
return peers, errors.New("'Address' was not found")
}
}
if peer.Address == nil || len(*peer.Address) <= 0 {
return peers, errors.New("'Address' is invalid")
}
peer.Name, _ = readString(value.(map[string]interface{}), "Name")
peers = append(peers, peer)
}
}
}
return peers, nil
}
}
return peers, nil
}
func ReadConfig(configFile string) (config Config, err error) {
var bytes []byte
bytes, err = ioutil.ReadFile(configFile)
if err != nil {
return config, err
}
var dat map[string]interface{}
err = hjson.Unmarshal(bytes, &dat)
if err != nil {
return config, err
}
config.Interval, _ = readInt(dat, "Interval")
if config.Interval == nil {
config.Interval = new(int)
*config.Interval = 1000
} else if *config.Interval < 10 {
*config.Interval = 10
}
config.Timeout, _ = readInt(dat, "Timeout")
if config.Timeout == nil {
config.Timeout = new(int)
*config.Timeout = 1000
} else if *config.Timeout < 10 {
*config.Timeout = 10
}
config.DataBase, _ = readString(dat, "DataBase")
if config.DataBase == nil {
config.DataBase = new(string)
*config.DataBase = "data.db"
} else if len(*config.DataBase) <= 0 {
*config.DataBase = "data.db"
}
var str *string
str, _ = readString(dat, "KeepHistoryFor")
if str == nil {
config.KeepHistoryFor, _ = time.ParseDuration("672h")
} else {
config.KeepHistoryFor, err = time.ParseDuration("672h")
if err != nil {
return config, err
}
}
config.Peers, err = readPeers(dat, "Peers")
if err != nil {
return config, err
}
crc32q := crc32.MakeTable(0xD5828281)
for i := range config.Peers {
if config.Peers[i].Interval == nil {
config.Peers[i].Interval = config.Interval
} else if *config.Peers[i].Interval < 10 {
*config.Peers[i].Interval = 10
}
if config.Peers[i].Timeout == nil {
config.Peers[i].Timeout = config.Timeout
} else if *config.Peers[i].Timeout < 10 {
*config.Peers[i].Timeout = 10
}
config.Peers[i].ip = net.ParseIP(*config.Peers[i].Address)
if config.Peers[i].ip == nil {
return config, fmt.Errorf("'%s' is not a valid IP address\n", *config.Peers[i].Address)
}
if config.Peers[i].Name == nil {
config.Peers[i].Name = config.Peers[i].Address
}
if config.Peers[i].ID == nil {
config.Peers[i].ID = new(int64)
*config.Peers[i].ID = int64(crc32.Checksum([]byte(*config.Peers[i].Address), crc32q))
}
}
// search for double IDS
for i, peer1 := range config.Peers {
for j, peer2 := range config.Peers {
if i != j && *peer1.ID == *peer2.ID {
return config, fmt.Errorf("The peers %s and %s got the same IDs", *peer1.Address, *peer2.Address)
}
}
}
config.ListenAddress, err = readString(dat, "ListenAddress")
if err != nil {
if err.Error() == "invalid format" {
return config, errors.New("'ListenAddress' has an invalid format")
}
}
if config.ListenAddress == nil {
config.ListenAddress = new(string)
*config.ListenAddress = ":8000"
}
return config, err
}