Merge branch 'master' of gitlab.com:zxq9/zx

This commit is contained in:
Craig Everett 2020-07-01 02:55:23 +09:00
commit 13e06cf2c9
49 changed files with 86 additions and 131 deletions

View File

@ -1 +1 @@
0.10.7 0.10.9

View File

@ -1,6 +1,6 @@
{application,zx, {application,zx,
[{description,"An Erlang development tool and Zomp user client"}, [{description,"An Erlang development tool and Zomp user client"},
{vsn,"0.10.7"}, {vsn,"0.10.9"},
{applications,[stdlib,kernel]}, {applications,[stdlib,kernel]},
{modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key, {modules,[zx,zx_auth,zx_conn,zx_conn_sup,zx_daemon,zx_key,
zx_lib,zx_local,zx_net,zx_peer,zx_peer_man, zx_lib,zx_local,zx_net,zx_peer,zx_peer_man,

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

@ -24,7 +24,7 @@
%%% @end %%% @end
-module(zx). -module(zx).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(application). -behavior(application).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -9,7 +9,7 @@
%%% @end %%% @end
-module(zx_auth). -module(zx_auth).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end %%% @end
-module(zx_conn). -module(zx_conn).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_conn_sup). -module(zx_conn_sup).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -138,7 +138,7 @@
%%% @end %%% @end
-module(zx_daemon). -module(zx_daemon).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(gen_server). -behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
@ -1406,6 +1406,14 @@ eval_queue(Actions, State) ->
local_dispatch([], State) -> local_dispatch([], State) ->
State; State;
local_dispatch([{request, Pid, ID, {fetch, R, N, V}} | Rest], State) ->
Result =
case zomp_realm_man:lookup(R) of
{ok, RealmPID} -> local_fetch(RealmPID, {R, N, V});
Error -> Error
end,
Pid ! {result, ID, Result},
local_dispatch(Rest, State);
local_dispatch([{request, Pid, ID, Message} | Rest], State) -> local_dispatch([{request, Pid, ID, Message} | Rest], State) ->
Realm = element(2, Message), Realm = element(2, Message),
Result = Result =
@ -1452,6 +1460,46 @@ local_request(R, {list_sysops}) -> zomp_realm:list_sysops(R);
local_request(R, {list_type, T}) -> zomp_realm:list_type(R, T). local_request(R, {list_type, T}) -> zomp_realm:list_type(R, T).
local_fetch(RealmPID, PackageID = {_, N, V}) ->
{ok, PackageString} = zx_lib:package_string(PackageID),
ok = tell("Fetching ~s", [PackageString]),
case zomp_realm:fetch(RealmPID, {N, V}) of
{ok, Bin} -> do_import_package(Bin);
upstream -> local_fetch_upstream(PackageID, 0);
Error -> Error
end.
local_fetch_upstream(PackageID, Tries) ->
Realm = element(1, PackageID),
case zomp_node_man:lookup(Realm) of
{ok, NodePID} ->
ok = tell("Found node connector at ~p", [NodePID]),
ok = zomp_node:fetch(NodePID, PackageID),
wait_hops(PackageID);
wait ->
wait_upstream_node(PackageID, Tries);
error ->
{error, bad_realm}
end.
wait_hops(PackageID) ->
receive
{ok, PackageID, Bin} ->
do_import_package(Bin);
{hops, PackageID, Distance} ->
ok = tell("Fetch in progress. Hops: ~w", [Distance]),
wait_hops(PackageID)
after 60000 ->
{error, timeout}
end.
wait_upstream_node(PackageID, Tries) when Tries < 10 ->
_ = erlang:send_after(1000, self(), retry),
receive retry -> local_fetch_upstream(PackageID, Tries + 1) end;
wait_upstream_node(_, _) ->
{error, timeout}.
remote_dispatch([], State) -> remote_dispatch([], State) ->
State; State;
remote_dispatch([Action = {request, Pid, ID, Message} | Rest], remote_dispatch([Action = {request, Pid, ID, Message} | Rest],
@ -1628,7 +1676,8 @@ do_fetch(PackageID, Requestor, State = #s{id = ID}) ->
Action = {fetch, Realm, Name, Version}, Action = {fetch, Realm, Name, Version},
do_request(Requestor, Action, State); do_request(Requestor, Action, State);
Error -> Error ->
Requestor ! {result, ID, Error} Requestor ! {result, ID, Error},
State
end. end.
do_fetch2(Bin, Requestor, ID) -> do_fetch2(Bin, Requestor, ID) ->

View File

@ -8,7 +8,7 @@
%%% @end %%% @end
-module(zx_key). -module(zx_key).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").
@ -20,6 +20,7 @@
sign/2, verify/3]). sign/2, verify/3]).
-include("zx_logger.hrl"). -include("zx_logger.hrl").
-include_lib("public_key/include/OTP-PUB-KEY.hrl").
-spec generate_rsa(Owner) -> Result -spec generate_rsa(Owner) -> Result
@ -32,125 +33,30 @@
%% NOTE: The current version of this command is likely to only work on a unix system. %% NOTE: The current version of this command is likely to only work on a unix system.
generate_rsa(Owner) -> generate_rsa(Owner) ->
{ok, TmpDir} = zx_lib:mktemp_dir({"otpr", "zx"}), ok = tell("Generating keys. This can take several minutes. Please be patient..."),
PemFile = filename:join(TmpDir, "zx-tmp.pem"), Key =
PubFile = filename:join(TmpDir, "zx-tmp.pub.der"), #'RSAPrivateKey'{modulus = Mod,
KeyFile = filename:join(TmpDir, "zx-tmp.key.der"), publicExponent = PE} =
ok = tell("Generating keys. Please be patient..."), public_key:generate_key({rsa, 16384, 65537}),
case gen_p_key(KeyFile) of Pub =
ok -> #'RSAPublicKey'{modulus = Mod,
ok = der_to_pem(KeyFile, PemFile), publicExponent = PE},
{ok, PemBin} = file:read_file(PemFile),
[PemData] = public_key:pem_decode(PemBin),
Pub = public_key:pem_entry_decode(PemData),
PubDer = public_key:der_encode('RSAPublicKey', Pub),
ok = file:write_file(PubFile, PubDer),
generate_rsa2(Owner, PemFile, KeyFile, PubFile);
{error, no_ssl} ->
ok = tell(error, "OpenSSL not found."),
{error, keygen_fail}
end.
generate_rsa2(Owner, PemFile, KeyFile, PubFile) ->
{ok, PubBin} = file:read_file(PubFile),
{ok, KeyBin} = file:read_file(KeyFile),
Pub = public_key:der_decode('RSAPublicKey', PubBin),
Key = public_key:der_decode('RSAPrivateKey', KeyBin),
TestMessage = <<"Some test data to sign.">>, TestMessage = <<"Some test data to sign.">>,
Signature = public_key:sign(TestMessage, sha512, Key), Signature = public_key:sign(TestMessage, sha512, Key),
case public_key:verify(TestMessage, sha512, Signature, Pub) of case public_key:verify(TestMessage, sha512, Signature, Pub) of
true -> true ->
PubHash = crypto:hash(sha512, PubBin), KeyDER = public_key:der_encode('RSAPrivateKey', Key),
KeyData = {PubHash, {none, PubBin}, {none, KeyBin}}, PubDER = public_key:der_encode('RSAPublicKey', Pub),
PubHash = crypto:hash(sha512, PubDER),
KeyData = {PubHash, {none, PubDER}, {none, KeyDER}},
ok = zx_daemon:register_key(Owner, KeyData), ok = zx_daemon:register_key(Owner, KeyData),
ok = zx_lib:rm_rf(filename:dirname(KeyFile)),
{ok, PubHash}; {ok, PubHash};
false -> false ->
ok = lists:foreach(fun file:delete/1, [PemFile, KeyFile, PubFile]),
ok = tell(error, "Something has gone wrong."), ok = tell(error, "Something has gone wrong."),
{error, keygen_fail} {error, keygen_fail}
end. end.
-spec gen_p_key(KeyFile) -> Result
when KeyFile :: file:filename(),
Result :: ok
| {error, no_ssl}.
%% @private
%% Format an openssl shell command that will generate proper 16k RSA keys.
gen_p_key(KeyFile) ->
case openssl() of
{ok, OpenSSL} ->
Command =
io_lib:format("~ts genpkey"
" -algorithm rsa"
" -out ~ts"
" -outform DER"
" -pkeyopt rsa_keygen_bits:16384",
[OpenSSL, KeyFile]),
Out = os:cmd(Command),
io:format(Out);
Error ->
Error
end.
-spec der_to_pem(KeyFile, PemFile) -> ok
when KeyFile :: file:filename(),
PemFile :: file:filename().
%% @private
%% Format an openssl shell command that will convert the given keyfile to a pemfile.
%% The reason for this conversion is to sidestep some formatting weirdness that OpenSSL
%% injects into its generated DER formatted key output (namely, a few empty headers)
%% which Erlang's ASN.1 defintion files do not take into account. A conversion to PEM
%% then a conversion back to DER (via Erlang's ASN.1 module) resolves this in a reliable
%% way.
der_to_pem(KeyFile, PemFile) ->
case openssl() of
{ok, OpenSSL} ->
Command =
io_lib:format("~ts rsa"
" -inform DER"
" -in ~ts"
" -outform PEM"
" -pubout"
" -out ~ts",
[OpenSSL, KeyFile, PemFile]),
Out = os:cmd(Command),
io:format(Out);
Error ->
Error
end.
-spec openssl() -> Result
when Result :: {ok, Executable}
| {error, no_ssl},
Executable :: file:filename().
%% @private
%% Attempt to locate the installed openssl executable for use in shell commands.
%% TODO: Determine whether it is even worth it to perform this check VS restricting
%% os:cmd/1 directed zx_key functions by platform.
openssl() ->
OpenSSL =
case os:type() of
{unix, _} -> "openssl";
{win32, _} -> "openssl.exe"
end,
case os:find_executable(OpenSSL) of
false ->
M = "OpenSSL not foud in PATH. Install OpenSSL or add to path and retry.",
ok = tell(error, M),
{error, no_ssl};
Path ->
ok = tell("OpenSSL executable found at: ~ts", [Path]),
{ok, OpenSSL}
end.
-spec save(Type, KeyID, Key) -> Result -spec save(Type, KeyID, Key) -> Result
when Type :: private | public, when Type :: private | public,
KeyID :: zx:key_id(), KeyID :: zx:key_id(),

View File

@ -10,7 +10,7 @@
%%% @end %%% @end
-module(zx_lib). -module(zx_lib).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_local). -module(zx_local).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").
@ -1937,7 +1937,7 @@ store_realm(#realm_init{realm = Realm,
"3. Become the prime node: `zx takeover ~ts`~n" "3. Become the prime node: `zx takeover ~ts`~n"
"~n" "~n"
"ZX CLIENT and ZOMP DISTRIBUTION NODE configuration requires one command:~n" "ZX CLIENT and ZOMP DISTRIBUTION NODE configuration requires one command:~n"
"1. Configure the realm: `zx add realm ~ts`~n" "1. Configure the realm: `zx import realm ~ts`~n"
"~n" "~n"
"How to distribute ~ts is up to you.~n" "How to distribute ~ts is up to you.~n"
"=============================================================================~n", "=============================================================================~n",

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_net). -module(zx_net).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -8,7 +8,7 @@
%%% @end %%% @end
-module(zx_peer). -module(zx_peer).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").
@ -166,7 +166,7 @@ handle_message(<<Command:8, Bin/binary>>) ->
9 -> deferred(fun zx_daemon:search/1, Payload); 9 -> deferred(fun zx_daemon:search/1, Payload);
10 -> deferred(fun zx_daemon:list_deps/1, Payload); 10 -> deferred(fun zx_daemon:list_deps/1, Payload);
11 -> deferred(fun zx_daemon:list_sysops/1, Payload); 11 -> deferred(fun zx_daemon:list_sysops/1, Payload);
12 -> zx_daemon:fetch(Payload); 12 -> deferred(fun zx_daemon:fetch/1, Payload);
13 -> zx_daemon:keychain(Payload); 13 -> zx_daemon:keychain(Payload);
14 -> zx_daemon:install(Payload); 14 -> zx_daemon:install(Payload);
15 -> zx_daemon:build(Payload); 15 -> zx_daemon:build(Payload);

View File

@ -9,7 +9,7 @@
%%% @end %%% @end
-module(zx_peer_man). -module(zx_peer_man).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(gen_server). -behavior(gen_server).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_peer_sup). -module(zx_peer_sup).
-vsn("0.10.7"). -vsn("0.10.9").
-behaviour(supervisor). -behaviour(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -10,7 +10,7 @@
%%% @end %%% @end
-module(zx_peers). -module(zx_peers).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_proxy). -module(zx_proxy).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_sup). -module(zx_sup).
-vsn("0.10.7"). -vsn("0.10.9").
-behavior(supervisor). -behavior(supervisor).
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").

View File

@ -6,7 +6,7 @@
%%% @end %%% @end
-module(zx_tty). -module(zx_tty).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -5,7 +5,7 @@
%%% @end %%% @end
-module(zx_userconf). -module(zx_userconf).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -7,7 +7,7 @@
%%% @end %%% @end
-module(zx_zsp). -module(zx_zsp).
-vsn("0.10.7"). -vsn("0.10.9").
-author("Craig Everett <zxq9@zxq9.com>"). -author("Craig Everett <zxq9@zxq9.com>").
-copyright("Craig Everett <zxq9@zxq9.com>"). -copyright("Craig Everett <zxq9@zxq9.com>").
-license("GPL-3.0"). -license("GPL-3.0").

View File

@ -9,7 +9,7 @@
{license,"MIT"}. {license,"MIT"}.
{modules,[]}. {modules,[]}.
{name,"zx"}. {name,"zx"}.
{package_id,{"otpr","zx",{0,10,7}}}. {package_id,{"otpr","zx",{0,10,9}}}.
{prefix,"zx_"}. {prefix,"zx_"}.
{repo_url,"https://gitlab.com/zxq9/zx"}. {repo_url,"https://gitlab.com/zxq9/zx"}.
{tags,["tools","package manager","erlang"]}. {tags,["tools","package manager","erlang"]}.