-
Notifications
You must be signed in to change notification settings - Fork 36
/
daemon.go
216 lines (174 loc) · 4.23 KB
/
daemon.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
package p2pd
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/libp2p/go-libp2p-daemon/config"
"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/protocol"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
multierror "github.com/hashicorp/go-multierror"
logging "github.com/ipfs/go-log"
dht "github.com/libp2p/go-libp2p-kad-dht"
dhtopts "github.com/libp2p/go-libp2p-kad-dht/opts"
ps "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
var log = logging.Logger("p2pd")
type Daemon struct {
ctx context.Context
host host.Host
listener manet.Listener
dht *dht.IpfsDHT
pubsub *ps.PubSub
mx sync.Mutex
// stream handlers: map of protocol.ID to multi-address
handlers map[protocol.ID]ma.Multiaddr
// closed is set when the daemon is shutting down
closed bool
}
func NewDaemon(ctx context.Context, maddr ma.Multiaddr, dhtMode string, opts ...libp2p.Option) (*Daemon, error) {
d := &Daemon{
ctx: ctx,
handlers: make(map[protocol.ID]ma.Multiaddr),
}
if dhtMode != "" {
var dhtOpts []dhtopts.Option
if dhtMode == config.DHTClientMode {
dhtOpts = append(dhtOpts, dht.Mode(dht.ModeClient))
} else if dhtMode == config.DHTServerMode {
dhtOpts = append(dhtOpts, dht.Mode(dht.ModeServer))
}
opts = append(opts, libp2p.Routing(d.DHTRoutingFactory(dhtOpts)))
}
h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
d.host = h
l, err := manet.Listen(maddr)
if err != nil {
h.Close()
return nil, err
}
d.listener = l
go d.listen()
go d.trapSignals()
return d, nil
}
func (d *Daemon) Listener() manet.Listener {
return d.listener
}
func (d *Daemon) DHTRoutingFactory(opts []dhtopts.Option) func(host.Host) (routing.PeerRouting, error) {
makeRouting := func(h host.Host) (routing.PeerRouting, error) {
dhtInst, err := dht.New(d.ctx, h, opts...)
if err != nil {
return nil, err
}
d.dht = dhtInst
return dhtInst, nil
}
return makeRouting
}
func (d *Daemon) EnableRelayV2() error {
_, err := relay.New(d.host)
return err
}
func (d *Daemon) EnablePubsub(router string, sign, strict bool) error {
var opts []ps.Option
if !sign {
opts = append(opts, ps.WithMessageSigning(false))
} else if !strict {
opts = append(opts, ps.WithStrictSignatureVerification(false))
} else {
opts = append(opts, ps.WithMessageSignaturePolicy(ps.StrictSign))
}
switch router {
case "floodsub":
pubsub, err := ps.NewFloodSub(d.ctx, d.host, opts...)
if err != nil {
return err
}
d.pubsub = pubsub
return nil
case "gossipsub":
pubsub, err := ps.NewGossipSub(d.ctx, d.host, opts...)
if err != nil {
return err
}
d.pubsub = pubsub
return nil
default:
return fmt.Errorf("unknown pubsub router: %s", router)
}
}
func (d *Daemon) EnableEcho() error {
d.host.SetStreamHandler("/echo/1.0.0", func(s network.Stream) {
_, err := io.Copy(s, s)
if err != nil {
s.Reset()
} else {
s.Close()
}
})
return nil
}
func (d *Daemon) ID() peer.ID {
return d.host.ID()
}
func (d *Daemon) Addrs() []ma.Multiaddr {
return d.host.Addrs()
}
func (d *Daemon) listen() {
for {
if d.isClosed() {
return
}
c, err := d.listener.Accept()
if err != nil {
log.Errorw("error accepting connection", "error", err)
continue
}
log.Debug("incoming connection")
go d.handleConn(c)
}
}
func (d *Daemon) isClosed() bool {
d.mx.Lock()
defer d.mx.Unlock()
return d.closed
}
func clearUnixSockets(path ma.Multiaddr) error {
c, _ := ma.SplitFirst(path)
if c.Protocol().Code != ma.P_UNIX {
return nil
}
if err := os.Remove(c.Value()); err != nil {
return err
}
return nil
}
func (d *Daemon) Close() error {
d.mx.Lock()
d.closed = true
d.mx.Unlock()
var merr *multierror.Error
if err := d.host.Close(); err != nil {
merr = multierror.Append(err)
}
listenAddr := d.listener.Multiaddr()
if err := d.listener.Close(); err != nil {
merr = multierror.Append(merr, err)
}
if err := clearUnixSockets(listenAddr); err != nil {
merr = multierror.Append(merr, err)
}
return merr.ErrorOrNil()
}