Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/prism/proxy/internal/geodata"
|
||||
)
|
||||
|
||||
// Display merges stored settings with effective defaults for the settings UI.
|
||||
func Display(raw map[string]string) map[string]string {
|
||||
out := make(map[string]string, len(raw)+8)
|
||||
for k, v := range raw {
|
||||
out[k] = v
|
||||
}
|
||||
applyGeoDisplayDefaults(out)
|
||||
applyScalarDisplayDefaults(out)
|
||||
applyLocaleDisplayDefaults(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// NormalizeSubmit clears values that match built-in defaults so empty means default.
|
||||
func NormalizeSubmit(req map[string]string) {
|
||||
normalizeGeoSubmit(req)
|
||||
normalizeLocaleSubmit(req)
|
||||
}
|
||||
|
||||
func applyGeoDisplayDefaults(out map[string]string) {
|
||||
def := geodata.DefaultConfig()
|
||||
if strings.TrimSpace(out["geo_country_mmdb_url"]) == "" {
|
||||
out["geo_country_mmdb_url"] = def.CountryMmdbURL
|
||||
}
|
||||
if strings.TrimSpace(out["geo_geoip_url"]) == "" {
|
||||
out["geo_geoip_url"] = def.GeoIPURL
|
||||
}
|
||||
if strings.TrimSpace(out["geo_geosite_url"]) == "" {
|
||||
out["geo_geosite_url"] = def.GeoSiteURL
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeGeoSubmit(req map[string]string) {
|
||||
def := geodata.DefaultConfig()
|
||||
if strings.TrimSpace(req["geo_country_mmdb_url"]) == def.CountryMmdbURL {
|
||||
req["geo_country_mmdb_url"] = ""
|
||||
}
|
||||
if strings.TrimSpace(req["geo_geoip_url"]) == def.GeoIPURL {
|
||||
req["geo_geoip_url"] = ""
|
||||
}
|
||||
if strings.TrimSpace(req["geo_geosite_url"]) == def.GeoSiteURL {
|
||||
req["geo_geosite_url"] = ""
|
||||
}
|
||||
}
|
||||
|
||||
func applyScalarDisplayDefaults(out map[string]string) {
|
||||
if strings.TrimSpace(out["login_max_attempts"]) == "" {
|
||||
out["login_max_attempts"] = strconv.Itoa(5)
|
||||
}
|
||||
if strings.TrimSpace(out["login_lock_minutes"]) == "" {
|
||||
out["login_lock_minutes"] = strconv.Itoa(15)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package settings
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDisplayFillsGeoDefaults(t *testing.T) {
|
||||
out := Display(map[string]string{
|
||||
"geo_country_mmdb_url": "",
|
||||
"geo_geoip_url": "",
|
||||
"geo_geosite_url": "",
|
||||
})
|
||||
if out["geo_country_mmdb_url"] == "" || out["geo_geoip_url"] == "" || out["geo_geosite_url"] == "" {
|
||||
t.Fatalf("expected geo defaults, got %+v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeSubmitClearsDefaultGeoURLs(t *testing.T) {
|
||||
out := Display(map[string]string{})
|
||||
req := map[string]string{
|
||||
"geo_country_mmdb_url": out["geo_country_mmdb_url"],
|
||||
"geo_geoip_url": out["geo_geoip_url"],
|
||||
"geo_geosite_url": out["geo_geosite_url"],
|
||||
}
|
||||
NormalizeSubmit(req)
|
||||
if req["geo_country_mmdb_url"] != "" || req["geo_geoip_url"] != "" || req["geo_geosite_url"] != "" {
|
||||
t.Fatalf("expected empty submit, got %+v", req)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package settings
|
||||
|
||||
// Well-known settings keys stored in SQLite.
|
||||
const (
|
||||
KeyHTTPPort = "http_port"
|
||||
KeySocksPort = "socks_port"
|
||||
KeyAPIPort = "api_port"
|
||||
KeyMihomoAPIPort = "mihomo_api_port"
|
||||
KeyAPISecret = "api_secret"
|
||||
KeyAdminPassword = "admin_password"
|
||||
KeyAuthEnabled = "auth_enabled"
|
||||
KeyLogLevel = "log_level"
|
||||
KeyHealthIntervalSec = "health_interval_sec"
|
||||
KeySubSyncEnabled = "sub_sync_enabled"
|
||||
KeyDefaultPolicy = "default_policy"
|
||||
KeyGeoAutoDownload = "geo_auto_download"
|
||||
KeyLoginMaxAttempts = "login_max_attempts"
|
||||
KeyLoginLockMinutes = "login_lock_minutes"
|
||||
KeyProxyAuthUser = "proxy_auth_user"
|
||||
KeyProxyAuthPass = "proxy_auth_pass"
|
||||
KeyGeoCountryMmdbURL = "geo_country_mmdb_url"
|
||||
KeyGeoGeoIPURL = "geo_geoip_url"
|
||||
KeyGeoGeoSiteURL = "geo_geosite_url"
|
||||
KeyUILocale = "ui_locale"
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
package settings
|
||||
|
||||
import "strings"
|
||||
|
||||
var validUILocales = map[string]bool{
|
||||
"zh-CN": true,
|
||||
"zh-TW": true,
|
||||
"en": true,
|
||||
}
|
||||
|
||||
func NormalizeUILocale(v string) string {
|
||||
v = strings.TrimSpace(v)
|
||||
if validUILocales[v] {
|
||||
return v
|
||||
}
|
||||
return "zh-CN"
|
||||
}
|
||||
|
||||
func applyLocaleDisplayDefaults(out map[string]string) {
|
||||
if strings.TrimSpace(out[KeyUILocale]) == "" {
|
||||
out[KeyUILocale] = "zh-CN"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeLocaleSubmit(req map[string]string) {
|
||||
if v, ok := req[KeyUILocale]; ok {
|
||||
req[KeyUILocale] = NormalizeUILocale(v)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user