-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathrabbit_web_mqtt_handler.erl
More file actions
557 lines (520 loc) · 23.7 KB
/
Copy pathrabbit_web_mqtt_handler.erl
File metadata and controls
557 lines (520 loc) · 23.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
%%
-module(rabbit_web_mqtt_handler).
-behaviour(cowboy_websocket).
-behaviour(cowboy_sub_protocol).
-include_lib("kernel/include/logger.hrl").
-include_lib("rabbit_common/include/logging.hrl").
-include_lib("rabbitmq_mqtt/include/rabbit_mqtt.hrl").
-include_lib("rabbitmq_mqtt/include/rabbit_mqtt_packet.hrl").
-export([
init/2,
websocket_init/1,
websocket_handle/2,
websocket_info/2,
terminate/3
]).
-export([conserve_resources/3]).
-export([info/2]).
%% cowboy_sub_protocol
-export([upgrade/4,
upgrade/5,
takeover/7]).
-define(APP, rabbitmq_web_mqtt).
-record(state, {
socket :: {rabbit_proxy_socket, any(), any()} | rabbit_net:socket(),
parse_state = rabbit_mqtt_packet:init_state() :: rabbit_mqtt_packet:state(),
proc_state = connect_packet_unprocessed :: connect_packet_unprocessed |
rabbit_mqtt_processor:state(),
connection_state = running :: running | blocked,
blocked_by = sets:new([{version, 2}]) :: sets:set(rabbit_alarm:resource_alarm_source()),
stats_timer :: option(rabbit_event:state()),
keepalive = rabbit_mqtt_keepalive:init() :: rabbit_mqtt_keepalive:state(),
conn_name :: option(binary())
}).
-type state() :: #state{}.
%% Close frame status codes as defined in https://www.rfc-editor.org/rfc/rfc6455#section-7.4.1
-define(CLOSE_NORMAL, 1000).
-define(CLOSE_SERVER_GOING_DOWN, 1001).
-define(CLOSE_PROTOCOL_ERROR, 1002).
-define(CLOSE_UNACCEPTABLE_DATA_TYPE, 1003).
%% cowboy_sub_protcol
upgrade(Req, Env, Handler, HandlerState) ->
upgrade(Req, Env, Handler, HandlerState, #{}).
%% We create a proxy socket for HTTP/2 even if no proxy was used,
%% and add a special field 'http_version' to indicate this is HTTP/2.
upgrade(Req=#{version := 'HTTP/2', pid := Parent, peer := Peer, sock := Sock},
Env, Handler, HandlerState, Opts) ->
%% Cowboy doesn't expose the socket when HTTP/2 is used.
%% We take it directly from the connection's state.
%%
%% @todo Ideally we would not need the real socket for
%% normal operations. But we currently need it for
%% the heartbeat processes to do their job. In the
%% future we should not rely on those processes
%% and instead do the heartbeating directly in the
%% Websocket handler.
RealSocket = element(4,element(1,sys:get_state(Parent))),
ProxyInfo = case Req of
#{proxy_header := ProxyHeader} ->
ProxyHeader#{http_version => 'HTTP/2'};
_ ->
{SrcAddr, SrcPort} = Peer,
{DestAddr, DestPort} = Sock,
#{
http_version => 'HTTP/2',
src_address => SrcAddr,
src_port => SrcPort,
dest_address => DestAddr,
dest_port => DestPort
}
end,
ProxySocket = {rabbit_proxy_socket, RealSocket, ProxyInfo},
cowboy_websocket:upgrade(Req, Env, Handler, HandlerState#state{socket = ProxySocket}, Opts);
upgrade(Req, Env, Handler, HandlerState, Opts) ->
cowboy_websocket:upgrade(Req, Env, Handler, HandlerState, Opts).
%% This is only called for HTTP/1.1.
takeover(Parent, Ref, Socket, Transport, Opts, Buffer, {Handler, HandlerState}) ->
Sock = case HandlerState#state.socket of
undefined ->
Socket;
ProxyInfo ->
{rabbit_proxy_socket, Socket, ProxyInfo}
end,
cowboy_websocket:takeover(Parent, Ref, Socket, Transport, Opts, Buffer,
{Handler, HandlerState#state{socket = Sock}}).
%% cowboy_websocket
init(Req, Opts) ->
case check_origin(Req) of
ok ->
case cowboy_req:parse_header(<<"sec-websocket-protocol">>, Req) of
undefined ->
no_supported_sub_protocol(undefined, Req);
Protocol ->
case lists:search(fun(P) -> P =:= <<"mqtt">> orelse P =:= <<"mqttv3.1">> end, Protocol) of
false ->
no_supported_sub_protocol(Protocol, Req);
{value, MatchedProtocol} ->
Req1 = cowboy_req:set_resp_header(<<"sec-websocket-protocol">>, MatchedProtocol, Req),
State = #state{socket = maps:get(proxy_header, Req, undefined),
stats_timer = rabbit_event:init_stats_timer()},
WsOpts0 = proplists:get_value(ws_opts, Opts, #{}),
MaxFrameSize = persistent_term:get(
?PERSISTENT_TERM_MAX_PACKET_SIZE_UNAUTHENTICATED) + 4096,
WsOpts = maps:merge(#{compress => true,
max_frame_size => MaxFrameSize}, WsOpts0),
{?MODULE, Req1, State, WsOpts#{data_delivery => relay}}
end
end;
{error, origin_not_allowed} ->
?LOG_WARNING("Web MQTT: WebSocket connection rejected, "
"origin not in allow_origins: ~tp",
[cowboy_req:header(<<"origin">>, Req)]),
{ok,
cowboy_req:reply(403, #{<<"connection">> => <<"close">>}, Req),
#state{}}
end.
%% We cannot use a gen_server call, because the handler process is a
%% special cowboy_websocket process (not a gen_server) which assumes
%% all gen_server calls are supervisor calls, and does not pass on the
%% request to this callback module. (see cowboy_websocket:loop/3 and
%% cowboy_children:handle_supervisor_call/4) However using a generic
%% gen:call with a special label ?MODULE works fine.
-spec info(pid(), rabbit_types:info_keys()) ->
rabbit_types:infos().
info(Pid, all) ->
info(Pid, ?INFO_ITEMS);
info(Pid, Items) ->
{ok, Res} = gen:call(Pid, ?MODULE, {info, Items}),
Res.
-spec websocket_init(state()) ->
{cowboy_websocket:commands(), state()} |
{cowboy_websocket:commands(), state(), hibernate}.
websocket_init(State0 = #state{socket = Sock}) ->
logger:set_process_metadata(#{domain => ?RMQLOG_DOMAIN_CONN ++ [web_mqtt]}),
rabbit_access_control:set_max_heap_size_unauthenticated(?APP_NAME),
case rabbit_net:connection_string(Sock, inbound) of
{ok, ConnStr} ->
ConnName = rabbit_data_coercion:to_binary(ConnStr),
?LOG_INFO("Accepting Web MQTT connection ~s", [ConnName]),
LoginTimeout = application:get_env(rabbitmq_mqtt, login_timeout, 10_000),
erlang:send_after(LoginTimeout, self(), login_timeout),
Alarms = rabbit_alarm:register(self(), {?MODULE, conserve_resources, []}),
State = State0#state{conn_name = ConnName,
blocked_by = sets:from_list(Alarms, [{version, 2}])},
process_flag(trap_exit, true),
{[], State, hibernate};
{error, Reason} ->
{[{shutdown_reason, Reason}], State0}
end.
-spec conserve_resources(pid(),
rabbit_alarm:resource_alarm_source(),
rabbit_alarm:resource_alert()) -> ok.
conserve_resources(Pid, Source, {_, Conserve, _}) ->
Pid ! {conserve_resources, Source, Conserve},
ok.
-spec websocket_handle(ping | pong | {text | binary | ping | pong, binary()}, State) ->
{cowboy_websocket:commands(), State} |
{cowboy_websocket:commands(), State, hibernate}.
websocket_handle({binary, Data}, State) ->
handle_data(Data, State);
%% Silently ignore ping and pong frames as Cowboy will automatically reply to ping frames.
websocket_handle({Ping, _}, State)
when Ping =:= ping orelse Ping =:= pong ->
{[], State, hibernate};
websocket_handle(Ping, State)
when Ping =:= ping orelse Ping =:= pong ->
{[], State, hibernate};
%% Log and close connection when receiving any other unexpected frames.
websocket_handle(Frame, State) ->
?LOG_INFO("Web MQTT: unexpected WebSocket frame ~tp", [Frame]),
stop(State, ?CLOSE_UNACCEPTABLE_DATA_TYPE, <<"unexpected WebSocket frame">>).
-spec websocket_info(any(), State) ->
{cowboy_websocket:commands(), State} |
{cowboy_websocket:commands(), State, hibernate}.
websocket_info({conserve_resources, Source, Conserve},
#state{blocked_by = BlockedBy0} = State) ->
BlockedBy = case Conserve of
true ->
sets:add_element(Source, BlockedBy0);
false ->
sets:del_element(Source, BlockedBy0)
end,
handle_credits(State#state{blocked_by = BlockedBy});
websocket_info({bump_credit, Msg}, State) ->
credit_flow:handle_bump_msg(Msg),
handle_credits(State);
websocket_info({reply, Data}, State) ->
{[{binary, Data}], State, hibernate};
websocket_info({stop, CloseCode, Error, SendWill}, State) ->
stop({SendWill, State}, CloseCode, Error);
websocket_info({'EXIT', _, _}, State) ->
stop(State);
websocket_info({'$gen_cast', QueueEvent = {queue_event, _, _}},
State = #state{proc_state = PState0}) ->
case rabbit_mqtt_processor:handle_queue_event(QueueEvent, PState0) of
{ok, PState} ->
handle_credits(State#state{proc_state = PState});
{error, Reason, PState} ->
?LOG_ERROR("Web MQTT connection ~p failed to handle queue event: ~p",
[State#state.conn_name, Reason]),
stop(State#state{proc_state = PState})
end;
websocket_info({'$gen_cast', {duplicate_id, SendWill}},
State = #state{proc_state = ProcState,
conn_name = ConnName}) ->
?LOG_WARNING("Web MQTT disconnecting a client with duplicate ID '~s' (~p)",
[rabbit_mqtt_processor:info(client_id, ProcState), ConnName]),
rabbit_mqtt_processor:send_disconnect(?RC_SESSION_TAKEN_OVER, ProcState),
defer_close(?CLOSE_NORMAL, SendWill),
{[], State};
websocket_info({'$gen_cast', {close_connection, Reason}},
State = #state{proc_state = ProcState,
conn_name = ConnName}) ->
?LOG_WARNING("Web MQTT disconnecting client with ID '~s' (~p), reason: ~s",
[rabbit_mqtt_processor:info(client_id, ProcState), ConnName, Reason]),
case Reason of
maintenance ->
rabbit_mqtt_processor:send_disconnect(?RC_SERVER_SHUTTING_DOWN, ProcState),
defer_close(?CLOSE_SERVER_GOING_DOWN),
{[], State};
_ ->
stop(State)
end;
websocket_info({'$gen_cast', {force_event_refresh, Ref}}, State0) ->
Infos = infos(?EVENT_KEYS, State0),
rabbit_event:notify(connection_created, Infos, Ref),
State = rabbit_event:init_stats_timer(State0, #state.stats_timer),
{[], State, hibernate};
websocket_info({'$gen_cast', refresh_config},
State0 = #state{proc_state = PState0,
conn_name = ConnName}) ->
PState = rabbit_mqtt_processor:update_trace(ConnName, PState0),
State = State0#state{proc_state = PState},
{[], State, hibernate};
websocket_info({keepalive, Req}, State = #state{proc_state = ProcState,
keepalive = KState0,
conn_name = ConnName}) ->
case rabbit_mqtt_keepalive:handle(Req, KState0) of
{ok, KState} ->
{[], State#state{keepalive = KState}, hibernate};
{error, timeout} ->
?LOG_ERROR("keepalive timeout in Web MQTT connection ~p", [ConnName]),
rabbit_mqtt_processor:send_disconnect(?RC_KEEP_ALIVE_TIMEOUT, ProcState),
defer_close(?CLOSE_NORMAL),
{[], State};
{error, Reason} ->
?LOG_ERROR("keepalive error in Web MQTT connection ~p: ~p",
[ConnName, Reason]),
stop(State)
end;
websocket_info(credential_expired,
State = #state{proc_state = ProcState,
conn_name = ConnName}) ->
?LOG_WARNING("Web MQTT disconnecting client with ID '~s' (~p) because credential expired",
[rabbit_mqtt_processor:info(client_id, ProcState), ConnName]),
rabbit_mqtt_processor:send_disconnect(?RC_MAXIMUM_CONNECT_TIME, ProcState),
defer_close(?CLOSE_NORMAL),
{[], State};
websocket_info(emit_stats, State) ->
{[], emit_stats(State), hibernate};
websocket_info({{'DOWN', _QName}, _MRef, process, _Pid, _Reason} = Evt,
State = #state{proc_state = PState0}) ->
case rabbit_mqtt_processor:handle_down(Evt, PState0) of
{ok, PState} ->
handle_credits(State#state{proc_state = PState});
{error, Reason} ->
stop(State, ?CLOSE_NORMAL, Reason)
end;
websocket_info({'DOWN', _MRef, process, QPid, _Reason}, State) ->
rabbit_amqqueue_common:notify_sent_queue_down(QPid),
{[], State, hibernate};
websocket_info({shutdown, Reason}, #state{conn_name = ConnName} = State) ->
%% rabbitmq_management plugin requests to close connection.
?LOG_INFO("Web MQTT closing connection ~tp: ~tp", [ConnName, Reason]),
stop(State, ?CLOSE_NORMAL, Reason);
websocket_info(connection_created, State) ->
Infos = infos(?EVENT_KEYS, State),
rabbit_core_metrics:connection_created(self(), Infos),
rabbit_event:notify(connection_created, Infos),
{[], State, hibernate};
websocket_info({?MODULE, From, {info, Items}}, State) ->
Infos = infos(Items, State),
gen:reply(From, Infos),
{[], State, hibernate};
websocket_info(login_timeout, #state{proc_state = connect_packet_unprocessed,
conn_name = ConnName} = State) ->
?LOG_ERROR("closing Web MQTT connection ~tp (login timeout)", [ConnName]),
stop(State);
websocket_info(login_timeout, State) ->
{[], State, hibernate};
websocket_info(increase_max_frame_size, State) ->
MaxFrameSize = persistent_term:get(
?PERSISTENT_TERM_MAX_PACKET_SIZE_AUTHENTICATED) + 4096,
{[{set_options, #{max_frame_size => MaxFrameSize}}], State, hibernate};
websocket_info(Msg, State) ->
?LOG_WARNING("Web MQTT: unexpected message ~tp", [Msg]),
{[], State, hibernate}.
terminate(Reason, Request, #state{} = State) ->
terminate(Reason, Request, {true, State});
terminate(_Reason, _Request,
{SendWill, #state{conn_name = ConnName,
proc_state = PState,
keepalive = KState} = State}) ->
?LOG_INFO("Web MQTT closing connection ~ts", [ConnName]),
maybe_emit_stats(State),
_ = rabbit_mqtt_keepalive:cancel_timer(KState),
case PState of
connect_packet_unprocessed ->
ok;
_ ->
Infos = infos(?EVENT_KEYS, State),
rabbit_mqtt_processor:terminate(SendWill, Infos, PState)
end.
%% Internal.
no_supported_sub_protocol(Protocol, Req) ->
%% The client MUST include "mqtt" in the list of WebSocket Sub Protocols it offers [MQTT-6.0.0-3].
?LOG_ERROR("Web MQTT: 'mqtt' not included in client offered subprotocols: ~tp", [Protocol]),
{ok,
cowboy_req:reply(400, #{<<"connection">> => <<"close">>}, Req),
#state{}}.
check_origin(Req) ->
case application:get_env(?APP, allow_origins, []) of
[] ->
ok;
AllowedOrigins ->
case cowboy_req:header(<<"origin">>, Req) of
undefined ->
ok;
Origin ->
case lists:member(binary_to_list(Origin), AllowedOrigins) of
true -> ok;
false -> {error, origin_not_allowed}
end
end
end.
handle_data(Data, State0 = #state{}) ->
case handle_data1(Data, State0) of
{ok, State1 = #state{connection_state = blocked}, hibernate} ->
{[{active, false}], State1, hibernate};
Other ->
Other
end.
handle_data1(<<>>, State) ->
{ok, ensure_stats_timer(control_throttle(State)), hibernate};
handle_data1(Data, State = #state{socket = Socket,
parse_state = ParseState,
proc_state = ProcState,
conn_name = ConnName}) ->
try rabbit_mqtt_packet:parse(Data, ParseState) of
{more, ParseState1} ->
{ok, ensure_stats_timer(
control_throttle(
State#state{parse_state = ParseState1})), hibernate};
{ok, Packet, Rest, ParseState1} ->
case ProcState of
connect_packet_unprocessed ->
case rabbit_mqtt_processor:init(Packet, rabbit_net:unwrap_socket(Socket),
ConnName, fun send_reply/1) of
{ok, ProcState1} ->
?LOG_INFO("Accepted Web MQTT connection ~ts for client ID ~ts",
[ConnName, rabbit_mqtt_processor:info(client_id, ProcState1)]),
self() ! increase_max_frame_size,
handle_data1(
Rest, State#state{parse_state = ParseState1,
proc_state = ProcState1});
{error, Reason} ->
?LOG_ERROR("Rejected Web MQTT connection ~ts: ~p", [ConnName, Reason]),
self() ! {stop, ?CLOSE_PROTOCOL_ERROR, connect_packet_rejected,
_SendWill = false},
{[], State}
end;
_ ->
case rabbit_mqtt_processor:process_packet(Packet, ProcState) of
{ok, ProcState1} ->
handle_data1(
Rest,
State#state{parse_state = ParseState1,
proc_state = ProcState1});
{error, Reason, _} ->
stop_mqtt_protocol_error(State, Reason, ConnName);
{stop, {disconnect, server_initiated}, _} ->
defer_close(?CLOSE_PROTOCOL_ERROR),
{[], State};
{stop, {disconnect, {client_initiated, SendWill}}, ProcState1} ->
stop({SendWill, State#state{proc_state = ProcState1}})
end
end;
{error, {disconnect_reason_code, ReasonCode}} ->
rabbit_mqtt_processor:send_disconnect(ReasonCode, ProcState),
defer_close(?CLOSE_PROTOCOL_ERROR),
{[], State};
{error, Reason} ->
stop_mqtt_protocol_error(State, Reason, ConnName)
catch _:Reason:Stacktrace ->
?LOG_DEBUG("Web MQTT cannot parse a packet, reason: ~tp, "
"stacktrace: ~tp, payload (first 100 bytes): ~tp",
[Reason, Stacktrace, rabbit_mqtt_util:truncate_binary(Data, 100)]),
stop_mqtt_protocol_error(State, cannot_parse, ConnName)
end.
%% Allow DISCONNECT packet to be sent to client before closing the connection.
defer_close(CloseStatusCode) ->
defer_close(CloseStatusCode, true).
defer_close(CloseStatusCode, SendWill) ->
self() ! {stop, CloseStatusCode, server_initiated_disconnect, SendWill},
ok.
stop_mqtt_protocol_error(State, Reason, ConnName) ->
?LOG_WARNING("Web MQTT protocol error ~tp for connection ~tp", [Reason, ConnName]),
stop(State, ?CLOSE_PROTOCOL_ERROR, Reason).
stop(State) ->
stop(State, ?CLOSE_NORMAL, "MQTT died").
stop(State, CloseCode, Error0) ->
Error = rabbit_data_coercion:to_binary(Error0),
{[{close, CloseCode, Error}], State}.
handle_credits(State0) ->
State = #state{connection_state = CS} = control_throttle(State0),
Active = case CS of
running -> true;
blocked -> false
end,
{[{active, Active}], State, hibernate}.
control_throttle(State = #state{connection_state = ConnState,
blocked_by = BlockedBy,
proc_state = PState,
keepalive = KState
}) ->
Conserve = not sets:is_empty(BlockedBy),
Throttle = case PState of
connect_packet_unprocessed -> Conserve;
_ -> rabbit_mqtt_processor:throttle(Conserve, PState)
end,
case {ConnState, Throttle} of
{running, true} ->
State#state{connection_state = blocked,
keepalive = rabbit_mqtt_keepalive:cancel_timer(KState)};
{blocked,false} ->
State#state{connection_state = running,
keepalive = rabbit_mqtt_keepalive:start_timer(KState)};
{_, _} ->
State
end.
-spec send_reply(iodata()) -> ok.
send_reply(Data) ->
self() ! {reply, Data},
ok.
ensure_stats_timer(State) ->
rabbit_event:ensure_stats_timer(State, #state.stats_timer, emit_stats).
maybe_emit_stats(#state{stats_timer = undefined}) ->
ok;
maybe_emit_stats(State) ->
rabbit_event:if_enabled(State, #state.stats_timer,
fun() -> emit_stats(State) end).
emit_stats(State=#state{proc_state = connect_packet_unprocessed}) ->
%% Avoid emitting stats on terminate when the connection has not yet been
%% established, as this causes orphan entries on the stats database
rabbit_event:reset_stats_timer(State, #state.stats_timer);
emit_stats(State) ->
[{_, Pid},
{_, RecvOct},
{_, SendOct},
{_, Reductions}] = infos(?SIMPLE_METRICS, State),
Infos = infos(?OTHER_METRICS, State),
rabbit_core_metrics:connection_stats(Pid, Infos),
rabbit_core_metrics:connection_stats(Pid, RecvOct, SendOct, Reductions),
State1 = rabbit_event:reset_stats_timer(State, #state.stats_timer),
ensure_stats_timer(State1).
infos(Items, State) ->
[{Item, i(Item, State)} || Item <- Items].
i(pid, _) ->
self();
i(SockStat, #state{socket = Sock})
when SockStat =:= recv_oct;
SockStat =:= recv_cnt;
SockStat =:= send_oct;
SockStat =:= send_cnt;
SockStat =:= send_pend ->
case rabbit_net:getstat(Sock, [SockStat]) of
{ok, [{_, N}]} when is_number(N) ->
N;
_ ->
0
end;
i(reductions, _) ->
{reductions, Reductions} = erlang:process_info(self(), reductions),
Reductions;
i(garbage_collection, _) ->
rabbit_misc:get_gc_info(self());
i(protocol, #state{proc_state = PState}) ->
{'Web MQTT', rabbit_mqtt_processor:proto_version_tuple(PState)};
i(SSL, #state{socket = Sock})
when SSL =:= ssl;
SSL =:= ssl_protocol;
SSL =:= ssl_key_exchange;
SSL =:= ssl_cipher;
SSL =:= ssl_hash ->
rabbit_ssl:info(SSL, {rabbit_net:unwrap_socket(Sock),
rabbit_net:maybe_get_proxy_socket(Sock)});
i(name, S) ->
i(conn_name, S);
i(conn_name, #state{conn_name = Val}) ->
Val;
i(Cert, #state{socket = Sock})
when Cert =:= peer_cert_issuer;
Cert =:= peer_cert_subject;
Cert =:= peer_cert_serial_number;
Cert =:= peer_cert_validity ->
rabbit_ssl:cert_info(Cert, rabbit_net:unwrap_socket(Sock));
i(state, S) ->
i(connection_state, S);
i(connection_state, #state{proc_state = connect_packet_unprocessed}) ->
starting;
i(connection_state, #state{connection_state = Val}) ->
Val;
i(timeout, #state{keepalive = KState}) ->
rabbit_mqtt_keepalive:interval_secs(KState);
i(Key, #state{proc_state = PState}) ->
rabbit_mqtt_processor:info(Key, PState).