Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit c325d65

Browse files
Merge pull request #279 from rabbitmq/ranch_proxy_header
Use the built-in Ranch PROXY protocol support
2 parents a69599e + 0c50525 commit c325d65

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ define PROJECT_APP_EXTRA_KEYS
2020
endef
2121

2222
LOCAL_DEPS = compiler syntax_tools xmerl
23-
DEPS = lager jsx ranch ranch_proxy_protocol recon
23+
DEPS = lager jsx ranch recon
2424

2525
# FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be
2626
# reviewed and merged.

mk/rabbitmq-components.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ dep_jsx = hex 2.9.0
116116
dep_lager = hex 3.6.5
117117
dep_ra = git https://github.com/rabbitmq/ra.git master
118118
dep_ranch = hex 1.7.1
119-
dep_ranch_proxy_protocol = hex 2.1.1
120119
dep_recon = hex 2.3.6
121120

122121
dep_sockjs = git https://github.com/rabbitmq/sockjs-erlang.git 405990ea62353d98d36dbf5e1e64942d9b0a1daf

src/rabbit_net.erl

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ controlling_process(Sock, Pid) when is_port(Sock) ->
115115
getstat(Sock, Stats) when ?IS_SSL(Sock) ->
116116
inet:getstat(ssl_get_socket(Sock), Stats);
117117
getstat(Sock, Stats) when is_port(Sock) ->
118+
inet:getstat(Sock, Stats);
119+
%% Used by Proxy protocol support in plugins
120+
getstat({rabbit_proxy_socket, Sock, _}, Stats) when ?IS_SSL(Sock) ->
121+
inet:getstat(ssl_get_socket(Sock), Stats);
122+
getstat({rabbit_proxy_socket, Sock, _}, Stats) when is_port(Sock) ->
118123
inet:getstat(Sock, Stats).
119124

120125
recv(Sock) when ?IS_SSL(Sock) ->
@@ -232,15 +237,12 @@ socket_ends(Sock, Direction) when ?IS_SSL(Sock);
232237
{_, {error, _Reason} = Error} ->
233238
Error
234239
end;
235-
socket_ends(Sock, Direction = inbound) when is_tuple(Sock) ->
236-
%% proxy protocol support
237-
%% hack: we have to check the record type
238-
{ok, {{FromAddress, FromPort}, {_, _}}} = case element(1, Sock) of
239-
proxy_socket -> ranch_proxy_protocol:proxyname(undefined, Sock);
240-
ssl_socket -> ranch_proxy_ssl:proxyname(Sock)
241-
end,
240+
socket_ends({rabbit_proxy_socket, CSocket, ProxyInfo}, Direction = inbound) ->
241+
#{
242+
src_address := FromAddress,
243+
src_port := FromPort
244+
} = ProxyInfo,
242245
{_From, To} = sock_funs(Direction),
243-
CSocket = unwrap_socket(Sock),
244246
case To(CSocket) of
245247
{ok, {ToAddress, ToPort}} ->
246248
{ok, {rdns(FromAddress), FromPort,
@@ -294,34 +296,12 @@ is_loopback(_) -> false.
294296

295297
ipv4(AB, CD) -> {AB bsr 8, AB band 255, CD bsr 8, CD band 255}.
296298

297-
unwrap_socket(Sock) when ?IS_SSL(Sock);
298-
is_port(Sock) ->
299+
unwrap_socket({rabbit_proxy_socket, Sock, _}) ->
299300
Sock;
300-
unwrap_socket(Sock) when is_tuple(Sock) ->
301-
%% proxy protocol support
302-
%% hack: we have to check the record type
303-
case element(1, Sock) of
304-
proxy_socket ->
305-
ranch_proxy_protocol:get_csocket(Sock);
306-
ssl_socket ->
307-
ranch_proxy_ssl:get_csocket(Sock)
308-
end;
309301
unwrap_socket(Sock) ->
310302
Sock.
311303

312-
maybe_get_proxy_socket(Sock) when ?IS_SSL(Sock);
313-
is_port(Sock) ->
314-
undefined;
315-
maybe_get_proxy_socket(Sock) when is_tuple(Sock) ->
316-
%% proxy protocol support
317-
%% hack: we have to check the record type
318-
case element(1, Sock) of
319-
proxy_socket ->
320-
Sock;
321-
ssl_socket ->
322-
Sock;
323-
_ ->
324-
undefined
325-
end;
304+
maybe_get_proxy_socket(Sock={rabbit_proxy_socket, _, _}) ->
305+
Sock;
326306
maybe_get_proxy_socket(_Sock) ->
327307
undefined.

0 commit comments

Comments
 (0)