-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_tls.go
More file actions
336 lines (296 loc) · 10.4 KB
/
Copy pathserver_tls.go
File metadata and controls
336 lines (296 loc) · 10.4 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
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
// Package zerohttp provides TLS and HTTPS server support. See [Server.ListenAndServeTLS] and [Server.StartAutoTLS].
package zerohttp
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/alexferl/zerohttp/extensions/http3"
"github.com/alexferl/zerohttp/extensions/webtransport"
"github.com/alexferl/zerohttp/log"
)
// ListenAndServeTLS starts the HTTPS server with the specified certificate files.
// It creates a TLS listener if one is not already configured and serves HTTPS
// traffic using the provided certificate and key files. If the TLS server is
// not configured, this method logs a debug message and returns nil without error.
//
// Parameters:
// - certFile: Path to the TLS certificate file in PEM format
// - keyFile: Path to the TLS private key file in PEM format
//
// This method blocks until the server encounters an error or is shut down.
// Returns any error encountered while starting or running the TLS server.
func (s *Server) ListenAndServeTLS(certFile, keyFile string) error {
s.mu.Lock()
if s.tlsServer == nil {
s.mu.Unlock()
s.logger.Debug("TLS server not configured, skipping")
return nil
}
s.logger.Debug("TLS server is configured, proceeding")
// Load certificates if provided
if certFile != "" && keyFile != "" {
s.logger.Debug("Loading TLS certificates", log.F("cert", certFile), log.F("key", keyFile))
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
s.mu.Unlock()
s.logger.Error("Failed to load TLS certificates", log.E(err))
return fmt.Errorf("failed to load certificates: %w", err)
}
if s.tlsServer.TLSConfig == nil {
s.tlsServer.TLSConfig = &tls.Config{}
}
s.tlsServer.TLSConfig.Certificates = []tls.Certificate{cert}
}
var err error
if s.tlsListener == nil {
s.logger.Debug("Creating TLS listener", log.F("addr", s.tlsServer.Addr))
s.tlsListener, err = tls.Listen("tcp", s.tlsServer.Addr, s.tlsServer.TLSConfig)
if err != nil {
s.logger.Error("Failed to create TLS listener", log.E(err))
s.mu.Unlock()
return err
}
s.logger.Debug("TLS listener created successfully")
}
s.mu.Unlock()
s.logger.Info("Starting HTTPS server",
log.F("addr", fmtHTTPSAddr(s.tlsListener.Addr().String())),
log.F("cert_file", certFile),
log.F("key_file", keyFile))
// Start HTTP/3 server in background if configured
if s.http3Server != nil {
go func() {
s.logger.Info("Starting HTTP/3 server",
log.F("cert_file", certFile),
log.F("key_file", keyFile))
if err := s.http3Server.ListenAndServeTLS(certFile, keyFile); err != nil {
s.logger.Error("HTTP/3 server error", log.E(err))
}
}()
}
// Start WebTransport server in background if configured
if s.webTransportServer != nil {
go func() {
s.logger.Info("Starting WebTransport server",
log.F("cert_file", certFile),
log.F("key_file", keyFile))
if err := s.webTransportServer.ListenAndServeTLS(certFile, keyFile); err != nil {
s.logger.Error("WebTransport server error", log.E(err))
}
}()
}
// Use Serve (not ServeTLS) since we already have a tls.Listener
return s.tlsServer.Serve(s.tlsListener)
}
// StartTLS is a convenience method that starts only the HTTPS server with
// the specified certificate files. If the TLS server is not configured,
// this method returns nil without error.
//
// Parameters:
// - certFile: Path to the TLS certificate file in PEM format
// - keyFile: Path to the TLS private key file in PEM format
//
// This is equivalent to calling ListenAndServeTLS directly.
// Returns any error encountered while starting or running the TLS server.
func (s *Server) StartTLS(certFile, keyFile string) error {
if s.tlsServer == nil {
return fmt.Errorf("TLS server not configured")
}
return s.ListenAndServeTLS(certFile, keyFile)
}
// StartAutoTLS starts the server with automatic TLS certificate management using Let's Encrypt.
// It starts both HTTP (for ACME challenges) and HTTPS servers.
// The HTTP server redirects to HTTPS and handles ACME challenges.
//
// Users must configure the AutocertManager with their desired host policy before calling
// this method. For example, using golang.org/x/crypto/acme/autocert:
//
// mgr := &autocert.Manager{
// Cache: autocert.DirCache("/var/cache/certs"),
// Prompt: autocert.AcceptTOS,
// HostPolicy: autocert.HostWhitelist("example.com"),
// }
// srv := zerohttp.New(WithAutocertManager(mgr))
// srv.StartAutoTLS()
//
// The HTTP server handles:
// - ACME challenge requests from Let's Encrypt
// - Redirects all other HTTP traffic to HTTPS
//
// Returns an error if the autocert manager is not configured or if any server fails to start.
func (s *Server) StartAutoTLS() error {
if s.autocertManager == nil {
return fmt.Errorf("autocert manager not configured")
}
s.logger.Info("Starting server with AutoTLS...")
errCh := make(chan error, 4)
httpReady := make(chan struct{})
if s.server == nil {
close(httpReady)
}
// Start HTTP server for ACME challenges and redirects
if s.server != nil {
go func() {
// Create a new server for HTTP with autocert handler
httpServer := &http.Server{
Addr: s.server.Addr,
Handler: s.autocertManager.HTTPHandler(s.createHTTPSRedirectHandler()),
}
ln, err := net.Listen("tcp", httpServer.Addr)
if err != nil {
s.logger.Error("Failed to bind HTTP listener", log.E(err))
errCh <- err
return
}
s.logger.Info("Starting HTTP server for ACME challenges and redirects",
log.F("addr", fmtHTTPAddr(httpServer.Addr)))
close(httpReady)
errCh <- httpServer.Serve(ln)
}()
}
certReady := make(chan struct{})
var certOnce sync.Once
signalCertReady := func() {
certOnce.Do(func() {
s.logger.Info("AutoTLS certificate is ready")
close(certReady)
})
}
// Start HTTPS server with autocert
if s.tlsServer != nil {
go func() {
// Configure TLS with autocert
if s.tlsServer.TLSConfig == nil {
s.tlsServer.TLSConfig = &tls.Config{}
}
s.tlsServer.TLSConfig.GetCertificate = func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
cert, err := s.autocertManager.GetCertificate(hello)
if err == nil {
// Signal that cert is ready (first successful retrieval)
signalCertReady()
}
return cert, err
}
s.logger.Info("Starting HTTPS server with AutoTLS",
log.F("addr", fmtHTTPSAddr(s.tlsServer.Addr)))
errCh <- s.tlsServer.ListenAndServeTLS("", "")
}()
}
// Warm-up goroutine: proactively fetch certificate for HTTP/3/WebTransport
if s.http3Server != nil || s.webTransportServer != nil {
go func() {
<-httpReady
hostnames := s.autocertManager.Hostnames()
if len(hostnames) == 0 {
s.logger.Error("AutocertManager returned no hostnames, cannot warm up certificate")
return
}
hello := &tls.ClientHelloInfo{ServerName: hostnames[0]}
// Attempt immediately before starting the ticker loop
// so a cached cert on restart doesn't incur a 2-second delay
cert, err := s.autocertManager.GetCertificate(hello)
if err == nil && cert != nil {
signalCertReady()
return
}
s.logger.Debug("Certificate not yet ready on first attempt, starting poll loop...", log.E(err))
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
timeout := time.After(5 * time.Minute)
for {
select {
case <-ticker.C:
cert, err := s.autocertManager.GetCertificate(hello)
if err != nil {
s.logger.Debug("Certificate not yet ready, retrying...", log.E(err))
continue
}
if cert != nil {
signalCertReady()
return
}
case <-timeout:
s.logger.Error("Timed out waiting for AutoTLS certificate")
return
}
}
}()
}
// Start HTTP/3 server with autocert if supported (after cert is ready)
if s.http3Server != nil {
if h3Autocert, ok := s.http3Server.(http3.ServerWithAutocert); ok {
go func() {
s.logger.Info("Waiting for certificate before starting HTTP/3...")
<-certReady
s.logger.Info("Starting HTTP/3 server with AutoTLS")
errCh <- h3Autocert.ListenAndServeTLSWithAutocert(s.autocertManager)
}()
}
}
// Start WebTransport server with autocert if supported (after cert is ready)
if s.webTransportServer != nil {
if wtAutocert, ok := s.webTransportServer.(webtransport.ServerWithAutocert); ok {
go func() {
s.logger.Info("Waiting for certificate before starting WebTransport...")
<-certReady
s.logger.Info("Starting WebTransport server with AutoTLS")
errCh <- wtAutocert.ListenAndServeTLSWithAutocert(s.autocertManager)
}()
}
}
return <-errCh
}
// ListenerTLSAddr returns the network address that the HTTPS server is listening on.
// If a TLS listener is configured, it returns the listener's actual address.
// If no TLS listener is configured but a TLS server is configured, it returns the server's configured address.
// If neither is configured, it returns an empty string.
//
// This method is thread-safe and can be called concurrently.
// The returned address includes both host and port (e.g., "127.0.0.1:8443").
func (s *Server) ListenerTLSAddr() string {
s.mu.RLock()
defer s.mu.RUnlock()
if s.tlsListener != nil {
return s.tlsListener.Addr().String()
}
if s.tlsServer != nil {
return s.tlsServer.Addr
}
return ""
}
// createHTTPSRedirectHandler creates an HTTP handler that redirects all requests
// to their HTTPS equivalent. This handler is used by the HTTP server when
// running in AutoTLS mode to ensure all traffic is encrypted.
//
// The redirect preserves the original request path and query parameters.
// Returns an http.Handler that performs permanent redirects (301) to HTTPS.
func (s *Server) createHTTPSRedirectHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Extract host from the request (without port)
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
// No port in Host, use as-is
host = r.Host
}
// Get the HTTPS port from the TLS server config
httpsPort := ""
if s.tlsServer != nil && s.tlsServer.Addr != "" {
_, port, err := net.SplitHostPort(s.tlsServer.Addr)
if err == nil && port != "" && port != "443" {
httpsPort = ":" + port
}
}
// Build HTTPS URL by copying the URL and changing scheme
target := *r.URL
target.Scheme = "https"
target.Host = host + httpsPort
httpsURL := target.String()
s.logger.Debug("Redirecting HTTP to HTTPS",
log.F("from", r.URL.String()),
log.F("to", httpsURL))
http.Redirect(w, r, httpsURL, http.StatusMovedPermanently)
})
}