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.
This commit is contained in:
Jason Rhinelander 2022-09-19 11:34:27 -03:00
parent 8b321612da
commit 71ea4f4fa2
No known key found for this signature in database
GPG Key ID: C4992CE7A88D4262
2 changed files with 16 additions and 22 deletions

View File

@ -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<std::string> token;
std::string range = "::/0";
std::string token;
std::optional<std::string> 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<nlohmann::json> 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;

View File

@ -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<std::string> 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<std::string>();
}
@ -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())