Support proxy protocol of websocket (#2372)
Support proxy protocol of websocket
This commit is contained in:
parent
24f8c3a391
commit
0d6b937127
|
@ -64,25 +64,27 @@ start_listener(Proto, ListenOn, Options) when Proto == ssl; Proto == tls ->
|
||||||
|
|
||||||
%% Start MQTT/WS listener
|
%% Start MQTT/WS listener
|
||||||
start_listener(Proto, ListenOn, Options) when Proto == http; Proto == ws ->
|
start_listener(Proto, ListenOn, Options) when Proto == http; Proto == ws ->
|
||||||
Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
|
start_http_listener(fun cowboy:start_clear/3, 'mqtt:ws', ListenOn, ranch_opts(Options), ws_opts(Options));
|
||||||
start_http_listener(fun cowboy:start_clear/3, 'mqtt:ws', ListenOn, ranch_opts(Options), Dispatch);
|
|
||||||
|
|
||||||
%% Start MQTT/WSS listener
|
%% Start MQTT/WSS listener
|
||||||
start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss ->
|
start_listener(Proto, ListenOn, Options) when Proto == https; Proto == wss ->
|
||||||
Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
|
start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), ws_opts(Options)).
|
||||||
start_http_listener(fun cowboy:start_tls/3, 'mqtt:wss', ListenOn, ranch_opts(Options), Dispatch).
|
|
||||||
|
|
||||||
start_mqtt_listener(Name, ListenOn, Options) ->
|
start_mqtt_listener(Name, ListenOn, Options) ->
|
||||||
SockOpts = esockd:parse_opt(Options),
|
SockOpts = esockd:parse_opt(Options),
|
||||||
esockd:open(Name, ListenOn, merge_default(SockOpts),
|
esockd:open(Name, ListenOn, merge_default(SockOpts),
|
||||||
{emqx_connection, start_link, [Options -- SockOpts]}).
|
{emqx_connection, start_link, [Options -- SockOpts]}).
|
||||||
|
|
||||||
start_http_listener(Start, Name, ListenOn, RanchOpts, Dispatch) ->
|
start_http_listener(Start, Name, ListenOn, RanchOpts, ProtoOpts) ->
|
||||||
Start(Name, with_port(ListenOn, RanchOpts), #{env => #{dispatch => Dispatch}}).
|
Start(Name, with_port(ListenOn, RanchOpts), ProtoOpts).
|
||||||
|
|
||||||
mqtt_path(Options) ->
|
mqtt_path(Options) ->
|
||||||
proplists:get_value(mqtt_path, Options, "/mqtt").
|
proplists:get_value(mqtt_path, Options, "/mqtt").
|
||||||
|
|
||||||
|
ws_opts(Options) ->
|
||||||
|
Dispatch = cowboy_router:compile([{'_', [{mqtt_path(Options), emqx_ws_connection, Options}]}]),
|
||||||
|
#{env => #{dispatch => Dispatch}, proxy_header => proplists:get_value(proxy_protocol, Options, false)}.
|
||||||
|
|
||||||
ranch_opts(Options) ->
|
ranch_opts(Options) ->
|
||||||
NumAcceptors = proplists:get_value(acceptors, Options, 4),
|
NumAcceptors = proplists:get_value(acceptors, Options, 4),
|
||||||
MaxConnections = proplists:get_value(max_connections, Options, 1024),
|
MaxConnections = proplists:get_value(max_connections, Options, 1024),
|
||||||
|
@ -163,4 +165,3 @@ format({Addr, Port}) when is_list(Addr) ->
|
||||||
io_lib:format("~s:~w", [Addr, Port]);
|
io_lib:format("~s:~w", [Addr, Port]);
|
||||||
format({Addr, Port}) when is_tuple(Addr) ->
|
format({Addr, Port}) when is_tuple(Addr) ->
|
||||||
io_lib:format("~s:~w", [esockd_net:ntoab(Addr), Port]).
|
io_lib:format("~s:~w", [esockd_net:ntoab(Addr), Port]).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue