Skip to content

Commit 584c078

Browse files
wxiaoguangGiteaBot
andauthored
Make mailer SMTP check have timed context (#24751)
Make mailer SMTP check have timed context Otherwise Gitea may block for long time if the DNS request blocks. --------- Co-authored-by: Giteabot <[email protected]>
1 parent c367b63 commit 584c078

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

modules/setting/mailer.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package setting
55

66
import (
7+
"context"
78
"net"
89
"net/mail"
910
"strings"
@@ -198,7 +199,7 @@ func loadMailerFrom(rootCfg ConfigProvider) {
198199
ips := tryResolveAddr(MailService.SMTPAddr)
199200
if MailService.Protocol == "smtp" {
200201
for _, ip := range ips {
201-
if !ip.IsLoopback() {
202+
if !ip.IP.IsLoopback() {
202203
log.Warn("connecting over insecure SMTP protocol to non-local address is not recommended")
203204
break
204205
}
@@ -258,20 +259,21 @@ func loadNotifyMailFrom(rootCfg ConfigProvider) {
258259
log.Info("Notify Mail Service Enabled")
259260
}
260261

261-
func tryResolveAddr(addr string) []net.IP {
262+
func tryResolveAddr(addr string) []net.IPAddr {
262263
if strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]") {
263264
addr = addr[1 : len(addr)-1]
264265
}
265266
ip := net.ParseIP(addr)
266267
if ip != nil {
267-
ips := make([]net.IP, 1)
268-
ips[0] = ip
269-
return ips
268+
return []net.IPAddr{{IP: ip}}
270269
}
271-
ips, err := net.LookupIP(addr)
270+
271+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
272+
defer cancel()
273+
ips, err := net.DefaultResolver.LookupIPAddr(ctx, addr)
272274
if err != nil {
273275
log.Warn("could not look up mailer.SMTP_ADDR: %v", err)
274-
return make([]net.IP, 0)
276+
return nil
275277
}
276278
return ips
277279
}

0 commit comments

Comments
 (0)