-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpcoord.go
349 lines (300 loc) · 9.21 KB
/
mpcoord.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
package main
import (
"context"
"flag"
"fmt"
discovery "github.com/libp2p/go-libp2p-discovery"
"io"
"log"
"math/rand"
"net"
"os"
"os/exec"
"time"
// We need to import libp2p's libraries that we use in this project.
"github.com/libp2p/go-libp2p"
circuit "github.com/libp2p/go-libp2p-circuit"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
ma "github.com/multiformats/go-multiaddr"
)
// Protocol defines the libp2p protocol that we will use for the libp2p proxy
// service that we are going to provide. This will tag the streams used for
// this service. Streams are multiplexed and their protocol tag helps
// libp2p handle them to the right handler functions.
const Protocol = "/mpcoord/0.0.1"
const Rendezvous = "/mpcoord"
// makeRandomHost creates a libp2p host with a randomly generated identity.
// This step is described in depth in other tutorials.
func makeRandomHost() (host.Host, *kaddht.IpfsDHT) {
ctx := context.Background()
port := 10000 + rand.Intn(10000)
host, err := libp2p.New(ctx,
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)),
libp2p.EnableRelay(circuit.OptHop, circuit.OptDiscovery))
if err != nil {
log.Fatal(err)
}
// Bootstrap the DHT. In the default configuration, this spawns a Background
// thread that will refresh the peer table every five minutes.
dht, err := kaddht.New(ctx, host)
if err != nil {
log.Fatal(err)
}
err = dht.Bootstrap(ctx)
if err != nil {
log.Fatal(err)
}
return host, dht
}
// ProxyService provides HTTP proxying on top of libp2p by launching an
// HTTP server which tunnels the requests to a destination peer running
// ProxyService too.
type ProxyService struct {
host host.Host
remotePeer peer.ID
}
// NewProxyService attaches a proxy service to the given libp2p Host.
// The localListenAddr parameter specifies the address on which the
// HTTP proxy server listens. The remotePeer parameter specifies the peer
// ID of the remote peer in charge of performing the HTTP requests.
//
// ProxyAddr/remotePeer may be nil/"" it is not necessary that this host
// provides a listening HTTP server (and instead its only function is to
// perform the proxied http requests it receives from a different peer.
//
// The addresses for the remotePeer peer should be part of the host's peerstore.
func NewProxyService(h host.Host) *ProxyService {
// We let our host know that it needs to handle streams tagged with the
// protocol id that we have defined, and then handle them to
// our own streamHandling function.
h.SetStreamHandler(Protocol, func(stream network.Stream) {
handleRemoteConnection(stream)
})
return &ProxyService{
host: h,
}
}
func startDiscovery(dht *kaddht.IpfsDHT) chan peer.AddrInfo {
// Advertise our presence.
routingDiscovery := discovery.NewRoutingDiscovery(dht)
discovery.Advertise(context.Background(), routingDiscovery, Rendezvous)
// Look for peers regularly.
peerChan := make(chan peer.AddrInfo, 100)
go func() {
for {
tmpPeerChan, err := routingDiscovery.FindPeers(context.Background(), Rendezvous)
if err != nil {
log.Fatal(err)
}
for peerInfo := range tmpPeerChan {
if peerInfo.ID != dht.Host().ID() {
peerChan <- peerInfo
}
}
time.Sleep(time.Minute)
}
}()
return peerChan
}
// handleRemoteConnection is our function to handle any libp2p-net streams that belong
// to our protocol. The streams should contain an HTTP request which we need
// to parse, make on behalf of the original node, and then write the response
// on the stream, before closing it.
func handleRemoteConnection(stream network.Stream) {
log.Println("server: forwarding remote connection to local server")
port := 20000 + rand.Intn(10000)
go runExternal("incoming-connection", port)
// Connect.
for i := 0; i < 60; i++ {
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Println("Warning:", err, ", retrying…")
time.Sleep(time.Second)
continue
}
// Forward between stream and conn.
go forward(stream, conn)
go forward(conn, stream)
return
}
log.Println("Error: could not reach local server.")
}
func (p *ProxyService) Serve(remotePeer peer.ID, port int) {
log.Println("client: listening for local requests")
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
}
go func() {
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go p.handleLocalConnection(conn, remotePeer)
}
}()
}
func (p *ProxyService) handleLocalConnection(conn net.Conn, remotePeer peer.ID) {
log.Println("client: forwarding local connection to remote peer ", remotePeer)
// We need to send the request to the remote libp2p peer, so
// we open a stream to it
stream, err := p.host.NewStream(context.Background(), remotePeer, Protocol)
if err != nil {
log.Println(err)
return
}
// Forward between stream and conn.
go forward(stream, conn)
go forward(conn, stream)
}
func runExternal(event string, port int) {
cmd := exec.Command("make", event)
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%d", port))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
func forward(dst io.WriteCloser, src io.ReadCloser) {
_, err := io.Copy(dst, src)
if err != nil {
log.Println(err)
}
dst.Close()
}
func parseAddress(addr string) *peer.AddrInfo {
parsed, err := ma.NewMultiaddr(addr)
if err != nil {
log.Fatalln(err)
}
peerInfo, err := peer.AddrInfoFromP2pAddr(parsed)
if err != nil {
log.Fatal(err)
}
return peerInfo
}
func addRelayAddress(relayAddr string, peerInfo *peer.AddrInfo) {
if relayAddr == "" {
return
}
addr := fmt.Sprintf("%s/p2p-circuit/p2p/%s", relayAddr, peer.IDB58Encode(peerInfo.ID))
maddr, err := ma.NewMultiaddr(addr)
if err != nil {
log.Println("Warning:", err)
return
}
peerInfo.Addrs = append(peerInfo.Addrs, maddr)
}
// addAddrToPeerstore parses a peer multiaddress and adds
// it to the given host's peerstore, so it knows how to
// contact it. It returns the peer ID of the remote peer.
func addAddrToPeerstore(h host.Host, addr string) *peer.AddrInfo {
peerInfo := parseAddress(addr)
// We have a peer ID and a targetAddr so we add
// it to the peerstore so LibP2P knows how to contact it
h.Peerstore().AddAddrs(peerInfo.ID, peerInfo.Addrs, peerstore.PermanentAddrTTL)
return peerInfo
}
func connectToPeer(h host.Host, addr string) (*peer.AddrInfo, error) {
peerInfo := parseAddress(addr)
err := h.Connect(context.Background(), *peerInfo)
if err != nil {
return nil, err
}
return peerInfo, nil
}
const help = `
This example creates a simple TCP Proxy using two libp2p peers. The first peer
provides an TCP server locally which tunnels the TCP requests with libp2p
to a remote peer. The remote peer performs the requests and
send the sends the response back.
Usage: Start remote peer first with: ./mpcoord
Then start the local peer with: ./mpcoord -c <remote-peer-multiaddress>
`
func main() {
rand.Seed(time.Now().UnixNano())
flag.Usage = func() {
log.Println(help)
flag.PrintDefaults()
}
// Parse some flags
remotePeer := flag.String("c", "", "remote peer address")
pureRelay := flag.Bool("R", false, "run as a relay only")
relayPeer := flag.String("r", "", "connect to this relay")
flag.Parse()
name := ""
if *pureRelay {
name = "relay"
} else if *remotePeer != "" {
name = "client"
} else {
name = "server"
}
log.SetFlags(log.Lshortfile)
log.SetPrefix(name + ": ")
host, dht := makeRandomHost()
addr := ""
log.Println("Node", host.ID())
log.Println("libp2p-peer addresses:")
for _, a := range host.Addrs() {
addr = fmt.Sprintf("%s/p2p/%s", a, peer.IDB58Encode(host.ID()))
fmt.Println(addr)
}
if *relayPeer != "" {
log.Println("Connecting to relay", *relayPeer)
connectToPeer(host, *relayPeer)
addr = fmt.Sprintf("%s/p2p-circuit/p2p/%s", *relayPeer, peer.IDB58Encode(host.ID()))
fmt.Println(addr)
}
// Save our address to a file.
filename := "local/" + name + ".p2p"
fd, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
_, err = io.WriteString(fd, addr)
if err != nil {
log.Fatal(err)
}
fd.Close()
log.Println("Wrote my address in", filename)
if *pureRelay {
log.Println("Running as relay.")
<-make(chan struct{}) // hang forever as relay
} else {
// Start the service.
proxy := NewProxyService(host)
peerChan := make(chan peer.AddrInfo, 1)
if name == "client" {
// Client mode: connect to the provided peer.
remotePeerInfo := parseAddress(*remotePeer)
peerChan <- *remotePeerInfo
close(peerChan)
} else {
// Auto mode: discover peers.
peerChan = startDiscovery(dht)
}
for peerInfo := range peerChan {
log.Println("Found peer", peerInfo)
addRelayAddress(*relayPeer, &peerInfo)
// Make sure our host knows how to reach remotePeer.
err := host.Connect(context.Background(), peerInfo)
if err != nil {
log.Println("Failed to connect, skipping")
continue
}
port := 30000 + rand.Intn(10000)
// Listen for local backend connections.
proxy.Serve(peerInfo.ID, port)
// The backend client will connect to the proxy.Serve above.
runExternal("outgoing-connection", port)
}
}
}