summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-01-12 04:09:24 +0800
committerGitHub <noreply@github.com>2023-01-11 20:09:24 +0000
commit2220e5d2455b28450e2f505152f9fbb8ac597e80 (patch)
tree8c41bd3dde9b970ce8db2b98d25d6f25cd1e2f66 /modules
parent477a1cc40ebd3ecb116c632b0717bba748e914d2 (diff)
downloadgitea-2220e5d2455b28450e2f505152f9fbb8ac597e80.tar.gz
gitea-2220e5d2455b28450e2f505152f9fbb8ac597e80.zip
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.
Diffstat (limited to 'modules')
-rw-r--r--modules/setting/mailer.go15
-rw-r--r--modules/setting/mailer_test.go43
-rw-r--r--modules/setting/setting.go4
3 files changed, 57 insertions, 5 deletions
diff --git a/modules/setting/mailer.go b/modules/setting/mailer.go
index e7cc812eef..a5d311454d 100644
--- a/modules/setting/mailer.go
+++ b/modules/setting/mailer.go
@@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/log"
shellquote "github.com/kballard/go-shellquote"
+ ini "gopkg.in/ini.v1"
)
// Mailer represents mail service.
@@ -49,8 +50,8 @@ type Mailer struct {
// MailService the global mailer
var MailService *Mailer
-func newMailService() {
- sec := Cfg.Section("mailer")
+func parseMailerConfig(rootCfg *ini.File) {
+ sec := rootCfg.Section("mailer")
// Check mailer setting.
if !sec.Key("ENABLED").MustBool() {
return
@@ -70,9 +71,14 @@ func newMailService() {
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
givenHost := sec.Key("HOST").String()
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)
}
+ if addr == "" {
+ addr = "127.0.0.1"
+ }
sec.Key("SMTP_ADDR").MustString(addr)
sec.Key("SMTP_PORT").MustString(port)
}
@@ -172,6 +178,9 @@ func newMailService() {
default:
log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
MailService.Protocol = "smtps"
+ if MailService.SMTPPort == "" {
+ MailService.SMTPPort = "465"
+ }
}
}
}
diff --git a/modules/setting/mailer_test.go b/modules/setting/mailer_test.go
new file mode 100644
index 0000000000..0fc9b0e73f
--- /dev/null
+++ b/modules/setting/mailer_test.go
@@ -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)
+ })
+ }
+}
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 07290fbfeb..fa65b94891 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -1340,7 +1340,7 @@ func NewServices() {
newCacheService()
newSessionService()
newCORSService()
- newMailService()
+ parseMailerConfig(Cfg)
newRegisterMailService()
newNotifyMailService()
newProxyService()
@@ -1357,5 +1357,5 @@ func NewServices() {
// NewServicesForInstall initializes the services for install
func NewServicesForInstall() {
newService()
- newMailService()
+ parseMailerConfig(Cfg)
}