|
@@ -33,35 +33,34 @@
|
|
|
-export([shutdown/2]).
|
|
-export([shutdown/2]).
|
|
|
|
|
|
|
|
-record(pstate, {
|
|
-record(pstate, {
|
|
|
- zone,
|
|
|
|
|
- sendfun,
|
|
|
|
|
- peername,
|
|
|
|
|
- peercert,
|
|
|
|
|
- proto_ver,
|
|
|
|
|
- proto_name,
|
|
|
|
|
- ackprops,
|
|
|
|
|
- client_id,
|
|
|
|
|
- is_assigned,
|
|
|
|
|
- conn_pid,
|
|
|
|
|
- conn_props,
|
|
|
|
|
- ack_props,
|
|
|
|
|
- username,
|
|
|
|
|
- session,
|
|
|
|
|
- clean_start,
|
|
|
|
|
- topic_aliases,
|
|
|
|
|
- packet_size,
|
|
|
|
|
- will_topic,
|
|
|
|
|
- will_msg,
|
|
|
|
|
- keepalive,
|
|
|
|
|
- mountpoint,
|
|
|
|
|
- is_super,
|
|
|
|
|
- is_bridge,
|
|
|
|
|
- enable_ban,
|
|
|
|
|
- enable_acl,
|
|
|
|
|
- recv_stats,
|
|
|
|
|
- send_stats,
|
|
|
|
|
- connected,
|
|
|
|
|
- connected_at
|
|
|
|
|
|
|
+ zone,
|
|
|
|
|
+ sendfun,
|
|
|
|
|
+ peername,
|
|
|
|
|
+ peercert,
|
|
|
|
|
+ proto_ver,
|
|
|
|
|
+ proto_name,
|
|
|
|
|
+ client_id,
|
|
|
|
|
+ is_assigned,
|
|
|
|
|
+ conn_pid,
|
|
|
|
|
+ conn_props,
|
|
|
|
|
+ ack_props,
|
|
|
|
|
+ username,
|
|
|
|
|
+ session,
|
|
|
|
|
+ clean_start,
|
|
|
|
|
+ topic_aliases,
|
|
|
|
|
+ packet_size,
|
|
|
|
|
+ will_topic,
|
|
|
|
|
+ will_msg,
|
|
|
|
|
+ keepalive,
|
|
|
|
|
+ mountpoint,
|
|
|
|
|
+ is_super,
|
|
|
|
|
+ is_bridge,
|
|
|
|
|
+ enable_ban,
|
|
|
|
|
+ enable_acl,
|
|
|
|
|
+ recv_stats,
|
|
|
|
|
+ send_stats,
|
|
|
|
|
+ connected,
|
|
|
|
|
+ connected_at
|
|
|
}).
|
|
}).
|
|
|
|
|
|
|
|
-type(state() :: #pstate{}).
|
|
-type(state() :: #pstate{}).
|
|
@@ -71,6 +70,8 @@
|
|
|
-compile(export_all).
|
|
-compile(export_all).
|
|
|
-endif.
|
|
-endif.
|
|
|
|
|
|
|
|
|
|
+-define(NO_PROPS, undefined).
|
|
|
|
|
+
|
|
|
-define(LOG(Level, Format, Args, PState),
|
|
-define(LOG(Level, Format, Args, PState),
|
|
|
emqx_logger:Level([{client, PState#pstate.client_id}], "MQTT(~s@~s): " ++ Format,
|
|
emqx_logger:Level([{client, PState#pstate.client_id}], "MQTT(~s@~s): " ++ Format,
|
|
|
[PState#pstate.client_id, esockd_net:format(PState#pstate.peername) | Args])).
|
|
[PState#pstate.client_id, esockd_net:format(PState#pstate.peername) | Args])).
|
|
@@ -106,9 +107,10 @@ init(#{peername := Peername, peercert := Peercert, sendfun := SendFun}, Options)
|
|
|
|
|
|
|
|
init_username(Peercert, Options) ->
|
|
init_username(Peercert, Options) ->
|
|
|
case proplists:get_value(peer_cert_as_username, Options) of
|
|
case proplists:get_value(peer_cert_as_username, Options) of
|
|
|
- cn -> esockd_peercert:common_name(Peercert);
|
|
|
|
|
- dn -> esockd_peercert:subject(Peercert);
|
|
|
|
|
- _ -> undefined
|
|
|
|
|
|
|
+ cn -> esockd_peercert:common_name(Peercert);
|
|
|
|
|
+ dn -> esockd_peercert:subject(Peercert);
|
|
|
|
|
+ crt -> Peercert;
|
|
|
|
|
+ _ -> undefined
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
set_username(Username, PState = #pstate{username = undefined}) ->
|
|
set_username(Username, PState = #pstate{username = undefined}) ->
|
|
@@ -603,10 +605,10 @@ send(Packet = ?PACKET(Type), PState = #pstate{proto_ver = Ver, sendfun = SendFun
|
|
|
%%------------------------------------------------------------------------------
|
|
%%------------------------------------------------------------------------------
|
|
|
%% Assign a clientid
|
|
%% Assign a clientid
|
|
|
|
|
|
|
|
-maybe_assign_client_id(PState = #pstate{client_id = <<>>, ackprops = AckProps}) ->
|
|
|
|
|
|
|
+maybe_assign_client_id(PState = #pstate{client_id = <<>>, ack_props = AckProps}) ->
|
|
|
ClientId = emqx_guid:to_base62(emqx_guid:gen()),
|
|
ClientId = emqx_guid:to_base62(emqx_guid:gen()),
|
|
|
AckProps1 = set_property('Assigned-Client-Identifier', ClientId, AckProps),
|
|
AckProps1 = set_property('Assigned-Client-Identifier', ClientId, AckProps),
|
|
|
- PState#pstate{client_id = ClientId, is_assigned = true, ackprops = AckProps1};
|
|
|
|
|
|
|
+ PState#pstate{client_id = ClientId, is_assigned = true, ack_props = AckProps1};
|
|
|
maybe_assign_client_id(PState) ->
|
|
maybe_assign_client_id(PState) ->
|
|
|
PState.
|
|
PState.
|
|
|
|
|
|
|
@@ -671,7 +673,7 @@ authenticate(Credentials, Password) ->
|
|
|
{error, Error}
|
|
{error, Error}
|
|
|
end.
|
|
end.
|
|
|
|
|
|
|
|
-set_property(Name, Value, undefined) ->
|
|
|
|
|
|
|
+set_property(Name, Value, ?NO_PROPS) ->
|
|
|
#{Name => Value};
|
|
#{Name => Value};
|
|
|
set_property(Name, Value, Props) ->
|
|
set_property(Name, Value, Props) ->
|
|
|
Props#{Name => Value}.
|
|
Props#{Name => Value}.
|