diff --git a/src/mail/ngx_mail_proxy_module.c b/src/mail/ngx_mail_proxy_module.c index 84a7f61a4..ecfcd489f 100644 --- a/src/mail/ngx_mail_proxy_module.c +++ b/src/mail/ngx_mail_proxy_module.c @@ -18,6 +18,7 @@ typedef struct { ngx_flag_t xclient; ngx_flag_t smtp_auth; ngx_flag_t proxy_protocol; + ngx_uint_t proxy_protocol_version; size_t buffer_size; ngx_msec_t timeout; } ngx_mail_proxy_conf_t; @@ -40,6 +41,13 @@ static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child); +static ngx_conf_enum_t ngx_mail_proxy_protocol_versions[] = { + { ngx_string("1"), 1 }, + { ngx_string("2"), 2 }, + { ngx_null_string, 0 } +}; + + static ngx_command_t ngx_mail_proxy_commands[] = { { ngx_string("proxy"), @@ -91,6 +99,13 @@ static ngx_command_t ngx_mail_proxy_commands[] = { offsetof(ngx_mail_proxy_conf_t, proxy_protocol), NULL }, + { ngx_string("proxy_protocol_version"), + NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, + ngx_conf_set_enum_slot, + NGX_MAIL_SRV_CONF_OFFSET, + offsetof(ngx_mail_proxy_conf_t, proxy_protocol_version), + &ngx_mail_proxy_protocol_versions }, + ngx_null_command }; @@ -905,18 +920,27 @@ ngx_mail_proxy_write_handler(ngx_event_t *wev) static ngx_int_t ngx_mail_proxy_send_proxy_protocol(ngx_mail_session_t *s) { - u_char *p; - ssize_t n, size; - ngx_connection_t *c; - u_char buf[NGX_PROXY_PROTOCOL_V1_MAX_HEADER]; + u_char *p; + ssize_t n, size; + ngx_connection_t *c; + ngx_mail_proxy_conf_t *pcf; + u_char buf[NGX_PROXY_PROTOCOL_V2_MAX_HEADER]; s->connection->log->action = "sending PROXY protocol header to upstream"; ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, "mail proxy send PROXY protocol header"); - p = ngx_proxy_protocol_write(s->connection, buf, - buf + NGX_PROXY_PROTOCOL_V1_MAX_HEADER); + pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); + + if (pcf->proxy_protocol_version == 2) { + p = ngx_proxy_protocol_v2_write_tlvs(s->connection, buf, + buf + sizeof(buf), NULL); + } else { + p = ngx_proxy_protocol_write(s->connection, buf, + buf + NGX_PROXY_PROTOCOL_V1_MAX_HEADER); + } + if (p == NULL) { ngx_mail_proxy_internal_server_error(s); return NGX_ERROR; @@ -1385,6 +1409,7 @@ ngx_mail_proxy_create_conf(ngx_conf_t *cf) pcf->xclient = NGX_CONF_UNSET; pcf->smtp_auth = NGX_CONF_UNSET; pcf->proxy_protocol = NGX_CONF_UNSET; + pcf->proxy_protocol_version = NGX_CONF_UNSET_UINT; pcf->buffer_size = NGX_CONF_UNSET_SIZE; pcf->timeout = NGX_CONF_UNSET_MSEC; @@ -1403,6 +1428,8 @@ ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) ngx_conf_merge_value(conf->xclient, prev->xclient, 1); ngx_conf_merge_value(conf->smtp_auth, prev->smtp_auth, 0); ngx_conf_merge_value(conf->proxy_protocol, prev->proxy_protocol, 0); + ngx_conf_merge_uint_value(conf->proxy_protocol_version, + prev->proxy_protocol_version, 1); ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, (size_t) ngx_pagesize); ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);