forked from OpenBazaar/openbazaar-go
/
config.go
264 lines (229 loc) · 6.66 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
263
264
package repo
import (
"encoding/json"
"github.com/ipfs/go-ipfs/repo"
"github.com/ipfs/go-ipfs/repo/config"
"io/ioutil"
"path"
)
var DefaultBootstrapAddresses = []string{
"/ip4/107.170.133.32/tcp/4001/ipfs/QmbY4yo9Eifg7DPjL7qK5JvNdiJaRAD7N76gVg4YoQsvgA", // Le Marché Serpette
"/ip4/139.59.174.197/tcp/4001/ipfs/QmcCoBtYyduyurcLHRF14QhhA88YojJJpGFuMHoMZuU8sc", // Brixton-Village
"/ip4/139.59.6.222/tcp/4001/ipfs/QmdzzGGc9xZq8w4z42vSHe32DZM7VXfDUFEUyfPvYNYhXE", // Johari
"/ip4/46.101.198.170/tcp/4001/ipfs/QmePWxsFT9wY3QuukgVDB7XZpqdKhrqJTHTXU7ECLDWJqX", // Duo Search
}
type APIConfig struct {
Authenticated bool
AllowedIPs []string
Username string
Password string
CORS *string
Enabled bool
HTTPHeaders map[string][]string
SSL bool
SSLCert string
SSLKey string
}
type TorConfig struct {
Password string
TorControl string
}
type WalletConfig struct {
Type string
Binary string
MaxFee int
FeeAPI string
HighFeeDefault int
MediumFeeDefault int
LowFeeDefault int
TrustedPeer string
RPCUser string
RPCPassword string
}
func GetAPIConfig(cfgPath string) (*APIConfig, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
api := cfg.(map[string]interface{})["JSON-API"]
headers := make(map[string][]string)
h := api.(map[string]interface{})["HTTPHeaders"]
if h == nil {
headers = nil
} else {
headers = h.(map[string][]string)
}
enabled := api.(map[string]interface{})["Enabled"].(bool)
authenticated := api.(map[string]interface{})["Authenticated"].(bool)
allowedIPs := api.(map[string]interface{})["AllowedIPs"].([]interface{})
var allowedIPstrings []string
for _, ip := range allowedIPs {
allowedIPstrings = append(allowedIPstrings, ip.(string))
}
username := api.(map[string]interface{})["Username"].(string)
password := api.(map[string]interface{})["Password"].(string)
c := api.(map[string]interface{})["CORS"]
var cors *string
if c == nil {
cors = nil
} else {
crs := c.(string)
cors = &crs
}
sslEnabled := api.(map[string]interface{})["SSL"].(bool)
certFile := api.(map[string]interface{})["SSLCert"].(string)
keyFile := api.(map[string]interface{})["SSLKey"].(string)
apiConfig := &APIConfig{
Authenticated: authenticated,
AllowedIPs: allowedIPstrings,
Username: username,
Password: password,
CORS: cors,
Enabled: enabled,
HTTPHeaders: headers,
SSL: sslEnabled,
SSLCert: certFile,
SSLKey: keyFile,
}
return apiConfig, nil
}
func GetWalletConfig(cfgPath string) (*WalletConfig, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
wallet := cfg.(map[string]interface{})["Wallet"]
feeAPI := wallet.(map[string]interface{})["FeeAPI"].(string)
trustedPeer := wallet.(map[string]interface{})["TrustedPeer"].(string)
low := wallet.(map[string]interface{})["LowFeeDefault"].(float64)
medium := wallet.(map[string]interface{})["MediumFeeDefault"].(float64)
high := wallet.(map[string]interface{})["HighFeeDefault"].(float64)
maxFee := wallet.(map[string]interface{})["MaxFee"].(float64)
walletType := wallet.(map[string]interface{})["Type"].(string)
binary := wallet.(map[string]interface{})["Binary"].(string)
rpcUser := wallet.(map[string]interface{})["RPCUser"].(string)
rpcPassword := wallet.(map[string]interface{})["RPCPassword"].(string)
wCfg := &WalletConfig{
Type: walletType,
Binary: binary,
MaxFee: int(maxFee),
FeeAPI: feeAPI,
HighFeeDefault: int(high),
MediumFeeDefault: int(medium),
LowFeeDefault: int(low),
TrustedPeer: trustedPeer,
RPCUser: rpcUser,
RPCPassword: rpcPassword,
}
return wCfg, nil
}
func GetTorConfig(cfgPath string) (TorConfig, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return TorConfig{}, err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
tc := cfg.(map[string]interface{})["Tor-config"]
pw := tc.(map[string]interface{})["Password"].(string)
controlUrl := tc.(map[string]interface{})["TorControl"].(string)
return TorConfig{TorControl: controlUrl, Password: pw}, nil
}
func GetDropboxApiToken(cfgPath string) (string, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return "", err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
token := cfg.(map[string]interface{})["Dropbox-api-token"].(string)
return token, nil
}
func GetCrosspostGateway(cfgPath string) ([]string, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
gwys := cfg.(map[string]interface{})["Crosspost-gateways"].([]interface{})
var urls []string
for _, gw := range gwys {
urls = append(urls, gw.(string))
}
return urls, nil
}
func GetResolverUrl(cfgPath string) (string, error) {
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
return "", err
}
var cfg interface{}
json.Unmarshal(file, &cfg)
r := cfg.(map[string]interface{})["Resolver"].(string)
return r, nil
}
func extendConfigFile(r repo.Repo, key string, value interface{}) error {
if err := r.SetConfigKey(key, value); err != nil {
return err
}
return nil
}
func InitConfig(repoRoot string) (*config.Config, error) {
bootstrapPeers, err := config.ParseBootstrapPeers(DefaultBootstrapAddresses)
if err != nil {
return nil, err
}
datastore := datastoreConfig(repoRoot)
conf := &config.Config{
// Setup the node's default addresses.
// NOTE: two swarm listen addrs, one TCP, one UTP.
Addresses: config.Addresses{
Swarm: []string{
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
"/ip4/0.0.0.0/tcp/9005/ws",
"/ip6/::/tcp/9005/ws",
},
API: "",
Gateway: "/ip4/127.0.0.1/tcp/4002",
},
Datastore: datastore,
Bootstrap: config.BootstrapPeerStrings(bootstrapPeers),
Discovery: config.Discovery{config.MDNS{
Enabled: false,
Interval: 10,
}},
// Setup the node mount points
Mounts: config.Mounts{
IPFS: "/ipfs",
IPNS: "/ipns",
},
Ipns: config.Ipns{
ResolveCacheSize: 128,
RecordLifetime: "7d",
RepublishPeriod: "24h",
QuerySize: 5,
},
Gateway: config.Gateway{
RootRedirect: "",
Writable: false,
PathPrefixes: []string{},
},
}
return conf, nil
}
func datastoreConfig(repoRoot string) config.Datastore {
dspath := path.Join(repoRoot, "datastore")
return config.Datastore{
Path: dspath,
Type: "leveldb",
StorageMax: "10GB",
StorageGCWatermark: 90, // 90%
GCPeriod: "1h",
}
}