diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-05-17 04:55:51 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-16 22:55:51 +0200 |
commit | 584c0789fa72f5e21167ba5f1370b7918e604247 (patch) | |
tree | 4363761096061a6c1405a1fbfcd6f51426e72aa6 /modules | |
parent | c367b63b7f931b58320c48388cc94e66f679d0a7 (diff) | |
download | gitea-584c0789fa72f5e21167ba5f1370b7918e604247.tar.gz gitea-584c0789fa72f5e21167ba5f1370b7918e604247.zip |
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 <teabot@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/setting/mailer.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/modules/setting/mailer.go b/modules/setting/mailer.go index 39afce7d46..a2bc2df444 100644 --- a/modules/setting/mailer.go +++ b/modules/setting/mailer.go @@ -4,6 +4,7 @@ package setting import ( + "context" "net" "net/mail" "strings" @@ -198,7 +199,7 @@ func loadMailerFrom(rootCfg ConfigProvider) { ips := tryResolveAddr(MailService.SMTPAddr) if MailService.Protocol == "smtp" { for _, ip := range ips { - if !ip.IsLoopback() { + if !ip.IP.IsLoopback() { log.Warn("connecting over insecure SMTP protocol to non-local address is not recommended") break } @@ -258,20 +259,21 @@ func loadNotifyMailFrom(rootCfg ConfigProvider) { log.Info("Notify Mail Service Enabled") } -func tryResolveAddr(addr string) []net.IP { +func tryResolveAddr(addr string) []net.IPAddr { if strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]") { addr = addr[1 : len(addr)-1] } ip := net.ParseIP(addr) if ip != nil { - ips := make([]net.IP, 1) - ips[0] = ip - return ips + return []net.IPAddr{{IP: ip}} } - ips, err := net.LookupIP(addr) + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + ips, err := net.DefaultResolver.LookupIPAddr(ctx, addr) if err != nil { log.Warn("could not look up mailer.SMTP_ADDR: %v", err) - return make([]net.IP, 0) + return nil } return ips } |