@@ -9,17 +9,22 @@ import (
9
9
"context"
10
10
"fmt"
11
11
"os"
12
+ "strings"
12
13
"time"
13
14
14
15
"github.com/0xProject/0x-mesh/keys"
15
16
"github.com/0xProject/0x-mesh/loghooks"
16
17
"github.com/0xProject/0x-mesh/p2p"
17
18
libp2p "github.com/libp2p/go-libp2p"
18
19
autonat "github.com/libp2p/go-libp2p-autonat-svc"
20
+ relay "github.com/libp2p/go-libp2p-circuit"
19
21
connmgr "github.com/libp2p/go-libp2p-connmgr"
20
22
p2pcrypto "github.com/libp2p/go-libp2p-crypto"
23
+ host "github.com/libp2p/go-libp2p-host"
24
+ dht "github.com/libp2p/go-libp2p-kad-dht"
21
25
p2pnet "github.com/libp2p/go-libp2p-net"
22
26
peer "github.com/libp2p/go-libp2p-peer"
27
+ routing "github.com/libp2p/go-libp2p-routing"
23
28
ma "github.com/multiformats/go-multiaddr"
24
29
"github.com/plaid/go-envvar/envvar"
25
30
log "github.com/sirupsen/logrus"
@@ -43,10 +48,12 @@ const (
43
48
// Config contains configuration options for a Node.
44
49
type Config struct {
45
50
// Verbosity is the logging verbosity: 0=panic, 1=fatal, 2=error, 3=warn, 4=info, 5=debug 6=trace
46
- Verbosity int `envvar:"VERBOSITY" default:"6"`
47
- // P2PListenPort is the port on which to listen for new connections. It can be
48
- // set to 0 to make the OS automatically choose any available port.
49
- P2PListenPort int `envvar:"P2P_LISTEN_PORT" default:"0"`
51
+ Verbosity int `envvar:"VERBOSITY" default:"5"`
52
+ // P2PListenPort is the port on which to listen for new connections.
53
+ P2PListenPort int `envvar:"P2P_LISTEN_PORT"`
54
+ // PublicIPAddrs is a comma separated list of public IPv4 addresses at which
55
+ // the bootstrap node is accessible.
56
+ PublicIPAddrs string `envvar:"PUBLIC_IP_ADDRS"`
50
57
// PrivateKey path is the path to a private key file which will be used for
51
58
// signing messages and generating a peer ID.
52
59
PrivateKeyPath string `envvar:"PRIVATE_KEY_PATH" default:"0x_mesh/keys/privkey"`
@@ -73,6 +80,29 @@ func main() {
73
80
log .WithField ("error" , err ).Fatal ("could not initialize private key" )
74
81
}
75
82
83
+ // We need to declare the newDHT function ahead of time so we can use it in
84
+ // the libp2p.Routing option.
85
+ var kadDHT * dht.IpfsDHT
86
+ newDHT := func (h host.Host ) (routing.PeerRouting , error ) {
87
+ var err error
88
+ kadDHT , err = p2p .NewDHT (ctx , h )
89
+ if err != nil {
90
+ log .WithField ("error" , err ).Fatal ("could not create DHT" )
91
+ }
92
+ return kadDHT , err
93
+ }
94
+ // Parse advertiseAddresses from Public IPs
95
+ ipAddrs := strings .Split (config .PublicIPAddrs , "," )
96
+ advertiseAddrs := make ([]ma.Multiaddr , len (ipAddrs ))
97
+ for i , ipAddr := range ipAddrs {
98
+ maddrString := fmt .Sprintf ("/ip4/%s/tcp/%d" , ipAddr , config .P2PListenPort )
99
+ ma , err := ma .NewMultiaddr (maddrString )
100
+ if err != nil {
101
+ log .Fatal (err )
102
+ }
103
+ advertiseAddrs [i ] = ma
104
+ }
105
+
76
106
// Set up the transport and the host.
77
107
// Note: 0.0.0.0 will use all available addresses.
78
108
hostAddr , err := ma .NewMultiaddr (fmt .Sprintf ("/ip4/0.0.0.0/tcp/%d" , config .P2PListenPort ))
@@ -84,6 +114,10 @@ func main() {
84
114
libp2p .ListenAddrs (hostAddr ),
85
115
libp2p .Identity (privKey ),
86
116
libp2p .ConnectionManager (connManager ),
117
+ libp2p .EnableRelay (relay .OptHop ),
118
+ libp2p .EnableAutoRelay (),
119
+ libp2p .Routing (newDHT ),
120
+ libp2p .AddrsFactory (newAddrsFactory (advertiseAddrs )),
87
121
}
88
122
basicHost , err := libp2p .New (ctx , opts ... )
89
123
if err != nil {
@@ -101,12 +135,6 @@ func main() {
101
135
log .WithField ("error" , err ).Fatal ("could not enable AutoNAT service" )
102
136
}
103
137
104
- // Set up DHT for peer discovery.
105
- kadDHT , err := p2p .NewDHT (ctx , basicHost )
106
- if err != nil {
107
- log .WithField ("error" , err ).Fatal ("could not create DHT" )
108
- }
109
-
110
138
// Initialize the DHT and then connect to the other bootstrap nodes.
111
139
if err := kadDHT .Bootstrap (ctx ); err != nil {
112
140
log .WithField ("error" , err ).Fatal ("could not bootstrap DHT" )
@@ -130,7 +158,8 @@ func main() {
130
158
}
131
159
132
160
log .WithFields (map [string ]interface {}{
133
- "addrs" : basicHost .Addrs (),
161
+ "addrs" : basicHost .Addrs (),
162
+ "config" : config ,
134
163
}).Info ("started bootstrap node" )
135
164
136
165
// Sleep until stopped
@@ -183,3 +212,9 @@ func (n *notifee) OpenedStream(network p2pnet.Network, stream p2pnet.Stream) {}
183
212
184
213
// ClosedStream is called when a stream closed
185
214
func (n * notifee ) ClosedStream (network p2pnet.Network , stream p2pnet.Stream ) {}
215
+
216
+ func newAddrsFactory (advertiseAddrs []ma.Multiaddr ) func ([]ma.Multiaddr ) []ma.Multiaddr {
217
+ return func ([]ma.Multiaddr ) []ma.Multiaddr {
218
+ return advertiseAddrs
219
+ }
220
+ }
0 commit comments