2021-05-02 22:49:16 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2021-05-03 21:04:48 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2023-09-02 15:58:37 +00:00
|
|
|
"fmt"
|
2021-05-03 21:04:48 +00:00
|
|
|
"net"
|
2021-05-02 22:49:16 +00:00
|
|
|
"net/http"
|
2023-09-02 15:58:37 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2023-09-27 11:34:29 +00:00
|
|
|
"time"
|
2021-05-02 22:49:16 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2021-07-20 09:03:09 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
2021-05-02 22:49:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-06 11:46:14 +00:00
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-10-05 20:19:33 +00:00
|
|
|
"goauthentik.io/internal/gounicorn"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2"
|
2021-12-16 10:00:19 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2023-11-06 11:46:14 +00:00
|
|
|
"goauthentik.io/internal/web/brand_tls"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebServer struct {
|
|
|
|
Bind string
|
|
|
|
BindTLS bool
|
|
|
|
|
2021-05-03 21:04:48 +00:00
|
|
|
stop chan struct{} // channel for waiting shutdown
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
ProxyServer *proxyv2.ProxyServer
|
2023-11-06 11:46:14 +00:00
|
|
|
BrandTLS *brand_tls.Watcher
|
2021-07-29 09:30:30 +00:00
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
g *gounicorn.GoUnicorn
|
|
|
|
gr bool
|
2021-05-02 22:49:16 +00:00
|
|
|
m *mux.Router
|
|
|
|
lh *mux.Router
|
|
|
|
log *log.Entry
|
2023-09-02 15:58:37 +00:00
|
|
|
uc *http.Client
|
|
|
|
ul *url.URL
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
const UnixSocketName = "authentik-core.sock"
|
|
|
|
|
|
|
|
func NewWebServer() *WebServer {
|
2021-10-11 15:51:49 +00:00
|
|
|
l := log.WithField("logger", "authentik.router")
|
2021-05-02 22:49:16 +00:00
|
|
|
mainHandler := mux.NewRouter()
|
2023-07-06 16:16:26 +00:00
|
|
|
mainHandler.Use(web.ProxyHeaders())
|
2021-05-02 22:49:16 +00:00
|
|
|
mainHandler.Use(handlers.CompressHandler)
|
2022-07-30 15:51:01 +00:00
|
|
|
loggingHandler := mainHandler.NewRoute().Subrouter()
|
|
|
|
loggingHandler.Use(web.NewLoggingHandler(l, nil))
|
2021-05-04 12:28:48 +00:00
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
tmp := os.TempDir()
|
|
|
|
socketPath := path.Join(tmp, "authentik-core.sock")
|
|
|
|
|
|
|
|
// create http client to talk to backend, normal client if we're in debug more
|
|
|
|
// and a client that connects to our socket when in non debug mode
|
|
|
|
var upstreamClient *http.Client
|
|
|
|
if config.Get().Debug {
|
|
|
|
upstreamClient = http.DefaultClient
|
|
|
|
} else {
|
|
|
|
upstreamClient = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", socketPath)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u, _ := url.Parse("http://localhost:8000")
|
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
ws := &WebServer{
|
|
|
|
m: mainHandler,
|
2022-07-30 15:51:01 +00:00
|
|
|
lh: loggingHandler,
|
2021-09-04 11:52:47 +00:00
|
|
|
log: l,
|
2023-09-02 15:58:37 +00:00
|
|
|
gr: true,
|
|
|
|
uc: upstreamClient,
|
|
|
|
ul: u,
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
ws.configureStatic()
|
|
|
|
ws.configureProxy()
|
2023-09-02 15:58:37 +00:00
|
|
|
ws.g = gounicorn.New(func() bool {
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/-/health/live/", ws.ul.String()), nil)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Warning("failed to create request for healthcheck")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "goauthentik.io/router/healthcheck")
|
|
|
|
res, err := ws.upstreamHttpClient().Do(req)
|
|
|
|
if err == nil && res.StatusCode == 204 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2021-05-02 22:49:16 +00:00
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
func (ws *WebServer) Start() {
|
2023-09-02 15:58:37 +00:00
|
|
|
go ws.runMetricsServer()
|
|
|
|
go ws.attemptStartBackend()
|
2021-06-29 14:21:00 +00:00
|
|
|
go ws.listenPlain()
|
|
|
|
go ws.listenTLS()
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 15:58:37 +00:00
|
|
|
func (ws *WebServer) attemptStartBackend() {
|
|
|
|
for {
|
|
|
|
if !ws.gr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := ws.g.Start()
|
|
|
|
log.WithField("logger", "authentik.router").WithError(err).Warning("gunicorn process died, restarting")
|
2023-09-27 11:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithField("logger", "authentik.router").WithError(err).Error("gunicorn failed to start, restarting")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
failedChecks := 0
|
|
|
|
for range time.NewTicker(30 * time.Second).C {
|
|
|
|
if !ws.g.IsRunning() {
|
|
|
|
log.WithField("logger", "authentik.router").Warningf("gunicorn process failed healthcheck %d times", failedChecks)
|
|
|
|
failedChecks += 1
|
|
|
|
}
|
|
|
|
if failedChecks >= 3 {
|
|
|
|
log.WithField("logger", "authentik.router").WithError(err).Error("gunicorn process failed healthcheck three times, restarting")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 15:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) Core() *gounicorn.GoUnicorn {
|
|
|
|
return ws.g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) upstreamHttpClient() *http.Client {
|
|
|
|
return ws.uc
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
func (ws *WebServer) Shutdown() {
|
2023-09-02 15:58:37 +00:00
|
|
|
ws.log.Info("shutting down gunicorn")
|
|
|
|
ws.g.Kill()
|
2021-06-23 18:40:51 +00:00
|
|
|
ws.stop <- struct{}{}
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) listenPlain() {
|
2022-08-03 19:33:27 +00:00
|
|
|
ln, err := net.Listen("tcp", config.Get().Listen.HTTP)
|
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")
|
|
|
|
return
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
2021-07-20 09:03:09 +00:00
|
|
|
proxyListener := &proxyproto.Listener{Listener: ln}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
2022-08-03 19:33:27 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Listen.HTTP).Info("Starting HTTP server")
|
2021-07-20 09:03:09 +00:00
|
|
|
ws.serve(proxyListener)
|
2022-08-03 19:33:27 +00:00
|
|
|
ws.log.WithField("listen", config.Get().Listen.HTTP).Info("Stopping HTTP server")
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
2021-05-03 21:04:48 +00:00
|
|
|
|
|
|
|
func (ws *WebServer) serve(listener net.Listener) {
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: ws.m,
|
|
|
|
}
|
|
|
|
|
|
|
|
// See https://golang.org/pkg/net/http/#Server.Shutdown
|
|
|
|
idleConnsClosed := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-ws.stop // wait notification for stopping server
|
|
|
|
|
|
|
|
// We received an interrupt signal, shut down.
|
|
|
|
if err := srv.Shutdown(context.Background()); err != nil {
|
|
|
|
// Error from closing listeners, or context timeout:
|
2021-12-16 10:00:19 +00:00
|
|
|
ws.log.WithError(err).Warning("HTTP server Shutdown")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
|
|
|
close(idleConnsClosed)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := srv.Serve(listener)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2021-12-16 10:00:19 +00:00
|
|
|
ws.log.WithError(err).Error("ERROR: http.Serve()")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
|
|
|
<-idleConnsClosed
|
|
|
|
}
|