补齐IMAP和POP3配置并统一邮件协议监听端口

This commit is contained in:
xuans committed 2026-09-06 15:53:59 +08:00
1 parent 9b60eb2177
commit 24829c1199
27 files changed
+868 -80

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
package config
// 邮件监听与发信中继相互独立。零值表示沿用兼容默认值,不表示关闭服务。
type SMTPListenerConfig struct {
Port int `json:"port"`
STARTTLSPort int `json:"starttlsPort"`
TLSPort int `json:"tlsPort"`
}
type IMAPListenerConfig struct {
TLSPort int `json:"tlsPort"`
}
type POP3ListenerConfig struct {
Port int `json:"port"`
TLSPort int `json:"tlsPort"`
}
// ResolveMailPorts 是公开配置视图、冲突校验及真实监听的共同端口来源。
func ResolveMailPorts(smtp SMTPSectionConfig, email EmailSectionConfig) MailPorts {
portOr := func(port, fallback int) int {
if validPort(port) {
return port
}
return fallback
}
return MailPorts{
SMTP: portOr(smtp.Listener.Port, portOr(email.Policy.SMTPPort, 25)),
SMTPSTARTTLS: portOr(smtp.Listener.STARTTLSPort, 587),
SMTPTLS: portOr(smtp.Listener.TLSPort, 465),
// 旧版 imapPort 从未用于监听;保留原有的 993,避免升级后擅自切换客户端端口。
IMAP: portOr(email.IMAP.TLSPort, 993),
POP3: portOr(email.POP3.Port, portOr(email.Policy.POP3Port, 110)),
POP3TLS: portOr(email.POP3.TLSPort, 995),
}
}
func validateMailListeners(smtp SMTPSectionConfig, email EmailSectionConfig) error {
for _, port := range []int{
smtp.Listener.Port, smtp.Listener.STARTTLSPort, smtp.Listener.TLSPort,
email.IMAP.TLSPort, email.POP3.Port, email.POP3.TLSPort,
} {
if port != 0 && !validPort(port) {
return ErrPortConflict
}
}
return nil
}
+148
View File
@@ -0,0 +1,148 @@
package config
import (
"context"
"encoding/json"
"errors"
"testing"
)
func TestMailListenerConfigurationOverridesLegacyPolicyAndIgnoresRelay(t *testing.T) {
previous := CurrentManager()
defer SetRuntimeManager(previous)
runtime := validRuntimeConfig()
runtime.SMTP.Listener = SMTPListenerConfig{Port: 2525, STARTTLSPort: 2587, TLSPort: 2465}
runtime.SMTP.Relay.RelayPort = 1993
runtime.Email.IMAP = IMAPListenerConfig{TLSPort: 1993}
runtime.Email.POP3 = POP3ListenerConfig{Port: 1110, TLSPort: 1995}
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
want := MailPorts{SMTP: 2525, SMTPSTARTTLS: 2587, SMTPTLS: 2465, IMAP: 1993, POP3: 1110, POP3TLS: 1995}
if got := CurrentMailPorts(); got != want {
t.Fatalf("listeners = %+v, want %+v", got, want)
}
}
func TestMailListenerValidationCoversEveryActualListener(t *testing.T) {
for name, mutate := range map[string]func(*RuntimeConfig){
"smtp_http": func(r *RuntimeConfig) { r.SMTP.Listener.Port = 80 },
"smtp_starttls_https": func(r *RuntimeConfig) { r.SMTP.Listener.STARTTLSPort = 443 },
"smtp_tls_imap": func(r *RuntimeConfig) { r.SMTP.Listener.TLSPort = 993 },
"imap_pop3_tls": func(r *RuntimeConfig) { r.Email.IMAP.TLSPort = 995 },
"pop3_smtp": func(r *RuntimeConfig) { r.Email.POP3.Port = 25 },
"pop3_tls_starttls": func(r *RuntimeConfig) { r.Email.POP3.TLSPort = 587 },
"http_default": func(r *RuntimeConfig) { r.Root.HTTP.Port = 0; r.Email.IMAP.TLSPort = 80 },
"smtp_negative": func(r *RuntimeConfig) { r.SMTP.Listener.Port = -1 },
"starttls_overflow": func(r *RuntimeConfig) { r.SMTP.Listener.STARTTLSPort = 65536 },
"tls_negative": func(r *RuntimeConfig) { r.SMTP.Listener.TLSPort = -1 },
"imap_overflow": func(r *RuntimeConfig) { r.Email.IMAP.TLSPort = 65536 },
"pop3_negative": func(r *RuntimeConfig) { r.Email.POP3.Port = -1 },
"pop3_tls_overflow": func(r *RuntimeConfig) { r.Email.POP3.TLSPort = 65536 },
} {
t.Run(name, func(t *testing.T) {
runtime := validRuntimeConfig()
mutate(&runtime)
if err := ValidateRuntimeConfig(runtime, RuntimeValidationOptions{HasSuperAdmin: true}); !errors.Is(err, ErrPortConflict) {
t.Fatalf("validation = %v, want port conflict", err)
}
})
}
}
func TestMailListenerCandidateApplyAndReloadPreservesAllSections(t *testing.T) {
root := t.TempDir()
initial := validRuntimeConfig()
writeRuntimeFixture(t, root, initial)
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
manager, err := NewConfigManager(root, options)
if err != nil {
t.Fatal(err)
}
for _, section := range []Section{SectionSMTP, SectionEmail} {
current := manager.Snapshot()
var value any
if section == SectionSMTP {
current.SMTP.Listener = SMTPListenerConfig{Port: 2525, STARTTLSPort: 2587, TLSPort: 2465}
value = current.SMTP
} else {
current.Email.IMAP.TLSPort = 1993
current.Email.POP3 = POP3ListenerConfig{Port: 1110, TLSPort: 1995}
value = current.Email
}
payload, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
updated, err := manager.Update(context.Background(), UpdateRequest{
Section: section, ExpectedGeneration: current.Generation, Payload: payload, Actor: authorizedConfigActor(),
})
if err != nil {
t.Fatal(err)
}
if manager.Snapshot().Generation != current.Generation {
t.Fatal("saving candidate published it prematurely")
}
if _, err := manager.ApplyPendingAuthorized(context.Background(), updated.Generation, updated.CandidateID, section, authorizedConfigActor()); err != nil {
t.Fatal(err)
}
}
reloaded, err := NewConfigLoader(root, options).Load()
if err != nil {
t.Fatal(err)
}
want := MailPorts{SMTP: 2525, SMTPSTARTTLS: 2587, SMTPTLS: 2465, IMAP: 1993, POP3: 1110, POP3TLS: 1995}
if got := ResolveMailPorts(reloaded.SMTP, reloaded.Email); got != want {
t.Fatalf("reloaded listeners = %+v, want %+v", got, want)
}
if reloaded.Email.Policy != initial.Email.Policy || reloaded.SMTP.Relay != initial.SMTP.Relay {
t.Fatal("listener change overwrote policy or relay")
}
}
func TestMailListenerConflictsUseEffectiveWebPorts(t *testing.T) {
previous := RuntimeSetupPort()
t.Cleanup(func() { SetRuntimeSetupPort(previous) })
for _, tc := range []struct {
name string
http HTTPConfig
envHTTP string
envHTTPS string
setupPort int
mailPort int
conflict bool
}{
{"environment_http", HTTPConfig{}, "9080", "", 8080, 9080, true},
{"setup_http", HTTPConfig{}, "", "", 8080, 8080, true},
{"configured_http", HTTPConfig{Port: 7080}, "9080", "", 8080, 7080, true},
{"unused_environment_http", HTTPConfig{Port: 7080}, "9080", "", 8080, 9080, false},
{"environment_https", HTTPConfig{HTTPSEnabled: true}, "", "9443", 8080, 9443, true},
{"default_https", HTTPConfig{HTTPSEnabled: true}, "", "", 8080, 443, true},
{"proxy_no_internal_https", HTTPConfig{TLSMode: HTTPSTLSModeProxy}, "", "9443", 8080, 9443, false},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("SMAIL_HTTP_PORT", tc.envHTTP)
t.Setenv("SMAIL_HTTPS_PORT", tc.envHTTPS)
SetRuntimeSetupPort(tc.setupPort)
ports := ResolveMailPorts(SMTPSectionConfig{}, EmailSectionConfig{})
ports.IMAP = tc.mailPort
err := validateRuntimePorts(tc.http, ports)
if errors.Is(err, ErrPortConflict) != tc.conflict {
t.Fatalf("validation = %v, want conflict %v", err, tc.conflict)
}
})
}
}
func TestOldPartitionPayloadsKeepExistingListenerDefaults(t *testing.T) {
var smtp SMTPSectionConfig
var email EmailSectionConfig
if err := ParseStrict([]byte(`{"meta":{"schema":1,"generation":1},"relay":{"relayPort":2465}}`), &smtp); err != nil {
t.Fatal(err)
}
if err := ParseStrict([]byte(`{"meta":{"schema":1,"generation":1},"policy":{"smtpPort":2525,"imapPort":1143,"pop3Port":1110}}`), &email); err != nil {
t.Fatal(err)
}
want := MailPorts{SMTP: 2525, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 1110, POP3TLS: 995}
if got := ResolveMailPorts(smtp, email); got != want {
t.Fatalf("legacy listeners changed: %+v", got)
}
}
+12 -14
View File
@@ -162,9 +162,12 @@ func cloneConfig(source *Config) *Config {
}
type MailPorts struct {
SMTP int
IMAP int
POP3 int
SMTP int
SMTPSTARTTLS int
SMTPTLS int
IMAP int
POP3 int
POP3TLS int
}
type MailDomains struct {
@@ -273,18 +276,13 @@ func CurrentSMTPRuntimeSettings() (SMTPRuntimeSettings, error) {
}
func CurrentMailPorts() MailPorts {
ports := MailPorts{SMTP: 25, IMAP: 143, POP3: 110}
policy := CurrentEmailPolicy()
if policy.SMTPPort > 0 && policy.SMTPPort <= 65535 {
ports.SMTP = policy.SMTPPort
if manager := CurrentManager(); manager != nil {
if runtime := manager.Snapshot(); runtime.Generation != 0 {
return ResolveMailPorts(runtime.SMTP, runtime.Email)
}
}
if policy.IMAPPort > 0 && policy.IMAPPort <= 65535 {
ports.IMAP = policy.IMAPPort
}
if policy.POP3Port > 0 && policy.POP3Port <= 65535 {
ports.POP3 = policy.POP3Port
}
return ports
// 旧版 SMTPPort 属于发信中继,不能用作本机监听端口。
return ResolveMailPorts(SMTPSectionConfig{}, EmailSectionConfig{})
}
func cloneHTTPConfig(source HTTPConfig) HTTPConfig {
clone := source
+5 -5
View File
@@ -70,7 +70,7 @@ func TestCurrentMailPortsUsesSafeDefaultsWithoutRuntimeManager(t *testing.T) {
defer SetRuntimeManager(previousManager)
SetRuntimeManager(nil)
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, IMAP: 143, POP3: 110}) {
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 110, POP3TLS: 995}) {
t.Fatalf("mail ports = %+v, want safe defaults", got)
}
}
@@ -131,7 +131,7 @@ func TestCurrentMailPortsReadsEmailPolicySnapshot(t *testing.T) {
runtime.Email.Policy.POP3Port = 1110
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 2525, IMAP: 1143, POP3: 1110}) {
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 2525, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 1110, POP3TLS: 995}) {
t.Fatalf("mail ports = %+v, want policy ports", got)
}
}
@@ -146,7 +146,7 @@ func TestCurrentMailPortsFallsBackForInvalidPolicyValues(t *testing.T) {
runtime.Email.Policy.POP3Port = -1
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, IMAP: 143, POP3: 110}) {
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 110, POP3TLS: 995}) {
t.Fatalf("mail ports = %+v, want defaults for invalid policy values", got)
}
}
@@ -160,7 +160,7 @@ func TestCurrentMailPortsDoesNotReadLegacyRelayPort(t *testing.T) {
runtime.Email.Policy = EmailConfig{}
SetRuntimeManager(&ConfigManager{snapshots: runtimeAccessSnapshotStore{runtime: runtime}})
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, IMAP: 143, POP3: 110}) {
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 110, POP3TLS: 995}) {
t.Fatalf("mail ports = %+v, legacy relay port leaked into listener ports", got)
}
}
@@ -170,7 +170,7 @@ func TestCurrentMailPortsUsesDefaultsForUnavailableSnapshot(t *testing.T) {
defer SetRuntimeManager(previousManager)
SetRuntimeManager(&ConfigManager{})
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, IMAP: 143, POP3: 110}) {
if got := CurrentMailPorts(); got != (MailPorts{SMTP: 25, SMTPSTARTTLS: 587, SMTPTLS: 465, IMAP: 993, POP3: 110, POP3TLS: 995}) {
t.Fatalf("mail ports = %+v, want defaults for unavailable snapshot", got)
}
}
+8 -6
View File
@@ -55,7 +55,10 @@ func ValidateRuntimeConfig(runtime RuntimeConfig, options RuntimeValidationOptio
if err := validateDomains(runtime.Root.Domains); err != nil {
return err
}
if err := validateRuntimePorts(runtime.Root.HTTP, runtime.Email.Policy); err != nil {
if err := validateMailListeners(runtime.SMTP, runtime.Email); err != nil {
return err
}
if err := validateRuntimePorts(runtime.Root.HTTP, ResolveMailPorts(runtime.SMTP, runtime.Email)); err != nil {
return err
}
if err := validatePluginSection(runtime.Plugins); err != nil {
@@ -65,9 +68,6 @@ func ValidateRuntimeConfig(runtime RuntimeConfig, options RuntimeValidationOptio
if !options.HasSuperAdmin || runtime.Root.Domains.Primary == "" || len(runtime.Root.Domains.Accepted) == 0 || runtime.Root.Domains.Web == "" || !runtime.Root.Database.IsStructured() {
return ErrInvalidRuntimeConfig
}
if runtime.Email.Policy.SMTPPort == 0 || runtime.Email.Policy.IMAPPort == 0 || runtime.Email.Policy.POP3Port == 0 {
return ErrInvalidRuntimeConfig
}
if runtime.Root.HTTP.HTTPSEnabled && runtime.Root.HTTP.HTTPSPort == 0 {
return ErrInvalidRuntimeConfig
}
@@ -126,8 +126,10 @@ func normalizeConfigAccessHost(value string) (string, error) {
return normalizeConfigDomain(value)
}
func validateRuntimePorts(http HTTPConfig, email EmailConfig) error {
ports := []int{http.Port, email.SMTPPort, email.IMAPPort, email.POP3Port}
func validateRuntimePorts(http HTTPConfig, mail MailPorts) error {
// 与实际 Web 监听使用相同的环境变量、启动端口及反代默认值。
http = ResolveHTTPPortDefaults(http, RuntimeSetupPort())
ports := []int{http.Port, mail.SMTP, mail.SMTPSTARTTLS, mail.SMTPTLS, mail.IMAP, mail.POP3, mail.POP3TLS}
if http.HTTPSEnabled || http.HTTPSPort != 0 {
ports = append(ports, http.HTTPSPort)
}
+1 -1
View File
@@ -61,7 +61,7 @@ func TestValidateRuntimeConfigRejectsCrossSectionViolations(t *testing.T) {
mutate func(*RuntimeConfig, *RuntimeValidationOptions)
want error
}{
{name: "port_conflict", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) { runtime.Email.Policy.IMAPPort = 443 }, want: ErrPortConflict},
{name: "port_conflict", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) { runtime.Email.IMAP.TLSPort = 443 }, want: ErrPortConflict},
{name: "primary_domain_missing", mutate: func(runtime *RuntimeConfig, _ *RuntimeValidationOptions) {
runtime.Root.Domains.Accepted = []string{"example.com"}
}, want: ErrInvalidDomainConfig},
+7 -4
View File
@@ -54,13 +54,16 @@ const (
)
type SMTPSectionConfig struct {
Meta SectionMeta `json:"meta"`
Relay SMTPConfig `json:"relay"`
Meta SectionMeta `json:"meta"`
Relay SMTPConfig `json:"relay"`
Listener SMTPListenerConfig `json:"listener,omitempty"`
}
type EmailSectionConfig struct {
Meta SectionMeta `json:"meta"`
Policy EmailConfig `json:"policy"`
Meta SectionMeta `json:"meta"`
Policy EmailConfig `json:"policy"`
IMAP IMAPListenerConfig `json:"imap,omitempty"`
POP3 POP3ListenerConfig `json:"pop3,omitempty"`
}
type SSLSectionConfig struct {
+1 -1
View File
@@ -15,7 +15,7 @@ func TestSnapshotStoreRejectsInvalidCandidate(t *testing.T) {
t.Fatal(err)
}
invalid := validRuntimeConfig()
invalid.Email.Policy.IMAPPort = invalid.Root.HTTP.HTTPSPort
invalid.Email.IMAP.TLSPort = invalid.Root.HTTP.HTTPSPort
if err := store.Publish(invalid); err == nil {
t.Fatal("invalid runtime configuration was published")
}
+4 -4
View File
@@ -180,7 +180,7 @@ func TestSnapshotStoreRejectsInvalidCandidateWithoutChangingSnapshot(t *testing.
}
invalid := cloneRuntimeConfig(initial)
invalid.Email.Policy.IMAPPort = invalid.Root.HTTP.HTTPSPort
invalid.Email.IMAP.TLSPort = invalid.Root.HTTP.HTTPSPort
if err := store.Publish(invalid); !errors.Is(err, ErrPortConflict) {
t.Fatalf("publish error = %v, want %v", err, ErrPortConflict)
}
@@ -191,8 +191,8 @@ func TestSnapshotStoreRejectsInvalidCandidateWithoutChangingSnapshot(t *testing.
if err != nil {
t.Fatal(err)
}
if after.Email.Policy.IMAPPort != before.Email.Policy.IMAPPort {
t.Fatalf("snapshot changed after rejection: before=%d after=%d", before.Email.Policy.IMAPPort, after.Email.Policy.IMAPPort)
if after.Email.IMAP.TLSPort != before.Email.IMAP.TLSPort {
t.Fatalf("snapshot changed after rejection: before=%d after=%d", before.Email.IMAP.TLSPort, after.Email.IMAP.TLSPort)
}
}
@@ -216,7 +216,7 @@ func TestConfigManagerRejectsInvalidUpdateWithoutDiskOrStatusMutation(t *testing
beforeStatus := manager.Status()
current := manager.Snapshot()
invalidEmail := current.Email
invalidEmail.Policy.IMAPPort = current.Root.HTTP.HTTPSPort
invalidEmail.IMAP.TLSPort = current.Root.HTTP.HTTPSPort
payload, err := json.Marshal(invalidEmail)
if err != nil {
t.Fatal(err)
+8 -2
View File
@@ -444,8 +444,10 @@ func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (
"security": runtime.Root.Security,
}, nil
case config.SectionSMTP:
ports := config.ResolveMailPorts(runtime.SMTP, runtime.Email)
return map[string]any{
"meta": runtime.SMTP.Meta,
"meta": runtime.SMTP.Meta,
"listener": config.SMTPListenerConfig{Port: ports.SMTP, STARTTLSPort: ports.SMTPSTARTTLS, TLSPort: ports.SMTPTLS},
"relay": map[string]any{
"relayHost": runtime.SMTP.Relay.RelayHost,
"relayPort": runtime.SMTP.Relay.RelayPort,
@@ -460,7 +462,11 @@ func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (
"relayPasswordStatus": publicSecret(runtime.SMTP.Relay.Password),
}, nil
case config.SectionEmail:
return runtime.Email, nil
ports := config.ResolveMailPorts(runtime.SMTP, runtime.Email)
value := runtime.Email
value.IMAP = config.IMAPListenerConfig{TLSPort: ports.IMAP}
value.POP3 = config.POP3ListenerConfig{Port: ports.POP3, TLSPort: ports.POP3TLS}
return value, nil
case config.SectionSSL:
return runtime.SSL, nil
case config.SectionPlugins:
@@ -0,0 +1,49 @@
package controllers
import (
"encoding/json"
"testing"
"git.pcux.cn/xuans/Smail/config"
)
func TestPublicMailPortsMatchListenersWithoutChangingSnapshot(t *testing.T) {
for _, custom := range []bool{false, true} {
runtime := config.RuntimeConfig{
SMTP: config.SMTPSectionConfig{Relay: config.SMTPConfig{RelayPort: 9999}},
Email: config.EmailSectionConfig{Policy: config.EmailConfig{SMTPPort: 2525, IMAPPort: 1143, POP3Port: 1110}},
}
if custom {
runtime.SMTP.Listener = config.SMTPListenerConfig{Port: 3025, STARTTLSPort: 3587, TLSPort: 3465}
runtime.Email.IMAP.TLSPort = 3993
runtime.Email.POP3 = config.POP3ListenerConfig{Port: 3110, TLSPort: 3995}
}
before := runtime.Email
want := config.ResolveMailPorts(runtime.SMTP, runtime.Email)
smtpPublic, err := publicConfigSection(runtime, config.SectionSMTP)
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(smtpPublic)
if err != nil {
t.Fatal(err)
}
var smtp struct {
Listener config.SMTPListenerConfig `json:"listener"`
}
if err := json.Unmarshal(data, &smtp); err != nil {
t.Fatal(err)
}
emailPublic, err := publicConfigSection(runtime, config.SectionEmail)
if err != nil {
t.Fatal(err)
}
email := emailPublic.(config.EmailSectionConfig)
if smtp.Listener.Port != want.SMTP || smtp.Listener.STARTTLSPort != want.SMTPSTARTTLS || smtp.Listener.TLSPort != want.SMTPTLS || email.IMAP.TLSPort != want.IMAP || email.POP3.Port != want.POP3 || email.POP3.TLSPort != want.POP3TLS {
t.Fatal("public ports differ from listener configuration")
}
if before != runtime.Email {
t.Fatal("public view changed runtime configuration")
}
}
}
+2 -2
View File
@@ -20,14 +20,14 @@ func Stop() {
}
}
// StarTLS 启动TLS端口监听,不加密的代码就懒得写了
// StarTLS 启动隐式 TLS 监听,不提供明文认证入口。
func StarTLS() error {
server, err := newIMAPServer()
if err != nil {
return err
}
instanceTLS = server
const imapTLSPort = 993
imapTLSPort := config.CurrentMailPorts().IMAP
log.Infof("IMAP With TLS Server Start On Port :%d", imapTLSPort)
if err := instanceTLS.ListenAndServeTLS(fmt.Sprintf(":%d", imapTLSPort)); err != nil {
return fmt.Errorf("serve IMAP TLS: %w", err)
+3 -2
View File
@@ -30,10 +30,11 @@ func StartWithTls() error {
tlsConfig.Certificates = []tls.Certificate{crt}
tlsConfig.Time = time.Now
tlsConfig.Rand = rand.Reader
instanceTls = gopop.NewPop3Server(995, "pop."+domains.Primary, true, tlsConfig, action{})
port := config.CurrentMailPorts().POP3TLS
instanceTls = gopop.NewPop3Server(port, "pop."+domains.Primary, true, tlsConfig, action{})
instanceTls.ConnectAliveTime = 5 * time.Minute
log.Infof("POP3 With TLS Server Start On Port :995")
log.Infof("POP3 With TLS Server Start On Port :%d", port)
err = instanceTls.Start()
if err != nil {
+3 -3
View File
@@ -34,7 +34,7 @@ func StartWithTLSNew() error {
instanceTlsNew = smtp.NewServer(be)
instanceTlsNew.Addr = ":587"
instanceTlsNew.Addr = fmt.Sprintf(":%d", config.CurrentMailPorts().SMTPSTARTTLS)
instanceTlsNew.Domain = domains.Primary
instanceTlsNew.ReadTimeout = 10 * time.Second
instanceTlsNew.WriteTimeout = 10 * time.Second
@@ -51,7 +51,7 @@ func StartWithTLSNew() error {
instanceTlsNew.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}, MinVersion: tls.VersionTLS12}
log.Println("Starting Smtp With STARTTLS Server Port:", instanceTlsNew.Addr)
// 587端口使用STARTTLS(先明文连接,再升级TLS),而非隐式TLS
// 提交端口使用 STARTTLS(先连接,再升级 TLS),而非隐式 TLS。
if err := instanceTlsNew.ListenAndServe(); err != nil && !errors.Is(err, smtp.ErrServerClosed) {
return fmt.Errorf("serve SMTP STARTTLS: %w", err)
}
@@ -71,7 +71,7 @@ func StartWithTLS() error {
instanceTls = smtp.NewServer(be)
instanceTls.Addr = ":465"
instanceTls.Addr = fmt.Sprintf(":%d", config.CurrentMailPorts().SMTPTLS)
instanceTls.Domain = domains.Primary
instanceTls.ReadTimeout = 10 * time.Second
instanceTls.WriteTimeout = 10 * time.Second