-
Notifications
You must be signed in to change notification settings - Fork 12
/
defaults.go
71 lines (59 loc) · 2.16 KB
/
defaults.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
package http
import (
"crypto/tls"
"net/http"
"time"
)
var (
// DefaultCurvePreferences defines the recommended elliptic curves for modern TLS
DefaultCurvePreferences = []tls.CurveID{
tls.CurveP256,
tls.X25519, // Go 1.8 only
}
// DefaultCipherSuites defines the recommended cipher suites for modern TLS
DefaultCipherSuites = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// Best disabled, as they don't provide Forward Secrecy,
// but might be necessary for some clients
// tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
}
// DefaultMinVersion defines the recommended minimum version to use for the TLS protocol (1.2)
DefaultMinVersion uint16 = tls.VersionTLS12
// DefaultReadTimeout sets the maximum time a client has to fully stream a request (5s)
DefaultReadTimeout = 5 * time.Second
// DefaultWriteTimeout sets the maximum amount of time a handler has to fully process a request (10s)
DefaultWriteTimeout = 10 * time.Second
// DefaultIdleTimeout sets the maximum amount of time a Keep-Alive connection can remain idle before
// being recycled (120s)
DefaultIdleTimeout = 120 * time.Second
)
// ServerWithDefaults patches a http.Server based on a best practice configuration
// from Cloudflare: https://blog.cloudflare.com/exposing-go-on-the-internet/
//
// You can override the defaults by mutating the Default* variables exposed
// by this package
func ServerWithDefaults(srv *http.Server) *http.Server {
if srv.TLSConfig == nil {
srv.TLSConfig = &tls.Config{}
}
srv.TLSConfig.PreferServerCipherSuites = true
srv.TLSConfig.MinVersion = DefaultMinVersion
srv.TLSConfig.CurvePreferences = DefaultCurvePreferences
srv.TLSConfig.CipherSuites = DefaultCipherSuites
if srv.ReadTimeout == 0 {
srv.ReadTimeout = DefaultReadTimeout
}
if srv.WriteTimeout == 0 {
srv.WriteTimeout = DefaultWriteTimeout
}
if srv.IdleTimeout == 0 {
srv.IdleTimeout = DefaultIdleTimeout
}
return srv
}