-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
138 lines (115 loc) · 3.06 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/libp2p/go-libp2p"
"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/routing"
disc "github.com/libp2p/go-libp2p-discovery"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
mplex "github.com/libp2p/go-libp2p-mplex"
tls "github.com/libp2p/go-libp2p-tls"
yamux "github.com/libp2p/go-libp2p-yamux"
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
"github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
"github.com/multiformats/go-multiaddr"
)
type discoveryNotifee struct {
h host.Host
ctx context.Context
}
func (m *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
if m.h.Network().Connectedness(pi.ID) != network.Connected {
fmt.Printf("Found %s!\n", pi.ID.ShortString())
m.h.Connect(m.ctx, pi)
}
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
transports := libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(ws.New),
)
muxers := libp2p.ChainOptions(
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
)
security := libp2p.Security(tls.ID, tls.New)
listenAddrs := libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/tcp/0/ws",
)
var dht *kaddht.IpfsDHT
newDHT := func(h host.Host) (routing.PeerRouting, error) {
var err error
dht, err = kaddht.New(ctx, h)
return dht, err
}
routing := libp2p.Routing(newDHT)
host, err := libp2p.New(
transports,
listenAddrs,
muxers,
security,
routing,
)
if err != nil {
panic(err)
}
// TODO: Replace our stream handler with a pubsub instance, and a handler
// to field incoming messages on our topic.
host.SetStreamHandler(chatProtocol, chatHandler)
for _, addr := range host.Addrs() {
fmt.Println("Listening on", addr)
}
targetAddr, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/63785/p2p/QmWjz6xb8v9K4KnYEwP5Yk75k5mMBCehzWFLCvvQpYxF3d")
if err != nil {
panic(err)
}
targetInfo, err := peer.AddrInfoFromP2pAddr(targetAddr)
if err != nil {
panic(err)
}
err = host.Connect(ctx, *targetInfo)
if err != nil {
fmt.Fprintf(os.Stderr, "connecting to bootstrap: %s", err)
} else {
fmt.Println("Connected to", targetInfo.ID)
}
notifee := &discoveryNotifee{h: host, ctx: ctx}
mdns := mdns.NewMdnsService(host, "", notifee)
if err := mdns.Start(); err != nil {
panic(err)
}
err = dht.Bootstrap(ctx)
if err != nil {
panic(err)
}
routingDiscovery := disc.NewRoutingDiscovery(dht)
disc.Advertise(ctx, routingDiscovery, string(chatProtocol))
peers, err := disc.FindPeers(ctx, routingDiscovery, string(chatProtocol))
if err != nil {
panic(err)
}
for _, peer := range peers {
notifee.HandlePeerFound(peer)
}
donec := make(chan struct{}, 1)
go chatInputLoop(ctx, host, donec)
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT)
select {
case <-stop:
host.Close()
os.Exit(0)
case <-donec:
host.Close()
}
}