-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.go
393 lines (342 loc) · 10.4 KB
/
main.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
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/libp2p/go-libp2p"
relay "github.com/libp2p/go-libp2p-circuit"
connmgr "github.com/libp2p/go-libp2p-connmgr"
p2pd "github.com/libp2p/go-libp2p-daemon"
config "github.com/libp2p/go-libp2p-daemon/config"
ps "github.com/libp2p/go-libp2p-pubsub"
quic "github.com/libp2p/go-libp2p-quic-transport"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
multiaddr "github.com/multiformats/go-multiaddr"
promhttp "github.com/prometheus/client_golang/prometheus/promhttp"
_ "net/http/pprof"
)
func pprofHTTP(port int) {
listen := func(p int) error {
addr := fmt.Sprintf("localhost:%d", p)
log.Printf("registering pprof debug http handler at: http://%s/debug/pprof/\n", addr)
switch err := http.ListenAndServe(addr, nil); err {
case nil:
// all good, server is running and exited normally.
return nil
case http.ErrServerClosed:
// all good, server was shut down.
return nil
default:
// error, try another port
log.Printf("error registering pprof debug http handler at: %s: %s\n", addr, err)
return err
}
}
if port > 0 {
// we have a user-assigned port.
_ = listen(port)
return
}
// we don't have a user assigned port, try sequentially to bind between [6060-7080]
for i := 6060; i <= 7080; i++ {
if listen(i) == nil {
return
}
}
}
func main() {
identify.ClientVersion = "p2pd/0.1"
maddrString := flag.String("listen", "/unix/tmp/p2pd.sock", "daemon control listen multiaddr")
quiet := flag.Bool("q", false, "be quiet")
id := flag.String("id", "", "peer identity; private key file")
bootstrap := flag.Bool("b", false, "connects to bootstrap peers and bootstraps the dht if enabled")
bootstrapPeers := flag.String("bootstrapPeers", "", "comma separated list of bootstrap peers; defaults to the IPFS DHT peers")
dht := flag.Bool("dht", false, "Enables the DHT in full node mode")
dhtClient := flag.Bool("dhtClient", false, "Enables the DHT in client mode")
connMgr := flag.Bool("connManager", false, "Enables the Connection Manager")
connMgrLo := flag.Int("connLo", 256, "Connection Manager Low Water mark")
connMgrHi := flag.Int("connHi", 512, "Connection Manager High Water mark")
connMgrGrace := flag.Duration("connGrace", 120, "Connection Manager grace period (in seconds)")
QUIC := flag.Bool("quic", false, "Enables the QUIC transport")
natPortMap := flag.Bool("natPortMap", false, "Enables NAT port mapping")
pubsub := flag.Bool("pubsub", false, "Enables pubsub")
pubsubRouter := flag.String("pubsubRouter", "gossipsub", "Specifies the pubsub router implementation")
pubsubSign := flag.Bool("pubsubSign", true, "Enables pubsub message signing")
pubsubSignStrict := flag.Bool("pubsubSignStrict", true, "Enables or disables pubsub strict signature verification")
gossipsubHeartbeatInterval := flag.Duration("gossipsubHeartbeatInterval", 0, "Specifies the gossipsub heartbeat interval")
gossipsubHeartbeatInitialDelay := flag.Duration("gossipsubHeartbeatInitialDelay", 0, "Specifies the gossipsub initial heartbeat delay")
relayEnabled := flag.Bool("relay", true, "Enables circuit relay")
relayActive := flag.Bool("relayActive", false, "Enables active mode for relay")
relayHop := flag.Bool("relayHop", false, "Enables hop for relay")
relayDiscovery := flag.Bool("relayDiscovery", false, "Enables passive discovery for relay")
autoRelay := flag.Bool("autoRelay", false, "Enables autorelay")
autonat := flag.Bool("autonat", false, "Enables the AutoNAT service")
hostAddrs := flag.String("hostAddrs", "", "comma separated list of multiaddrs the host should listen on")
announceAddrs := flag.String("announceAddrs", "", "comma separated list of multiaddrs the host should announce to the network")
noListen := flag.Bool("noListenAddrs", false, "sets the host to listen on no addresses")
metricsAddr := flag.String("metricsAddr", "", "an address to bind the metrics handler to")
configFilename := flag.String("f", "", "a file from which to read a json representation of the deamon config")
configStdin := flag.Bool("i", false, "have the daemon read the json config from stdin")
pprof := flag.Bool("pprof", false, "Enables the HTTP pprof handler, listening on the first port "+
"available in the range [6060-7800], or on the user-provided port via -pprofPort")
pprofPort := flag.Uint("pprofPort", 0, "Binds the HTTP pprof handler to a specific port; "+
"has no effect unless the pprof option is enabled")
flag.Parse()
var c config.Config
var opts []libp2p.Option
if *configStdin {
stdin := bufio.NewReader(os.Stdin)
body, err := ioutil.ReadAll(stdin)
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(body, &c); err != nil {
log.Fatal(err)
}
} else if *configFilename != "" {
body, err := ioutil.ReadFile(*configFilename)
if err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(body, &c); err != nil {
log.Fatal(err)
}
} else {
c = config.NewDefaultConfig()
}
maddr, err := multiaddr.NewMultiaddr(*maddrString)
if err != nil {
log.Fatal(err)
}
c.ListenAddr = config.JSONMaddr{maddr}
if *id != "" {
c.ID = *id
}
if *hostAddrs != "" {
addrStrings := strings.Split(*hostAddrs, ",")
ha := make([]multiaddr.Multiaddr, len(addrStrings))
for i, s := range addrStrings {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
log.Fatal(err)
}
(ha)[i] = ma
}
c.HostAddresses = ha
}
if *announceAddrs != "" {
addrStrings := strings.Split(*announceAddrs, ",")
ha := make([]multiaddr.Multiaddr, len(addrStrings))
for i, s := range addrStrings {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
log.Fatal(err)
}
(ha)[i] = ma
}
c.AnnounceAddresses = ha
}
if *connMgr {
c.ConnectionManager.Enabled = true
c.ConnectionManager.GracePeriod = *connMgrGrace
c.ConnectionManager.HighWaterMark = *connMgrHi
c.ConnectionManager.LowWaterMark = *connMgrLo
}
if *QUIC {
c.QUIC = true
}
if *natPortMap {
c.NatPortMap = true
}
if *relayEnabled {
c.Relay.Enabled = true
if *relayActive {
c.Relay.Active = true
}
if *relayHop {
c.Relay.Hop = true
}
if *relayDiscovery {
c.Relay.Discovery = true
}
}
if *autoRelay {
c.Relay.Auto = true
}
if *noListen {
c.NoListen = true
}
if *autonat {
c.AutoNat = true
}
if *pubsub {
c.PubSub.Enabled = true
c.PubSub.Router = *pubsubRouter
c.PubSub.Sign = *pubsubSign
c.PubSub.SignStrict = *pubsubSignStrict
if *gossipsubHeartbeatInterval > 0 {
c.PubSub.GossipSubHeartbeat.Interval = *gossipsubHeartbeatInterval
}
if *gossipsubHeartbeatInitialDelay > 0 {
c.PubSub.GossipSubHeartbeat.InitialDelay = *gossipsubHeartbeatInitialDelay
}
}
if *bootstrapPeers != "" {
addrStrings := strings.Split(*bootstrapPeers, ",")
bps := make([]multiaddr.Multiaddr, len(addrStrings))
for i, s := range addrStrings {
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
log.Fatal(err)
}
(bps)[i] = ma
}
c.Bootstrap.Peers = bps
}
if *bootstrap {
c.Bootstrap.Enabled = true
}
if *quiet {
c.Quiet = true
}
if *metricsAddr != "" {
c.MetricsAddress = *metricsAddr
}
if *dht {
c.DHT.Mode = config.DHTFullMode
} else if *dhtClient {
c.DHT.Mode = config.DHTClientMode
}
if *pprof {
c.PProf.Enabled = true
if pprofPort != nil {
c.PProf.Port = *pprofPort
}
}
if err := c.Validate(); err != nil {
log.Fatal(err)
}
if c.PProf.Enabled {
// an invalid port number will fail within the function.
go pprofHTTP(int(c.PProf.Port))
}
// collect opts
if c.ID != "" {
key, err := p2pd.ReadIdentity(c.ID)
if err != nil {
log.Fatal(err)
}
opts = append(opts, libp2p.Identity(key))
}
if len(c.HostAddresses) > 0 {
opts = append(opts, libp2p.ListenAddrs(c.HostAddresses...))
}
if len(c.AnnounceAddresses) > 0 {
opts = append(opts, libp2p.AddrsFactory(func([]multiaddr.Multiaddr) []multiaddr.Multiaddr {
return c.AnnounceAddresses
}))
}
if c.ConnectionManager.Enabled {
cm := connmgr.NewConnManager(c.ConnectionManager.LowWaterMark,
c.ConnectionManager.HighWaterMark,
c.ConnectionManager.GracePeriod)
opts = append(opts, libp2p.ConnectionManager(cm))
}
if c.QUIC {
opts = append(opts,
libp2p.DefaultTransports,
libp2p.Transport(quic.NewTransport),
)
if len(c.HostAddresses) == 0 {
log.Fatal("if we explicitly specify a transport, we must also explicitly specify the listen addrs")
}
}
if c.NatPortMap {
opts = append(opts, libp2p.NATPortMap())
}
if c.Relay.Enabled {
var relayOpts []relay.RelayOpt
if c.Relay.Active {
relayOpts = append(relayOpts, relay.OptActive)
}
if c.Relay.Hop {
relayOpts = append(relayOpts, relay.OptHop)
}
if c.Relay.Discovery {
relayOpts = append(relayOpts, relay.OptDiscovery)
}
opts = append(opts, libp2p.EnableRelay(relayOpts...))
if c.Relay.Auto {
opts = append(opts, libp2p.EnableAutoRelay())
}
}
if c.NoListen {
opts = append(opts, libp2p.NoListenAddrs)
}
// start daemon
d, err := p2pd.NewDaemon(context.Background(), &c.ListenAddr, c.DHT.Mode, opts...)
if err != nil {
log.Fatal(err)
}
if c.AutoNat {
var opts []libp2p.Option
// allow the AutoNAT service to dial back quic addrs.
if c.QUIC {
opts = append(opts,
libp2p.DefaultTransports,
libp2p.Transport(quic.NewTransport),
)
}
err := d.EnableAutoNAT(opts...)
if err != nil {
log.Fatal(err)
}
}
if c.PubSub.Enabled {
if c.PubSub.GossipSubHeartbeat.Interval > 0 {
ps.GossipSubHeartbeatInterval = c.PubSub.GossipSubHeartbeat.Interval
}
if c.PubSub.GossipSubHeartbeat.InitialDelay > 0 {
ps.GossipSubHeartbeatInitialDelay = c.PubSub.GossipSubHeartbeat.InitialDelay
}
err = d.EnablePubsub(c.PubSub.Router, c.PubSub.Sign, c.PubSub.SignStrict)
if err != nil {
log.Fatal(err)
}
}
if len(c.Bootstrap.Peers) > 0 {
p2pd.BootstrapPeers = c.Bootstrap.Peers
}
if c.Bootstrap.Enabled {
err = d.Bootstrap()
if err != nil {
log.Fatal(err)
}
}
if !c.Quiet {
fmt.Printf("Control socket: %s\n", c.ListenAddr.String())
fmt.Printf("Peer ID: %s\n", d.ID().Pretty())
fmt.Printf("Peer Addrs:\n")
for _, addr := range d.Addrs() {
fmt.Printf("%s\n", addr.String())
}
if c.Bootstrap.Enabled && len(c.Bootstrap.Peers) > 0 {
fmt.Printf("Bootstrap peers:\n")
for _, p := range p2pd.BootstrapPeers {
fmt.Printf("%s\n", p)
}
}
}
if c.MetricsAddress != "" {
http.Handle("/metrics", promhttp.Handler())
go func() { log.Println(http.ListenAndServe(c.MetricsAddress, nil)) }()
}
select {}
}