This commit is contained in:
2026-05-31 11:32:33 +09:00
parent 547cbc319d
commit 57c5253513
2 changed files with 98 additions and 94 deletions
+46 -42
View File
@@ -85,35 +85,39 @@ disconnected(State) ->
end. end.
do_connect(State = #s{host = Host = {Addr, Port}, id = ID}, Ref, From) -> do_connect(State = #s{host = Host = {Addr, Port}}, Ref, From) ->
Options = [{mode, binary}, {active, once}, {packet, 4}, {keepalive, true}], Options = [{mode, binary}, {active, once}, {packet, 4}, {keepalive, true}],
{TempID, TempKey = #{public := TPK}} = hz_key_master:make_key(),
ok = tell(info, "Using TempID: ~p", [TempID]),
case gen_tcp:connect(Addr, Port, Options, 5000) of case gen_tcp:connect(Addr, Port, Options, 5000) of
{ok, Socket} -> {ok, Socket} ->
ok = tell(info, "Socket: ~p", [Socket]), ok = tell(info, "Socket: ~p", [Socket]),
NextState = State#s{socket = Socket}, NextState = State#s{socket = Socket},
ok = send(Socket, <<"GajuExpress 1 RECVR:", ID/binary>>), ok = send(Socket, <<"GajuExpress:001:RECVR:", TPK/binary>>),
handshake(NextState, Ref, From); handshake(NextState, Ref, From, TempKey);
Error -> Error ->
ok = tell(warning, "Failed to connect to ~p with ~p", [Host, Error]), ok = tell(warning, "Failed to connect to ~p with ~p", [Host, Error]),
retire(State, normal, "Connect failed") retire(State, normal, "Connect failed")
end. end.
handshake(State = #s{socket = Socket}, Ref, From) -> handshake(State = #s{socket = Socket}, Ref, From, TempKey) ->
ok = active_once(State), ok = active_once(State),
receive receive
{tcp, Socket, <<"GajuExpress 1 RECVR:", Challenge/binary>>} -> {tcp, Socket, <<"GajuExpress:001:RECVR:", PPK:32/binary, EPK:32/binary>>} ->
tell(info, "Got challenge ~p", [Challenge]), PermanentID = gmser_api_encoder:encode(account_pubkey, PPK),
case is_sus(Challenge) of EphemeralID = gmser_api_encoder:encode(account_pubkey, EPK),
false -> tell(info, "Got keys ~s, ~s", [PermanentID, EphemeralID]),
tell(info, "Not sus"), % case is_sus(Challenge) of
From ! {Ref, {ok, Challenge}}, % false ->
authenticate(State); % tell(info, "Not sus"),
true -> % From ! {Ref, {ok, Challenge}},
tell(info, "Sus"), % authenticate(State);
From ! {Ref, {error, "Challenge was sus."}}, % true ->
retire(State, normal) % tell(info, "Sus"),
end; % From ! {Ref, {error, "Challenge was sus."}},
% retire(State, normal)
% end;
{tcp_closed, Socket} -> {tcp_closed, Socket} ->
From ! {Ref, {error, tcp_closed}}, From ! {Ref, {error, tcp_closed}},
retire(State, normal, "Handshake died") retire(State, normal, "Handshake died")
@@ -122,32 +126,32 @@ handshake(State = #s{socket = Socket}, Ref, From) ->
retire(State, normal, "Handshake timed out") retire(State, normal, "Handshake timed out")
end. end.
is_sus(Challenge) -> %is_sus(Challenge) ->
case string:split(Challenge, "_", all) of % case string:split(Challenge, "_", all) of
[<<"GajuExpress-Challenge">>, <<"TS-", TS/binary>>, Rand] -> is_sus2(TS, Rand); % [<<"GajuExpress-Challenge">>, <<"TS-", TS/binary>>, Rand] -> is_sus2(TS, Rand);
_ -> true % _ -> true
end. % end.
%
is_sus2(TS, Rand) -> %is_sus2(TS, Rand) ->
case decode_challenge(TS, Rand) of % case decode_challenge(TS, Rand) of
{ok, Seconds} -> is_sus3(Seconds); % {ok, Seconds} -> is_sus3(Seconds);
error -> true % error -> true
end. % end.
%
is_sus3(Seconds) -> %is_sus3(Seconds) ->
Now = erlang:system_time(seconds), % Now = erlang:system_time(seconds),
FiveMins = 5 * 60, % FiveMins = 5 * 60,
abs(Seconds - Now) > FiveMins. % abs(Seconds - Now) > FiveMins.
%
decode_challenge(TS, Rand) -> %decode_challenge(TS, Rand) ->
try % try
Seconds = binary_to_integer(TS), % Seconds = binary_to_integer(TS),
true = is_binary(base64:decode(Rand)), % true = is_binary(base64:decode(Rand)),
{ok, Seconds} % {ok, Seconds}
catch % catch
error:_ -> % error:_ ->
error % error
end. % end.
authenticate(State = #s{socket = Socket}) -> authenticate(State = #s{socket = Socket}) ->
+52 -52
View File
@@ -470,76 +470,76 @@ should_enable_quote(#s{dest = DestT, path = PathP}) ->
length(DestKey) > 0 andalso length(Path) > 0. length(DestKey) > 0 andalso length(Path) > 0.
do_ul(State = #s{quote = none, rider = none}) -> %do_ul(State = #s{quote = none, rider = none}) ->
check_quote(State); % check_quote(State);
do_ul(State) -> do_ul(State) ->
ok = tell(info, "Would do_ul."), ok = tell(info, "Would do_ul."),
State. State.
check_quote(State = #s{dest = DestT}) -> %check_quote(State = #s{dest = DestT}) ->
Dest = wxTextCtrl:getValue(DestT), % Dest = wxTextCtrl:getValue(DestT),
case gmser_api_encoder:safe_decode(account_pubkey, list_to_binary(Dest)) of % case gmser_api_encoder:safe_decode(account_pubkey, list_to_binary(Dest)) of
{ok, PubKey} -> % {ok, PubKey} ->
check_quote2(State, PubKey); % check_quote2(State, PubKey);
{error, Reason} -> % {error, Reason} ->
tell(warning, "Destination Key decode failed with: ~p", [Reason]), % tell(warning, "Destination Key decode failed with: ~p", [Reason]),
State % State
end. % end.
check_quote2(State = #s{path = PathP}, PubKey) -> %check_quote2(State = #s{path = PathP}, PubKey) ->
Path = wxFilePickerCtrl:getPath(PathP), % Path = wxFilePickerCtrl:getPath(PathP),
case filelib:is_file(Path) of % case filelib:is_file(Path) of
true -> % true ->
check_quote3(State, PubKey, Path); % check_quote3(State, PubKey, Path);
false -> % false ->
tell(info, "File path isn't a file"), % tell(info, "File path isn't a file"),
State % State
end. % end.
check_quote3(State = #s{ttl = TTL_T}, PubKey, Path) -> %check_quote3(State = #s{ttl = TTL_T}, PubKey, Path) ->
TTL_S = wxTextCtrl:getValue(TTL_T), % TTL_S = wxTextCtrl:getValue(TTL_T),
case string_to_int(TTL_S) of % case string_to_int(TTL_S) of
{ok, TTL} -> % {ok, TTL} ->
check_quote4(State, PubKey, Path, TTL); % check_quote4(State, PubKey, Path, TTL);
error -> % error ->
tell(info, "TTL isn't an integer"), % tell(info, "TTL isn't an integer"),
State % State
end. % end.
check_quote4(State = #s{sign = SigC}, PubKey, Path, TTL) -> %check_quote4(State = #s{sign = SigC}, PubKey, Path, TTL) ->
SigYN = wxCheckBox:is_checked(SigC), % SigYN = wxCheckBox:is_checked(SigC),
check_quote5(State, PubKey, Path, TTL, SigYN). % check_quote5(State, PubKey, Path, TTL, SigYN).
check_quote5(State, PubKey, Path, TTL, SigYN) -> %check_quote5(State, PubKey, Path, TTL, SigYN) ->
Tar = tar_path(), % Tar = tar_path(),
case erl_tar:create(TarPath, Path, [compressed, dereference]) of % case erl_tar:create(TarPath, Path, [compressed, dereference]) of
ok -> % ok ->
check_quote6(State, PubKey, TTL, SigYN, Tar); % check_quote6(State, PubKey, TTL, SigYN, Tar);
{error, Reason} -> % {error, Reason} ->
tell(warning, "Tar operation failed with: ~p", [Reason]), % tell(warning, "Tar operation failed with: ~p", [Reason]),
State % State
end. % end.
check_quote6(State, PubKey, TTL, SigYN, Tar) -> %check_quote6(State, PubKey, TTL, SigYN, Tar) ->
tar_path() -> %tar_path() ->
TarFile = integer_to_list(erlang:system_time(seconds)) ++ "tar.gz", % TarFile = integer_to_list(erlang:system_time(seconds)) ++ "tar.gz",
filename:join(zx_lib:path(tmp, "otpr", "gajudesk"), TarFile). % filename:join(zx_lib:path(tmp, "otpr", "gajudesk"), TarFile).
check_quote6(State, PubKey, Path, TTL, SigYN, Tar) -> %check_quote6(State, PubKey, Path, TTL, SigYN, Tar) ->
% PID = spawn_link(gd_n_rider, init, [PubKey, {"localhost", 7777}]), % PID = spawn_link(gd_n_rider, init, [PubKey, {"localhost", 7777}]),
string_to_int(S) -> %string_to_int(S) ->
try % try
{ok, list_to_integer(S)} % {ok, list_to_integer(S)}
catch % catch
error:bad_arg -> error % error:bad_arg -> error
end. % end.
do_close(#s{frame = Frame, prefs = Prefs}) -> do_close(#s{frame = Frame, prefs = Prefs}) ->