forked from ipfs/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoconf.go
More file actions
152 lines (131 loc) · 4.75 KB
/
autoconf.go
File metadata and controls
152 lines (131 loc) · 4.75 KB
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
package main
import (
"fmt"
"path/filepath"
"slices"
"strings"
"time"
autoconf "github.com/ipfs/boxo/autoconf"
)
// AutoConfConfig contains the configuration for the autoconf subsystem
type AutoConfConfig struct {
// Enabled determines whether to use autoconf
// Default: true
Enabled bool
// URL is the HTTP(S) URL to fetch the autoconf.json from
// Default: https://conf.ipfs-mainnet.org/autoconf.json
URL string
// RefreshInterval is how often to refresh autoconf data
// Default: 24h
RefreshInterval time.Duration
// CacheDir is the directory to cache autoconf data
// Default: $RAINBOW_DATADIR/.autoconf-cache
CacheDir string
}
// getNativeSystems returns the list of systems that should be used natively based on routing type
func getNativeSystems(routingType string) []string {
switch routingType {
case "dht", "accelerated", "standard", "auto":
return []string{autoconf.SystemAminoDHT}
case "off", "none", "custom":
return []string{}
default:
goLog.Warnf("getNativeSystems: unknown routing type %q, assuming no native systems", routingType)
return []string{}
}
}
// getRoutingType converts DHTRouting enum to string routing type for autoconf
func getRoutingType(dhtRouting DHTRouting) string {
switch dhtRouting {
case DHTOff:
return "off"
case DHTStandard:
return "standard"
case DHTAccelerated:
return "accelerated"
default:
// Any other value (including "custom") is treated as auto
return "auto"
}
}
// autoconfDisabledError returns a consistent error message when auto placeholder is found but autoconf is disabled
func autoconfDisabledError(configType, envVar, flag string) error {
return fmt.Errorf("'auto' placeholder found in %s but autoconf is disabled. Set explicit %s with %s or %s, or re-enable autoconf",
configType, configType, envVar, flag)
}
// expandAutoBootstrap expands "auto" placeholders in bootstrap peers
func expandAutoBootstrap(bootstrapStr string, cfg Config, autoConfData *autoconf.Config) ([]string, error) {
if bootstrapStr == "" {
return []string{}, nil
}
bootstrapList := strings.Split(bootstrapStr, ",")
for i, s := range bootstrapList {
bootstrapList[i] = strings.TrimSpace(s)
}
if !cfg.AutoConf.Enabled {
if slices.Contains(bootstrapList, autoconf.AutoPlaceholder) {
return nil, autoconfDisabledError("bootstrap peers", "RAINBOW_BOOTSTRAP", "--bootstrap")
}
return bootstrapList, nil
}
routingType := getRoutingType(cfg.DHTRouting)
nativeSystems := getNativeSystems(routingType)
return autoconf.ExpandBootstrapPeers(bootstrapList, autoConfData, nativeSystems), nil
}
// expandAutoDNSResolvers expands "auto" placeholders in DNS resolvers
func expandAutoDNSResolvers(resolversList []string, cfg Config, autoConfData *autoconf.Config) (map[string]string, error) {
resolversMap := make(map[string]string, len(resolversList))
for _, resolver := range resolversList {
parts := strings.SplitN(resolver, ":", 2)
if len(parts) == 2 {
domain := strings.TrimSpace(parts[0])
url := strings.TrimSpace(parts[1])
resolversMap[domain] = url
}
}
if !cfg.AutoConf.Enabled {
for _, url := range resolversMap {
if url == autoconf.AutoPlaceholder {
return nil, autoconfDisabledError("DNS resolvers", "RAINBOW_DNSLINK_RESOLVERS", "--dnslink-resolvers")
}
}
return resolversMap, nil
}
return autoconf.ExpandDNSResolvers(resolversMap, autoConfData), nil
}
// expandAutoHTTPRouters expands "auto" placeholders in HTTP routers
func expandAutoHTTPRouters(routers []string, cfg Config, autoConfData *autoconf.Config) ([]string, error) {
if !cfg.AutoConf.Enabled {
if slices.Contains(routers, autoconf.AutoPlaceholder) {
return nil, autoconfDisabledError("HTTP routers", "RAINBOW_HTTP_ROUTERS", "--http-routers")
}
return routers, nil
}
routingType := getRoutingType(cfg.DHTRouting)
nativeSystems := getNativeSystems(routingType)
// Rainbow only uses read-only endpoints for providers, peers, and IPNS
return autoconf.ExpandDelegatedEndpoints(routers, autoConfData, nativeSystems,
autoconf.RoutingV1ProvidersPath,
autoconf.RoutingV1PeersPath,
autoconf.RoutingV1IPNSPath), nil
}
// createAutoConfClient creates an autoconf client with the given configuration
func createAutoConfClient(config AutoConfConfig) (*autoconf.Client, error) {
if config.CacheDir == "" {
config.CacheDir = filepath.Join(".", ".autoconf-cache")
}
if config.RefreshInterval == 0 {
config.RefreshInterval = autoconf.DefaultRefreshInterval
}
if config.URL == "" {
config.URL = autoconf.MainnetAutoConfURL
}
return autoconf.NewClient(
autoconf.WithCacheDir(config.CacheDir),
autoconf.WithUserAgent("rainbow/"+version),
autoconf.WithCacheSize(autoconf.DefaultCacheSize),
autoconf.WithTimeout(autoconf.DefaultTimeout),
autoconf.WithURL(config.URL),
autoconf.WithRefreshInterval(config.RefreshInterval),
)
}