469 lines
19 KiB
Go
469 lines
19 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"net/mail"
|
|
"net/url"
|
|
"sea-vhost/internal/scope"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"sea-vhost/internal/service"
|
|
)
|
|
|
|
type secretServiceConfig struct {
|
|
AutoCreate bool `json:"autoCreate,omitempty"`
|
|
UserPasswordLogin *bool `json:"userPasswordLogin,omitempty"`
|
|
AdminPasswordLogin *bool `json:"adminPasswordLogin,omitempty"`
|
|
Host string `json:"host,omitempty"`
|
|
Port int `json:"port,omitempty"`
|
|
Username string `json:"username,omitempty"`
|
|
Secret string `json:"secret,omitempty"`
|
|
From string `json:"from,omitempty"`
|
|
AdminRecipients string `json:"adminRecipients,omitempty"`
|
|
StartTLS bool `json:"startTls,omitempty"`
|
|
Issuer string `json:"issuer,omitempty"`
|
|
ClientID string `json:"clientId,omitempty"`
|
|
RedirectURI string `json:"redirectUri,omitempty"`
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
}
|
|
|
|
func (s *Server) adminOverview(w http.ResponseWriter, r *http.Request) {
|
|
users, err := s.store.ListTenants(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取用户")
|
|
return
|
|
}
|
|
projects, err := s.store.ListAdminProjects(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取虚拟主机")
|
|
return
|
|
}
|
|
domains, err := s.store.AllDomains(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取域名")
|
|
return
|
|
}
|
|
ips, err := s.store.ListIPAddresses(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取 IP 地址池")
|
|
return
|
|
}
|
|
logs, err := s.store.AdminActivities(r.Context(), 100)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取操作日志")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"users": users, "projects": projects, "domains": domains, "ipAddresses": ips, "logs": logs})
|
|
}
|
|
|
|
func (s *Server) adminSettings(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodGet {
|
|
smtp := s.readServiceConfig(r, "smtp_config")
|
|
oidc := s.readServiceConfig(r, "oidc_config")
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"domainApproval": s.store.DomainApproval(r.Context(), s.cfg.DomainApproval),
|
|
"filingRequired": s.store.Setting(r.Context(), "filing_required", "false") == "true",
|
|
"cdnCnameTarget": s.store.Setting(r.Context(), "cdn_cname_target", ""),
|
|
"graceDays": settingInt(s.store.Setting(r.Context(), "lifecycle_grace_days", "3"), 3),
|
|
"recycleDays": settingInt(s.store.Setting(r.Context(), "lifecycle_recycle_days", "7"), 7),
|
|
"backupRetentionDays": settingInt(s.store.Setting(r.Context(), "backup_retention_days", "14"), 14),
|
|
"smtp": map[string]any{"enabled": smtp.Enabled, "host": smtp.Host, "port": smtp.Port, "username": smtp.Username, "from": smtp.From, "adminRecipients": smtp.AdminRecipients, "startTls": smtp.StartTLS, "secretConfigured": smtp.Secret != ""},
|
|
"oidc": map[string]any{"enabled": oidc.Enabled, "issuer": oidc.Issuer, "clientId": oidc.ClientID, "redirectUri": oidc.RedirectURI, "secretConfigured": oidc.Secret != "", "bindingOnly": !oidc.AutoCreate, "autoCreate": oidc.AutoCreate, "userPasswordLogin": oidcPasswordAllowed(oidc, "tenant"), "adminPasswordLogin": oidcPasswordAllowed(oidc, "admin"), "passwordLogin": oidcPasswordAllowed(oidc, "tenant")},
|
|
})
|
|
return
|
|
}
|
|
var input struct {
|
|
DomainApproval bool `json:"domainApproval"`
|
|
FilingRequired bool `json:"filingRequired"`
|
|
CDNCnameTarget string `json:"cdnCnameTarget"`
|
|
GraceDays int `json:"graceDays"`
|
|
RecycleDays int `json:"recycleDays"`
|
|
BackupRetentionDays int `json:"backupRetentionDays"`
|
|
SMTP secretServiceConfig `json:"smtp"`
|
|
OIDC secretServiceConfig `json:"oidc"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
input.CDNCnameTarget = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(input.CDNCnameTarget), "."))
|
|
if input.GraceDays != 3 || input.RecycleDays != 7 || input.BackupRetentionDays != 14 {
|
|
writeError(w, http.StatusUnprocessableEntity, "当前生命周期固定为宽限 3 天、回收保留 7 天、备份保留 14 天")
|
|
return
|
|
}
|
|
if input.CDNCnameTarget != "" && net.ParseIP(input.CDNCnameTarget) == nil && !validHostname(input.CDNCnameTarget) {
|
|
writeError(w, http.StatusUnprocessableEntity, "CDN CNAME 目标无效")
|
|
return
|
|
}
|
|
if err := s.validateServiceConfigs(r, &input.SMTP, &input.OIDC); err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
if err := s.store.SetDomainApproval(r.Context(), input.DomainApproval); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法保存设置")
|
|
return
|
|
}
|
|
for key, value := range map[string]string{"filing_required": strconv.FormatBool(input.FilingRequired), "cdn_cname_target": input.CDNCnameTarget, "lifecycle_grace_days": "3", "lifecycle_recycle_days": "7", "backup_retention_days": "14"} {
|
|
if err := s.store.SetSetting(r.Context(), key, value); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法保存设置")
|
|
return
|
|
}
|
|
}
|
|
if err := s.writeServiceConfig(r, "smtp_config", input.SMTP); err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
if err := s.writeServiceConfig(r, "oidc_config", input.OIDC); err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
s.store.Log(r.Context(), nil, user.ID, "update", "system_settings", "", requestClientIP(r), true, "更新系统、通知与 SSO 设置")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) validateServiceConfigs(r *http.Request, smtpConfig, oidcConfig *secretServiceConfig) error {
|
|
smtpConfig.Host = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(smtpConfig.Host), "."))
|
|
smtpConfig.Username = strings.TrimSpace(smtpConfig.Username)
|
|
smtpConfig.From = strings.TrimSpace(smtpConfig.From)
|
|
smtpConfig.AdminRecipients = strings.TrimSpace(smtpConfig.AdminRecipients)
|
|
issuer := strings.TrimSuffix(strings.TrimSpace(oidcConfig.Issuer), "/")
|
|
oidcConfig.Issuer = issuer
|
|
oidcConfig.ClientID = strings.TrimSpace(oidcConfig.ClientID)
|
|
oidcConfig.RedirectURI = strings.TrimSpace(oidcConfig.RedirectURI)
|
|
if smtpConfig.Enabled {
|
|
if net.ParseIP(smtpConfig.Host) == nil && !validHostname(smtpConfig.Host) {
|
|
return errors.New("SMTP 主机无效")
|
|
}
|
|
if smtpConfig.Port < 1 || smtpConfig.Port > 65535 {
|
|
return errors.New("SMTP 端口无效")
|
|
}
|
|
if parsed, err := mail.ParseAddress(smtpConfig.From); err != nil || parsed.Address != smtpConfig.From {
|
|
return errors.New("SMTP 发件地址无效")
|
|
}
|
|
addresses := strings.FieldsFunc(smtpConfig.AdminRecipients, func(char rune) bool { return char == ',' || char == ';' || char == '\n' })
|
|
if len(addresses) == 0 {
|
|
return errors.New("至少需要一个管理员收件地址")
|
|
}
|
|
for _, address := range addresses {
|
|
address = strings.TrimSpace(address)
|
|
if parsed, err := mail.ParseAddress(address); err != nil || parsed.Address != address {
|
|
return errors.New("管理员收件地址格式无效")
|
|
}
|
|
}
|
|
existing := s.readServiceConfig(r, "smtp_config")
|
|
if smtpConfig.Username != "" && strings.TrimSpace(smtpConfig.Secret) == "" && existing.Secret == "" {
|
|
return errors.New("SMTP 认证密码不能为空")
|
|
}
|
|
if smtpConfig.Username != "" && smtpConfig.Port != 465 && !smtpConfig.StartTLS {
|
|
return errors.New("SMTP 认证必须启用 TLS")
|
|
}
|
|
}
|
|
existingOIDC := s.readServiceConfig(r, "oidc_config")
|
|
if oidcConfig.UserPasswordLogin == nil {
|
|
oidcConfig.UserPasswordLogin = existingOIDC.UserPasswordLogin
|
|
}
|
|
if oidcConfig.AdminPasswordLogin == nil {
|
|
oidcConfig.AdminPasswordLogin = existingOIDC.AdminPasswordLogin
|
|
}
|
|
if !oidcPasswordAllowed(*oidcConfig, "tenant") || !oidcPasswordAllowed(*oidcConfig, "admin") {
|
|
candidate := *oidcConfig
|
|
if candidate.Secret == "" {
|
|
candidate.Secret = existingOIDC.Secret
|
|
}
|
|
if !validOIDCConfig(candidate) {
|
|
return errors.New("关闭密码登录前必须启用并完整配置 SSO")
|
|
}
|
|
if !oidcPasswordAllowed(candidate, "admin") {
|
|
current := currentUser(r)
|
|
if _, err := s.store.OIDCIdentity(r.Context(), current.ID, oidcProviderKey(candidate)); err != nil {
|
|
return errors.New("请先为当前管理员绑定该 SSO 身份,再关闭管理员密码登录")
|
|
}
|
|
}
|
|
}
|
|
if oidcConfig.Enabled {
|
|
issuerURL, err := url.Parse(oidcConfig.Issuer)
|
|
if err != nil || issuerURL.Scheme != "https" || issuerURL.Host == "" || issuerURL.User != nil || issuerURL.RawQuery != "" || issuerURL.Fragment != "" {
|
|
return errors.New("OIDC Issuer 必须是有效的 HTTPS 地址")
|
|
}
|
|
redirectURL, err := url.Parse(oidcConfig.RedirectURI)
|
|
managementDomain := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(s.cfg.ManagementDomain), "."))
|
|
if err != nil || redirectURL.Scheme != "https" || !strings.EqualFold(redirectURL.Hostname(), managementDomain) || redirectURL.Path != "/api/v1/auth/oidc/callback" || redirectURL.RawQuery != "" || redirectURL.Fragment != "" || redirectURL.User != nil {
|
|
return errors.New("OIDC 回调地址必须是管理域名下的 /api/v1/auth/oidc/callback")
|
|
}
|
|
if oidcConfig.ClientID == "" || len(oidcConfig.ClientID) > 255 {
|
|
return errors.New("OIDC Client ID 无效")
|
|
}
|
|
existing := s.readServiceConfig(r, "oidc_config")
|
|
if strings.TrimSpace(oidcConfig.Secret) == "" && existing.Secret == "" {
|
|
return errors.New("OIDC Client Secret 不能为空")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Server) readServiceConfig(r *http.Request, key string) secretServiceConfig {
|
|
raw := s.store.Setting(r.Context(), key, "{}")
|
|
var cfg secretServiceConfig
|
|
if json.Unmarshal([]byte(raw), &cfg) != nil {
|
|
return secretServiceConfig{}
|
|
}
|
|
if cfg.Secret != "" {
|
|
encrypted, err := base64.RawStdEncoding.DecodeString(cfg.Secret)
|
|
if err == nil {
|
|
if value, openErr := s.secretBox.Open("system/"+key, encrypted); openErr == nil {
|
|
cfg.Secret = value
|
|
} else {
|
|
cfg.Secret = ""
|
|
}
|
|
}
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
func (s *Server) writeServiceConfig(r *http.Request, key string, cfg secretServiceConfig) error {
|
|
existing := s.readServiceConfig(r, key)
|
|
if strings.TrimSpace(cfg.Secret) == "" {
|
|
cfg.Secret = existing.Secret
|
|
}
|
|
if cfg.Secret != "" {
|
|
encrypted, err := s.secretBox.Seal("system/"+key, cfg.Secret)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg.Secret = base64.RawStdEncoding.EncodeToString(encrypted)
|
|
}
|
|
data, err := json.Marshal(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.store.SetSetting(r.Context(), key, string(data))
|
|
}
|
|
|
|
func (s *Server) adminIPs(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method == http.MethodGet {
|
|
items, err := s.store.ListIPAddresses(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取 IP 地址池")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"ipAddresses": items})
|
|
return
|
|
}
|
|
var input struct {
|
|
Address string `json:"address"`
|
|
AllocationMode string `json:"allocationMode"`
|
|
Label string `json:"label"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
ip := net.ParseIP(strings.TrimSpace(input.Address))
|
|
if ip == nil {
|
|
writeError(w, http.StatusUnprocessableEntity, "IP 地址无效")
|
|
return
|
|
}
|
|
family := "ipv6"
|
|
if ip.To4() != nil {
|
|
family = "ipv4"
|
|
}
|
|
if input.AllocationMode != "shared" && input.AllocationMode != "dedicated" {
|
|
writeError(w, http.StatusUnprocessableEntity, "IP 分配方式无效")
|
|
return
|
|
}
|
|
id, err := s.store.CreateIPAddress(r.Context(), ip.String(), family, input.AllocationMode, strings.TrimSpace(input.Label))
|
|
if err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, "IP 已存在或无法添加")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, map[string]any{"id": id})
|
|
}
|
|
|
|
func (s *Server) adminIP(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "IP 编号无效")
|
|
return
|
|
}
|
|
if err = s.store.DeleteIPAddress(r.Context(), id); err != nil {
|
|
writeError(w, http.StatusConflict, "已分配的 IP 不能删除")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) adminProjectNetwork(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "虚拟主机编号无效")
|
|
return
|
|
}
|
|
var input struct {
|
|
IPID *uint64 `json:"ipId"`
|
|
Dedicated bool `json:"dedicated"`
|
|
HTTPPort int `json:"httpPort"`
|
|
HTTPSPort int `json:"httpsPort"`
|
|
}
|
|
if err = decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
if input.HTTPPort < 1 || input.HTTPPort > 65535 || input.HTTPSPort < 1 || input.HTTPSPort > 65535 || input.HTTPPort == input.HTTPSPort {
|
|
writeError(w, http.StatusUnprocessableEntity, "HTTP/HTTPS 端口无效")
|
|
return
|
|
}
|
|
if input.Dedicated && input.IPID == nil {
|
|
writeError(w, http.StatusUnprocessableEntity, "独立 IP 模式必须选择 IP")
|
|
return
|
|
}
|
|
if err = s.projects.ConfigureProjectNetwork(r.Context(), id, input.IPID, input.Dedicated, input.HTTPPort, input.HTTPSPort); err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, "无法更新网络配置")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
s.store.Log(r.Context(), nil, user.ID, "update", "project_network", strconv.FormatUint(id, 10), requestClientIP(r), true, "更新虚拟主机 IP 与公开端口")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) adminPanelSites(w http.ResponseWriter, r *http.Request) {
|
|
items, err := s.projects.UnmanagedPanelSites(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusBadGateway, "无法读取宝塔站点")
|
|
return
|
|
}
|
|
catalog, err := s.projects.RuntimeCatalog(r.Context())
|
|
if err != nil {
|
|
writeError(w, http.StatusBadGateway, "无法读取运行环境")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"sites": items, "versions": catalog})
|
|
}
|
|
|
|
func (s *Server) adminAdoptPanelSite(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
writeError(w, http.StatusBadRequest, "宝塔站点编号无效")
|
|
return
|
|
}
|
|
var input struct {
|
|
PlanID uint64 `json:"planId"`
|
|
TenantID uint64 `json:"tenantId"`
|
|
Name string `json:"name"`
|
|
Runtime string `json:"runtime"`
|
|
Version string `json:"version"`
|
|
RunPath string `json:"runPath"`
|
|
AutoSSL bool `json:"autoSSL"`
|
|
}
|
|
if err = decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
created, err := s.projects.Adopt(r.Context(), service.AdoptProjectInput{NodeID: scope.Node(r.Context()), PlanID: input.PlanID, TenantID: input.TenantID, PanelSiteID: id, Name: input.Name, Runtime: input.Runtime, Version: input.Version, RunPath: input.RunPath, AutoSSL: input.AutoSSL})
|
|
user := currentUser(r)
|
|
if err != nil {
|
|
s.store.Log(r.Context(), nil, user.ID, "adopt", "panel_site", strconv.FormatInt(id, 10), requestClientIP(r), false, err.Error())
|
|
writeProjectMutationError(w, http.StatusUnprocessableEntity, err)
|
|
return
|
|
}
|
|
s.store.Log(scope.WithProject(r.Context(), created.Project.ID, created.Project.NodeID), &created.Project.TenantID, user.ID, "adopt", "panel_site", strconv.FormatInt(id, 10), requestClientIP(r), true, "接管宝塔站点到虚拟主机 "+created.Project.HostName)
|
|
writeJSON(w, http.StatusCreated, created)
|
|
}
|
|
|
|
func (s *Server) adminDomainMetadata(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "域名编号无效")
|
|
return
|
|
}
|
|
var input struct {
|
|
ResolutionMode string `json:"resolutionMode"`
|
|
CNAMETarget string `json:"cnameTarget"`
|
|
FilingStatus string `json:"filingStatus"`
|
|
FilingNumber string `json:"filingNumber"`
|
|
FilingNote string `json:"filingNote"`
|
|
}
|
|
if err = decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
if input.ResolutionMode != "direct" && input.ResolutionMode != "cname" && input.ResolutionMode != "cdn" {
|
|
writeError(w, http.StatusUnprocessableEntity, "解析模式无效")
|
|
return
|
|
}
|
|
if input.FilingStatus != "exempt" && input.FilingStatus != "pending" && input.FilingStatus != "approved" && input.FilingStatus != "rejected" {
|
|
writeError(w, http.StatusUnprocessableEntity, "备案状态无效")
|
|
return
|
|
}
|
|
input.CNAMETarget = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(input.CNAMETarget), "."))
|
|
input.FilingNumber = strings.TrimSpace(input.FilingNumber)
|
|
input.FilingNote = strings.TrimSpace(input.FilingNote)
|
|
if input.ResolutionMode == "cdn" && input.CNAMETarget == "" {
|
|
input.CNAMETarget = s.store.Setting(r.Context(), "cdn_cname_target", "")
|
|
}
|
|
if input.ResolutionMode == "direct" {
|
|
input.CNAMETarget = ""
|
|
}
|
|
if (input.ResolutionMode != "direct" && !validNodeDomain(input.CNAMETarget)) || len([]rune(input.FilingNumber)) > 80 || len([]rune(input.FilingNote)) > 500 || strings.ContainsAny(input.FilingNumber+input.FilingNote, "\x00") {
|
|
writeError(w, 422, "请检查 CNAME 目标、备案号和备注长度;CDN 模式需配置目标域名")
|
|
return
|
|
}
|
|
err = s.store.UpdateDomainPolicy(r.Context(), id, input.ResolutionMode, input.CNAMETarget, input.FilingStatus, input.FilingNumber, input.FilingNote)
|
|
s.resourceAudit(r, "update", "domain_policy", id, err, "更新自定义域名解析指引与备案信息")
|
|
if err != nil {
|
|
writeError(w, 422, "域名配置未保存,请检查是否为可管理的自定义域名")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) adminTenantExpiry(w http.ResponseWriter, r *http.Request) {
|
|
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "用户编号无效")
|
|
return
|
|
}
|
|
var input struct {
|
|
ExpiresAt *time.Time `json:"expiresAt"`
|
|
}
|
|
if err = decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
if err = s.store.SetTenantExpiry(r.Context(), id, input.ExpiresAt); err != nil {
|
|
writeError(w, http.StatusUnprocessableEntity, "无法更新到期时间")
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func settingInt(value string, fallback int) int {
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil || parsed < 0 {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
func validHostname(value string) bool {
|
|
if len(value) > 253 || value == "" {
|
|
return false
|
|
}
|
|
for _, label := range strings.Split(value, ".") {
|
|
if len(label) < 1 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
|
|
return false
|
|
}
|
|
for _, r := range label {
|
|
if !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-') {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|