mirror of
https://github.com/go-gitea/gitea
synced 2024-11-17 14:19:26 +01:00
Allow HOST has no port (#22280)
Fix #22274 This PR will allow `HOST` without port. Then a default port will be given in future steps.
This commit is contained in:
parent
477a1cc40e
commit
2220e5d245
@ -12,6 +12,7 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
|
||||||
shellquote "github.com/kballard/go-shellquote"
|
shellquote "github.com/kballard/go-shellquote"
|
||||||
|
ini "gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mailer represents mail service.
|
// Mailer represents mail service.
|
||||||
@ -49,8 +50,8 @@ type Mailer struct {
|
|||||||
// MailService the global mailer
|
// MailService the global mailer
|
||||||
var MailService *Mailer
|
var MailService *Mailer
|
||||||
|
|
||||||
func newMailService() {
|
func parseMailerConfig(rootCfg *ini.File) {
|
||||||
sec := Cfg.Section("mailer")
|
sec := rootCfg.Section("mailer")
|
||||||
// Check mailer setting.
|
// Check mailer setting.
|
||||||
if !sec.Key("ENABLED").MustBool() {
|
if !sec.Key("ENABLED").MustBool() {
|
||||||
return
|
return
|
||||||
@ -70,9 +71,14 @@ func newMailService() {
|
|||||||
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
|
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
|
||||||
givenHost := sec.Key("HOST").String()
|
givenHost := sec.Key("HOST").String()
|
||||||
addr, port, err := net.SplitHostPort(givenHost)
|
addr, port, err := net.SplitHostPort(givenHost)
|
||||||
if err != nil {
|
if err != nil && strings.Contains(err.Error(), "missing port in address") {
|
||||||
|
addr = givenHost
|
||||||
|
} else if err != nil {
|
||||||
log.Fatal("Invalid mailer.HOST (%s): %v", givenHost, err)
|
log.Fatal("Invalid mailer.HOST (%s): %v", givenHost, err)
|
||||||
}
|
}
|
||||||
|
if addr == "" {
|
||||||
|
addr = "127.0.0.1"
|
||||||
|
}
|
||||||
sec.Key("SMTP_ADDR").MustString(addr)
|
sec.Key("SMTP_ADDR").MustString(addr)
|
||||||
sec.Key("SMTP_PORT").MustString(port)
|
sec.Key("SMTP_PORT").MustString(port)
|
||||||
}
|
}
|
||||||
@ -172,6 +178,9 @@ func newMailService() {
|
|||||||
default:
|
default:
|
||||||
log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
|
log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
|
||||||
MailService.Protocol = "smtps"
|
MailService.Protocol = "smtps"
|
||||||
|
if MailService.SMTPPort == "" {
|
||||||
|
MailService.SMTPPort = "465"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
43
modules/setting/mailer_test.go
Normal file
43
modules/setting/mailer_test.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package setting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
ini "gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseMailerConfig(t *testing.T) {
|
||||||
|
iniFile := ini.Empty()
|
||||||
|
kases := map[string]*Mailer{
|
||||||
|
"smtp.mydomain.com": {
|
||||||
|
SMTPAddr: "smtp.mydomain.com",
|
||||||
|
SMTPPort: "465",
|
||||||
|
},
|
||||||
|
"smtp.mydomain.com:123": {
|
||||||
|
SMTPAddr: "smtp.mydomain.com",
|
||||||
|
SMTPPort: "123",
|
||||||
|
},
|
||||||
|
":123": {
|
||||||
|
SMTPAddr: "127.0.0.1",
|
||||||
|
SMTPPort: "123",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for host, kase := range kases {
|
||||||
|
t.Run(host, func(t *testing.T) {
|
||||||
|
iniFile.DeleteSection("mailer")
|
||||||
|
sec := iniFile.Section("mailer")
|
||||||
|
sec.NewKey("ENABLED", "true")
|
||||||
|
sec.NewKey("HOST", host)
|
||||||
|
|
||||||
|
// Check mailer setting
|
||||||
|
parseMailerConfig(iniFile)
|
||||||
|
|
||||||
|
assert.EqualValues(t, kase.SMTPAddr, MailService.SMTPAddr)
|
||||||
|
assert.EqualValues(t, kase.SMTPPort, MailService.SMTPPort)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1340,7 +1340,7 @@ func NewServices() {
|
|||||||
newCacheService()
|
newCacheService()
|
||||||
newSessionService()
|
newSessionService()
|
||||||
newCORSService()
|
newCORSService()
|
||||||
newMailService()
|
parseMailerConfig(Cfg)
|
||||||
newRegisterMailService()
|
newRegisterMailService()
|
||||||
newNotifyMailService()
|
newNotifyMailService()
|
||||||
newProxyService()
|
newProxyService()
|
||||||
@ -1357,5 +1357,5 @@ func NewServices() {
|
|||||||
// NewServicesForInstall initializes the services for install
|
// NewServicesForInstall initializes the services for install
|
||||||
func NewServicesForInstall() {
|
func NewServicesForInstall() {
|
||||||
newService()
|
newService()
|
||||||
newMailService()
|
parseMailerConfig(Cfg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user