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

Commit 62ad1f1

Browse files
committed
Add settings to disable TCP and TLS listeners explicitly
Fixes #31
1 parent 34d7ffa commit 62ad1f1

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PROJECT_MOD = rabbit_web_mqtt_app
44

55
define PROJECT_ENV
66
[
7-
{tcp_config, []},
7+
{tcp_config, [{ip, "127.0.0.1"}, {port, 15675}]},
88
{ssl_config, []},
99
{num_tcp_acceptors, 10},
1010
{num_ssl_acceptors, 10},

priv/schema/rabbitmq_web_mqtt.schema

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
{mapping, "web_mqtt.tcp.backlog", "rabbitmq_web_mqtt.tcp_config.backlog",
77
[{datatype, integer}]}.
8-
{mapping, "web_mqtt.tcp.listener", "rabbitmq_web_mqtt.tcp_config",
9-
[{datatype, ip}]}.
8+
{mapping, "web_mqtt.tcp.listener", "rabbitmq_web_mqtt.tcp_config", [
9+
{datatype, [{enum, [none]}, ip]}
10+
]}.
1011
{mapping, "web_mqtt.tcp.ip", "rabbitmq_web_mqtt.tcp_config.ip",
1112
[{datatype, string}, {validators, ["is_ip"]}]}.
1213
{mapping, "web_mqtt.tcp.port", "rabbitmq_web_mqtt.tcp_config.port",
@@ -17,19 +18,21 @@
1718
{translation,
1819
"rabbitmq_web_mqtt.tcp_config",
1920
fun(Conf) ->
20-
Setting = cuttlefish:conf_get("web_mqtt.tcp.listener", Conf),
21+
Setting = cuttlefish:conf_get("web_mqtt.tcp.listener", Conf, undefined),
2122
case Setting of
23+
none -> [];
24+
undefined -> [];
2225
{Ip, Port} when is_list(Ip), is_integer(Port) ->
23-
[{ip, Ip}, {port, Port}];
24-
_ -> Setting
26+
[{ip, Ip}, {port, Port}]
2527
end
2628
end
2729
}.
2830

2931
{mapping, "web_mqtt.ssl.backlog", "rabbitmq_web_mqtt.ssl_config.backlog",
3032
[{datatype, integer}]}.
31-
{mapping, "web_mqtt.ssl.listener", "rabbitmq_web_mqtt.ssl_config",
32-
[{datatype, ip}]}.
33+
{mapping, "web_mqtt.ssl.listener", "rabbitmq_web_mqtt.ssl_config", [
34+
{datatype, [{enum, [none]}, ip]}
35+
]}.
3336
{mapping, "web_mqtt.ssl.ip", "rabbitmq_web_mqtt.ssl_config.ip",
3437
[{datatype, string}, {validators, ["is_ip"]}]}.
3538
{mapping, "web_mqtt.ssl.port", "rabbitmq_web_mqtt.ssl_config.port",
@@ -46,8 +49,10 @@
4649
{translation,
4750
"rabbitmq_web_mqtt.ssl_config",
4851
fun(Conf) ->
49-
Setting = cuttlefish:conf_get("web_mqtt.ssl.listener", Conf),
52+
Setting = cuttlefish:conf_get("web_mqtt.ssl.listener", Conf, undefined),
5053
case Setting of
54+
none -> [];
55+
undefined -> [];
5156
{Ip, Port} when is_list(Ip), is_integer(Port) ->
5257
[{ip, Ip}, {port, Port}];
5358
_ -> Setting

src/rabbit_web_mqtt_app.erl

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,27 @@ mqtt_init() ->
5454
CowboyOpts = CowboyOpts0#{env => #{dispatch => Routes},
5555
middlewares => [cowboy_router, rabbit_web_mqtt_middleware, cowboy_handler],
5656
proxy_header => get_env(proxy_protocol, false)},
57-
{TCPConf, IpStr, Port} = get_tcp_conf(),
58-
59-
case ranch:start_listener(web_mqtt, get_env(num_tcp_acceptors, 10),
60-
ranch_tcp, TCPConf,
61-
rabbit_web_mqtt_connection_sup, CowboyOpts) of
62-
{ok, _} -> ok;
63-
{error, {already_started, _}} -> ok;
64-
{error, Err} ->
65-
rabbit_log_connection:error(
66-
"Failed to start a WebSocket (HTTP) listener. Error: ~p,"
67-
" listener settings: ~p~n",
68-
[Err, TCPConf]),
69-
throw(Err)
57+
case get_env(tcp_config, []) of
58+
[] ->
59+
ok;
60+
TCPConf0 ->
61+
{TCPConf, IpStr, Port} = get_tcp_conf(TCPConf0),
62+
case ranch:start_listener(web_mqtt, get_env(num_tcp_acceptors, 10),
63+
ranch_tcp, TCPConf,
64+
rabbit_web_mqtt_connection_sup, CowboyOpts) of
65+
{ok, _} -> ok;
66+
{error, {already_started, _}} -> ok;
67+
{error, ErrTCP} ->
68+
rabbit_log_connection:error(
69+
"Failed to start a WebSocket (HTTP) listener. Error: ~p,"
70+
" listener settings: ~p~n",
71+
[ErrTCP, TCPConf]),
72+
throw(ErrTCP)
73+
end,
74+
listener_started('http/web-mqtt', TCPConf),
75+
rabbit_log:info("rabbit_web_mqtt: listening for HTTP connections on ~s:~w~n",
76+
[IpStr, Port])
7077
end,
71-
listener_started('http/web-mqtt', TCPConf),
72-
rabbit_log:info("rabbit_web_mqtt: listening for HTTP connections on ~s:~w~n",
73-
[IpStr, Port]),
74-
7578
case get_env(ssl_config, []) of
7679
[] ->
7780
ok;
@@ -81,6 +84,18 @@ mqtt_init() ->
8184
{ok, _} = ranch:start_listener(web_mqtt_secure, get_env(num_ssl_acceptors, 10),
8285
ranch_ssl, TLSConf,
8386
rabbit_web_mqtt_connection_sup, CowboyOpts),
87+
case ranch:start_listener(web_mqtt_secure, get_env(num_ssl_acceptors, 10),
88+
ranch_ssl, TLSConf,
89+
rabbit_web_mqtt_connection_sup, CowboyOpts) of
90+
{ok, _} -> ok;
91+
{error, {already_started, _}} -> ok;
92+
{error, ErrTLS} ->
93+
rabbit_log_connection:error(
94+
"Failed to start a TLS WebSocket (HTTPS) listener. Error: ~p,"
95+
" listener settings: ~p~n",
96+
[ErrTLS, TLSConf]),
97+
throw(ErrTLS)
98+
end,
8499
listener_started('https/web-mqtt', TLSConf),
85100
rabbit_log:info("rabbit_web_mqtt: listening for HTTPS connections on ~s:~w~n",
86101
[TLSIpStr, TLSPort])
@@ -95,13 +110,13 @@ listener_started(Protocol, Listener) ->
95110
<- rabbit_networking:tcp_listener_addresses(Port)],
96111
ok.
97112

98-
get_tcp_conf() ->
99-
TCPConf0 = [{connection_type, supervisor}|get_env(tcp_config, [])],
100-
TCPConf1 = case proplists:get_value(port, TCPConf0) of
101-
undefined -> [{port, 15675}|TCPConf0];
102-
_ -> TCPConf0
113+
get_tcp_conf(TCPConf0) ->
114+
TCPConf1 = [{connection_type, supervisor}|TCPConf0],
115+
TCPConf2 = case proplists:get_value(port, TCPConf1) of
116+
undefined -> [{port, 15675}|TCPConf1];
117+
_ -> TCPConf1
103118
end,
104-
get_ip_port(TCPConf1).
119+
get_ip_port(TCPConf2).
105120

106121
get_tls_conf(TLSConf0) ->
107122
TLSConf1 = [{connection_type, supervisor}|TLSConf0],

test/config_schema_SUITE_data/rabbitmq_web_mqtt.snippets

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
[{rabbitmq_web_mqtt,
44
[{tcp_config, [{ip,"127.0.0.1"},{port,12345}]}]}],
55
[rabbitmq_web_mqtt]},
6+
{tcp_listener_none,
7+
"web_mqtt.tcp.listener = none",
8+
[{rabbitmq_web_mqtt,
9+
[{tcp_config, []}]}],
10+
[rabbitmq_web_mqtt]},
611
{tcp_config,
712
"web_mqtt.tcp.ip = 127.0.0.3
813
web_mqtt.tcp.port = 11122",
@@ -24,6 +29,11 @@
2429
[{rabbitmq_web_mqtt,
2530
[{ssl_config, [{ip,"127.0.0.4"},{port,15672}]}]}],
2631
[rabbitmq_web_mqtt]},
32+
{ssl_listener_none,
33+
"web_mqtt.ssl.listener = none",
34+
[{rabbitmq_web_mqtt,
35+
[{ssl_config, []}]}],
36+
[rabbitmq_web_mqtt]},
2737
{ssl,
2838
"web_mqtt.ssl.ip = 127.0.0.2
2939
web_mqtt.ssl.port = 15671

0 commit comments

Comments
 (0)