-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (122 loc) · 3.36 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
router "github.com/libp2p/go-libp2p-core/routing"
kaddht "github.com/libp2p/go-libp2p-kad-dht"
mplex "github.com/libp2p/go-libp2p-mplex"
pubsub "github.com/libp2p/go-libp2p-pubsub"
secio "github.com/libp2p/go-libp2p-secio"
yamux "github.com/libp2p/go-libp2p-yamux"
"github.com/libp2p/go-libp2p/p2p/discovery"
tcp "github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
"github.com/multiformats/go-multiaddr"
core "github.com/thiagozs/go-libp2p-pubsub/core"
ds "github.com/thiagozs/go-libp2p-pubsub/mdns"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// chainoptions for transport
transports := libp2p.ChainOptions(
libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(ws.New),
)
// chainoptions for muxers
muxers := libp2p.ChainOptions(
libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
)
// security instance
security := libp2p.Security(secio.ID, secio.New)
// listenAddress
listenAddrs := libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/0",
"/ip4/0.0.0.0/tcp/0/ws",
)
// create a hash table addrs
var dht *kaddht.IpfsDHT
newDHT := func(h host.Host) (router.PeerRouting, error) {
var err error
dht, err = kaddht.New(ctx, h)
return dht, err
}
// routing with opt with dht
routing := libp2p.Routing(newDHT)
// start libp2p
host, err := libp2p.New(ctx, transports, listenAddrs, muxers, security, routing)
if err != nil {
panic(err)
}
// start pubsub engine
ps, err := pubsub.NewGossipSub(ctx, host)
if err != nil {
panic(err)
}
// subscribe for a topic
sub, err := ps.Subscribe(core.Topic)
if err != nil {
panic(err)
}
// Start counters
cc := core.NewCounters()
// handler for messages
go core.PubsubHandler(cc, ctx, sub)
// list address for listering
for _, addr := range host.Addrs() {
fmt.Println("Listening on", addr)
}
// change for real bootstrap
//strAddr := "/ip4/127.0.0.1/tcp/4001/ipfs/QmXBbs4x3E7f8TNgZPCcG38vgRFiZdeAC6qPPVHKGtAR7x"
strAddr := "/dns4/bootstrapper-1.rhz.network/tcp/4001/ipfs/Qmf8Lt1FiQnG7tLrQbhwvUXzBMYsj6KicNdKiD1F2rSRW5"
targetAddr, err := multiaddr.NewMultiaddr(strAddr)
if err != nil {
panic(err)
}
targetInfo, err := peer.AddrInfoFromP2pAddr(targetAddr)
if err != nil {
panic(err)
}
// connect for the host
err = host.Connect(ctx, *targetInfo)
if err != nil {
panic(err)
}
fmt.Println("Connected to", targetInfo.ID)
// start domain name server
mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "mydns")
if err != nil {
panic(err)
}
mdns.RegisterNotifee(&ds.Notifee{H: host, CTX: ctx})
// tells the DHT to get into a bootstrapped state satisfying the IpfsRouter interface
err = dht.Bootstrap(ctx)
if err != nil {
panic(err)
}
// handler chat input
donec := make(chan struct{}, 1)
go core.ChatInputLoop(cc, ctx, host, ps, donec)
// monitoring program stats
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT)
select {
case <-stop: // exit program
mdns.Close()
dht.Close()
host.ConnManager().Close()
host.Network().Close()
host.Close()
os.Exit(0)
case <-donec: // finish command close with host
host.Close()
}
}