forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
815 lines (667 loc) · 21.5 KB
/
builder.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
package api
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"golang.org/x/crypto/acme/autocert"
"www.velocidex.com/golang/velociraptor/api/authenticators"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/server"
"www.velocidex.com/golang/velociraptor/services"
_ "www.velocidex.com/golang/velociraptor/result_sets/timed"
)
// Builder builds a new GUI and Frontend server based on configuration
// options. We support several modes:
// 1. Autocert mode uses Let's Encrypt for SSL certificate provisioning.
// 2. Otherwise Self signed SSL is used for both Frontend and GUI.
// 3. If the GUI and Frontend are on the same port, we build a single
// unified server but otherwise we build two separate servers.
//
// If `Frontend.use_plain_http` is set, we bring the frontend up with
// plain HTTP server. This is useful to SSL offload to a reverse proxy
// like nginx. NOTE: you will need to key nginx with Velociraptor's
// self signed certificates or a proper cert - remember to adjust the
// client's `use_self_signed_ssl` parameters as appropriate.
//
// If you are using a reverse proxy in front of Velociraptor make sure
// you disable buffering. With nginx the setting is
// `proxy_request_buffering off`
// http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_request_buffering
type Builder struct {
config_obj *config_proto.Config
server_obj *server.Server
GUIPort, FrontendPort uint32
AutocertCertCache string
}
func (self *Builder) StartServer(ctx context.Context, wg *sync.WaitGroup) error {
// Always start the prometheus monitoring service
err := StartMonitoringService(ctx, wg, self.config_obj)
if err != nil {
return err
}
// Start in autocert mode, only put the GUI behind autocert if the
// GUI port is 443.
if self.AutocertCertCache != "" && self.config_obj.GUI != nil &&
self.config_obj.GUI.BindPort == 443 {
return self.WithAutocertGUI(ctx, wg)
}
// Start in autocert mode, but only sign the frontend.
if self.AutocertCertCache != "" {
return self.withAutoCertFrontendSelfSignedGUI(
ctx, wg, self.config_obj, self.server_obj)
}
// All services are sharing the same port.
if self.GUIPort == self.FrontendPort {
return startSharedSelfSignedFrontend(ctx, wg, self.config_obj, self.server_obj)
}
return startSelfSignedFrontend(ctx, wg, self.config_obj, self.server_obj)
}
func NewServerBuilder(ctx context.Context,
config_obj *config_proto.Config,
wg *sync.WaitGroup) (*Builder, error) {
result := &Builder{config_obj: config_obj}
// Create a new server
server_obj, err := server.NewServer(ctx, config_obj, wg)
if err != nil {
return nil, err
}
result.server_obj = server_obj
// Fill in the usual defaults.
result.AutocertCertCache = config_obj.AutocertCertCache
result.GUIPort = config_obj.GUI.BindPort
result.FrontendPort = config_obj.Frontend.BindPort
return result, nil
}
func (self *Builder) Close() {
if self.server_obj != nil {
self.server_obj.Close()
}
}
func (self *Builder) WithAPIServer(ctx context.Context, wg *sync.WaitGroup) error {
return startAPIServer(ctx, wg, self.config_obj, self.server_obj)
}
func (self *Builder) withAutoCertFrontendSelfSignedGUI(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server) error {
if self.config_obj.Frontend == nil || self.config_obj.GUI == nil {
return errors.New("Frontend not configured")
}
logger := logging.GetLogger(config_obj, &logging.GUIComponent)
logger.Info("Autocert is enabled but GUI port is not 443, starting Frontend with autocert and GUI with self signed.")
if config_obj.Services.GuiServer && config_obj.GUI != nil {
mux := http.NewServeMux()
router, err := PrepareGUIMux(ctx, config_obj, server_obj, mux)
if err != nil {
return err
}
// Start the GUI separately on a different port.
if config_obj.GUI.UsePlainHttp {
err = StartHTTPGUI(ctx, wg, config_obj, router)
} else {
err = StartSelfSignedGUI(ctx, wg, config_obj, router)
}
if err != nil {
return err
}
}
if !config_obj.Services.FrontendServer {
return nil
}
// Launch a server for the frontend.
mux := http.NewServeMux()
err := server.PrepareFrontendMux(
config_obj, server_obj, mux)
if err != nil {
return err
}
return StartFrontendWithAutocert(ctx, wg,
self.config_obj, self.server_obj, mux)
}
// When the GUI and Frontend share the same port we start them with
// the same server.
func (self *Builder) WithAutocertGUI(
ctx context.Context,
wg *sync.WaitGroup) error {
if self.config_obj.Frontend == nil || self.config_obj.GUI == nil {
return errors.New("Frontend not configured")
}
mux := http.NewServeMux()
if self.config_obj.Services.FrontendServer {
err := server.PrepareFrontendMux(self.config_obj, self.server_obj, mux)
if err != nil {
return err
}
}
router, err := PrepareGUIMux(ctx, self.config_obj, self.server_obj, mux)
if err != nil {
return err
}
// Start comms over https.
return StartFrontendWithAutocert(ctx, wg,
self.config_obj, self.server_obj, router)
}
// When the GUI and Frontend share the same port we start them with
// the same server.
func startSharedSelfSignedFrontend(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server) error {
mux := http.NewServeMux()
if config_obj.Frontend == nil || config_obj.GUI == nil {
return errors.New("Frontend not configured")
}
if config_obj.Services.FrontendServer {
err := server.PrepareFrontendMux(config_obj, server_obj, mux)
if err != nil {
return err
}
}
router, err := PrepareGUIMux(ctx, config_obj, server_obj, mux)
if err != nil {
return err
}
// Combine both frontend and GUI on HTTP server.
if config_obj.GUI.UsePlainHttp && config_obj.Frontend.UsePlainHttp {
server_obj.Info("Frontend and GUI both share port with plain HTTP %v",
config_obj.Frontend.BindPort)
return StartFrontendPlainHttp(
ctx, wg, config_obj, server_obj, mux)
}
server_obj.Info("Frontend and GUI both share port %v",
config_obj.Frontend.BindPort)
auther, err := authenticators.NewAuthenticator(config_obj)
if err != nil {
return err
}
if config_obj.Frontend.RequireClientCertificates != auther.RequireClientCerts() {
return errors.New(
"When using configurations that place the Frontend and GUI on the same port and requiring mTLS client certificates, then the GUI must also use the client certificate authenticator. Either split the frotnend and GUI on different ports or use the ClientCertificate authenticator.")
}
if config_obj.Frontend.RequireClientCertificates {
server_obj.Info("Frontend and GUI will both require mTLS client side certificates!")
}
return StartFrontendHttps(ctx, wg, config_obj, server_obj, router)
}
// Start the Frontend and GUI on different ports using different
// server objects.
func startSelfSignedFrontend(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server) error {
if config_obj.Services == nil {
return errors.New("Frontend not configured")
}
// Launch a new server for the GUI.
if config_obj.Services.GuiServer {
mux := http.NewServeMux()
router, err := PrepareGUIMux(ctx, config_obj, server_obj, mux)
if err != nil {
return err
}
// Start the GUI separately on a different port.
if config_obj.GUI.UsePlainHttp {
err = StartHTTPGUI(ctx, wg, config_obj, router)
} else {
err = StartSelfSignedGUI(ctx, wg, config_obj, router)
}
if err != nil {
return err
}
}
if !config_obj.Services.FrontendServer {
return nil
}
// Launch a server for the frontend.
mux := http.NewServeMux()
server.PrepareFrontendMux(
config_obj, server_obj, mux)
if config_obj.Frontend.UsePlainHttp {
return StartFrontendPlainHttp(
ctx, wg, config_obj, server_obj, mux)
}
// Start comms over https.
return StartFrontendHttps(ctx, wg,
config_obj, server_obj, mux)
}
func getCertificates(config_obj *config_proto.Config) ([]tls.Certificate, error) {
// If we need to read TLS certs from a file then do it now.
if config_obj.Frontend.TlsCertificateFilename != "" {
cert, err := tls.LoadX509KeyPair(
config_obj.Frontend.TlsCertificateFilename,
config_obj.Frontend.TlsPrivateKeyFilename)
if err != nil {
return nil, err
}
return []tls.Certificate{cert}, nil
}
cert, err := tls.X509KeyPair(
[]byte(config_obj.Frontend.Certificate),
[]byte(config_obj.Frontend.PrivateKey))
if err != nil {
return nil, err
}
return []tls.Certificate{cert}, nil
}
// Starts the frontend over HTTPS.
func StartFrontendHttps(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server,
router http.Handler) error {
if config_obj.Frontend == nil {
return errors.New("Frontend server not configured")
}
tls_config := &tls.Config{}
err := getTLSConfig(config_obj, tls_config)
if err != nil {
return err
}
if config_obj.Frontend.RequireClientCertificates {
err = addClientCerts(config_obj, tls_config)
if err != nil {
return err
}
server_obj.Info("Frontend will require mTLS client side certificates!")
}
listenAddr := fmt.Sprintf(
"%s:%d",
config_obj.Frontend.BindAddress,
config_obj.Frontend.BindPort)
server := &http.Server{
Addr: listenAddr,
Handler: router,
ErrorLog: logging.NewPlainLogger(config_obj, &logging.FrontendComponent),
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
ReadTimeout: 500 * time.Second,
WriteTimeout: 900 * time.Second,
IdleTimeout: 150 * time.Second,
TLSConfig: tls_config,
}
wg.Add(1)
go func() {
defer wg.Done()
server_obj.Info("Frontend is ready to handle client TLS requests at <green>https://%s:%d/",
get_hostname(config_obj.Frontend.Hostname, config_obj.Frontend.BindAddress),
config_obj.Frontend.BindPort)
atomic.StoreInt32(&server_obj.Healthy, 1)
listener, err, closer := server_obj.NewLoadSheddingListener(server.Addr)
if err != nil {
server_obj.Error("Frontend server: Can not listen on %v: %v",
server.Addr, err)
return
}
defer closer()
err = server.ServeTLS(listener, "", "")
if err != nil && err != http.ErrServerClosed {
server_obj.Error("Frontend server error %v", err)
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
server_obj.Info("<red>Shutting down</> frontend")
atomic.StoreInt32(&server_obj.Healthy, 0)
time_ctx, cancel := context.WithTimeout(
context.Background(), 10*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
err := server.Shutdown(time_ctx)
if err != nil {
server_obj.Error("Frontend server error during shutdown %v", err)
}
}()
return nil
}
// Starts the frontend over HTTPS.
func StartFrontendPlainHttp(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server,
router http.Handler) error {
if config_obj.Frontend == nil {
return errors.New("Frontend server not configured")
}
listenAddr := fmt.Sprintf(
"%s:%d",
config_obj.Frontend.BindAddress,
config_obj.Frontend.BindPort)
server := &http.Server{
Addr: listenAddr,
Handler: router,
ErrorLog: logging.NewPlainLogger(config_obj, &logging.FrontendComponent),
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
ReadTimeout: 500 * time.Second,
WriteTimeout: 900 * time.Second,
IdleTimeout: 300 * time.Second,
}
wg.Add(1)
go func() {
defer wg.Done()
server_obj.Info("Frontend is ready to handle requests at <green>http://%s:%d/",
get_hostname(config_obj.Frontend.Hostname, config_obj.Frontend.BindAddress),
config_obj.Frontend.BindPort)
atomic.StoreInt32(&server_obj.Healthy, 1)
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
server_obj.Error("Frontend server error %v", err)
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
server_obj.Info("<red>Shutting down</> frontend")
atomic.StoreInt32(&server_obj.Healthy, 0)
server.SetKeepAlivesEnabled(false)
_ = server.Shutdown(ctx)
}()
return nil
}
// Starts both Frontend and GUI on the same server. This is used in
// Autocert configuration.
func StartFrontendWithAutocert(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config,
server_obj *server.Server,
mux http.Handler) error {
if config_obj.Frontend == nil {
return errors.New("Frontend server not configured")
}
logger := logging.Manager.GetLogger(config_obj, &logging.GUIComponent)
// Autocert directory must be unique since it is usually kept in
// shared storage.
cache_dir := config_obj.AutocertCertCache
if config_obj.Frontend.IsMinion {
cache_dir = filepath.Join(
cache_dir, services.GetNodeName(config_obj.Frontend))
err := os.MkdirAll(cache_dir, 0700)
if err != nil {
return err
}
}
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config_obj.Frontend.Hostname),
Cache: autocert.DirCache(cache_dir),
}
tls_config := &tls.Config{}
err := getTLSConfig(config_obj, tls_config)
if err != nil {
return err
}
auther, err := authenticators.NewAuthenticator(config_obj)
if err != nil {
return err
}
// The frontend can not work with client certs required, so if we
// are in autocert mode we need either both frontend and gui to be
// configured with client cert or neither.
if config_obj.Frontend.RequireClientCertificates != auther.RequireClientCerts() {
return errors.New(
"When using configurations that place the Frontend and GUI on the same port and requiring mTLS client certificates, then the GUI must also use the client certificate authenticator. Either split the Frotnend and GUI on different ports or use the ClientCertificate authenticator.")
}
if auther.RequireClientCerts() {
err = addClientCerts(config_obj, tls_config)
if err != nil {
return err
}
server_obj.Info("Frontend and GUI will require mTLS client side certificates!")
}
// Autocert selects its own certificates by itself
// https://cs.opensource.google/go/x/crypto/+/refs/tags/v0.5.0:acme/autocert/autocert.go;l=227
cert_manager_config := certManager.TLSConfig()
tls_config.GetCertificate = cert_manager_config.GetCertificate
tls_config.NextProtos = cert_manager_config.NextProtos
tls_config.Certificates = nil
server := &http.Server{
// ACME protocol requires TLS be served over port 443.
Addr: ":https",
Handler: mux,
ErrorLog: logging.NewPlainLogger(config_obj, &logging.FrontendComponent),
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
ReadTimeout: 500 * time.Second,
WriteTimeout: 900 * time.Second,
IdleTimeout: 300 * time.Second,
TLSConfig: tls_config,
}
// We must have port 80 open to serve the HTTP 01 challenge.
go func() {
err := http.ListenAndServe(":http", certManager.HTTPHandler(nil))
if err != nil {
logger := logging.Manager.GetLogger(config_obj, &logging.GUIComponent)
logger.Error("Failed to bind to http server: %v", err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
server_obj.Info("Frontend is ready to handle client requests at <green>https://%s/",
get_hostname(config_obj.Frontend.Hostname, config_obj.Frontend.BindAddress))
atomic.StoreInt32(&server_obj.Healthy, 1)
// TODO: When running in autocert mode we use the same
// port for the GUI and clients. If we load shed the
// clients we will also load shed the GUI... Does this
// makes sense?
listener, err, closer := server_obj.NewLoadSheddingListener(server.Addr)
if err != nil {
server_obj.Error("Frontend server: Can not listen on %v: %v",
server.Addr, err)
return
}
defer closer()
err = server.ServeTLS(listener, "", "")
if err != nil && err != http.ErrServerClosed {
server_obj.Error("Frontend server error: %v", err)
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
server_obj.Info("<red>Stopping Frontend Server")
atomic.StoreInt32(&server_obj.Healthy, 0)
timeout_ctx, cancel := context.WithTimeout(
context.Background(), 10*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
err := server.Shutdown(timeout_ctx)
if err != nil {
logger.Error("Frontend shutdown error: %v", err)
}
server_obj.Info("Shutdown frontend")
}()
return nil
}
func StartHTTPGUI(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config, mux http.Handler) error {
if config_obj.GUI == nil {
return errors.New("GUI server not configured")
}
logger := logging.Manager.GetLogger(config_obj, &logging.GUIComponent)
listenAddr := fmt.Sprintf("%s:%d",
config_obj.GUI.BindAddress,
config_obj.GUI.BindPort)
server := &http.Server{
Addr: listenAddr,
Handler: mux,
ErrorLog: logging.NewPlainLogger(config_obj, &logging.FrontendComponent),
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
ReadTimeout: 500 * time.Second,
WriteTimeout: 900 * time.Second,
IdleTimeout: 15 * time.Second,
}
logger.Info("GUI is ready to handle HTTP requests on <green>http://%s:%d/",
get_hostname(config_obj.Frontend.Hostname, config_obj.GUI.BindAddress),
config_obj.GUI.BindPort)
wg.Add(1)
go func() {
defer wg.Done()
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
logger.Error("GUI Server error: %v", err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
logger.Info("<red>Stopping GUI Server")
timeout_ctx, cancel := context.WithTimeout(
context.Background(), 10*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
err := server.Shutdown(timeout_ctx)
if err != nil {
logger.Error("GUI shutdown error: %v", err)
}
}()
return nil
}
func StartSelfSignedGUI(
ctx context.Context,
wg *sync.WaitGroup,
config_obj *config_proto.Config, mux http.Handler) error {
logger := logging.Manager.GetLogger(config_obj, &logging.GUIComponent)
if config_obj.GUI == nil {
return errors.New("GUI server not configured")
}
tls_config := &tls.Config{}
err := getTLSConfig(config_obj, tls_config)
if err != nil {
return err
}
// If we are using an authenticator that requires client side
// certs, add the required TLS config here.
auther, err := authenticators.NewAuthenticator(config_obj)
if err != nil {
return err
}
if auther.RequireClientCerts() {
err = addClientCerts(config_obj, tls_config)
if err != nil {
return err
}
}
listenAddr := fmt.Sprintf("%s:%d",
config_obj.GUI.BindAddress,
config_obj.GUI.BindPort)
server := &http.Server{
Addr: listenAddr,
Handler: mux,
ErrorLog: logging.NewPlainLogger(config_obj, &logging.FrontendComponent),
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
ReadTimeout: 500 * time.Second,
WriteTimeout: 900 * time.Second,
IdleTimeout: 15 * time.Second,
TLSConfig: tls_config,
}
logger.Info("GUI is ready to handle TLS requests on <green>https://%s:%d/",
get_hostname(config_obj.Frontend.Hostname, config_obj.GUI.BindAddress),
config_obj.GUI.BindPort)
wg.Add(1)
go func() {
defer wg.Done()
err := server.ListenAndServeTLS("", "")
if err != nil && err != http.ErrServerClosed {
logger.Error("GUI Server error: %v", err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
logger.Info("<red>Stopping GUI Server")
timeout_ctx, cancel := context.WithTimeout(
context.Background(), 10*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
err := server.Shutdown(timeout_ctx)
if err != nil {
logger.Error("GUI shutdown error: %v", err)
}
}()
return nil
}
func get_hostname(fe_hostname, bind_addr string) string {
if bind_addr == "0.0.0.0" || bind_addr == "" || bind_addr == "::" {
return fe_hostname
}
return bind_addr
}
func addClientCerts(config_obj *config_proto.Config, in *tls.Config) error {
// Require the browser to use client certificates
client_ca := x509.NewCertPool()
if config_obj.Client != nil {
client_ca.AppendCertsFromPEM([]byte(config_obj.Client.CaCertificate))
// Also trust any of our trusted root CAs.
if config_obj.Client.Crypto != nil &&
config_obj.Client.Crypto.RootCerts != "" {
if !client_ca.AppendCertsFromPEM(
[]byte(config_obj.Client.Crypto.RootCerts)) {
return errors.New(
"Unable to parse Crypto.root_certs in the config file.")
}
}
}
in.ClientAuth = tls.RequireAndVerifyClientCert
in.ClientCAs = client_ca
in.BuildNameToCertificate()
return nil
}
// Prepare a TLS config with correct cipher choices.
func getTLSConfig(config_obj *config_proto.Config, in *tls.Config) error {
certs, err := getCertificates(config_obj)
if err != nil {
return err
}
expected_clients := int64(20000)
if config_obj.Frontend != nil {
if config_obj.Frontend.Resources != nil &&
config_obj.Frontend.Resources.ExpectedClients > 0 {
expected_clients = config_obj.Frontend.Resources.ExpectedClients
}
}
in.Certificates = certs
// If the user requested it we loosen the TLS restrictions to
// accept default protocols.
if config_obj.Client != nil && config_obj.Client.Crypto != nil &&
config_obj.Client.Crypto.AllowWeakTlsServer {
return nil
}
in.MinVersion = tls.VersionTLS13
in.CurvePreferences = []tls.CurveID{
tls.CurveP521, tls.CurveP384, tls.CurveP256}
in.ClientSessionCache = tls.NewLRUClientSessionCache(int(expected_clients))
in.PreferServerCipherSuites = true
in.CipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
}
return nil
}