2021-05-02 23:49:16 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2021-05-03 22:04:48 +01:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2021-05-02 23:49:16 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
2021-07-20 10:03:09 +01:00
|
|
|
"github.com/pires/go-proxyproto"
|
2021-05-02 23:49:16 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/internal/config"
|
2021-10-05 21:19:33 +01:00
|
|
|
"goauthentik.io/internal/gounicorn"
|
2021-09-08 19:04:56 +01:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2"
|
2021-05-02 23:49:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebServer struct {
|
|
|
|
Bind string
|
|
|
|
BindTLS bool
|
|
|
|
|
|
|
|
LegacyProxy bool
|
|
|
|
|
2021-05-03 22:04:48 +01:00
|
|
|
stop chan struct{} // channel for waiting shutdown
|
|
|
|
|
2021-09-08 19:04:56 +01:00
|
|
|
ProxyServer *proxyv2.ProxyServer
|
2021-07-29 10:30:30 +01:00
|
|
|
|
2021-05-02 23:49:16 +01:00
|
|
|
m *mux.Router
|
|
|
|
lh *mux.Router
|
|
|
|
log *log.Entry
|
2021-10-05 21:19:33 +01:00
|
|
|
p *gounicorn.GoUnicorn
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:19:33 +01:00
|
|
|
func NewWebServer(g *gounicorn.GoUnicorn) *WebServer {
|
2021-10-11 16:51:49 +01:00
|
|
|
l := log.WithField("logger", "authentik.router")
|
2021-05-02 23:49:16 +01:00
|
|
|
mainHandler := mux.NewRouter()
|
2021-05-04 13:28:48 +01:00
|
|
|
if config.G.ErrorReporting.Enabled {
|
|
|
|
mainHandler.Use(recoveryMiddleware())
|
|
|
|
}
|
2021-05-02 23:49:16 +01:00
|
|
|
mainHandler.Use(handlers.ProxyHeaders)
|
|
|
|
mainHandler.Use(handlers.CompressHandler)
|
|
|
|
logginRouter := mainHandler.NewRoute().Subrouter()
|
2021-09-04 12:52:47 +01:00
|
|
|
logginRouter.Use(loggingMiddleware(l))
|
2021-05-04 13:28:48 +01:00
|
|
|
|
2021-05-02 23:49:16 +01:00
|
|
|
ws := &WebServer{
|
|
|
|
LegacyProxy: true,
|
|
|
|
|
|
|
|
m: mainHandler,
|
|
|
|
lh: logginRouter,
|
2021-09-04 12:52:47 +01:00
|
|
|
log: l,
|
2021-10-05 21:19:33 +01:00
|
|
|
p: g,
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
ws.configureStatic()
|
2021-11-02 13:44:37 +00:00
|
|
|
ws.configureRoutes()
|
2021-05-02 23:49:16 +01:00
|
|
|
ws.configureProxy()
|
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
2021-11-02 13:44:37 +00:00
|
|
|
func (ws *WebServer) configureRoutes() {
|
|
|
|
ws.m.Path("/api/v3/sentry/").HandlerFunc(ws.APISentryProxy)
|
|
|
|
}
|
|
|
|
|
2021-06-29 15:21:00 +01:00
|
|
|
func (ws *WebServer) Start() {
|
|
|
|
go ws.listenPlain()
|
|
|
|
go ws.listenTLS()
|
2021-06-23 19:40:51 +01:00
|
|
|
}
|
|
|
|
|
2021-06-29 15:21:00 +01:00
|
|
|
func (ws *WebServer) Shutdown() {
|
2021-06-23 19:40:51 +01:00
|
|
|
ws.stop <- struct{}{}
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) listenPlain() {
|
2021-05-03 22:04:48 +01:00
|
|
|
ln, err := net.Listen("tcp", config.G.Web.Listen)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Fatalf("failed to listen")
|
|
|
|
}
|
2021-11-19 09:37:13 +00:00
|
|
|
ws.log.WithField("listen", config.G.Web.Listen).Info("Listening")
|
2021-05-03 22:04:48 +01:00
|
|
|
|
2021-07-20 10:03:09 +01:00
|
|
|
proxyListener := &proxyproto.Listener{Listener: ln}
|
|
|
|
defer proxyListener.Close()
|
|
|
|
|
|
|
|
ws.serve(proxyListener)
|
2021-05-03 22:04:48 +01:00
|
|
|
|
2021-07-18 15:12:57 +01:00
|
|
|
err = http.ListenAndServe(config.G.Web.Listen, ws.m)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
ws.log.Errorf("ERROR: http.Serve() - %s", err)
|
|
|
|
}
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
2021-05-03 22:04:48 +01: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:
|
|
|
|
ws.log.Printf("HTTP server Shutdown: %v", err)
|
|
|
|
}
|
|
|
|
close(idleConnsClosed)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := srv.Serve(listener)
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
ws.log.Errorf("ERROR: http.Serve() - %s", err)
|
|
|
|
}
|
|
|
|
<-idleConnsClosed
|
|
|
|
}
|