This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
peer.go
302 lines (255 loc) · 7.5 KB
/
peer.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
package peer
import (
"bufio"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"sync"
"time"
host "github.com/libp2p/go-libp2p-host"
"github.com/peerbridge/peerbridge/pkg/color"
ipfslog "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
discovery "github.com/libp2p/go-libp2p-discovery"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/multiformats/go-multiaddr"
)
const (
// A stream protocol that will be used to
// identify streams belonging to our application.
streamProtocol protocol.ID = "/peerbridge/p2p/1.0.0"
// A discovery identifier string that is sent to other peers.
discoveryIdentifier = "dht.routing.peerbridge"
DefaultP2PPort = "9080"
)
// A binding to another peer, represented by a rw buffer.
type Binding = bufio.ReadWriter
type P2PService struct {
// The urls under which the peer can be accessed.
// This variable is set when `Run` is called.
URLs []url.URL
// The subscribers to new incoming messages of the peer.
incomingSubscribers []chan []byte
// The subscribers to new outgoing messages of the peer.
outgoingSubscribers []chan []byte
// All currently open bindings for peer streams.
bindings []*Binding
// A mutex to avoid race conditions when writing bindings
// from different goroutines.
mutex sync.RWMutex
// A background context in which p2p networking is done.
ctx context.Context
}
var Service = &P2PService{
ctx: context.Background(),
}
func GetP2PPort() string {
port := os.Getenv("P2P_PORT")
if port != "" {
return port
}
return DefaultP2PPort
}
// Initialize the blockchain peer.
// Use the parameter `bootstrapTarget` to add a
// target url for the bootstrapping service.
// Note that this method will never return.
func (service *P2PService) Run(bootstrapHost string) {
// Configure the ipfs loggers
ipfslog.SetAllLoggers(ipfslog.LevelError)
ipfslog.SetLogLevel("rendezvous", "info")
// Create the p2p host
host := service.newHost(GetP2PPort())
dht := service.newDHT(host, bootstrapHost)
// Set a default stream handler for incoming p2p connections
host.SetStreamHandler(streamProtocol, service.bind)
// Announce ourselves using a routing discovery
peers := service.findPeers(dht)
for peer := range peers {
if peer.ID == host.ID() {
continue
}
stream, err := host.NewStream(service.ctx, peer.ID, streamProtocol)
if err != nil {
log.Printf(
"Offline: %s\n",
color.Sprintf(fmt.Sprintf("%s", peer.ID), color.Warning),
)
continue
}
service.bind(stream)
log.Printf(
"Connected: %s\n",
color.Sprintf(fmt.Sprintf("%s", peer.ID), color.Success),
)
}
select {}
}
// Make a host that listens on the given multiaddress
func (service *P2PService) newHost(port string) host.Host {
// 0.0.0.0 will listen on any interface device.
addr := fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", port)
host, err := libp2p.New(context.Background(), libp2p.ListenAddrStrings(addr))
if err != nil {
panic(err)
}
log.Printf("Created a new p2p service which is reachable under:\n")
for _, addr := range host.Addrs() {
address := fmt.Sprintf("%s/p2p/%s", addr, host.ID())
u, err := url.Parse(address)
if err != nil {
continue
}
service.URLs = append(service.URLs, *u)
log.Printf("%s\n", color.Sprintf(address, color.Notice))
}
return host
}
// Get the peer urls from the bootstrap target via HTTP.
func (service *P2PService) requestPeerURLs(bootstrapHost string) (urls []string, err error) {
u := fmt.Sprintf("%s/peer/urls", bootstrapHost)
res, err := http.Get(u)
if err != nil {
return nil, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&urls)
return
}
// Make a dht that is used to discover and track new peers.
func (service *P2PService) newDHT(host host.Host, bootstrapHost string) *dht.IpfsDHT {
// Specify DHT options, in this case we want the service
// to serve as a bootstrap server
dht, err := dht.New(service.ctx, host, dht.Mode(dht.ModeServer))
if err != nil {
panic(err)
}
// Bootstrap the dht. In the default configuration, this spawns
// a background thread that will refresh the peer table every
// five minutes
if err = dht.Bootstrap(service.ctx); err != nil {
panic(err)
}
// If no bootstrap node was given, return the created dht
if bootstrapHost == "" {
return dht
}
// Poll until the bootstrap node is alive
var urls []string
for {
urls, _ := service.requestPeerURLs(bootstrapHost)
if urls != nil && len(urls) > 0 {
break
}
log.Println(color.Sprintf("Waiting until the bootstrap node is online...", color.Warning))
time.Sleep(time.Second * 1)
}
for _, url := range urls {
addr, err := multiaddr.NewMultiaddr(url)
if err != nil {
continue
}
peerInfo, _ := peer.AddrInfoFromP2pAddr(addr)
err = host.Connect(service.ctx, *peerInfo)
if err != nil {
continue
}
log.Printf(
"Connected to the bootstrap node via %s\n",
color.Sprintf(fmt.Sprintf("%s", addr), color.Notice),
)
return dht
}
panic("The bootstrap node could not be reached!")
}
// Find new peers using the dht and a routing discovery.
func (service *P2PService) findPeers(hashtable *dht.IpfsDHT) <-chan peer.AddrInfo {
d := discovery.NewRoutingDiscovery(hashtable)
discovery.Advertise(context.Background(), d, discoveryIdentifier)
peers, err := d.FindPeers(service.ctx, discoveryIdentifier)
if err != nil {
panic(err)
}
return peers
}
// Bind to another peer via an obtained stream.
func (service *P2PService) bind(stream network.Stream) {
// Create a new stream binding
reader := bufio.NewReader(stream)
writer := bufio.NewWriter(stream)
binding := bufio.NewReadWriter(reader, writer)
service.mutex.Lock()
service.bindings = append(service.bindings, binding)
service.mutex.Unlock()
// Continuously read incoming data
go service.listen(binding, func() {
log.Println(color.Sprintf("A node disconnected.", color.Warning))
// Remove the binding from the bindings list
service.mutex.Lock()
n := 0
for _, bi := range service.bindings {
if bi != binding {
service.bindings = append(service.bindings, bi)
n++
}
}
service.bindings = service.bindings[:n]
service.mutex.Unlock()
})
}
// Continously listen on a binding.
func (service *P2PService) listen(binding *Binding, onDisconnect func()) {
for {
str, err := binding.ReadString('\n')
bytes := []byte(str)
if err != nil {
// If an error occured, stop listening
break
}
if str == "\n" {
continue
}
for _, subscriber := range service.incomingSubscribers {
subscriber <- bytes
}
}
onDisconnect()
}
func (service *P2PService) SubscribeIncoming(channel chan []byte) {
service.incomingSubscribers = append(service.incomingSubscribers, channel)
}
func (service *P2PService) SubscribeOutgoing(channel chan []byte) {
service.outgoingSubscribers = append(service.outgoingSubscribers, channel)
}
// Broadcast an object to all bound peers and the dashboard.
// The object will be JSON serialized for transfer.
func (service *P2PService) Broadcast(object interface{}) {
bytes, err := json.Marshal(object)
if err != nil {
panic(err)
}
for _, subscriber := range service.outgoingSubscribers {
subscriber <- bytes
}
service.mutex.RLock()
for _, binding := range service.bindings {
_, err := binding.WriteString(fmt.Sprintf("%s\n", string(bytes)))
if err != nil {
log.Println("Error writing to buffer")
panic(err)
}
err = binding.Flush()
if err != nil {
log.Println("Error flushing buffer")
continue
}
}
service.mutex.RUnlock()
}