fix PktId

This commit is contained in:
anlicheng 2026-02-15 00:02:49 +08:00
parent 06e52f18ce
commit de30e7e520
4 changed files with 963 additions and 667 deletions

View File

@ -38,87 +38,96 @@
-ifndef('SDL_EMPTY_PB_H'). -ifndef('SDL_EMPTY_PB_H').
-define('SDL_EMPTY_PB_H', true). -define('SDL_EMPTY_PB_H', true).
-record(sdl_empty, -record(sdl_empty,
{ {pkt_id = 0 :: non_neg_integer() | undefined % = 1, optional, 32 bits
}). }).
-endif. -endif.
-ifndef('SDL_REGISTER_SUPER_PB_H'). -ifndef('SDL_REGISTER_SUPER_PB_H').
-define('SDL_REGISTER_SUPER_PB_H', true). -define('SDL_REGISTER_SUPER_PB_H', true).
-record(sdl_register_super, -record(sdl_register_super,
{client_id = <<>> :: unicode:chardata() | undefined, % = 1, optional {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
network_id = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits client_id = <<>> :: unicode:chardata() | undefined, % = 2, optional
mac = <<>> :: iodata() | undefined, % = 3, optional network_id = 0 :: non_neg_integer() | undefined, % = 3, optional, 32 bits
ip = 0 :: non_neg_integer() | undefined, % = 4, optional, 32 bits mac = <<>> :: iodata() | undefined, % = 4, optional
mask_len = 0 :: non_neg_integer() | undefined, % = 5, optional, 32 bits ip = 0 :: non_neg_integer() | undefined, % = 5, optional, 32 bits
hostname = <<>> :: unicode:chardata() | undefined, % = 6, optional mask_len = 0 :: non_neg_integer() | undefined, % = 6, optional, 32 bits
pub_key = <<>> :: unicode:chardata() | undefined, % = 7, optional hostname = <<>> :: unicode:chardata() | undefined, % = 7, optional
access_token = <<>> :: unicode:chardata() | undefined % = 8, optional pub_key = <<>> :: unicode:chardata() | undefined, % = 8, optional
access_token = <<>> :: unicode:chardata() | undefined % = 9, optional
}). }).
-endif. -endif.
-ifndef('SDL_REGISTER_SUPER_ACK_PB_H'). -ifndef('SDL_REGISTER_SUPER_ACK_PB_H').
-define('SDL_REGISTER_SUPER_ACK_PB_H', true). -define('SDL_REGISTER_SUPER_ACK_PB_H', true).
-record(sdl_register_super_ack, -record(sdl_register_super_ack,
{aes_key = <<>> :: iodata() | undefined, % = 1, optional {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
session_token = <<>> :: iodata() | undefined % = 2, optional aes_key = <<>> :: iodata() | undefined, % = 2, optional
session_token = <<>> :: iodata() | undefined % = 3, optional
}). }).
-endif. -endif.
-ifndef('SDL_REGISTER_SUPER_NAK_PB_H'). -ifndef('SDL_REGISTER_SUPER_NAK_PB_H').
-define('SDL_REGISTER_SUPER_NAK_PB_H', true). -define('SDL_REGISTER_SUPER_NAK_PB_H', true).
-record(sdl_register_super_nak, -record(sdl_register_super_nak,
{error_code = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
error_message = <<>> :: unicode:chardata() | undefined % = 2, optional error_code = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits
error_message = <<>> :: unicode:chardata() | undefined % = 3, optional
}). }).
-endif. -endif.
-ifndef('SDL_QUERY_INFO_PB_H'). -ifndef('SDL_QUERY_INFO_PB_H').
-define('SDL_QUERY_INFO_PB_H', true). -define('SDL_QUERY_INFO_PB_H', true).
-record(sdl_query_info, -record(sdl_query_info,
{dst_mac = <<>> :: iodata() | undefined % = 1, optional {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
dst_mac = <<>> :: iodata() | undefined % = 2, optional
}). }).
-endif. -endif.
-ifndef('SDL_PEER_INFO_PB_H'). -ifndef('SDL_PEER_INFO_PB_H').
-define('SDL_PEER_INFO_PB_H', true). -define('SDL_PEER_INFO_PB_H', true).
-record(sdl_peer_info, -record(sdl_peer_info,
{dst_mac = <<>> :: iodata() | undefined, % = 1, optional {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
v4_info = undefined :: sdlan_pb:sdl_v4_info() | undefined, % = 2, optional dst_mac = <<>> :: iodata() | undefined, % = 2, optional
v6_info :: sdlan_pb:sdl_v6_info() | undefined % = 3, optional v4_info = undefined :: sdlan_pb:sdl_v4_info() | undefined, % = 3, optional
v6_info :: sdlan_pb:sdl_v6_info() | undefined % = 4, optional
}). }).
-endif. -endif.
-ifndef('SDL_ARP_REQUEST_PB_H'). -ifndef('SDL_ARP_REQUEST_PB_H').
-define('SDL_ARP_REQUEST_PB_H', true). -define('SDL_ARP_REQUEST_PB_H', true).
-record(sdl_arp_request, -record(sdl_arp_request,
{target_ip = 0 :: non_neg_integer() | undefined % = 1, optional, 32 bits {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
target_ip = 0 :: non_neg_integer() | undefined % = 2, optional, 32 bits
}). }).
-endif. -endif.
-ifndef('SDL_ARP_RESPONSE_PB_H'). -ifndef('SDL_ARP_RESPONSE_PB_H').
-define('SDL_ARP_RESPONSE_PB_H', true). -define('SDL_ARP_RESPONSE_PB_H', true).
-record(sdl_arp_response, -record(sdl_arp_response,
{target_ip = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
target_mac = <<>> :: iodata() | undefined % = 2, optional target_ip = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits
target_mac = <<>> :: iodata() | undefined % = 3, optional
}). }).
-endif. -endif.
-ifndef('SDL_POLICY_REQUEST_PB_H'). -ifndef('SDL_POLICY_REQUEST_PB_H').
-define('SDL_POLICY_REQUEST_PB_H', true). -define('SDL_POLICY_REQUEST_PB_H', true).
-record(sdl_policy_request, -record(sdl_policy_request,
{src_identity_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
dst_identity_id = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits src_identity_id = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits
version = 0 :: non_neg_integer() | undefined % = 3, optional, 32 bits dst_identity_id = 0 :: non_neg_integer() | undefined, % = 3, optional, 32 bits
version = 0 :: non_neg_integer() | undefined % = 4, optional, 32 bits
}). }).
-endif. -endif.
-ifndef('SDL_POLICY_RESPONSE_PB_H'). -ifndef('SDL_POLICY_RESPONSE_PB_H').
-define('SDL_POLICY_RESPONSE_PB_H', true). -define('SDL_POLICY_RESPONSE_PB_H', true).
-record(sdl_policy_response, -record(sdl_policy_response,
{src_identity_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits {pkt_id = 0 :: non_neg_integer() | undefined, % = 1, optional, 32 bits
dst_identity_id = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits src_identity_id = 0 :: non_neg_integer() | undefined, % = 2, optional, 32 bits
version = 0 :: non_neg_integer() | undefined, % = 3, optional, 32 bits dst_identity_id = 0 :: non_neg_integer() | undefined, % = 3, optional, 32 bits
rules = <<>> :: iodata() | undefined % = 4, optional version = 0 :: non_neg_integer() | undefined, % = 4, optional, 32 bits
rules = <<>> :: iodata() | undefined % = 5, optional
}). }).
-endif. -endif.

