2021-05-02 22:49:16 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2021-05-03 21:04:48 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
2021-05-02 22:49:16 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"goauthentik.io/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WebServer struct {
|
|
|
|
Bind string
|
|
|
|
BindTLS bool
|
|
|
|
|
|
|
|
LegacyProxy bool
|
|
|
|
|
2021-05-03 21:04:48 +00:00
|
|
|
stop chan struct{} // channel for waiting shutdown
|
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
m *mux.Router
|
|
|
|
lh *mux.Router
|
|
|
|
log *log.Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWebServer() *WebServer {
|
|
|
|
mainHandler := mux.NewRouter()
|
2021-05-04 12:28:48 +00:00
|
|
|
if config.G.ErrorReporting.Enabled {
|
|
|
|
mainHandler.Use(recoveryMiddleware())
|
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
mainHandler.Use(handlers.ProxyHeaders)
|
|
|
|
mainHandler.Use(handlers.CompressHandler)
|
|
|
|
logginRouter := mainHandler.NewRoute().Subrouter()
|
|
|
|
logginRouter.Use(loggingMiddleware)
|
2021-05-04 12:28:48 +00:00
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
ws := &WebServer{
|
|
|
|
LegacyProxy: true,
|
|
|
|
|
|
|
|
m: mainHandler,
|
|
|
|
lh: logginRouter,
|
|
|
|
log: log.WithField("logger", "authentik.g.web"),
|
|
|
|
}
|
|
|
|
ws.configureStatic()
|
|
|
|
ws.configureProxy()
|
|
|
|
return ws
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
func (ws *WebServer) Start() {
|
|
|
|
go ws.listenPlain()
|
|
|
|
go ws.listenTLS()
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
func (ws *WebServer) Shutdown() {
|
2021-06-23 18:40:51 +00:00
|
|
|
ws.stop <- struct{}{}
|
2021-05-02 22:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebServer) listenPlain() {
|
2021-05-03 21:04:48 +00:00
|
|
|
ln, err := net.Listen("tcp", config.G.Web.Listen)
|
|
|
|
if err != nil {
|
|
|
|
ws.log.WithError(err).Fatalf("failed to listen")
|
|
|
|
}
|
|
|
|
ws.log.WithField("addr", config.G.Web.Listen).Info("Running")
|
|
|
|
|
|
|
|
ws.serve(ln)
|
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
ws.log.WithField("addr", config.G.Web.Listen).Info("Running")
|
2021-07-18 14:12:57 +00: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 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:
|
|
|
|
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
|
|
|
|
}
|