2022-04-04 02:15:26 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-07-25 10:43:50 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/sha1" //nolint:gosec // Usage is for collision avoidance not security.
|
2022-04-04 02:15:26 +00:00
|
|
|
"embed"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2022-07-25 10:43:50 +00:00
|
|
|
"mime"
|
2022-04-04 02:15:26 +00:00
|
|
|
"net/http"
|
2022-07-25 10:43:50 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-09-05 23:09:33 +00:00
|
|
|
"strings"
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
2022-07-25 10:43:50 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/handlers"
|
2022-07-11 06:24:09 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2022-04-04 02:15:26 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed locales
|
|
|
|
var locales embed.FS
|
|
|
|
|
|
|
|
//go:embed public_html
|
|
|
|
var assets embed.FS
|
|
|
|
|
|
|
|
func newPublicHTMLEmbeddedHandler() fasthttp.RequestHandler {
|
2022-07-25 10:43:50 +00:00
|
|
|
etags := map[string][]byte{}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-07-25 10:43:50 +00:00
|
|
|
getEmbedETags(assets, "public_html", etags)
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
p := path.Join("public_html", string(ctx.Path()))
|
|
|
|
|
|
|
|
if etag, ok := etags[p]; ok {
|
|
|
|
ctx.Response.Header.SetBytesKV(headerETag, etag)
|
|
|
|
ctx.Response.Header.SetBytesKV(headerCacheControl, headerValueCacheControlETaggedAssets)
|
|
|
|
|
|
|
|
if bytes.Equal(etag, ctx.Request.Header.PeekBytes(headerIfNoneMatch)) {
|
|
|
|
ctx.SetStatusCode(fasthttp.StatusNotModified)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if data, err = assets.ReadFile(p); err != nil {
|
|
|
|
hfsHandleErr(ctx, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := mime.TypeByExtension(path.Ext(p))
|
|
|
|
if len(contentType) == 0 {
|
|
|
|
contentType = http.DetectContentType(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetContentType(contentType)
|
|
|
|
ctx.SetBody(data)
|
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
|
|
|
|
var languages []string
|
|
|
|
|
|
|
|
entries, err := locales.ReadDir("locales")
|
|
|
|
if err == nil {
|
|
|
|
for _, entry := range entries {
|
2022-09-05 23:09:33 +00:00
|
|
|
if entry.IsDir() {
|
|
|
|
var lng string
|
|
|
|
|
|
|
|
switch len(entry.Name()) {
|
|
|
|
case 2:
|
|
|
|
lng = entry.Name()
|
|
|
|
case 0:
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
lng = strings.SplitN(entry.Name(), "-", 2)[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsStringInSlice(lng, languages) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
languages = append(languages, lng)
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
var (
|
|
|
|
language, variant, locale, namespace string
|
|
|
|
)
|
|
|
|
|
|
|
|
language = ctx.UserValue("language").(string)
|
|
|
|
namespace = ctx.UserValue("namespace").(string)
|
|
|
|
locale = language
|
|
|
|
|
|
|
|
if v := ctx.UserValue("variant"); v != nil {
|
|
|
|
variant = v.(string)
|
2022-04-09 11:38:17 +00:00
|
|
|
locale = fmt.Sprintf("%s-%s", language, variant)
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if data, err = locales.ReadFile(fmt.Sprintf("locales/%s/%s.json", locale, namespace)); err != nil {
|
2022-09-05 23:09:33 +00:00
|
|
|
if utils.IsStringInSliceFold(language, languages) {
|
2022-04-04 02:15:26 +00:00
|
|
|
data = []byte("{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data) == 0 {
|
|
|
|
hfsHandleErr(ctx, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:24:09 +00:00
|
|
|
middlewares.SetContentTypeApplicationJSON(ctx)
|
|
|
|
|
2022-04-04 02:15:26 +00:00
|
|
|
ctx.SetBody(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 10:43:50 +00:00
|
|
|
func getEmbedETags(embedFS embed.FS, root string, etags map[string][]byte) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
entries []fs.DirEntry
|
|
|
|
)
|
|
|
|
|
|
|
|
if entries, err = embedFS.ReadDir(root); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
getEmbedETags(embedFS, filepath.Join(root, entry.Name()), etags)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Join(root, entry.Name())
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
if data, err = embedFS.ReadFile(p); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := sha1.New() //nolint:gosec // Usage is for collision avoidance not security.
|
|
|
|
|
|
|
|
sum.Write(data)
|
|
|
|
|
|
|
|
etags[p] = []byte(fmt.Sprintf("%x", sum.Sum(nil)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 02:15:26 +00:00
|
|
|
func hfsHandleErr(ctx *fasthttp.RequestCtx, err error) {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, fs.ErrNotExist):
|
2022-07-25 10:43:50 +00:00
|
|
|
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusNotFound)
|
2022-04-04 02:15:26 +00:00
|
|
|
case errors.Is(err, fs.ErrPermission):
|
2022-07-25 10:43:50 +00:00
|
|
|
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusForbidden)
|
2022-04-04 02:15:26 +00:00
|
|
|
default:
|
2022-07-25 10:43:50 +00:00
|
|
|
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusInternalServerError)
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
}
|