EXAMPLES: fix IPV6 support for lua mailers script

While this used to work fine with legacy mailers, IPV6 server support
for lua mailers script was overlooked so it is currently broken.

Indeed, within the lua script, server address was parsed as an IPV4
address to extract both ip and port and pass them to smtp_send_email()
function from Thierry FOURNIER.

From lua point of view: when fetching server address from
ProxyMailers.mailservers, server ip and port are not separated. Each
server address is represented using haproxy server address custom-format
(the one used to specify server addresses within haproxy config,
see 11. Address formats in haproxy configuration manual):

It is a string that contains both proto hint, ip and port.
(Such addresses are manipulated using str2sa_range() and sa2str()
in haproxy's code)

Parsing these custom-format addresses from lua to support multiple address
families is feasible since the format is properly documented in haproxy
configuration.

However, to keep things simple, and given that smtp_send_email() relies
on Socket.connect() function to set-up the tcp connection:

Socket.connect() already supports the full server address custom-format
when no explicit port argument is provided. Thus with minor code changes
we're able to pass the server string as it is.

With this, IPV6 smtp servers from mailers section are now automatically
supported when using lua mailers script.
This commit is contained in:
Aurelien DARRAGON 2023-05-09 09:25:25 +02:00 committed by Christopher Faulet
parent e59f7583ee
commit c75c41c35f
1 changed files with 8 additions and 7 deletions

View File

@ -26,7 +26,11 @@ local mailqueue = core.queue()
-- EHLO was replaced with HELO for better compatibility with
-- basic mail server implementations
--
function smtp_send_email(server, port, domain, from, to, data)
-- <server> should contain the full server address (including port) in the
-- same format used in haproxy config file. It will be passed as it is to
-- tcp::connect() without explicit port argument. See Socket.connect()
-- manual for more information.
function smtp_send_email(server, domain, from, to, data)
local ret
local reason
local tcp = core.tcp()
@ -51,8 +55,8 @@ function smtp_send_email(server, port, domain, from, to, data)
end
end
if tcp:connect(server, port) == nil then
return false, "Can't connect to \""..server..":"..port.."\""
if tcp:connect(server) == nil then
return false, "Can't connect to \""..server.."\""
end
ret, reason = smtp_wait_code(tcp, '^220 ')
@ -400,11 +404,8 @@ core.register_task(function()
-- send email to all mailservers
for name, mailsrv in pairs(job.mailconf.mailservers) do
-- split mailsrv (ip:port) in 2 variables
local mailsrv_ip, mailsrv_port = string.match(mailsrv, "([^:]+):([^:]+)")
-- finally, send email to server
local ret, reason = smtp_send_email(mailsrv_ip, mailsrv_port,
local ret, reason = smtp_send_email(mailsrv,
job.mailconf.smtp_hostname,
job.mailconf.smtp_from,
job.mailconf.smtp_to,