2019-06-10 01:33:19 -07:00
|
|
|
package home
|
2018-11-21 10:42:55 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-05-21 04:55:42 -07:00
|
|
|
"io"
|
2018-11-21 10:42:55 -07:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-12-29 07:23:42 -07:00
|
|
|
|
2021-12-16 10:54:59 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2019-02-25 06:44:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 07:40:31 -07:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2018-11-21 10:42:55 -07:00
|
|
|
)
|
|
|
|
|
2021-11-15 04:35:46 -07:00
|
|
|
// TODO(a.garipov): Get rid of a global or generate from .twosky.json.
|
2021-07-29 07:40:31 -07:00
|
|
|
var allowedLanguages = stringutil.NewSet(
|
2021-04-20 06:26:19 -07:00
|
|
|
"be",
|
|
|
|
"bg",
|
|
|
|
"cs",
|
|
|
|
"da",
|
|
|
|
"de",
|
|
|
|
"en",
|
|
|
|
"es",
|
|
|
|
"fa",
|
2021-11-15 04:35:46 -07:00
|
|
|
"fi",
|
2021-04-20 06:26:19 -07:00
|
|
|
"fr",
|
|
|
|
"hr",
|
|
|
|
"hu",
|
|
|
|
"id",
|
|
|
|
"it",
|
|
|
|
"ja",
|
|
|
|
"ko",
|
|
|
|
"nl",
|
|
|
|
"no",
|
|
|
|
"pl",
|
|
|
|
"pt-br",
|
|
|
|
"pt-pt",
|
|
|
|
"ro",
|
|
|
|
"ru",
|
|
|
|
"si-lk",
|
|
|
|
"sk",
|
|
|
|
"sl",
|
|
|
|
"sr-cs",
|
|
|
|
"sv",
|
|
|
|
"th",
|
|
|
|
"tr",
|
2021-11-15 04:35:46 -07:00
|
|
|
"uk",
|
2021-04-20 06:26:19 -07:00
|
|
|
"vi",
|
|
|
|
"zh-cn",
|
|
|
|
"zh-hk",
|
|
|
|
"zh-tw",
|
|
|
|
)
|
2018-11-21 10:42:55 -07:00
|
|
|
|
|
|
|
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2018-11-22 07:56:57 -07:00
|
|
|
log.Printf("config.Language is %s", config.Language)
|
|
|
|
_, err := fmt.Fprintf(w, "%s\n", config.Language)
|
2018-11-21 10:42:55 -07:00
|
|
|
if err != nil {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("Unable to write response json: %s", err)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
2020-11-23 04:14:08 -07:00
|
|
|
// This use of ReadAll is safe, because request's body is now limited.
|
2021-05-21 04:55:42 -07:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-11-21 10:42:55 -07:00
|
|
|
if err != nil {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("failed to read request body: %s", err)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
language := strings.TrimSpace(string(body))
|
|
|
|
if language == "" {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := "empty language specified"
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2021-04-20 06:26:19 -07:00
|
|
|
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
2021-04-20 06:26:19 -07:00
|
|
|
|
|
|
|
if !allowedLanguages.Has(language) {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("unknown language specified: %s", language)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2021-04-20 06:26:19 -07:00
|
|
|
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 06:50:02 -07:00
|
|
|
func() {
|
|
|
|
config.Lock()
|
|
|
|
defer config.Unlock()
|
|
|
|
|
|
|
|
config.Language = language
|
|
|
|
}()
|
|
|
|
|
2019-10-30 01:52:58 -07:00
|
|
|
onConfigModified()
|
2021-12-16 10:54:59 -07:00
|
|
|
aghhttp.OK(w)
|
2018-11-21 10:42:55 -07:00
|
|
|
}
|