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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
package http
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/fcgi"
"net/url"
"strings"
"sync"
"github.com/hashicorp/go-multierror"
endure "github.com/spiral/endure/pkg/container"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/pkg/pool"
"github.com/spiral/roadrunner/v2/pkg/process"
"github.com/spiral/roadrunner/v2/pkg/worker"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/http/attributes"
httpConfig "github.com/spiral/roadrunner/v2/plugins/http/config"
"github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/spiral/roadrunner/v2/plugins/server"
"github.com/spiral/roadrunner/v2/plugins/status"
"github.com/spiral/roadrunner/v2/utils"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"golang.org/x/sys/cpu"
)
const (
// PluginName declares plugin name.
PluginName = "http"
// RR_HTTP env variable key (internal) if the HTTP presents
RR_MODE = "RR_MODE" //nolint:golint,stylecheck
// HTTPS_SCHEME
HTTPS_SCHEME = "https" //nolint:golint,stylecheck
)
// Middleware interface
type Middleware interface {
Middleware(f http.Handler) http.HandlerFunc
}
type middleware map[string]Middleware
// Plugin manages pool, http servers. The main http plugin structure
type Plugin struct {
sync.RWMutex
// plugins
server server.Server
log logger.Logger
// stdlog passed to the http/https/fcgi servers to log their internal messages
stdLog *log.Logger
cfg *httpConfig.HTTP `mapstructure:"http"`
// middlewares to chain
mdwr middleware
// Pool which attached to all servers
pool pool.Pool
// servers RR handler
handler *Handler
// servers
http *http.Server
https *http.Server
fcgi *http.Server
}
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (s *Plugin) Init(cfg config.Configurer, rrLogger logger.Logger, server server.Server) error {
const op = errors.Op("http_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(PluginName, &s.cfg)
if err != nil {
return errors.E(op, err)
}
err = s.cfg.InitDefaults()
if err != nil {
return errors.E(op, err)
}
// rr logger (via plugin)
s.log = rrLogger
// use time and date in UTC format
s.stdLog = log.New(logger.NewStdAdapter(s.log), "http_plugin: ", log.Ldate|log.Ltime|log.LUTC)
s.mdwr = make(map[string]Middleware)
if !s.cfg.EnableHTTP() && !s.cfg.EnableTLS() && !s.cfg.EnableFCGI() {
return errors.E(op, errors.Disabled)
}
// init if nil
if s.cfg.Env == nil {
s.cfg.Env = make(map[string]string)
}
s.cfg.Env[RR_MODE] = "http"
s.server = server
return nil
}
func (s *Plugin) logCallback(event interface{}) {
if ev, ok := event.(ResponseEvent); ok {
s.log.Debug(fmt.Sprintf("%d %s %s", ev.Response.Status, ev.Request.Method, ev.Request.URI),
"remote", ev.Request.RemoteAddr,
"elapsed", ev.Elapsed().String(),
)
}
}
// Serve serves the svc.
func (s *Plugin) Serve() chan error {
s.Lock()
defer s.Unlock()
const op = errors.Op("http_plugin_serve")
errCh := make(chan error, 2)
var err error
s.pool, err = s.server.NewWorkerPool(context.Background(), pool.Config{
Debug: s.cfg.Pool.Debug,
NumWorkers: s.cfg.Pool.NumWorkers,
MaxJobs: s.cfg.Pool.MaxJobs,
AllocateTimeout: s.cfg.Pool.AllocateTimeout,
DestroyTimeout: s.cfg.Pool.DestroyTimeout,
Supervisor: s.cfg.Pool.Supervisor,
}, s.cfg.Env, s.logCallback)
if err != nil {
errCh <- errors.E(op, err)
return errCh
}
s.handler, err = NewHandler(
s.cfg.MaxRequestSize,
*s.cfg.Uploads,
s.cfg.Cidrs,
s.pool,
)
if err != nil {
errCh <- errors.E(op, err)
return errCh
}
s.handler.AddListener(s.logCallback)
if s.cfg.EnableHTTP() {
if s.cfg.EnableH2C() {
s.http = &http.Server{Handler: h2c.NewHandler(s, &http2.Server{}), ErrorLog: s.stdLog}
} else {
s.http = &http.Server{Handler: s, ErrorLog: s.stdLog}
}
}
if s.cfg.EnableTLS() {
s.https = s.initSSL()
if s.cfg.SSLConfig.RootCA != "" {
err = s.appendRootCa()
if err != nil {
errCh <- errors.E(op, err)
return errCh
}
}
// if HTTP2Config not nil
if s.cfg.HTTP2Config != nil {
if err := s.initHTTP2(); err != nil {
errCh <- errors.E(op, err)
return errCh
}
}
}
if s.cfg.EnableFCGI() {
s.fcgi = &http.Server{Handler: s, ErrorLog: s.stdLog}
}
// start http, https and fcgi servers if requested in the config
go func() {
s.serveHTTP(errCh)
}()
go func() {
s.serveHTTPS(errCh)
}()
go func() {
s.serveFCGI(errCh)
}()
return errCh
}
func (s *Plugin) serveHTTP(errCh chan error) {
if s.http == nil {
return
}
const op = errors.Op("http_plugin_serve_http")
applyMiddlewares(s.http, s.mdwr, s.cfg.Middleware, s.log)
l, err := utils.CreateListener(s.cfg.Address)
if err != nil {
errCh <- errors.E(op, err)
return
}
err = s.http.Serve(l)
if err != nil && err != http.ErrServerClosed {
errCh <- errors.E(op, err)
return
}
}
func (s *Plugin) serveHTTPS(errCh chan error) {
if s.https == nil {
return
}
const op = errors.Op("http_plugin_serve_https")
applyMiddlewares(s.https, s.mdwr, s.cfg.Middleware, s.log)
l, err := utils.CreateListener(s.cfg.SSLConfig.Address)
if err != nil {
errCh <- errors.E(op, err)
return
}
err = s.https.ServeTLS(
l,
s.cfg.SSLConfig.Cert,
s.cfg.SSLConfig.Key,
)
if err != nil && err != http.ErrServerClosed {
errCh <- errors.E(op, err)
return
}
}
// serveFCGI starts FastCGI server.
func (s *Plugin) serveFCGI(errCh chan error) {
if s.fcgi == nil {
return
}
const op = errors.Op("http_plugin_serve_fcgi")
applyMiddlewares(s.fcgi, s.mdwr, s.cfg.Middleware, s.log)
l, err := utils.CreateListener(s.cfg.FCGIConfig.Address)
if err != nil {
errCh <- errors.E(op, err)
return
}
err = fcgi.Serve(l, s.fcgi.Handler)
if err != nil && err != http.ErrServerClosed {
errCh <- errors.E(op, err)
return
}
}
// Stop stops the http.
func (s *Plugin) Stop() error {
s.Lock()
defer s.Unlock()
var err error
if s.fcgi != nil {
err = s.fcgi.Shutdown(context.Background())
if err != nil && err != http.ErrServerClosed {
s.log.Error("error shutting down the fcgi server", "error", err)
// write error and try to stop other transport
err = multierror.Append(err)
}
}
if s.https != nil {
err = s.https.Shutdown(context.Background())
if err != nil && err != http.ErrServerClosed {
s.log.Error("error shutting down the https server", "error", err)
// write error and try to stop other transport
err = multierror.Append(err)
}
}
if s.http != nil {
err = s.http.Shutdown(context.Background())
if err != nil && err != http.ErrServerClosed {
s.log.Error("error shutting down the http server", "error", err)
// write error and try to stop other transport
err = multierror.Append(err)
}
}
s.pool.Destroy(context.Background())
return err
}
// ServeHTTP handles connection using set of middleware and pool PSR-7 server.
func (s *Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if headerContainsUpgrade(r) {
http.Error(w, "server does not support upgrade header", http.StatusInternalServerError)
return
}
if s.https != nil && r.TLS == nil && s.cfg.SSLConfig.Redirect {
s.redirect(w, r)
return
}
if s.https != nil && r.TLS != nil {
w.Header().Add("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
}
r = attributes.Init(r)
// protect the case, when user sendEvent Reset and we are replacing handler with pool
s.RLock()
s.handler.ServeHTTP(w, r)
s.RUnlock()
}
// Workers returns slice with the process states for the workers
func (s *Plugin) Workers() []process.State {
workers := s.pool.Workers()
ps := make([]process.State, 0, len(workers))
for i := 0; i < len(workers); i++ {
state, err := process.WorkerProcessState(workers[i])
if err != nil {
return nil
}
ps = append(ps, state)
}
return ps
}
// internal
func (s *Plugin) workers() []worker.BaseProcess {
return s.pool.Workers()
}
// Name returns endure.Named interface implementation
func (s *Plugin) Name() string {
return PluginName
}
// Reset destroys the old pool and replaces it with new one, waiting for old pool to die
func (s *Plugin) Reset() error {
s.Lock()
defer s.Unlock()
const op = errors.Op("http_plugin_reset")
s.log.Info("HTTP plugin got restart request. Restarting...")
s.pool.Destroy(context.Background())
s.pool = nil
var err error
s.pool, err = s.server.NewWorkerPool(context.Background(), pool.Config{
Debug: s.cfg.Pool.Debug,
NumWorkers: s.cfg.Pool.NumWorkers,
MaxJobs: s.cfg.Pool.MaxJobs,
AllocateTimeout: s.cfg.Pool.AllocateTimeout,
DestroyTimeout: s.cfg.Pool.DestroyTimeout,
Supervisor: s.cfg.Pool.Supervisor,
}, s.cfg.Env, s.logCallback)
if err != nil {
return errors.E(op, err)
}
s.log.Info("HTTP listeners successfully re-added")
s.log.Info("HTTP workers Pool successfully restarted")
s.handler, err = NewHandler(
s.cfg.MaxRequestSize,
*s.cfg.Uploads,
s.cfg.Cidrs,
s.pool,
)
if err != nil {
return errors.E(op, err)
}
s.log.Info("HTTP plugin successfully restarted")
return nil
}
// Collects collecting http middlewares
func (s *Plugin) Collects() []interface{} {
return []interface{}{
s.AddMiddleware,
}
}
// AddMiddleware is base requirement for the middleware (name and Middleware)
func (s *Plugin) AddMiddleware(name endure.Named, m Middleware) {
s.mdwr[name.Name()] = m
}
// Status return status of the particular plugin
func (s *Plugin) Status() status.Status {
workers := s.workers()
for i := 0; i < len(workers); i++ {
if workers[i].State().IsActive() {
return status.Status{
Code: http.StatusOK,
}
}
}
// if there are no workers, threat this as error
return status.Status{
Code: http.StatusServiceUnavailable,
}
}
// Ready return readiness status of the particular plugin
func (s *Plugin) Ready() status.Status {
workers := s.workers()
for i := 0; i < len(workers); i++ {
// If state of the worker is ready (at least 1)
// we assume, that plugin's worker pool is ready
if workers[i].State().Value() == worker.StateReady {
return status.Status{
Code: http.StatusOK,
}
}
}
// if there are no workers, threat this as no content error
return status.Status{
Code: http.StatusServiceUnavailable,
}
}
func (s *Plugin) redirect(w http.ResponseWriter, r *http.Request) {
target := &url.URL{
Scheme: HTTPS_SCHEME,
// host or host:port
Host: s.tlsAddr(r.Host, false),
Path: r.URL.Path,
RawQuery: r.URL.RawQuery,
}
http.Redirect(w, r, target.String(), http.StatusPermanentRedirect)
}
// https://golang.org/pkg/net/http/#Hijacker
//go:inline
func headerContainsUpgrade(r *http.Request) bool {
if _, ok := r.Header["Upgrade"]; ok {
return true
}
return false
}
// append RootCA to the https server TLS config
func (s *Plugin) appendRootCa() error {
const op = errors.Op("http_plugin_append_root_ca")
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil
}
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
CA, err := ioutil.ReadFile(s.cfg.SSLConfig.RootCA)
if err != nil {
return err
}
// should append our CA cert
ok := rootCAs.AppendCertsFromPEM(CA)
if !ok {
return errors.E(op, errors.Str("could not append Certs from PEM"))
}
// disable "G402 (CWE-295): TLS MinVersion too low. (Confidence: HIGH, Severity: HIGH)"
// #nosec G402
cfg := &tls.Config{
InsecureSkipVerify: false,
RootCAs: rootCAs,
}
s.http.TLSConfig = cfg
return nil
}
// Init https server
func (s *Plugin) initSSL() *http.Server {
var topCipherSuites []uint16
var defaultCipherSuitesTLS13 []uint16
hasGCMAsmAMD64 := cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
hasGCMAsmARM64 := cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
// Keep in sync with crypto/aes/cipher_s390x.go.
hasGCMAsmS390X := cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
hasGCMAsm := hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X
if hasGCMAsm {
// If AES-GCM hardware is provided then priorities AES-GCM
// cipher suites.
topCipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
}
defaultCipherSuitesTLS13 = []uint16{
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_CHACHA20_POLY1305_SHA256,
tls.TLS_AES_256_GCM_SHA384,
}
} else {
// Without AES-GCM hardware, we put the ChaCha20-Poly1305
// cipher suites first.
topCipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
}
defaultCipherSuitesTLS13 = []uint16{
tls.TLS_CHACHA20_POLY1305_SHA256,
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
}
}
DefaultCipherSuites := make([]uint16, 0, 22)
DefaultCipherSuites = append(DefaultCipherSuites, topCipherSuites...)
DefaultCipherSuites = append(DefaultCipherSuites, defaultCipherSuitesTLS13...)
sslServer := &http.Server{
Addr: s.tlsAddr(s.cfg.Address, true),
Handler: s,
ErrorLog: s.stdLog,
TLSConfig: &tls.Config{
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.CurveP384,
tls.CurveP521,
tls.X25519,
},
CipherSuites: DefaultCipherSuites,
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
},
}
return sslServer
}
// init http/2 server
func (s *Plugin) initHTTP2() error {
return http2.ConfigureServer(s.https, &http2.Server{
MaxConcurrentStreams: s.cfg.HTTP2Config.MaxConcurrentStreams,
})
}
// tlsAddr replaces listen or host port with port configured by SSLConfig config.
func (s *Plugin) tlsAddr(host string, forcePort bool) string {
// remove current forcePort first
host = strings.Split(host, ":")[0]
if forcePort || s.cfg.SSLConfig.Port != 443 {
host = fmt.Sprintf("%s:%v", host, s.cfg.SSLConfig.Port)
}
return host
}
func applyMiddlewares(server *http.Server, middlewares map[string]Middleware, order []string, log logger.Logger) {
if len(middlewares) == 0 {
return
}
for i := 0; i < len(order); i++ {
if mdwr, ok := middlewares[order[i]]; ok {
server.Handler = mdwr.Middleware(server.Handler)
} else {
log.Warn("requested middleware does not exist", "requested", order[i])
}
}
}
|