View File

@ -113,9 +113,9 @@ handle_event(internal, do_init, initializing, State=#state{conn = Conn, max_pack
{stop, Reason, State} {stop, Reason, State}
end; end;
handle_event(info, {frame, <<PacketId:32, ?PACKET_REGISTER_SUPER, Body/binary>>}, initialized, State=#state{stream = Stream}) -> handle_event(info, {frame, <<?PACKET_REGISTER_SUPER, Body/binary>>}, initialized, State=#state{stream = Stream}) ->
#sdl_register_super{ #sdl_register_super{
client_id = ClientId, network_id = NetworkId, mac = Mac, ip = Ip, mask_len = MaskLen, pkt_id = PktId, client_id = ClientId, network_id = NetworkId, mac = Mac, ip = Ip, mask_len = MaskLen,
hostname = HostName, pub_key = PubKey, access_token = AccessToken} = sdlan_pb:decode_msg(Body, sdl_register_super), hostname = HostName, pub_key = PubKey, access_token = AccessToken} = sdlan_pb:decode_msg(Body, sdl_register_super),
true = (Mac =/= <<>> andalso PubKey =/= <<>> andalso ClientId =/= <<>>), true = (Mac =/= <<>> andalso PubKey =/= <<>> andalso ClientId =/= <<>>),
@ -145,46 +145,50 @@ handle_event(info, {frame, <<PacketId:32, ?PACKET_REGISTER_SUPER, Body/binary>>}
{ok, AesKey, SessionToken} = sdlan_network:attach(NetworkPid, self(), ClientId, Mac, Ip, HostName), {ok, AesKey, SessionToken} = sdlan_network:attach(NetworkPid, self(), ClientId, Mac, Ip, HostName),
RsaPubKey = sdlan_cipher:rsa_pem_decode(PubKey), RsaPubKey = sdlan_cipher:rsa_pem_decode(PubKey),
RegisterSuperAck = sdlan_pb:encode_msg(#sdl_register_super_ack { RegisterSuperAck = sdlan_pb:encode_msg(#sdl_register_super_ack {
pkt_id = PktId,
aes_key = rsa_encode(AesKey, RsaPubKey), aes_key = rsa_encode(AesKey, RsaPubKey),
session_token = SessionToken session_token = SessionToken
}), }),
%% %%
{ok, _} = quicer:send(Stream, <<PacketId:32, ?PACKET_REGISTER_SUPER_ACK, RegisterSuperAck/binary>>), {ok, _} = quicer:send(Stream, <<?PACKET_REGISTER_SUPER_ACK, RegisterSuperAck/binary>>),
%% 线 %% 线
Result = sdlan_api:node_online(ClientId, NetworkId, sdlan_ipaddr:int_to_ipv4(Ip)), Result = sdlan_api:node_online(ClientId, NetworkId, sdlan_ipaddr:int_to_ipv4(Ip)),
logger:debug("[sdlan_register_worker] client_id: ~p, set none online result is: ~p", [ClientId, Result]), logger:debug("[sdlan_register_worker] client_id: ~p, set none online result is: ~p", [ClientId, Result]),
{next_state, registered, State#state{network_id = NetworkId, network_pid = NetworkPid, client_id = ClientId, mac = Mac, ip = Ip}}; {next_state, registered, State#state{network_id = NetworkId, network_pid = NetworkPid, client_id = ClientId, mac = Mac, ip = Ip}};
undefined -> undefined ->
logger:warning("[sdlan_register_worker] client_id: ~p, register get error: network not found", [ClientId]), logger:warning("[sdlan_register_worker] client_id: ~p, register get error: network not found", [ClientId]),
{ok, _} = quicer:send(Stream, register_nak_reply(PacketId, ?NAK_INTERNAL_FAULT, <<"Internal Error">>)), {ok, _} = quicer:send(Stream, register_nak_reply(PktId, ?NAK_INTERNAL_FAULT, <<"Internal Error">>)),
{stop, normal, State} {stop, normal, State}
end; end;
{ok, #{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}}} -> {ok, #{<<"error">> := #{<<"code">> := Code, <<"message">> := Message}}} ->
logger:warning("[sdlan_register_worker] network_id: ~p, client_id: ~p, register get error: ~ts, error_code: ~p", [NetworkId, ClientId, Message, Code]), logger:warning("[sdlan_register_worker] network_id: ~p, client_id: ~p, register get error: ~ts, error_code: ~p", [NetworkId, ClientId, Message, Code]),
{ok, _} = quicer:send(Stream, register_nak_reply(PacketId, Code, Message)), {ok, _} = quicer:send(Stream, register_nak_reply(PktId, Code, Message)),
{stop, normal, State}; {stop, normal, State};
{error, Reason} -> {error, Reason} ->
logger:warning("[sdlan_register_worker] network_id: ~p, client_id: ~p, register get error: ~p", [NetworkId, ClientId, Reason]), logger:warning("[sdlan_register_worker] network_id: ~p, client_id: ~p, register get error: ~p", [NetworkId, ClientId, Reason]),
{ok, _} = quicer:send(Stream, register_nak_reply(PacketId, ?NAK_NETWORK_FAULT, <<"Network Error">>)), {ok, _} = quicer:send(Stream, register_nak_reply(PktId, ?NAK_NETWORK_FAULT, <<"Network Error">>)),
{stop, normal, State} {stop, normal, State}
end; end;
handle_event(info, {frame, <<PacketId:32, ?PACKET_QUERY_INFO, Body/binary>>}, registered, #state{stream = Stream, network_pid = NetworkPid, mac = SrcMac}) when is_pid(NetworkPid) -> handle_event(info, {frame, <<?PACKET_QUERY_INFO, Body/binary>>}, registered, #state{stream = Stream, network_pid = NetworkPid, mac = SrcMac}) when is_pid(NetworkPid) ->
#sdl_query_info{dst_mac = DstMac} = sdlan_pb:decode_msg(Body, sdl_query_info), #sdl_query_info{pkt_id = PktId, dst_mac = DstMac} = sdlan_pb:decode_msg(Body, sdl_query_info),
case sdlan_network:peer_info(NetworkPid, SrcMac, DstMac) of case sdlan_network:peer_info(NetworkPid, SrcMac, DstMac) of
error -> error ->
logger:debug("[sdlan_channel] query_info src_mac is: ~p, dst_mac: ~p, nat_peer not found", logger:debug("[sdlan_channel] query_info src_mac is: ~p, dst_mac: ~p, nat_peer not found",
[sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac)]), [sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac)]),
EmptyResponse = sdlan_pb:encode_msg(#sdl_empty{}), EmptyResponse = sdlan_pb:encode_msg(#sdl_empty{
{ok, _} = quicer:send(Stream, <<PacketId:32, ?PACKET_PEER_INFO, EmptyResponse/binary>>), pkt_id = PktId
}),
{ok, _} = quicer:send(Stream, <<?PACKET_PEER_INFO, EmptyResponse/binary>>),
keep_state_and_data; keep_state_and_data;
{ok, {NatPeer = {{Ip0, Ip1, Ip2, Ip3}, NatPort}, NatType}, V6Info} -> {ok, {NatPeer = {{Ip0, Ip1, Ip2, Ip3}, NatPort}, NatType}, V6Info} ->
logger:debug("[sdlan_channel] query_info src_mac is: ~p, dst_mac: ~p, nat_peer: ~p", logger:debug("[sdlan_channel] query_info src_mac is: ~p, dst_mac: ~p, nat_peer: ~p",
[sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac), NatPeer]), [sdlan_util:format_mac(SrcMac), sdlan_util:format_mac(DstMac), NatPeer]),
PeerInfo = sdlan_pb:encode_msg(#sdl_peer_info{ PeerInfo = sdlan_pb:encode_msg(#sdl_peer_info{
pkt_id = PktId,
dst_mac = DstMac, dst_mac = DstMac,
v4_info = #sdl_v4_info { v4_info = #sdl_v4_info {
port = NatPort, port = NatPort,
@ -193,12 +197,12 @@ handle_event(info, {frame, <<PacketId:32, ?PACKET_QUERY_INFO, Body/binary>>}, re
}, },
v6_info = V6Info v6_info = V6Info
}), }),
{ok, _} = quicer:send(Stream, <<PacketId:32, ?PACKET_PEER_INFO, PeerInfo/binary>>), {ok, _} = quicer:send(Stream, <<?PACKET_PEER_INFO, PeerInfo/binary>>),
keep_state_and_data keep_state_and_data
end; end;
handle_event(info, {frame, <<0:32, ?PACKET_PING>>}, registered, State = #state{stream = Stream, ping_counter = PingCounter}) -> handle_event(info, {frame, <<?PACKET_PING>>}, registered, State = #state{stream = Stream, ping_counter = PingCounter}) ->
{ok, _} = quicer:send(Stream, <<0:32, ?PACKET_PONG>>), {ok, _} = quicer:send(Stream, <<?PACKET_PONG>>),
{keep_state, State#state{ping_counter = PingCounter + 1}}; {keep_state, State#state{ping_counter = PingCounter + 1}};
handle_event(info, {timeout, _, ping_ticker}, _, State = #state{client_id = ClientId, ping_counter = PingCounter}) -> handle_event(info, {timeout, _, ping_ticker}, _, State = #state{client_id = ClientId, ping_counter = PingCounter}) ->
@ -215,11 +219,11 @@ handle_event(info, {timeout, _, ping_ticker}, _, State = #state{client_id = Clie
%% %%
handle_event(info, {send_event, EventType, Event}, registered, #state{stream = Stream, client_id = ClientId}) -> handle_event(info, {send_event, EventType, Event}, registered, #state{stream = Stream, client_id = ClientId}) ->
logger:debug("[sdlan_channel] client_id: ~p, will send eventType: ~p, event: ~p", [ClientId, EventType, Event]), logger:debug("[sdlan_channel] client_id: ~p, will send eventType: ~p, event: ~p", [ClientId, EventType, Event]),
{ok, _} = quicer:send(Stream, <<0:32, ?PACKET_EVENT, EventType, Event/binary>>), {ok, _} = quicer:send(Stream, <<?PACKET_EVENT, EventType, Event/binary>>),
keep_state_and_data; keep_state_and_data;
%% %%
handle_event(info, {frame, <<0:32, ?PACKET_UNREGISTER>>}, registered, State=#state{client_id = ClientId, mac = Mac, network_pid = NetworkPid}) when is_pid(NetworkPid) -> handle_event(info, {frame, <<?PACKET_UNREGISTER>>}, registered, State=#state{client_id = ClientId, mac = Mac, network_pid = NetworkPid}) when is_pid(NetworkPid) ->
logger:warning("[sdlan_channel] unregister client_id: ~p", [ClientId]), logger:warning("[sdlan_channel] unregister client_id: ~p", [ClientId]),
sdlan_network:unregister(NetworkPid, ClientId, Mac), sdlan_network:unregister(NetworkPid, ClientId, Mac),
{stop, normal, State}; {stop, normal, State};
@ -280,10 +284,11 @@ decode_frames0(Rest, _MaxPacketSize, Frames) ->
-spec register_nak_reply(PacketId :: integer(), ErrorCode :: integer(), ErrorMsg :: binary()) -> binary(). -spec register_nak_reply(PacketId :: integer(), ErrorCode :: integer(), ErrorMsg :: binary()) -> binary().
register_nak_reply(PacketId, ErrorCode, ErrorMsg) when is_integer(PacketId), is_integer(ErrorCode), is_binary(ErrorMsg) -> register_nak_reply(PacketId, ErrorCode, ErrorMsg) when is_integer(PacketId), is_integer(ErrorCode), is_binary(ErrorMsg) ->
RegisterNakReply = sdlan_pb:encode_msg(#sdl_register_super_nak { RegisterNakReply = sdlan_pb:encode_msg(#sdl_register_super_nak {
pkt_id = PacketId,
error_code = ErrorCode, error_code = ErrorCode,
error_message = ErrorMsg error_message = ErrorMsg
}), }),
<<PacketId:32, ?PACKET_REGISTER_SUPER_NAK, RegisterNakReply/binary>>. <<?PACKET_REGISTER_SUPER_NAK, RegisterNakReply/binary>>.
rsa_encode(PlainText, RsaPubKey) when is_binary(PlainText) -> rsa_encode(PlainText, RsaPubKey) when is_binary(PlainText) ->
iolist_to_binary(sdlan_cipher:rsa_encrypt(PlainText, RsaPubKey)). iolist_to_binary(sdlan_cipher:rsa_encrypt(PlainText, RsaPubKey)).

File diff suppressed because it is too large Load Diff

View File

@ -29,12 +29,12 @@ message SDLWelcome {
// quic // quic
message SDLEmpty { message SDLEmpty {
uint32 pktId = 1; uint32 pkt_id = 1;
} }
// , // ,
message SDLRegisterSuper { message SDLRegisterSuper {
uint32 pktId = 1; uint32 pkt_id = 1;
string client_id = 2; string client_id = 2;
// https请求分配了 // https请求分配了
// (network_id, mac, ip, mask_len, hostname) // (network_id, mac, ip, mask_len, hostname)
@ -53,13 +53,13 @@ message SDLRegisterSuper {
// https的接口里面去完成 // https的接口里面去完成
// quic去通讯session_token校验 // quic去通讯session_token校验
message SDLRegisterSuperAck { message SDLRegisterSuperAck {
uint32 pktId = 1; uint32 pkt_id = 1;
bytes aes_key = 2; bytes aes_key = 2;
bytes session_token = 3; bytes session_token = 3;
} }
message SDLRegisterSuperNak { message SDLRegisterSuperNak {
uint32 pktId = 1; uint32 pkt_id = 1;
uint32 error_code = 2; uint32 error_code = 2;
string error_message = 3; string error_message = 3;
} }
@ -67,12 +67,12 @@ message SDLRegisterSuperNak {
// //
message SDLQueryInfo { message SDLQueryInfo {
uint32 pktId = 1; uint32 pkt_id = 1;
bytes dst_mac = 2; bytes dst_mac = 2;
} }
message SDLPeerInfo { message SDLPeerInfo {
uint32 pktId = 1; uint32 pkt_id = 1;
bytes dst_mac = 2; bytes dst_mac = 2;
SDLV4Info v4_info = 3; SDLV4Info v4_info = 3;
optional SDLV6Info v6_info = 4; optional SDLV6Info v6_info = 4;
@ -82,19 +82,19 @@ message SDLPeerInfo {
// arp请求是通过广播的形式获取到的macos这种tun的实现arp请求包的 // arp请求是通过广播的形式获取到的macos这种tun的实现arp请求包的
// mac对应的ip地址的广 // mac对应的ip地址的广
message SDLArpRequest { message SDLArpRequest {
uint32 pktId = 1; uint32 pkt_id = 1;
uint32 target_ip = 2; uint32 target_ip = 2;
} }
message SDLArpResponse { message SDLArpResponse {
uint32 pktId = 1; uint32 pkt_id = 1;
uint32 target_ip = 2; uint32 target_ip = 2;
bytes target_mac = 3; bytes target_mac = 3;
} }
// //
message SDLPolicyRequest { message SDLPolicyRequest {
uint32 pktId = 1; uint32 pkt_id = 1;
uint32 src_identity_id = 2; uint32 src_identity_id = 2;
uint32 dst_identity_id = 3; uint32 dst_identity_id = 3;
uint32 version = 4; uint32 version = 4;
@ -102,7 +102,7 @@ message SDLPolicyRequest {
// quic通讯rules部分已经没有了长度限制 // quic通讯rules部分已经没有了长度限制
message SDLPolicyResponse { message SDLPolicyResponse {
uint32 pktId = 1; uint32 pkt_id = 1;
uint32 src_identity_id = 2; uint32 src_identity_id = 2;
uint32 dst_identity_id = 3; uint32 dst_identity_id = 3;
// ; // ;