简化逻辑

This commit is contained in:
anlicheng 2026-04-16 16:48:54 +08:00
parent 9dd2bd3380
commit c54a5b35ad
3 changed files with 27 additions and 58 deletions

View File

@ -80,19 +80,19 @@ handle_cast(_Request, State) ->
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}. {stop, Reason :: term(), NewState :: #state{}}.
handle_info({udp, Sock, Ip, Port, Body}, State = #state{socket = Sock}) -> handle_info({udp, Sock, Ip, Port, Body}, State = #state{socket = Sock}) ->
case catch sdlan_pb:decode_msg(Body, 'SDLV6AssistProbe') of maybe
#'SDLV6AssistProbe'{pkt_id = PktId} -> ThrottleKey = {Ip, Port},
V6Bytes = sdlan_util:ipv6_to_bytes(Ip), ok ?= limit_check(ThrottleKey),
ReplyBin = sdlan_pb:encode_msg(#'SDLV6AssistProbeReply'{ #'SDLV6AssistProbe'{pkt_id = PktId} ?= catch sdlan_pb:decode_msg(Body, 'SDLV6AssistProbe'),
pkt_id = PktId, V6Bytes = sdlan_util:ipv6_to_bytes(Ip),
v6_info = #'SDLV6Info'{ ReplyBin = sdlan_pb:encode_msg(#'SDLV6AssistProbeReply'{
v6 = V6Bytes, pkt_id = PktId,
port = Port v6_info = #'SDLV6Info'{
} v6 = V6Bytes,
}), port = Port
ok = gen_udp:send(Sock, Ip, Port, ReplyBin); }
_Err -> }),
ok ok ?= gen_udp:send(Sock, Ip, Port, ReplyBin)
end, end,
{noreply, State}; {noreply, State};
handle_info(_Info, State) -> handle_info(_Info, State) ->
@ -107,4 +107,14 @@ terminate(_Reason, _State = #state{}) ->
Extra :: term()) -> Extra :: term()) ->
{ok, NewState :: #state{}} | {error, Reason :: term()}. {ok, NewState :: #state{}} | {error, Reason :: term()}.
code_change(_OldVsn, State = #state{}, _Extra) -> code_change(_OldVsn, State = #state{}, _Extra) ->
{ok, State}. {ok, State}.
%% 访
-spec limit_check(ThrottleKey :: any()) -> ok | limited.
limit_check(ThrottleKey) ->
case throttle:check(sdlan_ipv6_assist, ThrottleKey) of
{ok, _RestCount, _LeftToReset} ->
ok;
{limit_exceeded, 0, _LeftToReset} ->
limited
end.

View File

@ -22,10 +22,12 @@ start(_StartType, _StartArgs) ->
ok ok
end, end,
%% ipv6探测工具的使用频率
throttle:setup(sdlan_ipv6_assist, 60, per_minute),
%% %%
sdlan_hostname_regedit:init(), sdlan_hostname_regedit:init(),
sdlan_domain_regedit:init(), sdlan_domain_regedit:init(),
sdlan_token_store:init(),
%% %%
identity_policy_ets:init(), identity_policy_ets:init(),

View File

@ -1,43 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2026, <COMPANY>
%%% @doc
%%% Token存储
%%% @end
%%% Created : 16. 4 2026 12:00
%%%-------------------------------------------------------------------
-module(sdlan_token_store).
-author("anlicheng").
%% API
-export([init/0, store/1, lookup/1, delete/1, generate_token/0]).
-define(TABLE, sdlan_token_store).
-spec init() -> ok.
init() ->
ets:new(?TABLE, [named_table, set, public, {read_concurrency, true}, {write_concurrency, true}]).
generate_token() ->
Token = sdlan_util:rand_byte(128),
store(Token),
Token.
-spec store(Token :: binary()) -> ok.
store(Token) when is_binary(Token) ->
true = ets:insert(?TABLE, {Token}),
ok.
-spec lookup(Token :: binary()) -> boolean().
lookup(Token) when is_binary(Token) ->
case ets:lookup(?TABLE, Token) of
[{Token}] ->
true;
[] ->
false
end.
-spec delete(Token :: binary()) -> ok.
delete(Token) when is_binary(Token) ->
true = ets:delete(?TABLE, Token),
ok.