Packages
locus
1.6.2
2.3.15
2.3.14
2.3.13
2.3.12
2.3.11
2.3.10
2.3.9
2.3.8
2.3.7
2.3.6
2.3.5
2.3.4
2.3.3
retired
2.3.2
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.0
2.0.0
1.16.1
1.16.0
1.15.0
1.14.1
1.14.0
1.13.2
1.13.1
1.13.0
1.12.2
1.12.1
1.12.0
1.11.0
1.11.0-beta
1.10.2
1.10.1
1.10.0
1.9.0
1.9.0-beta
1.8.0
1.7.0
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.2
1.0.1
1.0.0
MMDB reader for geolocation and ASN lookup of IP addresses, supporting MaxMind GeoLite2/GeoIP2 and other providers
Current section
Files
Jump to
Current section
Files
src/locus_cli.erl
%% Copyright (c) 2017-2019 Guilherme Andrade
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% locus is an independent project and has not been authorized, sponsored,
%% or otherwise approved by MaxMind.
%%
%% locus includes code extracted from OTP source code, by Ericsson AB,
%% released under the Apache License 2.0.
%% @private
-module(locus_cli).
-ifdef(ESCRIPTIZING).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([main/1]).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
-spec main([string()]) -> ok | no_return().
main(Args) ->
ensure_apps_are_started([locus, getopt]),
case Args of
["analyze" | CmdArgs] ->
handle_analysis_command(CmdArgs);
_ ->
fall_from_grace(
"~n"
"Usage: locus [<command>] [<command_args>]~n"
"~n"
"Available commands:~n"
" analyze")
end.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
ensure_apps_are_started(Apps) ->
lists:foreach(
fun (App) ->
{ok, _} = application:ensure_all_started(App)
end,
Apps).
handle_analysis_command(CmdArgs) ->
OptSpecList =
[{load_timeout, undefined, "load-timeout", {integer,30}, "Database load timeout (in seconds)"},
{log_level, undefined, "log-level", {string,"error"}, "debug | info | warning | error"},
{url, undefined, undefined, utf8_binary, "Database URL (local or remote)"}],
case getopt:parse_and_check(OptSpecList, CmdArgs) of
{ok, {ParsedArgs, []}} ->
{load_timeout,LoadTimeoutSecs} = lists:keyfind(load_timeout, 1, ParsedArgs),
{log_level,StrLogLevel} = lists:keyfind(log_level, 1, ParsedArgs),
{url,DatabaseURL} = lists:keyfind(url, 1, ParsedArgs),
LoadTimeout = timer:seconds(LoadTimeoutSecs),
LogLevel = list_to_atom(StrLogLevel),
ok = locus_logger:set_loglevel(LogLevel),
prepare_analysis(DatabaseURL, LoadTimeout);
_ ->
getopt:usage(OptSpecList, "locus analyze"),
fall_from_grace()
end.
prepare_analysis(DatabaseURL, LoadTimeout) ->
DatabaseId = cli_analysis,
WaitRef = make_ref(),
BaseOpts = [{internal, {async_waiter, {self(),WaitRef}}}],
ExtraOpts =
case http_uri:parse(DatabaseURL) of
{ok, _} -> [no_cache];
{error, _} -> []
end,
stderr_println("Loading database from \"~ts\"...", [DatabaseURL]),
case locus:start_loader(DatabaseId, DatabaseURL, BaseOpts ++ ExtraOpts) of
ok ->
wait_for_analysis_database_load(DatabaseId, WaitRef, LoadTimeout)
end.
wait_for_analysis_database_load(DatabaseId, WaitRef, LoadTimeout) ->
receive
{WaitRef, {ok, LoadedVersion}} ->
stderr_println("Database version ~p successfully loaded", [LoadedVersion]),
perform_analysis(DatabaseId);
{WaitRef, {error, Reason}} ->
fall_from_grace("Failed to load database: ~p", [Reason])
after
LoadTimeout ->
fall_from_grace("Timeout loading the database")
end.
perform_analysis(DatabaseId) ->
stderr_println("Analyzing database for flaws..."),
case locus:analyze(DatabaseId) of
ok ->
stderr_println("Database is wholesome.");
{error, {flawed, Flaws}} ->
fall_from_grace("Database is corrupt or incompatible:~n"
++ lists:flatten(["* ~p~n" || _ <- Flaws]),
Flaws)
end.
fall_from_grace() ->
fall_from_grace("", []).
fall_from_grace(MsgFmt) ->
fall_from_grace(MsgFmt, []).
fall_from_grace(MsgFmt, MsgArgs) ->
_ = MsgFmt =/= "" andalso stderr_println("[ERROR] " ++ MsgFmt, MsgArgs),
erlang:halt(1, [{flush,true}]).
stderr_println(Fmt) ->
stderr_println(Fmt, []).
stderr_println(Fmt, Args) ->
io:format(standard_error, Fmt ++ "~n", Args).
-endif.