2021-09-08 18:04:56 +00:00
|
|
|
package proxyv2
|
|
|
|
|
|
|
|
import (
|
2021-11-13 21:44:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-08 18:04:56 +00:00
|
|
|
"net/http"
|
2021-11-10 16:16:13 +00:00
|
|
|
"strings"
|
2021-09-08 18:04:56 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/metrics"
|
|
|
|
"goauthentik.io/internal/utils/web"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ps *ProxyServer) HandlePing(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
before := time.Now()
|
|
|
|
rw.WriteHeader(204)
|
|
|
|
after := time.Since(before)
|
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ps.akAPI.Outpost.Name,
|
|
|
|
"method": r.Method,
|
2021-11-10 16:16:13 +00:00
|
|
|
"scheme": r.URL.Scheme,
|
2021-09-16 08:03:31 +00:00
|
|
|
"path": r.URL.Path,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
"type": "ping",
|
|
|
|
"user": "",
|
2021-09-08 18:04:56 +00:00
|
|
|
}).Observe(float64(after))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) HandleStatic(rw http.ResponseWriter, r *http.Request) {
|
2021-12-02 17:47:36 +00:00
|
|
|
staticFs := http.FileServer(http.Dir("./web/dist/"))
|
2021-09-08 18:04:56 +00:00
|
|
|
before := time.Now()
|
2021-11-19 09:50:56 +00:00
|
|
|
web.DisableIndex(http.StripPrefix("/akprox/static", staticFs)).ServeHTTP(rw, r)
|
2021-09-08 18:04:56 +00:00
|
|
|
after := time.Since(before)
|
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ps.akAPI.Outpost.Name,
|
|
|
|
"method": r.Method,
|
2021-11-10 16:16:13 +00:00
|
|
|
"scheme": r.URL.Scheme,
|
2021-09-16 08:03:31 +00:00
|
|
|
"path": r.URL.Path,
|
|
|
|
"host": web.GetHost(r),
|
|
|
|
"type": "ping",
|
|
|
|
"user": "",
|
2021-09-08 18:04:56 +00:00
|
|
|
}).Observe(float64(after))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) Handle(rw http.ResponseWriter, r *http.Request) {
|
2021-11-10 16:16:13 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/akprox/static") {
|
|
|
|
ps.HandleStatic(rw, r)
|
|
|
|
return
|
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
host := web.GetHost(r)
|
|
|
|
a, ok := ps.apps[host]
|
|
|
|
if !ok {
|
2021-09-24 21:32:16 +00:00
|
|
|
// If we only have one handler, host name switching doesn't matter
|
|
|
|
if len(ps.apps) == 1 {
|
2021-11-19 09:50:56 +00:00
|
|
|
ps.log.WithField("host", host).Trace("passing to single app mux")
|
2021-09-24 21:32:16 +00:00
|
|
|
for k := range ps.apps {
|
|
|
|
ps.apps[k].ServeHTTP(rw, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:04:56 +00:00
|
|
|
ps.log.WithField("host", host).Warning("no app for hostname")
|
2021-11-13 21:44:52 +00:00
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
j := json.NewEncoder(rw)
|
|
|
|
j.SetIndent("", "\t")
|
|
|
|
err := j.Encode(struct {
|
|
|
|
Message string
|
|
|
|
Host string
|
|
|
|
Detail string
|
|
|
|
}{
|
|
|
|
Message: "no app for hostname",
|
|
|
|
Host: host,
|
|
|
|
Detail: fmt.Sprintf("Check the outpost settings and make sure '%s' is included.", host),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ps.log.WithError(err).Warning("Failed to write error body")
|
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ps.log.WithField("host", host).Trace("passing to application mux")
|
|
|
|
a.ServeHTTP(rw, r)
|
|
|
|
}
|