2021-03-09 22:24:35 +00:00
|
|
|
#include "name.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
#include <llarp/util/str.hpp>
|
2020-09-17 19:18:08 +00:00
|
|
|
|
|
|
|
namespace llarp::service
|
|
|
|
{
|
|
|
|
std::optional<Address>
|
|
|
|
EncryptedName::Decrypt(std::string_view name) const
|
|
|
|
{
|
|
|
|
if (ciphertext.empty())
|
|
|
|
return std::nullopt;
|
2023-10-19 23:09:22 +00:00
|
|
|
const auto maybe = crypto::maybe_decrypt_name(ciphertext, nonce, name);
|
2020-09-17 19:18:08 +00:00
|
|
|
if (maybe.has_value())
|
|
|
|
return Address{*maybe};
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-10-10 20:49:53 +00:00
|
|
|
is_valid_name(std::string_view lnsName)
|
2020-09-17 19:18:08 +00:00
|
|
|
{
|
2021-04-04 11:30:46 +00:00
|
|
|
// make sure it ends with .loki because no fucking shit right?
|
|
|
|
if (not ends_with(lnsName, ".loki"))
|
|
|
|
return false;
|
2020-09-17 19:18:08 +00:00
|
|
|
// strip off .loki suffix
|
|
|
|
lnsName = lnsName.substr(0, lnsName.find_last_of('.'));
|
|
|
|
|
|
|
|
// ensure chars are sane
|
|
|
|
for (const auto ch : lnsName)
|
|
|
|
{
|
|
|
|
if (ch == '-')
|
|
|
|
continue;
|
|
|
|
if (ch == '.')
|
|
|
|
continue;
|
|
|
|
if (ch >= 'a' and ch <= 'z')
|
|
|
|
continue;
|
|
|
|
if (ch >= '0' and ch <= '9')
|
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// split into domain parts
|
|
|
|
const auto parts = split(lnsName, ".");
|
|
|
|
// get root domain
|
|
|
|
const auto primaryName = parts[parts.size() - 1];
|
|
|
|
constexpr size_t MaxNameLen = 32;
|
|
|
|
constexpr size_t MaxPunycodeNameLen = 63;
|
|
|
|
// check against lns name blacklist
|
|
|
|
if (primaryName == "localhost")
|
|
|
|
return false;
|
|
|
|
if (primaryName == "loki")
|
|
|
|
return false;
|
|
|
|
if (primaryName == "snode")
|
|
|
|
return false;
|
|
|
|
// check for dashes
|
|
|
|
if (primaryName.find("-") == std::string_view::npos)
|
|
|
|
return primaryName.size() <= MaxNameLen;
|
|
|
|
// check for dashes and end or beginning
|
|
|
|
if (*primaryName.begin() == '-' or *(primaryName.end() - 1) == '-')
|
|
|
|
return false;
|
|
|
|
// check for punycode name length
|
|
|
|
if (primaryName.size() > MaxPunycodeNameLen)
|
|
|
|
return false;
|
|
|
|
// check for xn--
|
|
|
|
return (primaryName[2] == '-' and primaryName[3] == '-')
|
|
|
|
? (primaryName[0] == 'x' and primaryName[1] == 'n')
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::service
|