fix node acl_changed

This commit is contained in:
anlicheng 2026-05-26 21:49:32 +08:00
parent bed29d89da
commit f1fcce9603
3 changed files with 42 additions and 26 deletions

18
API.md
View File

@ -140,7 +140,23 @@ url: /node/disable
method: post
params:
network_id: int
client_id: int
client_id: string
return:
{"result": "success"}
{"error": {"code": 1, "message": "错误描述"}}
```
#### 2. 节点导出的服务发生变化
```text
url: /node/acl_changed
method: post
params:
network_id: int
client_id: string
return:

View File

@ -23,17 +23,13 @@ handle_request("POST", "/node/disable", _, #{<<"network_id">> := NetworkId, <<"c
{ok, 200, sdlan_util:json_data(<<"success">>)}
end;
handle_request("POST", "/node/acl_changed", _, #{<<"network_id">> := NetworkId, <<"client_id">> := ClientId}) when NetworkId > 0 ->
handle_request("POST", "/node/acl_changed", _, #{<<"network_id">> := NetworkId, <<"client_ids">> := ClientIds}) when NetworkId > 0 ->
case sdlan_network:get_pid(NetworkId) of
undefined ->
{ok, 200, sdlan_util:json_error(-1, <<"network not found">>)};
Pid ->
case sdlan_network:exposed_service_changed(Pid, ClientId) of
ok ->
{ok, 200, sdlan_util:json_data(<<"success">>)};
{error, Reason} ->
{ok, 200, sdlan_util:json_error(-1, Reason)}
end
ok = sdlan_network:exposed_service_changed(Pid, ClientIds),
{ok, 200, sdlan_util:json_data(<<"success">>)}
end;
handle_request(_, Path, _, _) ->

View File

@ -155,9 +155,9 @@ update_hole(Pid, SessionToken, ClientId, Mac, Peer, NatType, V6Info) when is_pid
disable_client(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) ->
gen_server:call(Pid, {disable_client, ClientId}).
-spec exposed_service_changed(Pid :: pid(), ClientId :: binary()) -> ok | {error, Reason :: binary()}.
exposed_service_changed(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) ->
gen_server:call(Pid, {exposed_service_changed, ClientId}).
-spec exposed_service_changed(Pid :: pid(), ClientIds :: [binary()]) -> ok.
exposed_service_changed(Pid, ClientIds) when is_pid(Pid), is_binary(ClientIds) ->
gen_server:call(Pid, {exposed_service_changed, ClientIds}).
-spec get_channel(Pid :: pid(), ClientId :: binary()) -> error | {ok, ChannelPid :: pid()}.
get_channel(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) ->
@ -323,20 +323,24 @@ handle_call({command, ReceiverPid, ClientId, SubCommand}, _From, State = #state{
end;
%% acl改变
handle_call({exposed_service_changed, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) ->
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{client_id = ClientId0}}) when ClientId0 =:= ClientId ->
Object
end),
case select_endpoint(EndpointTable, MatchSpec) of
{ok, _Mac, #endpoint{channel_pid = ChannelPid, transport = Transport}} ->
handle_call({exposed_service_changed, ClientIds}, _From, State = #state{endpoint_table = EndpointTable}) ->
Endpoints = ets:foldl(fun({_, EP = #endpoint{client_id = ClientId0}}, Acc) ->
case lists:member(ClientId0, ClientIds) of
true ->
[EP|Acc];
false ->
Acc
end
end, [], EndpointTable),
ExposedServiceChangedEvent = sdlan_pb:encode_msg(#'SDLEvent' {
event = {exposed_service_changed, #'SDLEvent.ExposedServiceChanged'{}}
}),
Transport:send_event(ChannelPid, ExposedServiceChangedEvent),
maps:foreach(fun(#endpoint{channel_pid = ChannelPid, transport = Transport}) ->
Transport:send_event(ChannelPid, ExposedServiceChangedEvent)
end, Endpoints),
{reply, ok, State};
error ->
{reply, {error, <<"目标Node不在线"/utf8>>}, State}
end;
handle_call(debug_info, _From, State = #state{network_id = NetworkId, ipaddr = IpAddr, mask_len = MaskLen, owner_id = OwnerId, endpoint_table = EndpointTable}) ->
Reply = #{
@ -697,7 +701,7 @@ select_endpoint(Table, MatchSpec) ->
case catch ets:select(Table, MatchSpec, 1) of
{[], _Continuation} ->
error;
{[{Mac, Endpoint = #endpoint{}}], _Continuation} ->
{[{Mac, Endpoint = #endpoint{}}|_], _Continuation} ->
{ok, Mac, Endpoint};
'$end_of_table' ->
error;