统一邮件协议加密方式与端口联动
This commit is contained in:
1 parent
0975e26762
commit
b7a4614e5b
13 files changed
+369
-118
No files matched your search
@@ -1,10 +1,17 @@
|
||||
package config
|
||||
|
||||
// 邮件监听与发信中继相互独立。零值表示沿用兼容默认值,不表示关闭服务。
|
||||
const (
|
||||
MailSecurityBoth = "both"
|
||||
MailSecuritySTARTTLS = "starttls"
|
||||
MailSecurityImplicitTLS = "implicit_tls"
|
||||
)
|
||||
|
||||
// 邮件监听与发信中继相互独立。未设置加密模式的旧配置继续同时开放两套监听。
|
||||
type SMTPListenerConfig struct {
|
||||
Port int `json:"port"`
|
||||
STARTTLSPort int `json:"starttlsPort"`
|
||||
TLSPort int `json:"tlsPort"`
|
||||
Port int `json:"port"`
|
||||
STARTTLSPort int `json:"starttlsPort"`
|
||||
TLSPort int `json:"tlsPort"`
|
||||
Security string `json:"security,omitempty"`
|
||||
}
|
||||
|
||||
type IMAPListenerConfig struct {
|
||||
@@ -16,6 +23,32 @@ type POP3ListenerConfig struct {
|
||||
TLSPort int `json:"tlsPort"`
|
||||
}
|
||||
|
||||
func ResolveMailSecurity(listener SMTPListenerConfig) string {
|
||||
switch NormalizeMailSecurity(listener.Security) {
|
||||
case MailSecuritySTARTTLS:
|
||||
return MailSecuritySTARTTLS
|
||||
case MailSecurityImplicitTLS:
|
||||
return MailSecurityImplicitTLS
|
||||
case "", MailSecurityBoth:
|
||||
return MailSecurityBoth
|
||||
default:
|
||||
return listener.Security
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeMailSecurity 保留空值及未知值,只将旧版 TLS 名称规范为当前名称。
|
||||
func NormalizeMailSecurity(value string) string {
|
||||
if value == "tls" {
|
||||
return MailSecurityImplicitTLS
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// PublicMailSecurity 保留旧配置的空值,只规范公开视图中的旧版名称。
|
||||
func PublicMailSecurity(listener SMTPListenerConfig) string {
|
||||
return NormalizeMailSecurity(listener.Security)
|
||||
}
|
||||
|
||||
// ResolveMailPorts 是公开配置视图、冲突校验及真实监听的共同端口来源。
|
||||
func ResolveMailPorts(smtp SMTPSectionConfig, email EmailSectionConfig) MailPorts {
|
||||
portOr := func(port, fallback int) int {
|
||||
@@ -24,7 +57,7 @@ func ResolveMailPorts(smtp SMTPSectionConfig, email EmailSectionConfig) MailPort
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
return MailPorts{
|
||||
ports := MailPorts{
|
||||
SMTP: portOr(smtp.Listener.Port, portOr(email.Policy.SMTPPort, 25)),
|
||||
SMTPSTARTTLS: portOr(smtp.Listener.STARTTLSPort, 587),
|
||||
SMTPTLS: portOr(smtp.Listener.TLSPort, 465),
|
||||
@@ -33,9 +66,34 @@ func ResolveMailPorts(smtp SMTPSectionConfig, email EmailSectionConfig) MailPort
|
||||
POP3: portOr(email.POP3.Port, portOr(email.Policy.POP3Port, 110)),
|
||||
POP3TLS: portOr(email.POP3.TLSPort, 995),
|
||||
}
|
||||
switch ResolveMailSecurity(smtp.Listener) {
|
||||
case MailSecuritySTARTTLS:
|
||||
ports.SMTPTLS = 0
|
||||
ports.IMAP = portOr(email.IMAP.TLSPort, 143)
|
||||
ports.POP3TLS = 0
|
||||
case MailSecurityImplicitTLS:
|
||||
ports.SMTPSTARTTLS = 0
|
||||
ports.IMAP = portOr(email.IMAP.TLSPort, 993)
|
||||
ports.POP3 = 0
|
||||
}
|
||||
return ports
|
||||
}
|
||||
|
||||
func validateMailListeners(smtp SMTPSectionConfig, email EmailSectionConfig) error {
|
||||
if smtp.Listener.Security == MailSecurityBoth {
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
security := ResolveMailSecurity(smtp.Listener)
|
||||
if security != MailSecurityBoth && security != MailSecuritySTARTTLS && security != MailSecurityImplicitTLS {
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
relayConfigured := smtpRelayConfigured(smtp.Relay)
|
||||
if relayConfigured && security != MailSecurityBoth {
|
||||
relaySecurity := NormalizeMailSecurity(smtp.Relay.Security)
|
||||
if relaySecurity != security {
|
||||
return ErrInvalidSMTP
|
||||
}
|
||||
}
|
||||
for _, port := range []int{
|
||||
smtp.Listener.Port, smtp.Listener.STARTTLSPort, smtp.Listener.TLSPort,
|
||||
email.IMAP.TLSPort, email.POP3.Port, email.POP3.TLSPort,
|
||||
|
||||
@@ -146,3 +146,67 @@ func TestOldPartitionPayloadsKeepExistingListenerDefaults(t *testing.T) {
|
||||
t.Fatalf("legacy listeners changed: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnifiedMailSecurityResolvesOnlySelectedClientListeners(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
security string
|
||||
want MailPorts
|
||||
}{
|
||||
{MailSecuritySTARTTLS, MailPorts{SMTP: 25, SMTPSTARTTLS: 587, IMAP: 143, POP3: 110}},
|
||||
{MailSecurityImplicitTLS, MailPorts{SMTP: 25, SMTPTLS: 465, IMAP: 993, POP3TLS: 995}},
|
||||
} {
|
||||
smtp := SMTPSectionConfig{Listener: SMTPListenerConfig{Security: test.security}}
|
||||
if got := ResolveMailPorts(smtp, EmailSectionConfig{}); got != test.want {
|
||||
t.Fatalf("%s ports = %+v, want %+v", test.security, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyMailSecurityKeepsExistingListeners(t *testing.T) {
|
||||
smtp := SMTPSectionConfig{Listener: SMTPListenerConfig{Port: 2525, STARTTLSPort: 2587, TLSPort: 2465}}
|
||||
email := EmailSectionConfig{IMAP: IMAPListenerConfig{TLSPort: 2993}, POP3: POP3ListenerConfig{Port: 2110, TLSPort: 2995}}
|
||||
want := MailPorts{SMTP: 2525, SMTPSTARTTLS: 2587, SMTPTLS: 2465, IMAP: 2993, POP3: 2110, POP3TLS: 2995}
|
||||
if got := ResolveMailPorts(smtp, email); got != want {
|
||||
t.Fatalf("legacy ports = %+v, want %+v", got, want)
|
||||
}
|
||||
if got := ResolveMailSecurity(smtp.Listener); got != MailSecurityBoth {
|
||||
t.Fatalf("legacy security = %q, want %q", got, MailSecurityBoth)
|
||||
}
|
||||
if got := PublicMailSecurity(smtp.Listener); got != "" {
|
||||
t.Fatalf("legacy public security = %q, want empty", got)
|
||||
}
|
||||
if got := PublicMailSecurity(SMTPListenerConfig{Security: "tls"}); got != MailSecurityImplicitTLS {
|
||||
t.Fatalf("legacy TLS public security = %q, want %q", got, MailSecurityImplicitTLS)
|
||||
}
|
||||
if got := NormalizeMailSecurity("tls"); got != MailSecurityImplicitTLS {
|
||||
t.Fatalf("legacy relay security = %q, want %q", got, MailSecurityImplicitTLS)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMailListenerValidationAcceptsMatchingLegacyTLSNames(t *testing.T) {
|
||||
runtime := validRuntimeConfig()
|
||||
runtime.SMTP.Listener.Security = "tls"
|
||||
runtime.SMTP.Relay.Security = "tls"
|
||||
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true}); err != nil {
|
||||
t.Fatalf("matching legacy TLS validation = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMailListenerValidationRejectsUnknownSecurity(t *testing.T) {
|
||||
runtime := validRuntimeConfig()
|
||||
runtime.SMTP.Listener.Security = "unknown"
|
||||
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true}); !errors.Is(err, ErrInvalidRuntimeConfig) {
|
||||
t.Fatalf("validation = %v, want invalid runtime configuration", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMailListenerValidationAllowsLegacyEmptySecurityButRejectsExplicitBoth(t *testing.T) {
|
||||
runtime := validRuntimeConfig()
|
||||
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true}); err != nil {
|
||||
t.Fatalf("legacy empty security validation = %v", err)
|
||||
}
|
||||
runtime.SMTP.Listener.Security = MailSecurityBoth
|
||||
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true}); !errors.Is(err, ErrInvalidRuntimeConfig) {
|
||||
t.Fatalf("explicit both validation = %v, want invalid runtime configuration", err)
|
||||
}
|
||||
}
|
||||
@@ -289,6 +289,15 @@ func CurrentMailPorts() MailPorts {
|
||||
// 旧版 SMTPPort 属于发信中继,不能用作本机监听端口。
|
||||
return ResolveMailPorts(SMTPSectionConfig{}, EmailSectionConfig{})
|
||||
}
|
||||
|
||||
func CurrentMailSecurity() string {
|
||||
if manager := CurrentManager(); manager != nil {
|
||||
if runtime := manager.Snapshot(); runtime.Generation != 0 {
|
||||
return ResolveMailSecurity(runtime.SMTP.Listener)
|
||||
}
|
||||
}
|
||||
return MailSecurityBoth
|
||||
}
|
||||
func cloneHTTPConfig(source HTTPConfig) HTTPConfig {
|
||||
clone := source
|
||||
clone.TrustedProxyCIDRs = append([]string(nil), source.TrustedProxyCIDRs...)
|
||||
|
||||
@@ -149,8 +149,7 @@ func validateSMTP(smtp SMTPConfig) error {
|
||||
return ErrInvalidSMTP
|
||||
}
|
||||
}
|
||||
relayConfigured := smtp.RelayHost != "" || smtp.RelayPort != 0 || smtp.Username != "" ||
|
||||
smtp.Password.Configured() || smtp.Security != "" || smtp.From != ""
|
||||
relayConfigured := smtpRelayConfigured(smtp)
|
||||
if !relayConfigured {
|
||||
return nil
|
||||
}
|
||||
@@ -167,6 +166,12 @@ func validateSMTP(smtp SMTPConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// smtpRelayConfigured 只把实际连接或发件身份字段视为启用中继。
|
||||
// 推荐端口、统一加密方式和默认发件域名单独存在时,中继仍保持可选状态。
|
||||
func smtpRelayConfigured(smtp SMTPConfig) bool {
|
||||
return smtp.RelayHost != "" || smtp.Username != "" || smtp.Password.Configured() || smtp.From != ""
|
||||
}
|
||||
|
||||
func validateSSL(ssl SSLConfig) error {
|
||||
if !isControlledRelativePath(ssl.DefaultDirectory) {
|
||||
return ErrInvalidSSL
|
||||
|
||||
@@ -183,6 +183,15 @@ func TestValidateSMTPKeepsLoginVerificationIndependentFromRelay(t *testing.T) {
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
name: "unified defaults without relay host",
|
||||
smtp: SMTPConfig{
|
||||
RelayPort: 587,
|
||||
Security: "starttls",
|
||||
FromDomain: "example.com",
|
||||
},
|
||||
valid: true,
|
||||
},
|
||||
{
|
||||
name: "partially configured relay",
|
||||
smtp: SMTPConfig{
|
||||
|
||||
Reference in new issue
Block a user