forked from gitea/gitea
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>
This commit is contained in:
parent
c367b63b7f
commit
584c0789fa
|
@ -4,6 +4,7 @@
|
||||||
package setting
|
package setting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -198,7 +199,7 @@ func loadMailerFrom(rootCfg ConfigProvider) {
|
||||||
ips := tryResolveAddr(MailService.SMTPAddr)
|
ips := tryResolveAddr(MailService.SMTPAddr)
|
||||||
if MailService.Protocol == "smtp" {
|
if MailService.Protocol == "smtp" {
|
||||||
for _, ip := range ips {
|
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")
|
log.Warn("connecting over insecure SMTP protocol to non-local address is not recommended")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -258,20 +259,21 @@ func loadNotifyMailFrom(rootCfg ConfigProvider) {
|
||||||
log.Info("Notify Mail Service Enabled")
|
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, "]") {
|
if strings.HasPrefix(addr, "[") && strings.HasSuffix(addr, "]") {
|
||||||
addr = addr[1 : len(addr)-1]
|
addr = addr[1 : len(addr)-1]
|
||||||
}
|
}
|
||||||
ip := net.ParseIP(addr)
|
ip := net.ParseIP(addr)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
ips := make([]net.IP, 1)
|
return []net.IPAddr{{IP: ip}}
|
||||||
ips[0] = ip
|
|
||||||
return ips
|
|
||||||
}
|
}
|
||||||
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 {
|
if err != nil {
|
||||||
log.Warn("could not look up mailer.SMTP_ADDR: %v", err)
|
log.Warn("could not look up mailer.SMTP_ADDR: %v", err)
|
||||||
return make([]net.IP, 0)
|
return nil
|
||||||
}
|
}
|
||||||
return ips
|
return ips
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue