From 71ea4f4fa20efb525ae20b5cf59cb493c152854c Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Mon, 19 Sep 2022 11:34:27 -0300 Subject: [PATCH] RPC: Relax token/range argument handling - Accept empty string or `null` for token to mean "no token." - Accept `null` for range to mean "default range." - Don't use a default range (::0/0) in lokinet-vpn because this will fail if IPv6 ranges aren't supported on the platform (e.g. on Windows), and isn't necessary: if we omit it then the rpc code already uses ::0/0 or 0.0.0.0/0 by default, as needed. --- daemon/lokinet-vpn.cpp | 28 +++++++++++----------------- llarp/rpc/rpc_server.cpp | 10 +++++----- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/daemon/lokinet-vpn.cpp b/daemon/lokinet-vpn.cpp index dff05d0ee..c5c6a4f50 100644 --- a/daemon/lokinet-vpn.cpp +++ b/daemon/lokinet-vpn.cpp @@ -74,8 +74,8 @@ main(int argc, char* argv[]) oxenmq::address rpcURL("tcp://127.0.0.1:1190"); std::string exitAddress; std::string endpoint = "default"; - std::optional token; - std::string range = "::/0"; + std::string token; + std::optional range; oxenmq::LogLevel logLevel = oxenmq::LogLevel::warn; bool goUp = false; bool goDown = false; @@ -216,20 +216,11 @@ main(int argc, char* argv[]) } if (goUp) { - std::optional maybe_result; - if (token.has_value()) - { - maybe_result = LMQ_Request( - lmq, - connID, - "llarp.exit", - nlohmann::json{{"exit", exitAddress}, {"range", range}, {"token", *token}}); - } - else - { - maybe_result = LMQ_Request( - lmq, connID, "llarp.exit", nlohmann::json{{"exit", exitAddress}, {"range", range}}); - } + nlohmann::json opts{{"exit", exitAddress}, {"token", token}}; + if (range) + opts["range"] = *range; + + auto maybe_result = LMQ_Request(lmq, connID, "llarp.exit", opts); if (not maybe_result.has_value()) { @@ -245,7 +236,10 @@ main(int argc, char* argv[]) } if (goDown) { - LMQ_Request(lmq, connID, "llarp.exit", nlohmann::json{{"range", range}, {"unmap", true}}); + nlohmann::json opts{{"unmap", true}}; + if (range) + opts["range"] = *range; + LMQ_Request(lmq, connID, "llarp.exit", std::move(opts)); } return 0; diff --git a/llarp/rpc/rpc_server.cpp b/llarp/rpc/rpc_server.cpp index ecff8c3f3..b07000d0c 100644 --- a/llarp/rpc/rpc_server.cpp +++ b/llarp/rpc/rpc_server.cpp @@ -469,7 +469,7 @@ namespace llarp::rpc map = false; } const auto range_itr = obj.find("range"); - if (range_itr == obj.end()) + if (range_itr == obj.end() or range_itr->is_null()) { // platforms without ipv6 support will shit themselves // here if we give them an exit mapping that is ipv6 @@ -492,9 +492,9 @@ namespace llarp::rpc reply(CreateJSONError("ipv6 ranges not supported on this platform")); return; } - std::optional token; + std::string token; const auto token_itr = obj.find("token"); - if (token_itr != obj.end()) + if (token_itr != obj.end() and not token_itr->is_null()) { token = token_itr->get(); } @@ -518,10 +518,10 @@ namespace llarp::rpc ep->MapExitRange(range, addr); bool shouldSendAuth = false; - if (token.has_value()) + if (not token.empty()) { shouldSendAuth = true; - ep->SetAuthInfoForEndpoint(*exit, service::AuthInfo{*token}); + ep->SetAuthInfoForEndpoint(*exit, service::AuthInfo{token}); } auto onGoodResult = [r, reply](std::string reason) { if (r->HasClientExit())