-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
server.go
370 lines (333 loc) · 10.2 KB
/
server.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package layer4
import (
"bytes"
"errors"
"fmt"
"io"
"net"
"os"
"sync"
"sync/atomic"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
)
const MatchingTimeoutDefault = 3 * time.Second
// Server represents a Caddy layer4 server.
type Server struct {
// The network address to bind to. Any Caddy network address
// is an acceptable value:
// https://caddyserver.com/docs/conventions#network-addresses
Listen []string `json:"listen,omitempty"`
// Routes express composable logic for handling byte streams.
Routes RouteList `json:"routes,omitempty"`
// Maximum time connections have to complete the matching phase (the first terminal handler is matched). Default: 3s.
MatchingTimeout caddy.Duration `json:"matching_timeout,omitempty"`
logger *zap.Logger
listenAddrs []caddy.NetworkAddress
compiledRoute Handler
}
// Provision sets up the server.
func (s *Server) Provision(ctx caddy.Context, logger *zap.Logger) error {
s.logger = logger
if s.MatchingTimeout <= 0 {
s.MatchingTimeout = caddy.Duration(MatchingTimeoutDefault)
}
repl := caddy.NewReplacer()
for i, address := range s.Listen {
address = repl.ReplaceAll(address, "")
addr, err := caddy.ParseNetworkAddress(address)
if err != nil {
return fmt.Errorf("parsing listener address '%s' in position %d: %v", address, i, err)
}
s.listenAddrs = append(s.listenAddrs, addr)
}
err := s.Routes.Provision(ctx)
if err != nil {
return err
}
s.compiledRoute = s.Routes.Compile(s.logger, time.Duration(s.MatchingTimeout), nopHandler{})
return nil
}
func (s *Server) serve(ln net.Listener) error {
for {
conn, err := ln.Accept()
var nerr net.Error
if errors.As(err, &nerr) && nerr.Timeout() {
s.logger.Error("timeout accepting connection", zap.Error(err))
continue
}
if err != nil {
return err
}
go s.handle(conn)
}
}
func (s *Server) servePacket(pc net.PacketConn) error {
// Spawn a goroutine whose only job is to consume packets from the socket
// and send to the packets channel.
packets := make(chan packet, 10)
go func(packets chan packet) {
for {
buf := udpBufPool.Get().([]byte)
n, addr, err := pc.ReadFrom(buf)
if err != nil {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
continue
}
packets <- packet{err: err}
return
}
packets <- packet{
pooledBuf: buf,
n: n,
addr: addr,
}
}
}(packets)
// udpConns tracks active packetConns by downstream address:port. They will
// be removed from this map after being closed.
udpConns := make(map[string]*packetConn)
// closeCh is used to receive notifications of socket closures from
// packetConn, which allows us to remove stale connections (whose
// proxy handlers have completed) from the udpConns map.
closeCh := make(chan string, 10)
for {
select {
case addr := <-closeCh:
// UDP connection is closed (either implicitly through timeout or by
// explicit call to Close()).
delete(udpConns, addr)
case pkt := <-packets:
if pkt.err != nil {
return pkt.err
}
conn, ok := udpConns[pkt.addr.String()]
if !ok {
// No existing proxy handler is running for this downstream.
// Create one now.
conn = &packetConn{
PacketConn: pc,
readCh: make(chan *packet, 5),
addr: pkt.addr,
closeCh: closeCh,
}
udpConns[pkt.addr.String()] = conn
go func(conn *packetConn) {
s.handle(conn)
// It might seem cleaner to send to closeCh here rather than
// in packetConn, but doing it earlier in packetConn closes
// the gap between the proxy handler shutting down and new
// packets coming in from the same downstream. Should that
// happen, we'll just spin up a new handler concurrent to
// the old one shutting down.
}(conn)
}
conn.readCh <- &pkt
}
}
}
func (s *Server) handle(conn net.Conn) {
defer func() { _ = conn.Close() }()
buf := bufPool.Get().([]byte)
buf = buf[:0]
defer bufPool.Put(buf)
cx := WrapConnection(conn, buf, s.logger)
start := time.Now()
err := s.compiledRoute.Handle(cx)
duration := time.Since(start)
if err != nil {
s.logger.Error("handling connection", zap.String("remote", cx.RemoteAddr().String()), zap.Error(err))
}
s.logger.Debug("connection stats",
zap.String("remote", cx.RemoteAddr().String()),
zap.Uint64("read", cx.bytesRead),
zap.Uint64("written", cx.bytesWritten),
zap.Duration("duration", duration),
)
}
// UnmarshalCaddyfile sets up the Server from Caddyfile tokens. Syntax:
//
// <address:port> [<address:port>] {
// matching_timeout <duration>
// @a <matcher> [<matcher_args>]
// @b {
// <matcher> [<matcher_args>]
// <matcher> [<matcher_args>]
// }
// route @a @b {
// <handler> [<handler_args>]
// }
// @c <matcher> {
// <matcher_option> [<matcher_option_args>]
// }
// route @c {
// <handler> [<handler_args>]
// <handler> {
// <handler_option> [<handler_option_args>]
// }
// }
// }
func (s *Server) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Wrapper name and all same-line options are treated as network addresses
for ok := true; ok; ok = d.NextArg() {
s.Listen = append(s.Listen, d.Val())
}
if err := ParseCaddyfileNestedRoutes(d, &s.Routes, &s.MatchingTimeout); err != nil {
return err
}
return nil
}
type packet struct {
// The underlying bytes slice that was gotten from udpBufPool. It's up to
// packetConn to return it to udpBufPool once it's consumed.
pooledBuf []byte
// Number of bytes read from socket
n int
// Error that occurred while reading from socket
err error
// Address of downstream
addr net.Addr
}
type packetConn struct {
net.PacketConn
addr net.Addr
readCh chan *packet
closeCh chan string
// If not nil, then the previous Read() call didn't consume all the data
// from the buffer, and this packet will be reused in the next Read()
// without waiting for readCh.
lastPacket *packet
lastBuf *bytes.Reader
// stores time.Time as Unix as Read maybe called concurrently with SetReadDeadline
deadline atomic.Int64
deadlineTimer *time.Timer
idleTimer *time.Timer
}
// SetReadDeadline sets the deadline to wait for data from the underlying net.PacketConn.
func (pc *packetConn) SetReadDeadline(t time.Time) error {
pc.deadline.Store(t.Unix())
if pc.deadlineTimer != nil {
pc.deadlineTimer.Reset(time.Until(t))
} else {
pc.deadlineTimer = time.NewTimer(time.Until(t))
}
return nil
}
// TODO: idle timeout should be configurable per server
const udpAssociationIdleTimeout = 30 * time.Second
func isDeadlineExceeded(t time.Time) bool {
return !t.IsZero() && t.Before(time.Now())
}
func (pc *packetConn) Read(b []byte) (n int, err error) {
if pc.lastPacket != nil {
// There is a partial buffer to continue reading from the previous
// packet.
n, err = pc.lastBuf.Read(b)
if pc.lastBuf.Len() == 0 {
udpBufPool.Put(pc.lastPacket.pooledBuf)
pc.lastPacket = nil
pc.lastBuf = nil
}
return
}
// check deadline
if isDeadlineExceeded(time.Unix(pc.deadline.Load(), 0)) {
return 0, os.ErrDeadlineExceeded
}
// set or refresh idle timeout
if pc.idleTimer == nil {
pc.idleTimer = time.NewTimer(udpAssociationIdleTimeout)
} else {
pc.idleTimer.Reset(udpAssociationIdleTimeout)
}
var done bool
for !done {
select {
case pkt := <-pc.readCh:
if pkt == nil {
// Channel is closed. Return EOF below.
done = true
break
}
buf := bytes.NewReader(pkt.pooledBuf[:pkt.n])
n, err = buf.Read(b)
if buf.Len() == 0 {
// Buffer fully consumed, release it.
udpBufPool.Put(pkt.pooledBuf)
} else {
// Buffer only partially consumed. Keep track of it for
// next Read() call.
pc.lastPacket = pkt
pc.lastBuf = buf
}
return
case <-pc.deadlineTimer.C:
// deadline may change during the wait, recheck
if isDeadlineExceeded(time.Unix(pc.deadline.Load(), 0)) {
return 0, os.ErrDeadlineExceeded
}
// next loop will run. Don't call Read as that will reset the idle timer.
case <-pc.idleTimer.C:
done = true
break
}
}
// Idle timeout simulates socket closure.
//
// Although Close() also does this, we inform the server loop early about
// the closure to ensure that if any new packets are received from this
// connection in the meantime, a new handler will be started.
pc.closeCh <- pc.addr.String()
// Returning EOF here ensures that io.Copy() waiting on the downstream for
// reads will terminate.
return 0, io.EOF
}
func (pc *packetConn) Write(b []byte) (n int, err error) {
return pc.PacketConn.WriteTo(b, pc.addr)
}
func (pc *packetConn) Close() error {
if pc.lastPacket != nil {
udpBufPool.Put(pc.lastPacket.pooledBuf)
pc.lastPacket = nil
}
// This will abort any active Read() from another goroutine and return EOF
close(pc.readCh)
// Drain pending packets to ensure we release buffers back to the pool
for pkt := range pc.readCh {
udpBufPool.Put(pkt.pooledBuf)
}
// We may have already done this earlier in Read(), but just in case
// Read() wasn't being called, (re-)notify server loop we're closed.
pc.closeCh <- pc.addr.String()
// We don't call net.PacketConn.Close() here as we would stop the UDP
// server.
return nil
}
func (pc *packetConn) RemoteAddr() net.Addr { return pc.addr }
var udpBufPool = sync.Pool{
New: func() interface{} {
// Buffers need to be as large as the largest datagram we'll consume, because
// ReadFrom() can't resume partial reads. (This is standard for UDP
// sockets on *nix.) So our buffer sizes are 9000 bytes to accommodate
// networks with jumbo frames. See also https://github.com/golang/go/issues/18056
return make([]byte, 9000)
},
}
// Interface guard
var _ caddyfile.Unmarshaler = (*Server)(nil)