Skip to content

Commit

Permalink
fix: improve the error logs in proxy-protocol mode
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Apr 20, 2023
1 parent 4446082 commit 1c6ba77
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/esockd_proxy_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
-export([parse_v1/2, parse_v2/4, parse_pp2_tlv/2, parse_pp2_ssl/1]).
-endif.

-define(LOG(LEVEL, Format, Args), logger:log(LEVEL, "[~s] " ++ Format, [?MODULE | Args])).
-define(ERR_MSG_IN_PP_MODE, "The listener is working in proxy protocol mode").

%% Protocol Command
-define(LOCAL, 16#0).
-define(PROXY, 16#1).
Expand Down Expand Up @@ -83,20 +86,26 @@ recv(Transport, Sock, Timeout) ->
Transport:setopts(Sock, OriginOpts),
parse_v2(Cmd, Trans, ProxyInfo, #proxy_socket{inet = inet_family(AF), socket = Sock});
{error, Reason} ->
{error, {recv_proxy_info_error, Reason}}
?LOG(error, ?ERR_MSG_IN_PP_MODE", but some errors occurred "
"while waiting for proxy info: ~p", [Reason]),
{error, recv_proxy_info_error}
end;
{tcp_error, _Sock, Reason} ->
{error, {recv_proxy_info_error, Reason}};
?LOG(error, ?ERR_MSG_IN_PP_MODE", but got a TCP error "
"while waiting for proxy info: ~p", [Reason]),
{error, recv_proxy_info_error};
{tcp_closed, _Sock} ->
%% Socket closed before any data is received.
%% Here we return an atom here to avoid error level logging.
%% See the from the connection_crashed function in esockd_connection_sup.erl.
{error, proxy_proto_close};
{_, _Sock, ProxyInfo} ->
{error, {invalid_proxy_info, ProxyInfo}}
?LOG(error, ?ERR_MSG_IN_PP_MODE", but got invalid proxy info: ~p", [ProxyInfo]),
{error, invalid_proxy_info}
after
Timeout ->
{error, {proxy_proto_timeout, Timeout}}
?LOG(error, ?ERR_MSG_IN_PP_MODE", but timed out while waiting for proxy info", []),
{error, proxy_proto_timeout}
end.

parse_v1(ProxyInfo, ProxySock) ->
Expand Down

0 comments on commit 1c6ba77

Please sign in to comment.