From f2cbe7f3c27c9a78f4c9c56fd752eb4e6cfa9557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chmielowski?= Date: Mon, 16 Jan 2023 11:01:24 +0100 Subject: [PATCH] Recognize ws5/wss5 urls in mqtt_bridge --- src/mod_mqtt_bridge.erl | 5 +++-- src/mod_mqtt_bridge_session.erl | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/mod_mqtt_bridge.erl b/src/mod_mqtt_bridge.erl index 4aeafc7e6..696b781d5 100644 --- a/src/mod_mqtt_bridge.erl +++ b/src/mod_mqtt_bridge.erl @@ -107,7 +107,7 @@ mod_opt_type(replication_user) -> econf:jid(); mod_opt_type(servers) -> econf:and_then( - econf:map(econf:url([mqtt, mqtts, mqtt5, mqtt5s, ws, wss]), + econf:map(econf:url([mqtt, mqtts, mqtt5, mqtt5s, ws, wss, ws5, wss5]), econf:options( #{ publish => econf:map(econf:binary(), econf:binary(), [{return, map}]), @@ -130,7 +130,8 @@ mod_opt_type(servers) -> {ok, Scheme, _UserInfo, Host, Port, Path, _Query} = misc:uri_parse(Url, [{mqtt, 1883}, {mqtts, 8883}, {mqtt5, 1883}, {mqtt5s, 8883}, - {ws, 80}, {wss, 443}]), + {ws, 80}, {wss, 443}, + {ws5, 80}, {wss5, 443}]), Publish = maps:get(publish, Opts, #{}), Subscribe = maps:get(subscribe, Opts, #{}), Authentication = maps:get(authentication, Opts, []), diff --git a/src/mod_mqtt_bridge_session.erl b/src/mod_mqtt_bridge_session.erl index 8b41a8861..eb01f9fb7 100644 --- a/src/mod_mqtt_bridge_session.erl +++ b/src/mod_mqtt_bridge_session.erl @@ -88,14 +88,17 @@ start_link(Proc, Transport, Host, Port, Path, Publish, Subscribe, Authentication %%% gen_server callbacks %%%=================================================================== init([_Proc, Proto, Host, Port, Path, Publish, Subscribe, Authentication, ReplicationUser]) -> - {Version, Transport} = case Proto of - mqtt -> {4, gen_tcp}; - mqtts -> {4, ssl}; - mqtt5 -> {5, gen_tcp}; - mqtt5s -> {5, ssl}; - ws -> {4, gen_tcp}; - wss -> {4, ssl} - end, + {Version, Transport, IsWs} = + case Proto of + mqtt -> {4, gen_tcp, false}; + mqtts -> {4, ssl, false}; + mqtt5 -> {5, gen_tcp, false}; + mqtt5s -> {5, ssl, false}; + ws -> {4, gen_tcp, true}; + wss -> {4, ssl, true}; + ws5 -> {5, gen_tcp, true}; + wss5 -> {5, ssl, true} + end, State = #state{version = Version, id = p1_rand:uniform(65535), codec = mqtt_codec:new(4096), @@ -104,15 +107,15 @@ init([_Proc, Proto, Host, Port, Path, Publish, Subscribe, Authentication, Replic usr = jid:tolower(ReplicationUser), publish = Publish}, case Authentication of - #{certfile := Cert} when Proto == mqtts; Proto == mqtt5s; Proto == wss -> + #{certfile := Cert} when Transport == ssl -> Sock = ssl:connect(Host, Port, [binary, {active, true}, {certfile, Cert}]), - if Proto == ws orelse Proto == wss -> + if IsWs -> connect_ws(Host, Port, Path, Sock, State, ssl, none); true -> connect(Sock, State, ssl, none) end; #{username := User, password := Pass} -> Sock = Transport:connect(Host, Port, [binary, {active, true}]), - if Proto == ws orelse Proto == wss -> + if IsWs -> connect_ws(Host, Port, Path, Sock, State, Transport, {User, Pass}); true -> connect(Sock, State, Transport, {User, Pass}) end;