新增中继启用控制、列表展示、编辑和清除逻辑,保留安全配置边界。
This commit is contained in:
1 parent
a71d57e848
commit
3cca3438b8
13 files changed
+332
-24
No files matched your search
@@ -84,8 +84,7 @@ func validateMailListeners(smtp SMTPSectionConfig, email EmailSectionConfig) err
|
||||
if security != MailSecurityBoth && security != MailSecuritySTARTTLS && security != MailSecurityImplicitTLS {
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
relayConfigured := smtpRelayConfigured(smtp.Relay)
|
||||
if relayConfigured && security != MailSecurityBoth {
|
||||
if smtpRelayEnabled(smtp.Relay) && security != MailSecurityBoth {
|
||||
relaySecurity := NormalizeMailSecurity(smtp.Relay.Security)
|
||||
if relaySecurity != security {
|
||||
return ErrInvalidSMTP
|
||||
|
||||
@@ -870,6 +870,7 @@ func applySectionPayload(current RuntimeConfig, section Section, payload json.Ra
|
||||
return RuntimeConfig{}, errors.New("configuration payload is empty")
|
||||
}
|
||||
candidate := cloneRuntimeConfig(current)
|
||||
clearRelay := false
|
||||
switch section {
|
||||
case SectionRoot:
|
||||
var value struct {
|
||||
@@ -895,7 +896,15 @@ func applySectionPayload(current RuntimeConfig, section Section, payload json.Ra
|
||||
if value.SMTPSectionConfig.Relay.Password.Configured() {
|
||||
return RuntimeConfig{}, ErrInvalidSecretChange
|
||||
}
|
||||
value.SMTPSectionConfig.Relay.Password = current.SMTP.Relay.Password
|
||||
clearRelay = value.SMTPSectionConfig.Relay.Clear
|
||||
if !value.SMTPSectionConfig.Relay.relayEnabledSet {
|
||||
value.SMTPSectionConfig.Relay.RelayEnabled = current.SMTP.Relay.RelayEnabled
|
||||
}
|
||||
if clearRelay {
|
||||
clearSMTPRelay(&value.SMTPSectionConfig.Relay)
|
||||
} else {
|
||||
value.SMTPSectionConfig.Relay.Password = current.SMTP.Relay.Password
|
||||
}
|
||||
if smtpRelayConfigured(value.SMTPSectionConfig.Relay) && value.SMTPSectionConfig.Relay.Security == "" {
|
||||
if ResolveMailSecurity(value.SMTPSectionConfig.Listener) == MailSecurityImplicitTLS {
|
||||
value.SMTPSectionConfig.Relay.Security = MailSecurityImplicitTLS
|
||||
@@ -923,7 +932,15 @@ func applySectionPayload(current RuntimeConfig, section Section, payload json.Ra
|
||||
if value.MailSectionConfig.Relay.Password.Configured() {
|
||||
return RuntimeConfig{}, ErrInvalidSecretChange
|
||||
}
|
||||
value.MailSectionConfig.Relay.Password = current.SMTP.Relay.Password
|
||||
clearRelay = value.MailSectionConfig.Relay.Clear
|
||||
if !value.MailSectionConfig.Relay.relayEnabledSet {
|
||||
value.MailSectionConfig.Relay.RelayEnabled = current.SMTP.Relay.RelayEnabled
|
||||
}
|
||||
if clearRelay {
|
||||
clearSMTPRelay(&value.MailSectionConfig.Relay)
|
||||
} else {
|
||||
value.MailSectionConfig.Relay.Password = current.SMTP.Relay.Password
|
||||
}
|
||||
if smtpRelayConfigured(value.Relay) && value.Relay.Security == "" {
|
||||
if ResolveMailSecurity(value.Listener) == MailSecurityImplicitTLS {
|
||||
value.Relay.Security = MailSecurityImplicitTLS
|
||||
@@ -980,10 +997,29 @@ func applySectionPayload(current RuntimeConfig, section Section, payload json.Ra
|
||||
if err := applySecretChanges(&candidate, section, changes, vault); err != nil {
|
||||
return RuntimeConfig{}, err
|
||||
}
|
||||
if clearRelay {
|
||||
clearSMTPRelay(&candidate.SMTP.Relay)
|
||||
}
|
||||
candidate.SetupState = candidate.Root.SetupState
|
||||
return candidate, nil
|
||||
}
|
||||
|
||||
func clearSMTPRelay(relay *SMTPConfig) {
|
||||
if relay == nil {
|
||||
return
|
||||
}
|
||||
relay.RelayEnabled = false
|
||||
relay.RelayHost = ""
|
||||
relay.RelayPort = 0
|
||||
relay.Username = ""
|
||||
relay.Password = StoredSecret{}
|
||||
relay.Security = ""
|
||||
relay.From = ""
|
||||
relay.FromDomain = ""
|
||||
relay.Clear = false
|
||||
relay.relayEnabledSet = true
|
||||
}
|
||||
|
||||
func setCandidateGeneration(candidate *RuntimeConfig, generation uint64) {
|
||||
if candidate == nil {
|
||||
return
|
||||
|
||||
@@ -763,6 +763,7 @@ func validLegacyDatabasePassword(value string) bool {
|
||||
|
||||
func legacySMTPConfig(root string, vault *credential.Vault, legacy legacyMigrationConfig) (SMTPConfig, []string, error) {
|
||||
relay := SMTPConfig{
|
||||
RelayEnabled: legacy.SMTPHost != "" || legacy.SMTPPort != 0 || legacy.SMTPUsername != "" || legacy.SMTPFrom != "" || legacy.SMTPPassword != "",
|
||||
RelayHost: legacy.SMTPHost,
|
||||
RelayPort: legacy.SMTPPort,
|
||||
Username: legacy.SMTPUsername,
|
||||
|
||||
+41
-1
@@ -1,6 +1,9 @@
|
||||
package config
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SetupState string
|
||||
|
||||
@@ -82,6 +85,7 @@ type DatabaseConfig struct {
|
||||
}
|
||||
|
||||
type SMTPConfig struct {
|
||||
RelayEnabled bool `json:"enabled"`
|
||||
RelayHost string `json:"relayHost"`
|
||||
RelayPort int `json:"relayPort"`
|
||||
Username string `json:"username"`
|
||||
@@ -91,6 +95,42 @@ type SMTPConfig struct {
|
||||
FromDomain string `json:"fromDomain"`
|
||||
VerificationEnabled bool `json:"verificationEnabled"`
|
||||
LoginPolicy string `json:"loginPolicy"`
|
||||
Clear bool `json:"clear,omitempty"`
|
||||
relayEnabledSet bool `json:"-"`
|
||||
}
|
||||
|
||||
func (s *SMTPConfig) UnmarshalJSON(data []byte) error {
|
||||
type smtpConfigAlias SMTPConfig
|
||||
var value smtpConfigAlias
|
||||
if err := json.Unmarshal(data, &value); err != nil {
|
||||
return err
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
*s = SMTPConfig(value)
|
||||
_, enabledProvided := fields["enabled"]
|
||||
configured := smtpRelayFieldsConfigured(*s)
|
||||
s.relayEnabledSet = enabledProvided && (s.RelayEnabled || configured)
|
||||
if !enabledProvided {
|
||||
s.RelayEnabled = configured
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SMTPConfig) MarshalJSON() ([]byte, error) {
|
||||
type smtpConfigAlias SMTPConfig
|
||||
data, err := json.Marshal(smtpConfigAlias(s))
|
||||
if err != nil || s.RelayEnabled || s.relayEnabledSet {
|
||||
return data, err
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &fields); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
delete(fields, "enabled")
|
||||
return json.Marshal(fields)
|
||||
}
|
||||
|
||||
type EmailConfig struct {
|
||||
|
||||
@@ -288,6 +288,7 @@ func CurrentSMTPRuntimeSettings() (SMTPRuntimeSettings, error) {
|
||||
return SMTPRuntimeSettings{}, fmt.Errorf("runtime configuration is unavailable")
|
||||
}
|
||||
settings.Relay = SMTPConfig{
|
||||
RelayEnabled: cfg.SMTPHost != "" || cfg.SMTPPort != 0 || cfg.SMTPUsername != "" || cfg.SMTPFrom != "" || cfg.SMTPPassword != "",
|
||||
RelayHost: cfg.SMTPHost,
|
||||
RelayPort: cfg.SMTPPort,
|
||||
Username: cfg.SMTPUsername,
|
||||
|
||||
@@ -152,8 +152,7 @@ func validateSMTP(smtp SMTPConfig) error {
|
||||
return ErrInvalidSMTP
|
||||
}
|
||||
}
|
||||
relayConfigured := smtpRelayConfigured(smtp)
|
||||
if !relayConfigured {
|
||||
if !smtpRelayEnabled(smtp) {
|
||||
return nil
|
||||
}
|
||||
if smtp.RelayHost == "" || smtp.RelayPort == 0 || smtp.Security == "" || smtp.From == "" || smtp.FromDomain == "" {
|
||||
@@ -169,12 +168,22 @@ func validateSMTP(smtp SMTPConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// smtpRelayConfigured 只把实际连接或发件身份字段视为启用中继。
|
||||
// 推荐端口、统一加密方式和默认发件域名单独存在时,中继仍保持可选状态。
|
||||
func smtpRelayConfigured(smtp SMTPConfig) bool {
|
||||
func smtpRelayFieldsConfigured(smtp SMTPConfig) bool {
|
||||
return smtp.RelayHost != "" || smtp.Username != "" || smtp.Password.Configured() || smtp.From != ""
|
||||
}
|
||||
|
||||
func smtpRelayConfigured(smtp SMTPConfig) bool {
|
||||
return smtpRelayFieldsConfigured(smtp) || smtp.RelayEnabled
|
||||
}
|
||||
|
||||
func smtpRelayEnabled(smtp SMTPConfig) bool {
|
||||
return smtp.RelayEnabled || (!smtp.relayEnabledSet && smtpRelayFieldsConfigured(smtp))
|
||||
}
|
||||
|
||||
func (smtp SMTPConfig) RelayActive() bool {
|
||||
return smtpRelayEnabled(smtp)
|
||||
}
|
||||
|
||||
func validateSSL(ssl SSLConfig) error {
|
||||
if err := validateDNSProvider(ssl); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user