Просмотр исходного кода

feat(exhook): provide the `peerport` field

- both in `ConnInfo` and `ClientInfo`
JimMoen 2 лет назад
Родитель
Сommit
46201a8796

+ 4 - 0
apps/emqx_exhook/priv/protos/exhook.proto

@@ -368,6 +368,8 @@ message ConnInfo {
   string proto_ver = 7;
 
   uint32 keepalive = 8;
+
+  uint32 peerport = 9;
 }
 
 message ClientInfo {
@@ -397,6 +399,8 @@ message ClientInfo {
 
   // subject of client TLS cert
   string dn = 12;
+
+  uint32 peerport = 13;
 }
 
 message Message {

+ 4 - 1
apps/emqx_exhook/src/emqx_exhook_handler.erl

@@ -294,7 +294,7 @@ conninfo(
     ConnInfo =
         #{
             clientid := ClientId,
-            peername := {Peerhost, _},
+            peername := {Peerhost, PeerPort},
             sockname := {_, SockPort}
         }
 ) ->
@@ -307,6 +307,7 @@ conninfo(
         clientid => ClientId,
         username => maybe(Username),
         peerhost => ntoa(Peerhost),
+        peerport => PeerPort,
         sockport => SockPort,
         proto_name => ProtoName,
         proto_ver => stringfy(ProtoVer),
@@ -319,6 +320,7 @@ clientinfo(
             clientid := ClientId,
             username := Username,
             peerhost := PeerHost,
+            peerport := PeerPort,
             sockport := SockPort,
             protocol := Protocol,
             mountpoint := Mountpoiont
@@ -330,6 +332,7 @@ clientinfo(
         username => maybe(Username),
         password => maybe(maps:get(password, ClientInfo, undefined)),
         peerhost => ntoa(PeerHost),
+        peerport => PeerPort,
         sockport => SockPort,
         protocol => stringfy(Protocol),
         mountpoint => maybe(Mountpoiont),

+ 2 - 0
apps/emqx_exhook/test/emqx_exhook_SUITE.erl

@@ -118,6 +118,7 @@ t_access_failed_if_no_server_running(Config) ->
         clientid => <<"user-id-1">>,
         username => <<"usera">>,
         peerhost => {127, 0, 0, 1},
+        peerport => 3456,
         sockport => 1883,
         protocol => mqtt,
         mountpoint => undefined
@@ -301,6 +302,7 @@ t_simulated_handler(_) ->
         clientid => <<"user-id-1">>,
         username => <<"usera">>,
         peerhost => {127, 0, 0, 1},
+        peerport => 3456,
         sockport => 1883,
         protocol => mqtt,
         mountpoint => undefined

+ 5 - 0
apps/emqx_exhook/test/props/prop_exhook_hooks.erl

@@ -496,6 +496,9 @@ nodestr() ->
 peerhost(#{peername := {Host, _}}) ->
     ntoa(Host).
 
+peerport(#{peername := {_, Port}}) ->
+    Port.
+
 sockport(#{sockname := {_, Port}}) ->
     Port.
 
@@ -564,6 +567,7 @@ from_conninfo(ConnInfo) ->
         clientid => maps:get(clientid, ConnInfo),
         username => maybe(maps:get(username, ConnInfo, <<>>)),
         peerhost => peerhost(ConnInfo),
+        peerport => peerport(ConnInfo),
         sockport => sockport(ConnInfo),
         proto_name => maps:get(proto_name, ConnInfo),
         proto_ver => stringfy(maps:get(proto_ver, ConnInfo)),
@@ -577,6 +581,7 @@ from_clientinfo(ClientInfo) ->
         username => maybe(maps:get(username, ClientInfo, <<>>)),
         password => maybe(maps:get(password, ClientInfo, <<>>)),
         peerhost => ntoa(maps:get(peerhost, ClientInfo)),
+        peerport => maps:get(peerport, ClientInfo),
         sockport => maps:get(sockport, ClientInfo),
         protocol => stringfy(maps:get(protocol, ClientInfo)),
         mountpoint => maybe(maps:get(mountpoint, ClientInfo, <<>>)),