2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-12-18 08:01:19 +00:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-01 18:28:11 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2020-12-18 08:01:19 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/metric"
|
2020-12-18 08:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initializeRouter(enableProfiler bool) {
|
|
|
|
router = chi.NewRouter()
|
|
|
|
|
2020-12-20 09:22:16 +00:00
|
|
|
router.Use(middleware.GetHead)
|
2023-02-23 18:25:20 +00:00
|
|
|
router.Use(logger.NewStructuredLogger(logger.GetLogger()))
|
2020-12-18 08:01:19 +00:00
|
|
|
router.Use(middleware.Recoverer)
|
|
|
|
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
render.PlainText(w, r, "ok")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-12-18 15:04:42 +00:00
|
|
|
router.Group(func(router chi.Router) {
|
|
|
|
router.Use(checkAuth)
|
2021-07-11 13:26:51 +00:00
|
|
|
metric.AddMetricsEndpoint(metricsPath, router)
|
2020-12-18 08:01:19 +00:00
|
|
|
|
2020-12-18 15:04:42 +00:00
|
|
|
if enableProfiler {
|
|
|
|
logger.InfoToConsole("enabling the built-in profiler")
|
|
|
|
logger.Info(logSender, "", "enabling the built-in profiler")
|
|
|
|
router.Mount(pprofBasePath, middleware.Profiler())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkAuth(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !validateCredentials(r) {
|
|
|
|
w.Header().Set(common.HTTPAuthenticationHeader, "Basic realm=\"SFTPGo telemetry\"")
|
|
|
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateCredentials(r *http.Request) bool {
|
|
|
|
if !httpAuth.IsEnabled() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
username, password, ok := r.BasicAuth()
|
|
|
|
if !ok {
|
|
|
|
return false
|
2020-12-18 08:01:19 +00:00
|
|
|
}
|
2020-12-18 15:04:42 +00:00
|
|
|
return httpAuth.ValidateCredentials(username, password)
|
2020-12-18 08:01:19 +00:00
|
|
|
}
|