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"
|
2023-05-05 12:57:52 +00:00
|
|
|
"goauthentik.io/internal/utils"
|
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() {
|
2023-05-05 12:57:52 +00:00
|
|
|
tlsConfig := utils.GetTLSConfig()
|
|
|
|
tlsConfig.GetCertificate = ws.GetCertificate()
|
2021-05-03 21:04:48 +00:00
|
|
|
|
2022-08-03 19:33:27 +00:00
|
|
|
ln, err := net.Listen("tcp", config.Get().Listen.HTTPS)
|
2021-05-03 21:04:48 +00:00
|
|
|
if err != nil {
|
2023-03-07 12:27:46 +00:00
|
|
|
ws.log.WithError(err).Warning("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-08-03 19:33:27 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Listen.HTTPS).Info("Starting HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
ws.serve(tlsListener)
|
2022-08-03 19:33:27 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Listen.HTTPS).Info("Stopping HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|