forked from libp2p/go-libp2p-daemon
-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
187 lines (166 loc) · 3.55 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
package config
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/multiformats/go-multiaddr"
)
type JSONMaddr struct {
multiaddr.Multiaddr
}
func (jm *JSONMaddr) UnmarshalJSON(b []byte) error {
ma, err := multiaddr.NewMultiaddr(string(b))
if err != nil {
return err
}
*jm = JSONMaddr{ma}
return nil
}
type MaddrArray []multiaddr.Multiaddr
func (maa *MaddrArray) UnmarshalJSON(b []byte) error {
maStrings := strings.Split(string(b), ",")
*maa = make(MaddrArray, len(maStrings))
for i, s := range strings.Split(string(b), ",") {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
return err
}
(*maa)[i] = ma
}
return nil
}
type Bootstrap struct {
Enabled bool
Peers MaddrArray
}
type ConnectionManager struct {
Enabled bool
LowWaterMark int
HighWaterMark int
GracePeriod time.Duration
}
type GossipSubHeartbeat struct {
Interval time.Duration
InitialDelay time.Duration
}
type PubSub struct {
Enabled bool
Router string
Sign bool
SignStrict bool
GossipSubHeartbeat GossipSubHeartbeat
}
type Relay struct {
Enabled bool
Auto bool
Discovery bool
HopLimit int
}
type DHT struct {
Mode string
}
type PProf struct {
Enabled bool
Port uint
}
type Security struct {
Noise bool
TLS bool
}
const DHTFullMode = "full"
const DHTClientMode = "client"
const DHTServerMode = "server"
type Config struct {
ListenAddr JSONMaddr
Quiet bool
ID string
Bootstrap Bootstrap
DHT DHT
ConnectionManager ConnectionManager
NatPortMap bool
PubSub PubSub
Relay Relay
AutoNat bool
HostAddresses MaddrArray
AnnounceAddresses MaddrArray
NoListen bool
MetricsAddress string
PProf PProf
Security Security
}
func (c *Config) UnmarshalJSON(b []byte) error {
// settings defaults
type defaultConfig Config
ndc := defaultConfig(NewDefaultConfig())
dc := &ndc
if err := json.Unmarshal(b, dc); err != nil {
return err
}
*c = Config(*dc)
// validation
if err := c.Validate(); err != nil {
return err
}
return nil
}
func (c *Config) Validate() error {
if c.DHT.Mode != DHTClientMode && c.DHT.Mode != DHTFullMode && c.DHT.Mode != DHTServerMode && c.DHT.Mode != "" {
return fmt.Errorf("unknown DHT mode %s", c.DHT)
}
if c.Relay.Auto && (!c.Relay.Enabled || c.DHT.Mode == "") {
return fmt.Errorf("can't have autorelay enabled without Relay enabled and DHT enabled")
}
return nil
}
func NewDefaultConfig() Config {
defaultListen, _ := multiaddr.NewMultiaddr("/unix/tmp/p2pd.sock")
return Config{
ListenAddr: JSONMaddr{defaultListen},
Quiet: false,
ID: "",
Bootstrap: Bootstrap{
Enabled: false,
Peers: make(MaddrArray, 0),
},
DHT: DHT{
Mode: "",
},
ConnectionManager: ConnectionManager{
Enabled: false,
LowWaterMark: 256,
HighWaterMark: 512,
GracePeriod: 120 * time.Second,
},
NatPortMap: false,
PubSub: PubSub{
Enabled: false,
Router: "gossipsub",
Sign: true,
SignStrict: true,
GossipSubHeartbeat: GossipSubHeartbeat{
Interval: 0,
InitialDelay: 0,
},
},
Relay: Relay{
Enabled: true,
Auto: false,
Discovery: false,
HopLimit: 0,
},
AutoNat: false,
HostAddresses: make(MaddrArray, 0),
AnnounceAddresses: make(MaddrArray, 0),
NoListen: false,
MetricsAddress: "",
PProf: PProf{
Enabled: false,
Port: 0,
},
Security: Security{
Noise: true,
TLS: true,
},
}
}