-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
334 lines (287 loc) · 7.68 KB
/
listener.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
package magictls
import (
"crypto/tls"
"errors"
"io"
"log"
"net"
"os"
"sync"
"time"
)
var ErrDuplicateProtocol = errors.New("protocol already has a listener")
type queuePoint struct {
c net.Conn
e error
}
// Listener is a stream network listener supporting TLS and
// PROXY protocol automatically. It assumes no matter what the used protocol
// is, at least 16 bytes will always be initially sent (true for HTTP).
type Listener struct {
ports []net.Listener
portsLk sync.Mutex
addr net.Addr
queue chan queuePoint
proto map[string]*protoListener
protoLk sync.RWMutex
TLSConfig *tls.Config
Filters []Filter
*log.Logger
Timeout time.Duration
// threads
thCnt uint32
thMax uint32
thCntLock sync.RWMutex
thCntCond sync.Cond
}
// Listen creates a hybrid TCP/TLS listener accepting connections on the given
// network address using net.Listen. The configuration config must be non-nil
// and must include at least one certificate or else set GetCertificate. If
// not, then only PROXY protocol support will be available.
//
// If the connection uses TLS protocol, then Accept() returned net.Conn will
// actually be a tls.Conn object.
func Listen(network, laddr string, config *tls.Config) (*Listener, error) {
r := ListenNull()
r.TLSConfig = config
if err := r.Listen(network, laddr); err != nil {
return nil, err
}
return r, nil
}
// ListenNull creates a listener that is not actually listening to anything,
// but can be used to push connections via PushConn. This can be useful to use
// a http.Server with custom listeners.
func ListenNull() *Listener {
return &Listener{
queue: make(chan queuePoint, 8),
proto: make(map[string]*protoListener),
Filters: []Filter{DetectProxy, DetectTLS},
Timeout: 15 * time.Second,
thMax: 64,
}
}
// Listen makes the given listener listen on an extra port. Each listener will
// spawn a new goroutine.
func (r *Listener) Listen(network, laddr string) error {
return r.ListenFilter(network, laddr, nil)
}
// ListenFilter listens on a given port with the selected filters used instead
// of the default ones.
func (r *Listener) ListenFilter(network, laddr string, filters []Filter) error {
port, err := net.Listen(network, laddr)
if err != nil {
return err
}
if r.addr == nil {
r.addr = port.Addr()
}
r.portsLk.Lock()
defer r.portsLk.Unlock()
r.ports = append(r.ports, port)
go r.listenLoop(port, filters)
return nil
}
// ProtoListener returns a net.Listener that will receive connections for which
// TLS is enabled and the specified protocol(s) have been negociated between
// client and server.
func (r *Listener) ProtoListener(proto ...string) (net.Listener, error) {
r.protoLk.Lock()
defer r.protoLk.Unlock()
// check if none of proto are taken
for _, pr := range proto {
if _, found := r.proto[pr]; found {
return nil, ErrDuplicateProtocol
}
}
// create listener, register
l := &protoListener{
proto: proto,
queue: make(chan *queuePoint, 8),
parent: r,
}
for _, pr := range proto {
r.proto[pr] = l
}
return l, nil
}
// SetThreads sets the number of threads (goroutines) magictls will spawn in
// parallel when handling incoming connections. Note that once a connection
// leaves Accept() it is not tracked anymore.
// Filters will however run in parallel for those connections, meaning that
// one connection's handshake taking time will not block other connections.
func (r *Listener) SetThreads(count uint32) {
r.thCntLock.Lock()
defer r.thCntLock.Unlock()
r.thMax = count
}
// GetRunningThreads returns the current number of running threads.
func (r *Listener) GetRunningThreads() uint32 {
r.thCntLock.RLock()
defer r.thCntLock.RUnlock()
return r.thCnt
}
// Accept blocks until a connection is available, then return said connection
// or an error if the listener was closed.
func (r *Listener) Accept() (net.Conn, error) {
// TODO implement timeouts?
p, ok := <-r.queue
if !ok {
return nil, io.EOF
}
return p.c, p.e
}
// processFilters is run in a thread and will execute filters as needed.
func (r *Listener) processFilters(c net.Conn, filters []Filter) {
defer func() {
r.thCntLock.Lock()
r.thCnt -= 1
r.thCntLock.Unlock()
}()
cw := &Conn{
conn: c,
l: c.LocalAddr(),
r: c.RemoteAddr(),
}
var (
tlsconn *tls.Conn
negociatedProtocol string
)
if filters == nil {
filters = r.Filters
}
// for each filter
for _, f := range filters {
cw.SetReadDeadline(time.Now().Add(r.Timeout))
err := f(cw, r)
if err != nil {
if err == io.EOF {
// ignore EOF errors, those are typically not important
continue
}
if errors.Is(err, os.ErrDeadlineExceeded) {
// timeout reached for running this filter
continue
}
if ov, ok := err.(*Override); ok {
if ov.Conn != nil {
if t, ok := ov.Conn.(*tls.Conn); ok {
// keep this tls connection nearby
tlsconn = t
}
// perform override
cw = &Conn{
conn: ov.Conn,
l: ov.Conn.LocalAddr(),
r: ov.Conn.RemoteAddr(),
}
}
if ov.Protocol != "" {
negociatedProtocol = ov.Protocol
}
continue
}
// For now we ignore all filter errors
if r.Logger != nil {
r.Logger.Printf("filter error on new connection: %s", err)
}
cw.Close()
return
}
}
cw.SetReadDeadline(time.Time{}) // disable any timeout
var final net.Conn
final = cw
if !cw.isUsed() {
// skip cw
final = cw.conn
}
if tlsconn != nil && negociatedProtocol == "" {
// special case: this is a tls socket. Check NegotiatedProtocol
negociatedProtocol = tlsconn.ConnectionState().NegotiatedProtocol
}
if negociatedProtocol != "" {
// grab lock
r.protoLk.RLock()
v, ok := r.proto[negociatedProtocol]
r.protoLk.RUnlock()
if ok {
// send value
v.queue <- &queuePoint{c: final, e: nil}
return
}
}
r.queue <- queuePoint{c: final}
}
// Close() closes the socket.
func (r *Listener) Close() error {
r.portsLk.Lock()
defer r.portsLk.Unlock()
for n, port := range r.ports {
if err := port.Close(); err != nil {
r.ports = r.ports[n:] // drop any port that was successfully closed
return err
}
}
r.ports = nil
return nil
}
// Addr returns the address the socket is currently listening on, or nil for
// null listeners.
func (r *Listener) Addr() net.Addr {
return r.addr
}
func (r *Listener) listenLoop(port net.Listener, filterOverride []Filter) {
var tempDelay time.Duration // how long to sleep on accept failure
for {
c, err := port.Accept()
if err != nil {
// check for temporary error
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
continue
}
// send error & close
r.queue <- queuePoint{e: err}
close(r.queue)
return
} else {
// enable tcp keepalive
if kc, ok := c.(tcpKeepaliveConn); ok {
kc.SetKeepAlive(true)
kc.SetKeepAlivePeriod(3 * time.Minute)
}
r.HandleConn(c, filterOverride)
}
}
}
// PushConn allows pushing an existing connection to the queue as if it had
// just been accepted by the server. No auto-detection will be performed.
func (r *Listener) PushConn(c net.Conn) {
r.queue <- queuePoint{c: c}
}
// HandleConn will run detection on a given incoming connection and attempt to
// find if it should parse any kind of PROXY headers, or TLS handshake/etc.
func (r *Listener) HandleConn(c net.Conn, filterOverride []Filter) {
r.thCntLock.Lock()
if r.thCnt >= r.thMax {
// out of luck
r.thCntLock.Unlock()
c.Close()
return
}
r.thCnt += 1
r.thCntLock.Unlock()
go r.processFilters(c, filterOverride)
}
func (p *Listener) String() string {
return p.addr.String()
}