This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlisten.go
298 lines (248 loc) · 7.55 KB
/
listen.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
package conn
import (
"context"
"fmt"
"io"
"net"
"sync"
"time"
tec "github.com/jbenet/go-temp-err-catcher"
"github.com/jbenet/goprocess"
goprocessctx "github.com/jbenet/goprocess/context"
ic "github.com/libp2p/go-libp2p-crypto"
iconn "github.com/libp2p/go-libp2p-interface-conn"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
peer "github.com/libp2p/go-libp2p-peer"
transport "github.com/libp2p/go-libp2p-transport"
filter "github.com/libp2p/go-maddr-filter"
ma "github.com/multiformats/go-multiaddr"
msmux "github.com/multiformats/go-multistream"
)
const (
SecioTag = "/secio/1.0.0"
NoEncryptionTag = "/plaintext/1.0.0"
)
var connAcceptBuffer = 32
// AcceptTimeout is the maximum duration an Accept is allowed to take.
// This includes the time between accepting the raw network connection,
// protocol selection as well as the handshake, if applicable.
var AcceptTimeout = 60 * time.Second
// ConnWrapper is any function that wraps a raw multiaddr connection.
type ConnWrapper func(transport.Conn) transport.Conn
// listener is an object that can accept connections. It implements Listener
type listener struct {
transport.Listener
local peer.ID // LocalPeer is the identity of the local Peer
privk ic.PrivKey // private key to use to initialize secure conns
protec ipnet.Protector
filters *filter.Filters
wrapper ConnWrapper
catcher tec.TempErrCatcher
proc goprocess.Process
mux *msmux.MultistreamMuxer
incoming chan connErr
ctx context.Context
}
func (l *listener) teardown() error {
defer log.Debugf("listener closed: %s %s", l.local, l.Multiaddr())
return l.Listener.Close()
}
func (l *listener) Close() error {
log.Debugf("listener closing: %s %s", l.local, l.Multiaddr())
return l.proc.Close()
}
func (l *listener) String() string {
return fmt.Sprintf("<Listener %s %s>", l.local, l.Multiaddr())
}
func (l *listener) SetAddrFilters(fs *filter.Filters) {
l.filters = fs
}
type connErr struct {
conn transport.Conn
err error
}
// Accept waits for and returns the next connection to the listener.
func (l *listener) Accept() (transport.Conn, error) {
if c, ok := <-l.incoming; ok {
return c.conn, c.err
}
return nil, fmt.Errorf("listener is closed")
}
func (l *listener) Addr() net.Addr {
return l.Listener.Addr()
}
// Multiaddr is the identity of the local Peer.
// If there is an error converting from net.Addr to ma.Multiaddr,
// the return value will be nil.
func (l *listener) Multiaddr() ma.Multiaddr {
return l.Listener.Multiaddr()
}
// LocalPeer is the identity of the local Peer.
func (l *listener) LocalPeer() peer.ID {
return l.local
}
func (l *listener) Loggable() map[string]interface{} {
return map[string]interface{}{
"listener": map[string]interface{}{
"peer": l.LocalPeer(),
"address": l.Multiaddr(),
"secure": (l.privk != nil),
"inPrivNet": (l.protec != nil),
},
}
}
func (l *listener) handleIncoming() {
var wg sync.WaitGroup
defer func() {
wg.Wait()
close(l.incoming)
}()
wg.Add(1)
defer wg.Done()
for {
maconn, err := l.Listener.Accept()
if err != nil {
if l.catcher.IsTemporary(err) {
continue
}
select {
case <-l.proc.Closing():
case l.incoming <- connErr{err: err}:
}
return
}
log.Debugf("listener %s got connection: %s <---> %s", l, maconn.LocalMultiaddr(), maconn.RemoteMultiaddr())
if l.filters != nil && l.filters.AddrBlocked(maconn.RemoteMultiaddr()) {
log.Debugf("blocked connection from %s", maconn.RemoteMultiaddr())
maconn.Close()
continue
}
wg.Add(1)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(l.ctx, AcceptTimeout)
defer cancel()
result := make(chan transport.Conn, 1)
wg.Add(1)
go func(conn transport.Conn) {
defer wg.Done()
defer close(result)
if l.protec != nil {
pc, err := l.protec.Protect(conn)
if err != nil {
conn.Close()
log.Warning("protector failed: ", err)
return
}
conn = pc
}
// If we have a wrapper func, wrap this conn
if l.wrapper != nil {
conn = l.wrapper(conn)
}
// Negotiate secio (or no secio).
_, _, err = l.mux.Negotiate(conn)
if err != nil {
conn.Close()
log.Warning("incoming conn: negotiation of crypto protocol failed: ", err)
return
}
insecureConn := newSingleConn(ctx, l.local, "", conn)
if l.privk != nil && iconn.EncryptConnections {
secureConn, err := newSecureConn(ctx, l.privk, insecureConn)
if err != nil {
conn.Close()
log.Infof("ignoring conn we failed to secure: %s %s", err, insecureConn)
return
}
conn = secureConn
} else {
log.Warning("listener %s listening INSECURELY!", l)
conn = insecureConn
}
result <- conn
}(maconn)
select {
case <-ctx.Done():
log.Warning("incoming conn: conn not established in time:",
ctx.Err().Error())
// Will cause the other go routine to bail.
maconn.Close()
case c, ok := <-result: // connection completed (or errored)
if ok {
select {
case <-l.proc.Closing():
maconn.Close()
case l.incoming <- connErr{conn: c}:
}
}
}
}()
}
}
// WrapTransportListener wraps a raw transport.Listener in an iconn.Listener.
// If sk is not provided, transport encryption is disabled.
//
// The Listener will accept connections in the background and attempt to
// negotiate the protocol before making the wrapped connection available to Accept.
// If the negotiation and handshake take more than AcceptTimeout, the connection
// is dropped. However, note that once a connection handshake succeeds, it will
// wait indefinitely for an Accept call to service it (possibly consuming a goroutine).
//
// The context covers the listener and its background activities, but not the
// connections once returned from Accept. Calling Close and canceling the
// context are equivalent.
//
// The returned Listener implements ListenerConnWrapper.
func WrapTransportListener(ctx context.Context, ml transport.Listener, local peer.ID,
sk ic.PrivKey) (iconn.Listener, error) {
return WrapTransportListenerWithProtector(ctx, ml, local, sk, nil)
}
func WrapTransportListenerWithProtector(ctx context.Context, ml transport.Listener, local peer.ID,
sk ic.PrivKey, protec ipnet.Protector) (iconn.Listener, error) {
if protec == nil && ipnet.ForcePrivateNetwork {
log.Error("tried to listen with no Private Network Protector but usage" +
" of Private Networks is forced by the enviroment")
return nil, ipnet.ErrNotInPrivateNetwork
}
l := &listener{
Listener: ml,
local: local,
privk: sk,
protec: protec,
mux: msmux.NewMultistreamMuxer(),
incoming: make(chan connErr, connAcceptBuffer),
ctx: ctx,
}
l.proc = goprocessctx.WithContextAndTeardown(ctx, l.teardown)
l.catcher.IsTemp = func(e error) bool {
// ignore connection breakages up to this point. but log them
if e == io.EOF {
log.Debugf("listener ignoring conn with EOF: %s", e)
return true
}
te, ok := e.(tec.Temporary)
if ok {
log.Debugf("listener ignoring conn with temporary err: %s", e)
return te.Temporary()
}
return false
}
if iconn.EncryptConnections && sk != nil {
l.mux.AddHandler(SecioTag, nil)
} else {
l.mux.AddHandler(NoEncryptionTag, nil)
}
go l.handleIncoming()
log.Debugf("Conn Listener on %s", l.Multiaddr())
log.Event(ctx, "swarmListen", l)
return l, nil
}
type ListenerConnWrapper interface {
// SetConnWrapper assigns a ConnWrapper to wrap all raw incoming
// connections with. It must be called before any call to Accept.
SetConnWrapper(ConnWrapper)
}
func (l *listener) SetConnWrapper(cw ConnWrapper) {
l.wrapper = cw
}