forked from fragmenta/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
executable file
·228 lines (193 loc) · 7.53 KB
/
server.go
File metadata and controls
executable file
·228 lines (193 loc) · 7.53 KB
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
// Package server is a wrapper around the stdlib http server and x/autocert pkg.
package server
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"golang.org/x/crypto/acme/autocert"
)
// Server wraps the stdlib http server and x/autocert pkg with some setup.
type Server struct {
// Which port to serve on - in 2.0 pass as argument for New()
port int
// Which mode we're in, read from ENV variable
// Deprecated - due to be removed in 2.0
production bool
// Deprecated Logging - due to be removed in 2.0
// Instead use the structured logging with server/log
Logger Logger
// Deprecated configs will be removed from the server object in 2.0
// Use server/config instead to read the config from app.
// Server configs - access with Config(string)
configProduction map[string]string
configDevelopment map[string]string
configTest map[string]string
}
// New creates a new server instance
func New(port int, prod bool) (*Server, error) {
// Set up a new server
s := &Server{
port: port,
production: prod,
configProduction: make(map[string]string),
configDevelopment: make(map[string]string),
configTest: make(map[string]string),
Logger: log.New(os.Stderr, "fragmenta: ", log.LstdFlags),
}
// Old style config read - this will be going away in Fragmenta 2.0
// use server/config instead from the app
//err := s.readConfig()
//if err != nil {
// return s, err
//}
//err = s.readArguments()
//if err != nil {
// return s, err
//}
return s, nil
}
// Port returns the port of the server
func (s *Server) Port() int {
return s.port
}
// PortString returns a string port suitable for passing to http.Server
func (s *Server) PortString() string {
return fmt.Sprintf(":%d", s.port)
}
// Start starts an http server on the given port
func (s *Server) Start() error {
server := &http.Server{
// Set the port in the preferred string format
Addr: s.PortString(),
// The default server from net/http has no timeouts - set some limits
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 10 * time.Second, // IdleTimeout was introduced in Go 1.8
}
return server.ListenAndServe()
}
// StartTLS starts an https server on the given port
// with tls cert/key from config keys.
// Settings based on an article by Filippo Valsorda.
// https://blog.cloudflare.com/exposing-go-on-the-internet/
func (s *Server) StartTLS(cert, key string) error {
// Set up a new http server
server := &http.Server{
// Set the port in the preferred string format
Addr: s.PortString(),
// The default server from net/http has no timeouts - set some limits
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 10 * time.Second, // IdleTimeout was introduced in Go 1.8
// This TLS config follows recommendations in the above article
TLSConfig: &tls.Config{
// VersionTLS11 or VersionTLS12 would exclude many browsers
// inc. Android 4.x, IE 10, Opera 12.17, Safari 6
// So unfortunately not acceptable as a default yet
// Current default here for clarity
MinVersion: tls.VersionTLS10,
// Causes servers to use Go's default ciphersuite preferences,
// which are tuned to avoid attacks. Does nothing on clients.
PreferServerCipherSuites: true,
// Only use curves which have assembly implementations
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519, // Go 1.8 only
},
},
}
return server.ListenAndServeTLS(cert, key)
}
// StartTLSAuto starts an https server on the given port
// by requesting certs from an ACME provider using the http-01 challenge.
// it also starts a server on the port 80 to listen for challenges and redirect
// The server must be on a public IP which matches the
// DNS for the domains.
func (s *Server) StartTLSAuto(email, domains string) error {
autocertDomains := strings.Split(domains, " ")
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
Email: email, // Email for problems with certs
HostPolicy: autocert.HostWhitelist(autocertDomains...), // Domains to request certs for
Cache: autocert.DirCache("secrets"), // Cache certs in secrets folder
}
// Handle all :80 traffic using autocert to allow http-01 challenge responses
go func() {
http.ListenAndServe(":80", certManager.HTTPHandler(nil))
}()
server := s.ConfiguredTLSServer(certManager)
return server.ListenAndServeTLS("", "")
}
// StartTLSAutocert starts an https server on the given port
// by requesting certs from an ACME provider.
// The server must be on a public IP which matches the
// DNS for the domains.
func (s *Server) StartTLSAutocert(email string, domains string) error {
autocertDomains := strings.Split(domains, " ")
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
Email: email, // Email for problems with certs
HostPolicy: autocert.HostWhitelist(autocertDomains...), // Domains to request certs for
Cache: autocert.DirCache("secrets"), // Cache certs in secrets folder
}
server := s.ConfiguredTLSServer(certManager)
return server.ListenAndServeTLS("", "")
}
// ConfiguredTLSServer returns a TLS server instance with a secure config
// this server has read/write timeouts set to 20 seconds,
// prefers server cipher suites and only uses certain accelerated curves
// see - https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
func (s *Server) ConfiguredTLSServer(certManager *autocert.Manager) *http.Server {
return &http.Server{
// Set the port in the preferred string format
Addr: s.PortString(),
// The default server from net/http has no timeouts - set some limits
ReadHeaderTimeout: 30 * time.Second,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 10 * time.Second, // IdleTimeout was introduced in Go 1.8
// This TLS config follows recommendations in the above article
TLSConfig: &tls.Config{
// Pass in a cert manager if you want one set
// this will only be used if the server Certificates are empty
GetCertificate: certManager.GetCertificate,
// VersionTLS11 or VersionTLS12 would exclude many browsers
// inc. Android 4.x, IE 10, Opera 12.17, Safari 6
// So unfortunately not acceptable as a default yet
// Current default here for clarity
MinVersion: tls.VersionTLS10,
// Causes servers to use Go's default ciphersuite preferences,
// which are tuned to avoid attacks. Does nothing on clients.
PreferServerCipherSuites: true,
// Only use curves which have assembly implementations
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519, // Go 1.8 only
},
},
}
}
// StartRedirectAll starts redirecting all requests on the given port to the given host
// this should be called before StartTLS if redirecting http on port 80 to https
func (s *Server) StartRedirectAll(p int, host string) {
port := fmt.Sprintf(":%d", p)
// Listen and server on port p in a separate goroutine
go func() {
http.ListenAndServe(port, &redirectHandler{host: host})
}()
}
// redirectHandler is useful if serving tls direct (not behind a proxy)
// and a redirect from port 80 is required.
type redirectHandler struct {
host string
}
// ServeHTTP on this handler simply redirects to the main site
func (m *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, m.host+r.URL.String(), http.StatusMovedPermanently)
}