Skip to content

Commit

Permalink
IPC Socket Fixes (#2111)
Browse files Browse the repository at this point in the history
* Updated RpcServer Initialization and Logic

-- Moved all RPCServer initialization logic to rpcserver constructor
-- Fixed config logic, fxn binding to rpc address, fxn adding rpc cats
-- router hive failed CI/CD resulting from outdated reference to rpcBindAddr
-- ipc socket as default hidden from windows (for now)
  • Loading branch information
dr7ana authored Jan 20, 2023
1 parent 245f968 commit 0edfe8f
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 32 deletions.
3 changes: 3 additions & 0 deletions daemon/lokinet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ namespace
return;
}

// change cwd to dataDir to support relative paths in config
fs::current_path(conf->router.m_dataDir);

ctx = std::make_shared<llarp::Context>();
ctx->Configure(std::move(conf));

Expand Down
1 change: 0 additions & 1 deletion include/llarp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace llarp
std::shared_ptr<AbstractRouter> router = nullptr;
std::shared_ptr<EventLoop> loop = nullptr;
std::shared_ptr<NodeDB> nodedb = nullptr;
std::string nodedb_dir;

Context();
virtual ~Context() = default;
Expand Down
21 changes: 14 additions & 7 deletions llarp/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,12 @@ namespace llarp
void
ApiConfig::defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params)
{
constexpr Default DefaultRPCBindAddr{"tcp://127.0.0.1:1190"};
constexpr std::array DefaultRPCBind{
Default{"tcp://127.0.0.1:1190"},
#ifndef _WIN32
Default{"ipc://rpc.sock"},
#endif
};

conf.defineOption<bool>(
"api",
Expand All @@ -1128,20 +1133,22 @@ namespace llarp
conf.defineOption<std::string>(
"api",
"bind",
DefaultRPCBindAddr,
[this](std::string arg) {
if (arg.empty())
DefaultRPCBind,
MultiValue,
[this, first = true](std::string arg) mutable {
if (first)
{
arg = DefaultRPCBindAddr.val;
m_rpcBindAddresses.clear();
first = false;
}
if (arg.find("://") == std::string::npos)
{
arg = "tcp://" + arg;
}
m_rpcBindAddr = std::move(arg);
m_rpcBindAddresses.emplace_back(arg);
},
Comment{
"IP address and port to bind to.",
"IP addresses and ports to bind to.",
"Recommend localhost-only for security purposes.",
});

Expand Down
2 changes: 1 addition & 1 deletion llarp/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ namespace llarp
struct ApiConfig
{
bool m_enableRPCServer = false;
std::string m_rpcBindAddr;
std::vector<oxenmq::address> m_rpcBindAddresses;

void
defineConfigOptions(ConfigDefinition& conf, const ConfigGenParameters& params);
Expand Down
4 changes: 1 addition & 3 deletions llarp/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ namespace llarp
throw std::runtime_error("Config already exists");

config = std::move(conf);

nodedb_dir = fs::path{config->router.m_dataDir / nodedb_dirname}.string();
}

bool
Expand Down Expand Up @@ -92,7 +90,7 @@ namespace llarp
Context::makeNodeDB()
{
return std::make_shared<NodeDB>(
nodedb_dir, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
nodedb_dirname, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
}

std::shared_ptr<AbstractRouter>
Expand Down
15 changes: 3 additions & 12 deletions llarp/router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,8 @@ namespace llarp
log::clear_sinks();
log::add_sink(log_type, log_type == log::Type::System ? "lokinet" : conf.logging.m_logFile);

enableRPCServer = conf.api.m_enableRPCServer;

// re-add rpc log sink if rpc enabled, else free it
if (enableRPCServer and llarp::logRingBuffer)
if (m_Config->api.m_enableRPCServer and llarp::logRingBuffer)
log::add_sink(llarp::logRingBuffer, llarp::log::DEFAULT_PATTERN_MONO);
else
llarp::logRingBuffer = nullptr;
Expand All @@ -430,9 +428,6 @@ namespace llarp
m_lokidRpcClient = std::make_shared<rpc::LokidRpcClient>(m_lmq, weak_from_this());
}

if (enableRPCServer)
rpcBindAddr = oxenmq::address(conf.api.m_rpcBindAddr);

log::debug(logcat, "Starting RPC server");
if (not StartRpcServer())
throw std::runtime_error("Failed to start rpc server");
Expand Down Expand Up @@ -1260,12 +1255,8 @@ namespace llarp
bool
Router::StartRpcServer()
{
if (enableRPCServer)
{
m_RPCServer.reset(new rpc::RpcServer{m_lmq, this});
m_RPCServer->AsyncServeRPC(rpcBindAddr);
LogInfo("Bound RPC server to ", rpcBindAddr.full_address());
}
if (m_Config->api.m_enableRPCServer)
m_RPCServer = std::make_unique<rpc::RpcServer>(m_lmq, this);

return true;
}
Expand Down
3 changes: 0 additions & 3 deletions llarp/router/router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,6 @@ namespace llarp
void
PumpLL();

const oxenmq::address DefaultRPCBindAddr = oxenmq::address::tcp("127.0.0.1", 1190);
bool enableRPCServer = false;
oxenmq::address rpcBindAddr = DefaultRPCBindAddr;
std::unique_ptr<rpc::RpcServer> m_RPCServer;

const llarp_time_t _randomStartDelay;
Expand Down
14 changes: 11 additions & 3 deletions llarp/rpc/rpc_server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "rpc_server.hpp"
#include <llarp/router/route_poker.hpp>
#include <llarp/config/config.hpp>
#include <llarp/constants/platform.hpp>
#include <llarp/constants/version.hpp>
#include <nlohmann/json.hpp>
Expand All @@ -23,7 +24,15 @@ namespace llarp::rpc
{
RpcServer::RpcServer(LMQ_ptr lmq, AbstractRouter* r)
: m_LMQ{std::move(lmq)}, m_Router{r}, log_subs{*m_LMQ, llarp::logRingBuffer}
{}
{
for (const auto& addr : r->GetConfig()->api.m_rpcBindAddresses)
{
m_LMQ->listen_plain(addr.zmq_address());
LogInfo("Bound RPC server to ", addr.full_address());
}

this->AddRPCCategories();
}

/// maybe parse json from message paramter at index
std::optional<nlohmann::json>
Expand Down Expand Up @@ -141,9 +150,8 @@ namespace llarp::rpc
}

void
RpcServer::AsyncServeRPC(oxenmq::address url)
RpcServer::AddRPCCategories()
{
m_LMQ->listen_plain(url.zmq_address());
m_LMQ->add_category("llarp", oxenmq::AuthLevel::none)
.add_request_command("logs", [this](oxenmq::Message& msg) { HandleLogsSubRequest(msg); })
.add_request_command(
Expand Down
4 changes: 3 additions & 1 deletion llarp/rpc/rpc_server.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string_view>
#include <llarp/config/config.hpp>
#include <oxenmq/oxenmq.h>
#include <oxenmq/address.h>
#include <oxen/log/omq_logger.hpp>
Expand All @@ -18,8 +19,9 @@ namespace llarp::rpc
{
explicit RpcServer(LMQ_ptr, AbstractRouter*);
~RpcServer() = default;

void
AsyncServeRPC(oxenmq::address addr);
AddRPCCategories();

private:
void
Expand Down
2 changes: 1 addition & 1 deletion pybind/llarp/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace llarp
py::class_<ApiConfig>(mod, "ApiConfig")
.def(py::init<>())
.def_readwrite("enableRPCServer", &ApiConfig::m_enableRPCServer)
.def_readwrite("rpcBindAddr", &ApiConfig::m_rpcBindAddr);
.def_readwrite("rpcBindAddresses", &ApiConfig::m_rpcBindAddresses);

py::class_<LokidConfig>(mod, "LokidConfig")
.def(py::init<>())
Expand Down

0 comments on commit 0edfe8f

Please sign in to comment.