Skip to content

Commit 990a8c4

Browse files
committed
Allow HOST has no port (go-gitea#22280)
Fix go-gitea#22274 This PR will allow `HOST` without port. Then a default port will be given in future steps.
1 parent 41a06d2 commit 990a8c4

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

modules/setting/mailer.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/log"
1414

1515
shellquote "github.com/kballard/go-shellquote"
16+
ini "gopkg.in/ini.v1"
1617
)
1718

1819
// Mailer represents mail service.
@@ -50,8 +51,8 @@ type Mailer struct {
5051
// MailService the global mailer
5152
var MailService *Mailer
5253

53-
func newMailService() {
54-
sec := Cfg.Section("mailer")
54+
func parseMailerConfig(rootCfg *ini.File) {
55+
sec := rootCfg.Section("mailer")
5556
// Check mailer setting.
5657
if !sec.Key("ENABLED").MustBool() {
5758
return
@@ -71,9 +72,14 @@ func newMailService() {
7172
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
7273
givenHost := sec.Key("HOST").String()
7374
addr, port, err := net.SplitHostPort(givenHost)
74-
if err != nil {
75+
if err != nil && strings.Contains(err.Error(), "missing port in address") {
76+
addr = givenHost
77+
} else if err != nil {
7578
log.Fatal("Invalid mailer.HOST (%s): %v", givenHost, err)
7679
}
80+
if addr == "" {
81+
addr = "127.0.0.1"
82+
}
7783
sec.Key("SMTP_ADDR").MustString(addr)
7884
sec.Key("SMTP_PORT").MustString(port)
7985
}
@@ -173,6 +179,9 @@ func newMailService() {
173179
default:
174180
log.Error("unable to infer unspecified mailer.PROTOCOL from mailer.SMTP_PORT = %q, assume using smtps", MailService.SMTPPort)
175181
MailService.Protocol = "smtps"
182+
if MailService.SMTPPort == "" {
183+
MailService.SMTPPort = "465"
184+
}
176185
}
177186
}
178187
}

modules/setting/mailer_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package setting
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
ini "gopkg.in/ini.v1"
11+
)
12+
13+
func TestParseMailerConfig(t *testing.T) {
14+
iniFile := ini.Empty()
15+
kases := map[string]*Mailer{
16+
"smtp.mydomain.com": {
17+
SMTPAddr: "smtp.mydomain.com",
18+
SMTPPort: "465",
19+
},
20+
"smtp.mydomain.com:123": {
21+
SMTPAddr: "smtp.mydomain.com",
22+
SMTPPort: "123",
23+
},
24+
":123": {
25+
SMTPAddr: "127.0.0.1",
26+
SMTPPort: "123",
27+
},
28+
}
29+
for host, kase := range kases {
30+
t.Run(host, func(t *testing.T) {
31+
iniFile.DeleteSection("mailer")
32+
sec := iniFile.Section("mailer")
33+
sec.NewKey("ENABLED", "true")
34+
sec.NewKey("HOST", host)
35+
36+
// Check mailer setting
37+
parseMailerConfig(iniFile)
38+
39+
assert.EqualValues(t, kase.SMTPAddr, MailService.SMTPAddr)
40+
assert.EqualValues(t, kase.SMTPPort, MailService.SMTPPort)
41+
})
42+
}
43+
}

modules/setting/setting.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ func NewServices() {
13341334
newCacheService()
13351335
newSessionService()
13361336
newCORSService()
1337-
newMailService()
1337+
parseMailerConfig(Cfg)
13381338
newRegisterMailService()
13391339
newNotifyMailService()
13401340
newProxyService()
@@ -1351,5 +1351,5 @@ func NewServices() {
13511351
// NewServicesForInstall initializes the services for install
13521352
func NewServicesForInstall() {
13531353
newService()
1354-
newMailService()
1354+
parseMailerConfig(Cfg)
13551355
}

0 commit comments

Comments
 (0)