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) {
|
|
|
|
if ws.ProxyServer != nil {
|
|
|
|
appCert := ws.ProxyServer.GetCertificate(ch.ServerName)
|
|
|
|
if appCert != nil {
|
|
|
|
return appCert, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", config.G.Web.ListenTLS)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Fatalf("failed to listen")
|
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)
|
2021-12-22 09:33:21 +00:00
|
|
|
ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Starting HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
ws.serve(tlsListener)
|
2021-12-22 09:33:21 +00:00
|
|
|
ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Stopping HTTPS server")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|