-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
437 lines (362 loc) · 10.1 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package tcpause
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
type handler func(l net.Listener)
// TLSConfig holds all config values related to TLS
type TLSConfig struct {
CaCert string `mapstructure:"ca-cert"`
Cert string
Key string
}
// ProxyConfig holds all config values related to the proxy server itself
type ProxyConfig struct {
Addr string
RejectClients bool `mapstructure:"reject-clients"`
RetryAfterInterval time.Duration `mapstructure:"retry-after-interval"`
BlockPollInterval time.Duration `mapstructure:"block-poll-interval"`
ClosePollInterval time.Duration `mapstructure:"close-poll-interval"`
TLS TLSConfig
}
// ControlConfig holds all config values related to the control server
type ControlConfig struct {
Addr string
}
// UpstreamConfig holds all config values related to the upstream server
type UpstreamConfig struct {
Addr string
}
// Config holds all config values
type Config struct {
GracePeriod time.Duration `mapstructure:"grace-period"`
Proxy ProxyConfig
Control ControlConfig
Upstream UpstreamConfig
}
// server types
type server struct {
cfg Config
logger Logger
done chan struct{}
srv *http.Server
paused bool
tlsCfg *tls.Config
srvWg *sync.WaitGroup
clientWg *sync.WaitGroup
errChan chan error
}
// Server represents a server instance
type Server interface {
Start() error
Stop() error
Errors() <-chan error
}
// New creates a new server instance
func New(cfg Config, logger Logger) (Server, error) {
var tlsCfg *tls.Config
var err error
if cfg.Proxy.TLS.CaCert != "" && cfg.Proxy.TLS.Cert != "" && cfg.Proxy.TLS.Key != "" {
tlsCfg, err = createTLSConfig(cfg.Proxy.TLS)
if err != nil {
return nil, err
}
}
return &server{
cfg: cfg,
logger: logger,
done: make(chan struct{}, 1),
paused: false,
tlsCfg: tlsCfg,
srvWg: new(sync.WaitGroup),
clientWg: new(sync.WaitGroup),
errChan: make(chan error, 100),
}, nil
}
// Start starts up the server
func (s *server) Start() error {
s.srvWg.Add(2)
err := s.setup("proxy", s.cfg.Proxy.Addr, s.handleProxy)
if err != nil {
return err
}
return s.setup("control", s.cfg.Control.Addr, s.handleControl)
}
// Stop gracefully shuts down the server
func (s *server) Stop() error {
s.done <- struct{}{}
ctx, cancel := context.WithTimeout(context.Background(), s.cfg.GracePeriod)
err := s.srv.Shutdown(ctx)
cancel()
s.srvWg.Wait()
return err
}
// Errors returns the error channel
func (s *server) Errors() <-chan error {
return s.errChan
}
// createTLSConfig loads the ca certificate and creates a server TLS config out of it
func createTLSConfig(tlsCfg TLSConfig) (cfg *tls.Config, err error) {
// load CA cert
caCertPEM, err := ioutil.ReadFile(tlsCfg.CaCert)
if err != nil {
return
}
cas := x509.NewCertPool()
ok := cas.AppendCertsFromPEM(caCertPEM)
if !ok {
err = fmt.Errorf("could not add CA certificate %s", tlsCfg.CaCert)
return
}
// load server key pair
certPEM, err := ioutil.ReadFile(tlsCfg.Cert)
if err != nil {
return
}
keyPEM, err := ioutil.ReadFile(tlsCfg.Key)
if err != nil {
return
}
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return
}
return &tls.Config{
RootCAs: cas,
ClientCAs: cas,
Certificates: []tls.Certificate{cert},
ClientAuth: tls.VerifyClientCertIfGiven,
}, nil
}
// setup starts up a server with its own listener and handler function
func (s *server) setup(name, addr string, handler handler) error {
scheme, addr, err := parseAddr(addr)
if err != nil {
return err
}
name = strings.ToLower(name)
l, err := net.Listen(scheme, addr)
if err != nil {
return errors.Wrapf(err, "%s failed to listen", name)
}
s.logger.Info(fmt.Sprintf("%s listening on %s", strings.Title(name), l.Addr().String()))
go s.handleListener(l, addr, handler)
return nil
}
// handleListener handles a listener using the specified handler function
func (s *server) handleListener(l net.Listener, addr string, handler handler) {
defer s.srvWg.Done()
defer s.logger.Info(fmt.Sprintf("Listener %s shutdown", l.Addr().String()))
handler(l)
}
// handleControl handles all incoming connections to the proxy server
func (s *server) handleProxy(l net.Listener) {
var err error
for {
// do not accept any new connections when server is about to close
select {
case <-s.done:
if err = l.Close(); err != nil {
s.errChan <- errors.Wrap(err, "could not close proxy listener")
}
return
default:
}
// force next iteration of the loop after some time so that we can poll shutdown requests
switch l.(type) {
case *net.TCPListener:
err = l.(*net.TCPListener).SetDeadline(time.Now().Add(s.cfg.Proxy.ClosePollInterval))
case *net.UnixListener:
err = l.(*net.UnixListener).SetDeadline(time.Now().Add(s.cfg.Proxy.ClosePollInterval))
default:
err = nil
}
if err != nil {
s.errChan <- errors.Wrap(err, "could not set deadline on listener")
}
conn, err := l.Accept()
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
if err != nil {
s.errChan <- errors.Wrap(err, "could not accept connection")
continue
}
// handle client
s.clientWg.Add(1)
go s.handleConn(conn)
}
}
// handleControl handles all incoming connections to the control server
func (s *server) handleControl(l net.Listener) {
mux := http.NewServeMux()
mux.HandleFunc("/paused", s.handlePaused)
mux.HandleFunc("/paused/block-until-idle", s.handleBlockUntilIdle)
s.srv = &http.Server{
Handler: mux,
}
err := s.srv.Serve(l)
// server closed abnormally
if err != nil && err != http.ErrServerClosed {
err = errors.Wrap(err, "control failed")
s.errChan <- err
}
}
// handleConn handles one proxy connection
func (s *server) handleConn(conn net.Conn) {
defer func() {
s.clientWg.Done()
s.logger.Debug("Closing proxy connection")
err := conn.Close()
if err != nil {
s.errChan <- errors.Wrap(err, "could not close proxy connection")
}
}()
// handle the client accordingly when the upstream is paused
if s.paused {
if s.tlsCfg != nil && s.cfg.Proxy.RejectClients {
conn = s.rejectConn(conn)
return
}
s.blockConn()
}
// make a new connection to the upstream
scheme, addr, err := parseAddr(s.cfg.Upstream.Addr)
if err != nil {
s.errChan <- errors.Wrapf(err, "failed to connect to upstream %s", s.cfg.Upstream.Addr)
return
}
upstream, err := net.Dial(scheme, addr)
if err != nil {
s.errChan <- errors.Wrapf(err, "failed to connect to upstream %s", s.cfg.Upstream.Addr)
return
}
s.logger.Debug(fmt.Sprintf("Connected to upstream %s", s.cfg.Upstream.Addr))
// proxy the connection bidirectionally
errChan := make(chan error, 1)
go s.proxyConn(errChan, upstream, conn)
go s.proxyConn(errChan, conn, upstream)
<-errChan
err = upstream.Close()
if err != nil {
s.errChan <- errors.Wrap(err, "could not close upstream connection")
}
}
// proxyConn proxies a connection to another
func (s *server) proxyConn(errChan chan<- error, dst, src net.Conn) {
_, err := io.Copy(dst, src)
errChan <- err
}
// rejectConn actually accepts the TLS connection and sends proper http codes and headers
// so that the client knows when it can give it another try
func (s *server) rejectConn(conn net.Conn) net.Conn {
resp := fmt.Sprintf(
"HTTP/1.1 %d %s",
http.StatusServiceUnavailable,
http.StatusText(http.StatusServiceUnavailable),
)
retryAfter := s.cfg.Proxy.RetryAfterInterval
if retryAfter.Seconds() > 0 {
resp += fmt.Sprintf("\r\nRetry-After: %d", int(math.Ceil(s.cfg.Proxy.RetryAfterInterval.Seconds())))
}
resp += "\r\n\r\n"
// try to send response over TLS
tlsConn := tls.Server(conn, s.tlsCfg)
_, err := tlsConn.Write([]byte(resp))
// this is very ugly but in case the connection is in fact not TLS,
// we try to send the response again over the plain connection
// TODO: find a better solution to distinguish TLS from non-TLS
if err != nil {
_, err = conn.Write([]byte(resp))
if err != nil {
s.errChan <- errors.Wrap(err, "could not send response")
}
return tlsConn
}
return tlsConn
}
// blockConn blocks the request as long as the server is paused
func (s *server) blockConn() {
for s.paused {
time.Sleep(s.cfg.Proxy.BlockPollInterval)
}
}
// handlePaused handles un/pausing requests to upstream
func (s *server) handlePaused(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
s.getPausedStatus(w)
case http.MethodPut:
s.setPausedStatus(w, true)
case http.MethodDelete:
s.setPausedStatus(w, false)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
// getPausedStatus sends the current state to the client
func (s *server) getPausedStatus(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
var err error
if s.paused {
_, err = w.Write([]byte("{\"paused\": true}"))
} else {
_, err = w.Write([]byte("{\"paused\": false}"))
}
if err != nil {
s.errChan <- errors.Wrap(err, "control failed to write response")
}
}
// setPausedStatus sets the desired state
func (s *server) setPausedStatus(w http.ResponseWriter, paused bool) {
s.paused = paused
var code int
if paused {
code = http.StatusCreated
} else {
code = http.StatusNoContent
}
w.WriteHeader(code)
}
// parseAddr parses schema and host/path from a URI for use with net.Dial/Listen
func parseAddr(addr string) (s string, a string, err error) {
u, err := url.Parse(addr)
if err != nil {
return
}
s = u.Scheme
switch s {
case "unix":
a = u.Path
case "tcp":
a = u.Host
default:
err = fmt.Errorf("scheme %s not supported", s)
return
}
return
}
// handleBlockUntilIdle blocks for so long as there are unfinished requests running
// this allows someone to pause the server, then wait for all requests to finish
// and then do whatever is to be done on the upstream server
func (s *server) handleBlockUntilIdle(w http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
s.clientWg.Wait()
w.WriteHeader(http.StatusNoContent)
}