This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.go
107 lines (94 loc) · 2.58 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
package relaydaemon
import (
"encoding/json"
"os"
"time"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
)
// Config stores the full configuration of the relays, ACLs and other settings
// that influence behaviour of a relay daemon.
type Config struct {
Network NetworkConfig
ConnMgr ConnMgrConfig
RelayV2 RelayV2Config
ACL ACLConfig
Daemon DaemonConfig
}
// DaemonConfig controls settings for the relay-daemon itself.
type DaemonConfig struct {
PprofPort int
}
// NetworkConfig controls listen and annouce settings for the libp2p host.
type NetworkConfig struct {
ListenAddrs []string
AnnounceAddrs []string
}
// ConnMgrConfig controls the libp2p connection manager settings.
type ConnMgrConfig struct {
ConnMgrLo int
ConnMgrHi int
ConnMgrGrace time.Duration
}
// RelayV2Config controls activation of V2 circuits and resouce configuration
// for them.
type RelayV2Config struct {
Enabled bool
Resources relayv2.Resources
}
// ACLConfig provides filtering configuration to allow specific peers or
// subnets to be fronted by relays. In V2, this specifies the peers/subnets
// that are able to make reservations on the relay. In V1, this specifies the
// peers/subnets that can be contacted through the relays.
type ACLConfig struct {
AllowPeers []string
AllowSubnets []string
}
// DefaultConfig returns a default relay configuration using default resource
// settings and no ACLs.
func DefaultConfig() Config {
return Config{
Network: NetworkConfig{
ListenAddrs: []string{
"/ip4/0.0.0.0/udp/4001/quic-v1",
"/ip6/::/udp/4001/quic-v1",
"/ip4/0.0.0.0/udp/4001/webrtc-direct",
"/ip6/::/udp/4001/webrtc-direct",
"/ip4/0.0.0.0/udp/4001/quic-v1/webtransport",
"/ip6/::/udp/4001/quic-v1/webtransport",
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
},
},
ConnMgr: ConnMgrConfig{
ConnMgrLo: 512,
ConnMgrHi: 768,
ConnMgrGrace: 2 * time.Minute,
},
RelayV2: RelayV2Config{
Enabled: true,
Resources: relayv2.DefaultResources(),
},
Daemon: DaemonConfig{
PprofPort: 6060,
},
}
}
// LoadConfig reads a relay daemon JSON configuration from the given path.
// The configuration is first initialized with DefaultConfig, so all unset
// fields will take defaults from there.
func LoadConfig(cfgPath string) (Config, error) {
cfg := DefaultConfig()
if cfgPath != "" {
cfgFile, err := os.Open(cfgPath)
if err != nil {
return Config{}, err
}
defer cfgFile.Close()
decoder := json.NewDecoder(cfgFile)
err = decoder.Decode(&cfg)
if err != nil {
return Config{}, err
}
}
return cfg, nil
}