2022-01-25 16:04:28 +00:00
|
|
|
package debug
|
|
|
|
|
|
|
|
import (
|
2023-01-09 14:29:22 +00:00
|
|
|
"encoding/json"
|
2023-01-08 19:33:04 +00:00
|
|
|
"fmt"
|
2022-01-25 16:04:28 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/pprof"
|
|
|
|
|
2023-01-08 19:33:04 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-01-25 16:04:28 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-03 19:33:27 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2023-01-08 19:33:04 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2022-01-25 16:04:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func EnableDebugServer() {
|
|
|
|
l := log.WithField("logger", "authentik.go_debugger")
|
2023-01-08 13:19:08 +00:00
|
|
|
if !config.Get().Debug {
|
2022-01-25 16:04:28 +00:00
|
|
|
l.Info("not enabling debug server, set `AUTHENTIK_DEBUG` to `true` to enable it.")
|
2022-06-02 22:06:09 +00:00
|
|
|
return
|
2022-01-25 16:04:28 +00:00
|
|
|
}
|
2023-01-08 19:33:04 +00:00
|
|
|
h := mux.NewRouter()
|
2022-01-25 16:04:28 +00:00
|
|
|
h.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
h.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
h.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
h.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
h.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
2023-01-09 14:29:22 +00:00
|
|
|
h.HandleFunc("/debug/dump_config", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetEscapeHTML(true)
|
|
|
|
enc.SetIndent("", "\t")
|
2023-01-09 16:17:27 +00:00
|
|
|
_ = enc.Encode(config.Get())
|
2023-01-09 14:29:22 +00:00
|
|
|
})
|
2023-01-08 19:33:04 +00:00
|
|
|
h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2023-01-08 19:35:25 +00:00
|
|
|
_ = h.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
2023-01-08 19:33:04 +00:00
|
|
|
tpl, err := route.GetPathTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-08 19:35:25 +00:00
|
|
|
_, err = w.Write([]byte(fmt.Sprintf("<a href='%[1]s'>%[1]s</a><br>", tpl)))
|
|
|
|
if err != nil {
|
|
|
|
l.WithError(err).Warning("failed to write index")
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-08 19:33:04 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
go func() {
|
|
|
|
l.WithField("listen", config.Get().Listen.Debug).Info("Starting Debug server")
|
|
|
|
err := http.ListenAndServe(
|
|
|
|
config.Get().Listen.Debug,
|
|
|
|
web.NewLoggingHandler(l, nil)(h),
|
|
|
|
)
|
|
|
|
if l != nil {
|
|
|
|
l.WithError(err).Warn("failed to start debug server")
|
|
|
|
}
|
|
|
|
}()
|
2022-01-25 16:04:28 +00:00
|
|
|
}
|