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"
|
|
|
|
)
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
var (
|
|
|
|
//go:embed public_html
|
|
|
|
assets embed.FS
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
//go:embed locales
|
|
|
|
locales embed.FS
|
|
|
|
)
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
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-09-16 01:19:16 +00:00
|
|
|
getEmbedETags(assets, assetsRoot, etags)
|
2022-07-25 10:43:50 +00:00
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
2022-09-16 01:19:16 +00:00
|
|
|
p := path.Join(assetsRoot, string(ctx.Path()))
|
2022-07-25 10:43:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
func newLocalesPathResolver() func(ctx *fasthttp.RequestCtx) (supported bool, asset string) {
|
|
|
|
var (
|
|
|
|
languages, dirs []string
|
|
|
|
)
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
if !utils.IsStringInSlice(entry.Name(), dirs) {
|
|
|
|
dirs = append(dirs, entry.Name())
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:09:33 +00:00
|
|
|
if utils.IsStringInSlice(lng, languages) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
languages = append(languages, lng)
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
aliases := map[string]string{
|
|
|
|
"sv": "sv-SE",
|
|
|
|
"zh": "zh-CN",
|
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
return func(ctx *fasthttp.RequestCtx) (supported bool, asset string) {
|
|
|
|
var language, namespace, variant, locale string
|
|
|
|
|
|
|
|
language, namespace = ctx.UserValue("language").(string), ctx.UserValue("namespace").(string)
|
|
|
|
|
|
|
|
if !utils.IsStringInSlice(language, languages) {
|
|
|
|
return false, ""
|
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
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-09-16 01:19:16 +00:00
|
|
|
} else {
|
|
|
|
locale = language
|
2022-04-04 02:15:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
ll := language + "-" + strings.ToUpper(language)
|
|
|
|
alias, ok := aliases[locale]
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case ok:
|
|
|
|
return true, fmt.Sprintf("locales/%s/%s.json", alias, namespace)
|
|
|
|
case utils.IsStringInSlice(locale, dirs):
|
|
|
|
return true, fmt.Sprintf("locales/%s/%s.json", locale, namespace)
|
|
|
|
case utils.IsStringInSlice(ll, dirs):
|
|
|
|
return true, fmt.Sprintf("locales/%s-%s/%s.json", language, strings.ToUpper(language), namespace)
|
|
|
|
default:
|
|
|
|
return true, fmt.Sprintf("locales/%s/%s.json", locale, namespace)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
func newLocalesEmbeddedHandler() (handler fasthttp.RequestHandler) {
|
|
|
|
etags := map[string][]byte{}
|
2022-04-04 02:15:26 +00:00
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
getEmbedETags(locales, "locales", etags)
|
|
|
|
|
|
|
|
getAssetName := newLocalesPathResolver()
|
|
|
|
|
|
|
|
return func(ctx *fasthttp.RequestCtx) {
|
|
|
|
supported, asset := getAssetName(ctx)
|
|
|
|
|
|
|
|
if !supported {
|
|
|
|
handlers.SetStatusCodeResponse(ctx, fasthttp.StatusNotFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if etag, ok := etags[asset]; 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)
|
2022-04-04 02:15:26 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 01:19:16 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if data, err = locales.ReadFile(asset); err != nil {
|
|
|
|
data = []byte("{}")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|