-
Notifications
You must be signed in to change notification settings - Fork 14
/
profile.go
552 lines (499 loc) · 15.8 KB
/
profile.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package config
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
)
// Transformer is a function which takes configuration and applies some filter to it
type Transformer func(c *Config) error
// Profile contains the profile transformer the description of the profile
type Profile struct {
// Description briefly describes the functionality of the profile.
Description string
// Transform takes ipfs configuration and applies the profile to it.
Transform Transformer
// InitOnly specifies that this profile can only be applied on init.
InitOnly bool
}
// defaultServerFilters has is a list of IPv4 and IPv6 prefixes that are private, local only, or unrouteable.
// according to https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
// and https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml
var defaultServerFilters = []string{
"/ip4/10.0.0.0/ipcidr/8",
"/ip4/100.64.0.0/ipcidr/10",
"/ip4/169.254.0.0/ipcidr/16",
"/ip4/172.16.0.0/ipcidr/12",
"/ip4/192.0.0.0/ipcidr/24",
"/ip4/192.0.0.0/ipcidr/29",
"/ip4/192.0.0.8/ipcidr/32",
"/ip4/192.0.0.170/ipcidr/32",
"/ip4/192.0.0.171/ipcidr/32",
"/ip4/192.0.2.0/ipcidr/24",
"/ip4/192.168.0.0/ipcidr/16",
"/ip4/198.18.0.0/ipcidr/15",
"/ip4/198.51.100.0/ipcidr/24",
"/ip4/203.0.113.0/ipcidr/24",
"/ip4/240.0.0.0/ipcidr/4",
"/ip6/100::/ipcidr/64",
"/ip6/2001:2::/ipcidr/48",
"/ip6/2001:db8::/ipcidr/32",
"/ip6/fc00::/ipcidr/7",
"/ip6/fe80::/ipcidr/10",
}
func ExternalIP() (string, error) {
return ExternalIPWithPort(DefaultSwarmPort, DefaultSwarmPort, nil)
}
func ExternalIPWithPort(extPort, intPort int, swarmAddrs []string) (string, error) {
// check internal port against swarm listening if being overriden
if swarmAddrs != nil {
valid := false
for _, sa := range swarmAddrs {
parts := strings.Split(sa, "/")
if len(parts) != 5 {
return "", fmt.Errorf("invalid swarm listening address: %s", sa)
}
// found a match of the internal port
if parts[4] == fmt.Sprintf("%d", intPort) {
valid = true
break
}
}
if !valid {
return "", fmt.Errorf("internal port not found in swarm listening addresses: %d", intPort)
}
}
resp, err := http.Get("http://checkip.amazonaws.com")
if err != nil {
return "", fmt.Errorf("get external IP failed: [%v]", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("parse external IP failed: [%v]", err)
}
ip := string(body)
// remove last return
ip = strings.TrimSpace(ip)
address := fmt.Sprintf("/ip4/%s/tcp/%d", ip, extPort)
return address, nil
}
// Profiles is a map holding configuration transformers. Docs are in docs/config.md
var Profiles = map[string]Profile{
"server": {
Description: `Disables local host discovery, recommended when
running IPFS on machines with public IPv4 addresses.`,
Transform: func(c *Config) error {
c.Addresses.NoAnnounce = appendSingle(c.Addresses.NoAnnounce, defaultServerFilters)
c.Swarm.AddrFilters = appendSingle(c.Swarm.AddrFilters, defaultServerFilters)
c.Discovery.MDNS.Enabled = false
c.Swarm.DisableNatPortMap = true
return nil
},
},
"local-discovery": {
Description: `Sets default values to fields affected by the server
profile, enables discovery in local networks.`,
Transform: func(c *Config) error {
c.Addresses.NoAnnounce = deleteEntries(c.Addresses.NoAnnounce, defaultServerFilters)
c.Swarm.AddrFilters = deleteEntries(c.Swarm.AddrFilters, defaultServerFilters)
c.Discovery.MDNS.Enabled = true
c.Swarm.DisableNatPortMap = false
return nil
},
},
"test": {
Description: `Reduces external interference of IPFS daemon, this
is useful when using the daemon in test environments.`,
Transform: func(c *Config) error {
c.Addresses.API = Strings{"/ip4/127.0.0.1/tcp/0"}
c.Addresses.Gateway = Strings{"/ip4/127.0.0.1/tcp/0"}
c.Addresses.Swarm = []string{
"/ip4/127.0.0.1/tcp/0",
}
c.Swarm.DisableNatPortMap = true
c.Bootstrap = []string{}
c.Discovery.MDNS.Enabled = false
return nil
},
},
"default-networking": {
Description: `Restores default network settings.
Inverse profile of the test profile.`,
Transform: func(c *Config) error {
c.Addresses = addressesConfig()
bootstrapPeers, err := DefaultBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = appendSingle(c.Bootstrap, BootstrapPeerStrings(bootstrapPeers))
c.Swarm.DisableNatPortMap = false
c.Discovery.MDNS.Enabled = true
return nil
},
},
"announce-public": {
Description: `Announce public IP when running on cloud VM or local network.`,
Transform: func(c *Config) error {
address, err := ExternalIP()
if err != nil {
return err
}
c.Addresses.Announce = appendSingle(c.Addresses.Announce, []string{address})
return nil
},
},
"default-datastore": {
Description: `Configures the node to use the default datastore (flatfs).
Read the "flatfs" profile description for more information on this datastore.
This profile may only be applied when first initializing the node.
`,
InitOnly: true,
Transform: func(c *Config) error {
c.Datastore.Spec = flatfsSpec()
return nil
},
},
"flatfs": {
Description: `Configures the node to use the flatfs datastore.
This is the most battle-tested and reliable datastore, but it's significantly
slower than the badger datastore. You should use this datastore if:
* You need a very simple and very reliable datastore you and trust your
filesystem. This datastore stores each block as a separate file in the
underlying filesystem so it's unlikely to loose data unless there's an issue
with the underlying file system.
* You need to run garbage collection on a small (<= 10GiB) datastore. The
default datastore, badger, can leave several gigabytes of data behind when
garbage collecting.
* You're concerned about memory usage. In its default configuration, badger can
use up to several gigabytes of memory.
This profile may only be applied when first initializing the node.
`,
InitOnly: true,
Transform: func(c *Config) error {
c.Datastore.Spec = flatfsSpec()
return nil
},
},
"badgerds": {
Description: `Configures the node to use the badger datastore.
This is the fastest datastore. Use this datastore if performance, especially
when adding many gigabytes of files, is critical. However:
* This datastore will not properly reclaim space when your datastore is
smaller than several gigabytes. If you run IPFS with '--enable-gc' (you have
enabled block-level garbage collection), you plan on storing very little data in
your IPFS node, and disk usage is more critical than performance, consider using
flatfs.
* This datastore uses up to several gigabytes of memory.
This profile may only be applied when first initializing the node.`,
InitOnly: true,
Transform: func(c *Config) error {
c.Datastore.Spec = badgerSpec()
return nil
},
},
"lowpower": {
Description: `Reduces daemon overhead on the system. May affect node
functionality - performance of content discovery and data
fetching may be degraded.
`,
Transform: func(c *Config) error {
c.Routing.Type = "dhtclient"
c.AutoNAT.ServiceMode = AutoNATServiceDisabled
c.Reprovider.Interval = "0"
c.Swarm.ConnMgr.LowWater = 20
c.Swarm.ConnMgr.HighWater = 40
c.Swarm.ConnMgr.GracePeriod = time.Minute.String()
return nil
},
},
"randomports": {
Description: `Use a random port number for swarm.`,
Transform: func(c *Config) error {
port, err := getAvailablePort()
if err != nil {
return err
}
c.Addresses.Swarm = []string{
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
fmt.Sprintf("/ip6/::/tcp/%d", port),
}
return nil
},
},
"storage-host": {
Description: `Configures necessary flags and options for node to become a storage host.`,
Transform: func(c *Config) error {
if err := transformDefaultStorageHost(c); err != nil {
return err
}
return nil
},
},
"storage-host-dev": {
Description: `[dev] Configures necessary flags and options for node to become a storage host.`,
Transform: func(c *Config) error {
if err := transformDevStorageHost(c); err != nil {
return err
}
c.Services = DefaultServicesConfigDev()
return nil
},
},
"storage-host-testnet": {
Description: `[testnet] Configures necessary flags and options for node to become a storage host.`,
Transform: func(c *Config) error {
if err := transformDevStorageHost(c); err != nil {
return err
}
c.Services = DefaultServicesConfigTestnet()
return nil
},
},
"storage-repairer": {
Description: `Configures necessary flags and options for node to become a storage repairer.`,
Transform: func(c *Config) error {
if err := transformDefaultStorageRepairer(c); err != nil {
return err
}
return nil
},
},
"storage-repairer-dev": {
Description: `[dev] Configures necessary flags and options for node to become a storage repairer.`,
Transform: func(c *Config) error {
if err := transformDevStorageRepairer(c); err != nil {
return err
}
c.Services = DefaultServicesConfigDev()
return nil
},
},
"storage-repairer-testnet": {
Description: `[testnet] Configures necessary flags and options for node to become a storage repairer.`,
Transform: func(c *Config) error {
if err := transformDevStorageRepairer(c); err != nil {
return err
}
c.Services = DefaultServicesConfigTestnet()
return nil
},
},
"storage-challenger": {
Description: `Configures necessary flags and options for node to become a storage challenger.`,
Transform: func(c *Config) error {
bootstrapPeers, err := DefaultBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.HostChallengeEnabled = true
c.Experimental.Analytics = true
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
c.Services = DefaultServicesConfig()
c.Swarm.SwarmKey = DefaultSwarmKey
return nil
},
},
"storage-challenger-dev": {
Description: `[dev] Configures necessary flags and options for node to become a storage challenger.`,
Transform: func(c *Config) error {
if err := transformDevStorageChallenger(c); err != nil {
return err
}
c.Services = DefaultServicesConfigDev()
return nil
},
},
"storage-challenger-testnet": {
Description: `[testnet] Configures necessary flags and options for node to become a storage challenger.`,
Transform: func(c *Config) error {
if err := transformDevStorageChallenger(c); err != nil {
return err
}
c.Services = DefaultServicesConfigTestnet()
return nil
},
},
"storage-client": {
Description: `Configures necessary flags and options for node to pay to store files on the network.`,
Transform: func(c *Config) error {
bootstrapPeers, err := DefaultBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.StorageClientEnabled = true
c.Experimental.HostsSyncEnabled = true
c.Experimental.HostsSyncMode = DefaultHostsSyncMode.String()
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
c.Services = DefaultServicesConfig()
c.Swarm.SwarmKey = DefaultSwarmKey
return nil
},
},
"storage-client-dev": {
Description: `[dev] Configures necessary flags and options for node to pay to store files on the network.`,
Transform: func(c *Config) error {
if err := transformDevStorageClient(c); err != nil {
return err
}
c.Services = DefaultServicesConfigDev()
return nil
},
},
"storage-client-testnet": {
Description: `[testnet] Configures necessary flags and options for node to pay to store files on the network.`,
Transform: func(c *Config) error {
if err := transformDevStorageClient(c); err != nil {
return err
}
c.Services = DefaultServicesConfigTestnet()
return nil
},
},
}
func transformDefaultStorageHost(c *Config) error {
bootstrapPeers, err := DefaultBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.StorageHostEnabled = true
c.Experimental.Analytics = true
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
if c.Datastore.StorageMax == "10GB" {
c.Datastore.StorageMax = "1TB"
}
c.Services = DefaultServicesConfig()
c.Swarm.SwarmKey = DefaultSwarmKey
return nil
}
func transformDefaultStorageRepairer(c *Config) error {
c.Experimental.HostRepairEnabled = true
c.Experimental.HostsSyncEnabled = true
c.Experimental.HostsSyncMode = DefaultHostsSyncMode.String()
err := transformDefaultStorageHost(c)
if err != nil {
return err
}
return nil
}
// transformDevStorageHost transforms common host settings among different dev environments
func transformDevStorageHost(c *Config) error {
bootstrapPeers, err := DefaultTestnetBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.StorageHostEnabled = true
c.Experimental.Analytics = true
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
if c.Datastore.StorageMax == "10GB" {
c.Datastore.StorageMax = "1TB"
}
c.Services = DefaultServicesConfigDev()
c.Swarm.SwarmKey = DefaultTestnetSwarmKey
return nil
}
// transformDevStorageRepairer transforms common repairer settings among different dev environments
func transformDevStorageRepairer(c *Config) error {
c.Experimental.HostRepairEnabled = true
c.Experimental.HostsSyncEnabled = true
c.Experimental.HostsSyncMode = DefaultHostsSyncModeDev.String()
err := transformDevStorageHost(c)
if err != nil {
return err
}
return nil
}
// transformDevStorageChallenger transforms common Challenger settings among different dev environments
func transformDevStorageChallenger(c *Config) error {
bootstrapPeers, err := DefaultTestnetBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.HostChallengeEnabled = true
c.Experimental.Analytics = true
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
c.Swarm.SwarmKey = DefaultTestnetSwarmKey
return nil
}
// transformDevStorageClient transforms common client settings among different dev environments
func transformDevStorageClient(c *Config) error {
bootstrapPeers, err := DefaultTestnetBootstrapPeers()
if err != nil {
return err
}
c.Bootstrap = BootstrapPeerStrings(bootstrapPeers)
c.Experimental.Libp2pStreamMounting = true
c.Experimental.StorageClientEnabled = true
c.Experimental.HostsSyncEnabled = true
c.Experimental.HostsSyncMode = DefaultHostsSyncModeDev.String()
if len(c.Addresses.RemoteAPI) == 0 {
c.Addresses.RemoteAPI = Strings{"/ip4/0.0.0.0/tcp/5101"}
}
c.Swarm.SwarmKey = DefaultTestnetSwarmKey
return nil
}
func getAvailablePort() (port int, err error) {
ln, err := net.Listen("tcp", "[::]:0")
if err != nil {
return 0, err
}
defer ln.Close()
port = ln.Addr().(*net.TCPAddr).Port
return port, nil
}
func appendSingle(a []string, b []string) []string {
out := make([]string, 0, len(a)+len(b))
m := map[string]bool{}
for _, f := range a {
if !m[f] {
out = append(out, f)
}
m[f] = true
}
for _, f := range b {
if !m[f] {
out = append(out, f)
}
m[f] = true
}
return out
}
func deleteEntries(arr []string, del []string) []string {
m := map[string]struct{}{}
for _, f := range arr {
m[f] = struct{}{}
}
for _, f := range del {
delete(m, f)
}
return mapKeys(m)
}
func mapKeys(m map[string]struct{}) []string {
out := make([]string, 0, len(m))
for f := range m {
out = append(out, f)
}
return out
}