diff --git a/daemon/main.cpp b/daemon/main.cpp index d3ec8de27..a7e3f4e0f 100644 --- a/daemon/main.cpp +++ b/daemon/main.cpp @@ -83,7 +83,7 @@ resolvePath(std::string conffname) /// this sets up, configures and runs the main context static void -run_main_context(std::string conffname, bool multiThreaded, bool debugMode) +run_main_context(std::string conffname, bool multiThreaded, bool backgroundMode) { // this is important, can downgrade from Info though llarp::LogDebug("Running from: ", fs::current_path().string()); @@ -97,10 +97,10 @@ run_main_context(std::string conffname, bool multiThreaded, bool debugMode) #ifndef _WIN32 signal(SIGHUP, handle_signal); #endif - code = llarp_main_setup(ctx, debugMode); + code = llarp_main_setup(ctx); llarp::util::SetThreadName("llarp-mainloop"); if(code == 0) - code = llarp_main_run(ctx); + code = llarp_main_run(ctx, backgroundMode); llarp_main_free(ctx); } exit_code.set_value(code); @@ -140,16 +140,16 @@ main(int argc, char *argv[]) ("r,router", "generate router config", cxxopts::value()) ("f,force", "overwrite", cxxopts::value()) ("c,colour", "colour output", cxxopts::value()->default_value("true")) - ("d,debug", "debug mode - UNENCRYPTED TRAFFIC", cxxopts::value()) + ("b,background", "background mode (start, but do not connect to the network)", cxxopts::value()) ("config","path to configuration file", cxxopts::value()); options.parse_positional("config"); // clang-format on - bool genconfigOnly = false; - bool asRouter = false; - bool overWrite = false; - bool debugMode = false; + bool genconfigOnly = false; + bool asRouter = false; + bool overWrite = false; + bool backgroundMode = false; std::string conffname; // suggestions: confFName? conf_fname? try @@ -185,9 +185,9 @@ main(int argc, char *argv[]) genconfigOnly = true; } - if(result.count("debug") > 0) + if(result.count("background") > 0) { - debugMode = true; + backgroundMode = true; } if(result.count("force") > 0) @@ -319,7 +319,7 @@ main(int argc, char *argv[]) } std::thread main_thread{ - std::bind(&run_main_context, conffname, multiThreaded, debugMode)}; + std::bind(&run_main_context, conffname, multiThreaded, backgroundMode)}; auto ftr = exit_code.get_future(); do { diff --git a/include/llarp.h b/include/llarp.h index 24779c17d..72f4856d5 100644 --- a/include/llarp.h +++ b/include/llarp.h @@ -41,11 +41,11 @@ extern "C" /// setup main context, returns 0 on success int - llarp_main_setup(struct llarp_main *ptr, bool debugMode); + llarp_main_setup(struct llarp_main *ptr); /// run main context, returns 0 on success, blocks until program end int - llarp_main_run(struct llarp_main *ptr); + llarp_main_run(struct llarp_main *ptr, bool backgroundMode); /// free main context and end all operations void diff --git a/include/llarp.hpp b/include/llarp.hpp index d380350eb..9f34e22bf 100644 --- a/include/llarp.hpp +++ b/include/llarp.hpp @@ -81,10 +81,10 @@ namespace llarp GetDatabase(const byte_t *pk); int - Setup(bool debug = false); + Setup(); int - Run(); + Run(bool daemonMode); void HandleSignal(int sig); diff --git a/llarp/context.cpp b/llarp/context.cpp index 509902a4d..588ad0e41 100644 --- a/llarp/context.cpp +++ b/llarp/context.cpp @@ -192,40 +192,14 @@ __ ___ ____ _ _ ___ _ _ ____ } int - Context::Setup(bool debug) + Context::Setup() { llarp::LogInfo(LLARP_VERSION, " ", LLARP_RELEASE_MOTTO); llarp::LogInfo("starting up"); mainloop = llarp_make_ev_loop(); logic = std::make_shared< Logic >(); - if(debug) - { - static std::string WARNING = R"( -__ ___ ____ _ _ ___ _ _ ____ -\ \ / / \ | _ \| \ | |_ _| \ | |/ ___| - \ \ /\ / / _ \ | |_) | \| || || \| | | _ - \ V V / ___ \| _ <| |\ || || |\ | |_| | - \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____| - -This Lokinet session is not private!! - -Sending traffic unencrypted!! -__ ___ ____ _ _ ___ _ _ ____ -\ \ / / \ | _ \| \ | |_ _| \ | |/ ___| - \ \ /\ / / _ \ | |_) | \| || || \| | | _ - \ V V / ___ \| _ <| |\ || || |\ | |_| | - \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____| - - )"; - - std::cerr << WARNING << '\n'; - crypto = std::make_unique< NoOpCrypto >(); - } - else - { - crypto = std::make_unique< sodium::CryptoLibSodium >(); - } + crypto = std::make_unique< sodium::CryptoLibSodium >(); cryptoManager = std::make_unique< CryptoManager >(crypto.get()); router = std::make_unique< Router >(worker, mainloop, logic); @@ -248,7 +222,7 @@ __ ___ ____ _ _ ___ _ _ ____ } int - Context::Run() + Context::Run(bool backgroundMode) { if(router == nullptr) { @@ -259,9 +233,15 @@ __ ___ ____ _ _ ___ _ _ ____ if(!WritePIDFile()) return 1; // run - if(!router->Run(nodedb.get())) + if(!router->StartJsonRpc()) return 1; + if(!backgroundMode) + { + if(!router->Run()) + return 2; + } + // run net io thread llarp::LogInfo("running mainloop"); llarp_ev_loop_run_single_process(mainloop, logic); @@ -437,20 +417,20 @@ extern "C" } int - llarp_main_setup(struct llarp_main *ptr, bool debug) + llarp_main_setup(struct llarp_main *ptr) { - return ptr->ctx->Setup(debug); + return ptr->ctx->Setup(); } int - llarp_main_run(struct llarp_main *ptr) + llarp_main_run(struct llarp_main *ptr, bool backgroundMode) { if(!ptr) { llarp::LogError("No ptr passed in"); return 1; } - return ptr->ctx->Run(); + return ptr->ctx->Run(backgroundMode); } void diff --git a/llarp/router/abstractrouter.hpp b/llarp/router/abstractrouter.hpp index ad2891834..08fbf449e 100644 --- a/llarp/router/abstractrouter.hpp +++ b/llarp/router/abstractrouter.hpp @@ -127,7 +127,10 @@ namespace llarp Configure(Config *conf, llarp_nodedb *nodedb) = 0; virtual bool - Run(struct llarp_nodedb *nodedb) = 0; + StartJsonRpc() = 0; + + virtual bool + Run() = 0; /// stop running the router logic gracefully virtual void diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 315eeb805..858242d2e 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -110,11 +110,18 @@ namespace llarp util::StatusObject Router::ExtractStatus() const { - return util::StatusObject{ - {"dht", _dht->impl->ExtractStatus()}, - {"services", _hiddenServiceContext.ExtractStatus()}, - {"exit", _exitContext.ExtractStatus()}, - {"links", _linkManager.ExtractStatus()}}; + if(_running) + { + return util::StatusObject{ + {"dht", _dht->impl->ExtractStatus()}, + {"services", _hiddenServiceContext.ExtractStatus()}, + {"exit", _exitContext.ExtractStatus()}, + {"links", _linkManager.ExtractStatus()}}; + } + else + { + return util::StatusObject{{"notRunning", true}}; + } } bool @@ -825,11 +832,10 @@ namespace llarp } bool - Router::Run(struct llarp_nodedb *nodedb) + Router::StartJsonRpc() { if(_running || _stopping) return false; - this->_nodedb = nodedb; if(enableRPCServer) { @@ -849,6 +855,16 @@ namespace llarp } LogInfo("Bound RPC server to ", rpcBindAddr); } + + return true; + } + + bool + Router::Run() + { + if(_running || _stopping) + return false; + if(whitelistRouters) { rpcCaller = std::make_unique< rpc::Caller >(this); @@ -1001,7 +1017,7 @@ namespace llarp _dht->impl->Nodes()->PutNode(rc); } - LogInfo("have ", nodedb->num_loaded(), " routers"); + LogInfo("have ", _nodedb->num_loaded(), " routers"); ScheduleTicker(1000); _running.store(true); diff --git a/llarp/router/router.hpp b/llarp/router/router.hpp index dce19004f..e2d94536e 100644 --- a/llarp/router/router.hpp +++ b/llarp/router/router.hpp @@ -327,7 +327,10 @@ namespace llarp Configure(Config *conf, llarp_nodedb *nodedb = nullptr) override; bool - Run(struct llarp_nodedb *nodedb) override; + StartJsonRpc() override; + + bool + Run() override; /// stop running the router logic gracefully void diff --git a/llarp/rpc/rpc.cpp b/llarp/rpc/rpc.cpp index 7c0458749..c1447e29c 100644 --- a/llarp/rpc/rpc.cpp +++ b/llarp/rpc/rpc.cpp @@ -195,6 +195,13 @@ namespace llarp ~Handler() override = default; + Response + StartRouter() const + { + const bool rc = router->Run(); + return Response{{"status", rc}}; + } + Response DumpState() const { @@ -271,6 +278,10 @@ namespace llarp HandleJSONRPC(Method_t method, ABSL_ATTRIBUTE_UNUSED const Params& params) override { + if(method == "llarp.admin.start") + { + return StartRouter(); + } if(method == "llarp.admin.link.neighboors") { return ListNeighboors();