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 method: post
params: params:
network_id: int 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: return:

View File

@ -23,17 +23,13 @@ handle_request("POST", "/node/disable", _, #{<<"network_id">> := NetworkId, <<"c
{ok, 200, sdlan_util:json_data(<<"success">>)} {ok, 200, sdlan_util:json_data(<<"success">>)}
end; 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 case sdlan_network:get_pid(NetworkId) of
undefined -> undefined ->
{ok, 200, sdlan_util:json_error(-1, <<"network not found">>)}; {ok, 200, sdlan_util:json_error(-1, <<"network not found">>)};
Pid -> Pid ->
case sdlan_network:exposed_service_changed(Pid, ClientId) of ok = sdlan_network:exposed_service_changed(Pid, ClientIds),
ok -> {ok, 200, sdlan_util:json_data(<<"success">>)}
{ok, 200, sdlan_util:json_data(<<"success">>)};
{error, Reason} ->
{ok, 200, sdlan_util:json_error(-1, Reason)}
end
end; end;
handle_request(_, Path, _, _) -> 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) -> disable_client(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) ->
gen_server:call(Pid, {disable_client, ClientId}). gen_server:call(Pid, {disable_client, ClientId}).
-spec exposed_service_changed(Pid :: pid(), ClientId :: binary()) -> ok | {error, Reason :: binary()}. -spec exposed_service_changed(Pid :: pid(), ClientIds :: [binary()]) -> ok.
exposed_service_changed(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) -> exposed_service_changed(Pid, ClientIds) when is_pid(Pid), is_binary(ClientIds) ->
gen_server:call(Pid, {exposed_service_changed, ClientId}). gen_server:call(Pid, {exposed_service_changed, ClientIds}).
-spec get_channel(Pid :: pid(), ClientId :: binary()) -> error | {ok, ChannelPid :: pid()}. -spec get_channel(Pid :: pid(), ClientId :: binary()) -> error | {ok, ChannelPid :: pid()}.
get_channel(Pid, ClientId) when is_pid(Pid), is_binary(ClientId) -> 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; end;
%% acl改变 %% acl改变
handle_call({exposed_service_changed, ClientId}, _From, State = #state{endpoint_table = EndpointTable}) -> handle_call({exposed_service_changed, ClientIds}, _From, State = #state{endpoint_table = EndpointTable}) ->
MatchSpec = ets:fun2ms(fun(Object = {_Mac, #endpoint{client_id = ClientId0}}) when ClientId0 =:= ClientId -> Endpoints = ets:foldl(fun({_, EP = #endpoint{client_id = ClientId0}}, Acc) ->
Object case lists:member(ClientId0, ClientIds) of
end), true ->
case select_endpoint(EndpointTable, MatchSpec) of [EP|Acc];
{ok, _Mac, #endpoint{channel_pid = ChannelPid, transport = Transport}} -> false ->
ExposedServiceChangedEvent = sdlan_pb:encode_msg(#'SDLEvent' { Acc
event = {exposed_service_changed, #'SDLEvent.ExposedServiceChanged'{}} end
}), end, [], EndpointTable),
Transport:send_event(ChannelPid, ExposedServiceChangedEvent),
{reply, ok, State}; ExposedServiceChangedEvent = sdlan_pb:encode_msg(#'SDLEvent' {
error -> event = {exposed_service_changed, #'SDLEvent.ExposedServiceChanged'{}}
{reply, {error, <<"目标Node不在线"/utf8>>}, State} }),
end; maps:foreach(fun(#endpoint{channel_pid = ChannelPid, transport = Transport}) ->
Transport:send_event(ChannelPid, ExposedServiceChangedEvent)
end, Endpoints),
{reply, ok, State};
handle_call(debug_info, _From, State = #state{network_id = NetworkId, ipaddr = IpAddr, mask_len = MaskLen, owner_id = OwnerId, endpoint_table = EndpointTable}) -> handle_call(debug_info, _From, State = #state{network_id = NetworkId, ipaddr = IpAddr, mask_len = MaskLen, owner_id = OwnerId, endpoint_table = EndpointTable}) ->
Reply = #{ Reply = #{
@ -697,7 +701,7 @@ select_endpoint(Table, MatchSpec) ->
case catch ets:select(Table, MatchSpec, 1) of case catch ets:select(Table, MatchSpec, 1) of
{[], _Continuation} -> {[], _Continuation} ->
error; error;
{[{Mac, Endpoint = #endpoint{}}], _Continuation} -> {[{Mac, Endpoint = #endpoint{}}|_], _Continuation} ->
{ok, Mac, Endpoint}; {ok, Mac, Endpoint};
'$end_of_table' -> '$end_of_table' ->
error; error;