2021-05-03 21:04:48 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
|
2021-07-20 09:03:09 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
2021-05-03 21:04:48 +00:00
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
"goauthentik.io/internal/crypto"
|
2021-12-22 09:33:21 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2021-05-03 21:04:48 +00:00
|
|
|
)
|
|
|
|
|
2021-12-22 09:16:01 +00:00
|
|
|
func (ws *WebServer) GetCertificate() func(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2021-05-03 21:04:48 +00:00
|
|
|
cert, err := crypto.GenerateSelfSignedCert()
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Error("failed to generate default cert")
|
|
|
|
}
|
2021-12-22 09:16:01 +00:00
|
|
|
return func(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
2022-02-09 11:48:17 +00:00
|
|
|
if ch.ServerName == "" {
|
|
|
|
return &cert, nil
|
|
|
|
}
|
2021-12-22 09:16:01 +00:00
|
|
|
if ws.ProxyServer != nil {
|
|
|
|
appCert := ws.ProxyServer.GetCertificate(ch.ServerName)
|
|
|
|
if appCert != nil {
|
|
|
|
return appCert, nil
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 10:43:45 +00:00
|
|
|
if ws.TenantTLS != nil {
|
|
|
|
return ws.TenantTLS.GetCertificate(ch)
|
|
|
|
}
|
2021-12-22 09:16:01 +00:00
|
|
|
ws.log.Trace("using default, self-signed certificate")
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
|
|
|
|
func (ws *WebServer) listenTLS() {
|
2021-05-03 21:04:48 +00:00
|
|
|
tlsConfig := &tls.Config{
|
2021-12-22 09:16:01 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
MaxVersion: tls.VersionTLS12,
|
|
|
|
GetCertificate: ws.GetCertificate(),
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 09:33:35 +00:00
|
|
|
ln, err := net.Listen("tcp", config.Get().Web.ListenTLS)
|
2021-05-03 21:04:48 +00:00
|
|
|
if err != nil {
|
2022-01-03 11:36:11 +00:00
|
|
|
ws.log.WithError(err).Fatalf("failed to listen (TLS)")
|
2021-07-18 14:12:57 +00:00
|
|
|
return
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
2021-12-22 09:33:21 +00:00
|
|
|
proxyListener := &proxyproto.Listener{Listener: web.TCPKeepAliveListener{TCPListener: ln.(*net.TCPListener)}}
|
2021-07-20 09:03:09 +00:00
|
|
|
defer proxyListener.Close()
|
|
|
|
|
|
|
|
tlsListener := tls.NewListener(proxyListener, tlsConfig)
|
2022-07-26 09:33:35 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Web.ListenTLS).Info("Starting HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
ws.serve(tlsListener)
|
2022-07-26 09:33:35 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Web.ListenTLS).Info("Stopping HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|