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.
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}],
{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
{ok, Socket} ->
ok = tell(info, "Socket: ~p", [Socket]),
NextState = State#s{socket = Socket},
ok = send(Socket, <<"GajuExpress 1 RECVR:", ID/binary>>),
handshake(NextState, Ref, From);
ok = send(Socket, <<"GajuExpress:001:RECVR:", TPK/binary>>),
handshake(NextState, Ref, From, TempKey);
Error ->
ok = tell(warning, "Failed to connect to ~p with ~p", [Host, Error]),
retire(State, normal, "Connect failed")
end.
handshake(State = #s{socket = Socket}, Ref, From) ->
handshake(State = #s{socket = Socket}, Ref, From, TempKey) ->
ok = active_once(State),
receive
{tcp, Socket, <<"GajuExpress 1 RECVR:", Challenge/binary>>} ->
tell(info, "Got challenge ~p", [Challenge]),
case is_sus(Challenge) of
false ->
tell(info, "Not sus"),
From ! {Ref, {ok, Challenge}},
authenticate(State);
true ->
tell(info, "Sus"),
From ! {Ref, {error, "Challenge was sus."}},
retire(State, normal)
end;
{tcp, Socket, <<"GajuExpress:001:RECVR:", PPK:32/binary, EPK:32/binary>>} ->
PermanentID = gmser_api_encoder:encode(account_pubkey, PPK),
EphemeralID = gmser_api_encoder:encode(account_pubkey, EPK),
tell(info, "Got keys ~s, ~s", [PermanentID, EphemeralID]),
% case is_sus(Challenge) of
% false ->
% tell(info, "Not sus"),
% From ! {Ref, {ok, Challenge}},
% authenticate(State);
% true ->
% tell(info, "Sus"),
% From ! {Ref, {error, "Challenge was sus."}},
% retire(State, normal)
% end;
{tcp_closed, Socket} ->
From ! {Ref, {error, tcp_closed}},
retire(State, normal, "Handshake died")
@@ -122,32 +126,32 @@ handshake(State = #s{socket = Socket}, Ref, From) ->
retire(State, normal, "Handshake timed out")
end.
is_sus(Challenge) ->
case string:split(Challenge, "_", all) of
[<<"GajuExpress-Challenge">>, <<"TS-", TS/binary>>, Rand] -> is_sus2(TS, Rand);
_ -> true
end.
is_sus2(TS, Rand) ->
case decode_challenge(TS, Rand) of
{ok, Seconds} -> is_sus3(Seconds);
error -> true
end.
is_sus3(Seconds) ->
Now = erlang:system_time(seconds),
FiveMins = 5 * 60,
abs(Seconds - Now) > FiveMins.
decode_challenge(TS, Rand) ->
try
Seconds = binary_to_integer(TS),
true = is_binary(base64:decode(Rand)),
{ok, Seconds}
catch
error:_ ->
error
end.
%is_sus(Challenge) ->
% case string:split(Challenge, "_", all) of
% [<<"GajuExpress-Challenge">>, <<"TS-", TS/binary>>, Rand] -> is_sus2(TS, Rand);
% _ -> true
% end.
%
%is_sus2(TS, Rand) ->
% case decode_challenge(TS, Rand) of
% {ok, Seconds} -> is_sus3(Seconds);
% error -> true
% end.
%
%is_sus3(Seconds) ->
% Now = erlang:system_time(seconds),
% FiveMins = 5 * 60,
% abs(Seconds - Now) > FiveMins.
%
%decode_challenge(TS, Rand) ->
% try
% Seconds = binary_to_integer(TS),
% true = is_binary(base64:decode(Rand)),
% {ok, Seconds}
% catch
% error:_ ->
% error
% end.
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.
do_ul(State = #s{quote = none, rider = none}) ->
check_quote(State);
%do_ul(State = #s{quote = none, rider = none}) ->
% check_quote(State);
do_ul(State) ->
ok = tell(info, "Would do_ul."),
State.
check_quote(State = #s{dest = DestT}) ->
Dest = wxTextCtrl:getValue(DestT),
case gmser_api_encoder:safe_decode(account_pubkey, list_to_binary(Dest)) of
{ok, PubKey} ->
check_quote2(State, PubKey);
{error, Reason} ->
tell(warning, "Destination Key decode failed with: ~p", [Reason]),
State
end.
%check_quote(State = #s{dest = DestT}) ->
% Dest = wxTextCtrl:getValue(DestT),
% case gmser_api_encoder:safe_decode(account_pubkey, list_to_binary(Dest)) of
% {ok, PubKey} ->
% check_quote2(State, PubKey);
% {error, Reason} ->
% tell(warning, "Destination Key decode failed with: ~p", [Reason]),
% State
% end.
check_quote2(State = #s{path = PathP}, PubKey) ->
Path = wxFilePickerCtrl:getPath(PathP),
case filelib:is_file(Path) of
true ->
check_quote3(State, PubKey, Path);
false ->
tell(info, "File path isn't a file"),
State
end.
%check_quote2(State = #s{path = PathP}, PubKey) ->
% Path = wxFilePickerCtrl:getPath(PathP),
% case filelib:is_file(Path) of
% true ->
% check_quote3(State, PubKey, Path);
% false ->
% tell(info, "File path isn't a file"),
% State
% end.
check_quote3(State = #s{ttl = TTL_T}, PubKey, Path) ->
TTL_S = wxTextCtrl:getValue(TTL_T),
case string_to_int(TTL_S) of
{ok, TTL} ->
check_quote4(State, PubKey, Path, TTL);
error ->
tell(info, "TTL isn't an integer"),
State
end.
%check_quote3(State = #s{ttl = TTL_T}, PubKey, Path) ->
% TTL_S = wxTextCtrl:getValue(TTL_T),
% case string_to_int(TTL_S) of
% {ok, TTL} ->
% check_quote4(State, PubKey, Path, TTL);
% error ->
% tell(info, "TTL isn't an integer"),
% State
% end.
check_quote4(State = #s{sign = SigC}, PubKey, Path, TTL) ->
SigYN = wxCheckBox:is_checked(SigC),
check_quote5(State, PubKey, Path, TTL, SigYN).
%check_quote4(State = #s{sign = SigC}, PubKey, Path, TTL) ->
% SigYN = wxCheckBox:is_checked(SigC),
% check_quote5(State, PubKey, Path, TTL, SigYN).
check_quote5(State, PubKey, Path, TTL, SigYN) ->
Tar = tar_path(),
case erl_tar:create(TarPath, Path, [compressed, dereference]) of
ok ->
check_quote6(State, PubKey, TTL, SigYN, Tar);
{error, Reason} ->
tell(warning, "Tar operation failed with: ~p", [Reason]),
State
end.
%check_quote5(State, PubKey, Path, TTL, SigYN) ->
% Tar = tar_path(),
% case erl_tar:create(TarPath, Path, [compressed, dereference]) of
% ok ->
% check_quote6(State, PubKey, TTL, SigYN, Tar);
% {error, Reason} ->
% tell(warning, "Tar operation failed with: ~p", [Reason]),
% State
% end.
check_quote6(State, PubKey, TTL, SigYN, Tar) ->
%check_quote6(State, PubKey, TTL, SigYN, Tar) ->
tar_path() ->
TarFile = integer_to_list(erlang:system_time(seconds)) ++ "tar.gz",
filename:join(zx_lib:path(tmp, "otpr", "gajudesk"), TarFile).
%tar_path() ->
% TarFile = integer_to_list(erlang:system_time(seconds)) ++ "tar.gz",
% 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}]),
string_to_int(S) ->
try
{ok, list_to_integer(S)}
catch
error:bad_arg -> error
end.
%string_to_int(S) ->
% try
% {ok, list_to_integer(S)}
% catch
% error:bad_arg -> error
% end.
do_close(#s{frame = Frame, prefs = Prefs}) ->