2021-03-09 23:24:35 +01:00
|
|
|
#include "router_contact.hpp"
|
|
|
|
|
|
|
|
#include "constants/version.hpp"
|
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "net/net.hpp"
|
|
|
|
#include "util/bencode.hpp"
|
|
|
|
#include "util/buffer.hpp"
|
2022-07-16 02:41:14 +02:00
|
|
|
#include "util/logging.hpp"
|
2021-03-09 23:24:35 +01:00
|
|
|
#include "util/mem.hpp"
|
|
|
|
#include "util/time.hpp"
|
2018-05-27 15:42:55 +02:00
|
|
|
|
2022-02-17 19:44:31 +01:00
|
|
|
#include <oxenc/bt_serialize.h>
|
2020-09-25 20:05:28 +02:00
|
|
|
|
2022-10-05 20:05:25 +02:00
|
|
|
#include "util/file.hpp"
|
2018-08-31 14:46:54 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
namespace llarp
|
2018-07-08 15:26:24 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
static auto logcat = log::Cat("RC");
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
NetID&
|
2019-01-05 01:49:06 +01:00
|
|
|
NetID::DefaultValue()
|
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
static NetID defaultID(reinterpret_cast<const byte_t*>(llarp::DEFAULT_NETID));
|
2019-01-05 01:49:06 +01:00
|
|
|
return defaultID;
|
|
|
|
}
|
2018-12-21 14:08:01 +01:00
|
|
|
|
2019-08-27 01:29:17 +02:00
|
|
|
bool RouterContact::BlockBogons = true;
|
2018-12-17 21:46:08 +01:00
|
|
|
|
2022-07-18 19:11:57 +02:00
|
|
|
/// 1 day rc lifespan
|
|
|
|
constexpr auto rc_lifetime = 24h;
|
2020-01-18 22:07:21 +01:00
|
|
|
/// an RC inserted long enough ago (4 hrs) is considered stale and is removed
|
2022-07-18 19:11:57 +02:00
|
|
|
constexpr auto rc_stale_age = 4h;
|
|
|
|
/// window of time in which a router wil try to update their RC before it is marked stale
|
|
|
|
constexpr auto rc_update_window = 5min;
|
2020-01-18 22:37:42 +01:00
|
|
|
/// update RCs shortly before they are about to expire
|
2022-07-18 19:11:57 +02:00
|
|
|
constexpr auto rc_update_interval = rc_stale_age - rc_update_window;
|
|
|
|
|
|
|
|
llarp_time_t RouterContact::Lifetime = rc_lifetime;
|
|
|
|
llarp_time_t RouterContact::StaleInsertionAge = rc_stale_age;
|
|
|
|
llarp_time_t RouterContact::UpdateInterval = rc_update_interval;
|
|
|
|
|
|
|
|
/// how many rc lifetime intervals should we wait until purging an rc
|
|
|
|
constexpr auto expiration_lifetime_generations = 10;
|
|
|
|
/// the max age of an rc before we want to expire it
|
|
|
|
constexpr auto rc_expire_age = rc_lifetime * expiration_lifetime_generations;
|
2019-01-02 23:21:29 +01:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
NetID::NetID(const byte_t* val)
|
2019-01-05 01:49:06 +01:00
|
|
|
{
|
2021-04-03 14:19:46 +02:00
|
|
|
const size_t len = strnlen(reinterpret_cast<const char*>(val), size());
|
2019-01-05 01:49:06 +01:00
|
|
|
std::copy(val, val + len, begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
NetID::NetID() : NetID(DefaultValue().data())
|
Config file improvements (#1397)
* Config file API/comment improvements
API improvements:
=================
Make the config API use position-independent tag parameters (Required,
Default{123}, MultiValue) rather than a sequence of bools with
overloads. For example, instead of:
conf.defineOption<int>("a", "b", false, true, 123, [] { ... });
you now write:
conf.defineOption<int>("a", "b", MultiValue, Default{123}, [] { ... });
The tags are:
- Required
- MultiValue
- Default{value}
plus new abilities (see below):
- Hidden
- RelayOnly
- ClientOnly
- Comment{"line1", "line2", "line3"}
Made option definition more powerful:
=====================================
- `Hidden` allows you to define an option that won't show up in the
generated config file if it isn't set.
- `RelayOnly`/`ClientOnly` sets up an option that is only accepted and
only shows up for relay or client configs. (If neither is specified
the option shows up in both modes).
- `Comment{...}` lets the option comments be specified as part of the
defineOption.
Comment improvements
====================
- Rewrote comments for various options to expand on details.
- Inlined all the comments with the option definitions.
- Several options that were missing comments got comments added.
- Made various options for deprecated and or internal options hidden by
default so that they don't show up in a default config file.
- show the section comment (but not option comments) *after* the
[section] tag instead of before it as it makes more sense that way
(particularly for the [bind] section which has a new long comment to
describe how it works).
Disable profiling by default
============================
We had this weird state where we use and store profiling by default but
never *load* it when starting up. This commit makes us just not use
profiling at all unless explicitly enabled.
Other misc changes:
===================
- change default worker threads to 0 (= num cpus) instead of 1, and fix
it to allow 0.
- Actually apply worker-threads option
- fixed default data-dir value erroneously having quotes around it
- reordered ifname/ifaddr/mapaddr (was previously mapaddr/ifaddr/ifname)
as mapaddr is a sort of specialization of ifaddr and so makes more
sense to come after it (particularly because it now references ifaddr
in its help message).
- removed peer-stats option (since we always require it for relays and
never use it for clients)
- removed router profiles filename option (this doesn't need to be
configurable)
- removed defunct `service-node-seed` option
- Change default logging output file to "" (which means stdout), and
also made "-" work for stdout.
* Router hive compilation fixes
* Comments for SNApp SRV settings in ini file
* Add extra blank line after section comments
* Better deprecated option handling
Allow {client,relay}-only options in {relay,client} configs to be
specified as implicitly deprecated options: they warn, and don't set
anything.
Add an explicit `Deprecated` tag and move deprecated option handling
into definition.cpp.
* Move backwards compat options into section definitions
Keep the "addBackwardsCompatibleConfigOptions" only for options in
sections that no longer exist.
* Fix INI parsing issues & C++17-ify
- don't allow inline comments because it seems they aren't allowed in
ini formats in general, and is going to cause problems if there is a
comment character in a value (e.g. an exit auth string). Additionally
it was breaking on a line such as:
# some comment; see?
because it was treating only `; see?` as the comment and then producing
an error message about the rest of the line being invalid.
- make section parsing stricter: the `[` and `]` have to be at the
beginning at end of the line now (after stripping whitespace).
- Move whitespace stripping to the top since everything in here does it.
- chop off string_view suffix/prefix rather than maintaining position
values
- fix potential infinite loop/segfault when given a line such as `]foo[`
* Make config parsing failure fatal
Load() LogError's and returns false on failure, so we weren't aborting
on config file errors.
* Formatting: allow `{}` for empty functions/structs
Instead of using two lines when empty:
{
}
* Make default dns bind 127.0.0.1 on non-Linux
* Don't show empty section; fix tests
We can conceivably have sections that only make sense for clients or
relays, and so want to completely omit that section if we have no
options for the type of config being generated.
Also fixes missing empty lines between tests.
Co-authored-by: Thomas Winget <tewinget@gmail.com>
2020-10-08 00:22:58 +02:00
|
|
|
{}
|
2018-12-19 17:17:41 +01:00
|
|
|
|
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
NetID::operator==(const NetID& other) const
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
2018-12-28 16:04:05 +01:00
|
|
|
return ToString() == other.ToString();
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
NetID::ToString() const
|
|
|
|
{
|
2022-07-16 02:41:14 +02:00
|
|
|
return {begin(), std::find(begin(), end(), '\0')};
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
NetID::BDecode(llarp_buffer_t* buf)
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
|
|
|
Zero();
|
|
|
|
llarp_buffer_t strbuf;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (strbuf.sz > size())
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2019-01-02 02:03:53 +01:00
|
|
|
|
2019-01-02 02:04:04 +01:00
|
|
|
std::copy(strbuf.base, strbuf.base + strbuf.sz, begin());
|
2018-12-19 17:17:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
NetID::BEncode(llarp_buffer_t* buf) const
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
2019-01-02 02:03:53 +01:00
|
|
|
auto term = std::find(begin(), end(), '\0');
|
2019-04-19 20:24:33 +02:00
|
|
|
return bencode_write_bytestring(buf, data(), std::distance(begin(), term));
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
RouterContact::BEncode(llarp_buffer_t* buf) const
|
2020-09-25 20:05:28 +02:00
|
|
|
{
|
|
|
|
if (version == 0)
|
|
|
|
return BEncodeSignedSection(buf);
|
|
|
|
else if (version == 1)
|
|
|
|
{
|
2020-09-28 17:15:07 +02:00
|
|
|
// TODO: heapless serialization for this in lokimq's bt serialization.
|
2020-09-29 02:07:40 +02:00
|
|
|
if (not buf->writef("li1e%zu:", signature.size()))
|
2020-09-25 20:05:28 +02:00
|
|
|
return false;
|
|
|
|
if (not buf->write(signature.begin(), signature.end()))
|
|
|
|
return false;
|
|
|
|
if (not buf->write(signed_bt_dict.begin(), signed_bt_dict.end()))
|
|
|
|
return false;
|
|
|
|
if (not buf->writef("e"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-16 02:41:14 +02:00
|
|
|
std::string
|
|
|
|
RouterContact::ToTXTRecord() const
|
2020-10-12 18:18:46 +02:00
|
|
|
{
|
2022-07-16 02:41:14 +02:00
|
|
|
std::string result;
|
|
|
|
auto out = std::back_inserter(result);
|
2020-10-12 18:18:46 +02:00
|
|
|
for (const auto& addr : addrs)
|
2022-10-26 22:10:54 +02:00
|
|
|
fmt::format_to(out, "ai_addr={}; ai_pk={}; ", addr.toIpAddress(), addr.pubkey);
|
|
|
|
fmt::format_to(out, "updated={}; onion_pk={}; ", last_updated.count(), enckey.ToHex());
|
2020-10-12 18:18:46 +02:00
|
|
|
if (routerVersion.has_value())
|
2022-10-26 22:10:54 +02:00
|
|
|
fmt::format_to(out, "router_version={}; ", *routerVersion);
|
2022-07-16 02:41:14 +02:00
|
|
|
return result;
|
2020-10-12 18:18:46 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 17:05:18 +02:00
|
|
|
bool
|
|
|
|
RouterContact::FromOurNetwork() const
|
|
|
|
{
|
|
|
|
return netID == NetID::DefaultValue();
|
|
|
|
}
|
|
|
|
|
2020-09-25 20:05:28 +02:00
|
|
|
bool
|
|
|
|
RouterContact::BEncodeSignedSection(llarp_buffer_t* buf) const
|
2018-05-13 20:07:36 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write dict begin */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_start_dict(buf))
|
2018-06-10 16:05:48 +02:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write ai if they exist */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "a", 1))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeWriteList(addrs.begin(), addrs.end(), buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
|
2018-12-19 17:17:41 +01:00
|
|
|
/* write netid */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "i", 1))
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!netID.BEncode(buf))
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write signing pubkey */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "k", 1))
|
2018-05-22 20:41:38 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!pubkey.BEncode(buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
std::string nick = Nick();
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!nick.empty())
|
2018-05-22 17:54:19 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write nickname */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "n", 1))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, nick.c_str(), nick.size()))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-05-22 17:54:19 +02:00
|
|
|
}
|
2018-05-13 20:07:36 +02:00
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write encryption pubkey */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "p", 1))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!enckey.BEncode(buf))
|
2018-05-13 20:07:36 +02:00
|
|
|
return false;
|
2021-04-06 14:25:46 +02:00
|
|
|
|
2020-01-25 17:28:07 +01:00
|
|
|
// write router version if present
|
2020-05-20 21:46:08 +02:00
|
|
|
if (routerVersion)
|
2020-01-25 17:28:07 +01:00
|
|
|
{
|
2020-05-20 21:46:08 +02:00
|
|
|
if (not BEncodeWriteDictEntry("r", *routerVersion, buf))
|
2020-01-25 17:28:07 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-04-06 14:25:46 +02:00
|
|
|
|
|
|
|
if (version > 0)
|
|
|
|
{
|
|
|
|
// srv records if present
|
|
|
|
if (not BEncodeWriteDictList("s", srvRecords, buf))
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-30 20:48:43 +02:00
|
|
|
/* write last updated */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_bytestring(buf, "u", 1))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_uint64(buf, last_updated.count()))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-13 20:07:36 +02:00
|
|
|
|
2019-11-22 18:39:35 +01:00
|
|
|
/* write versions */
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_write_uint64_entry(buf, "v", 1, version))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-25 19:52:10 +02:00
|
|
|
|
2020-08-19 21:10:11 +02:00
|
|
|
if (serializeExit)
|
|
|
|
{
|
|
|
|
/* write xi if they exist */
|
|
|
|
if (!bencode_write_bytestring(buf, "x", 1))
|
|
|
|
return false;
|
|
|
|
/* no exits anymore in RCs */
|
|
|
|
const std::vector<AlignedBuffer<8>> exits{};
|
|
|
|
if (!BEncodeWriteList(exits.begin(), exits.end(), buf))
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-25 20:05:28 +02:00
|
|
|
|
|
|
|
if (version == 0)
|
|
|
|
{
|
|
|
|
/* write signature */
|
|
|
|
if (!bencode_write_bytestring(buf, "z", 1))
|
|
|
|
return false;
|
|
|
|
if (!signature.BEncode(buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
2018-05-11 01:32:46 +02:00
|
|
|
|
2018-08-31 15:51:24 +02:00
|
|
|
void
|
|
|
|
RouterContact::Clear()
|
|
|
|
{
|
|
|
|
addrs.clear();
|
|
|
|
signature.Zero();
|
|
|
|
nickname.Zero();
|
|
|
|
enckey.Zero();
|
|
|
|
pubkey.Zero();
|
2020-05-01 21:51:15 +02:00
|
|
|
routerVersion = std::optional<RouterVersion>{};
|
2020-04-07 20:38:56 +02:00
|
|
|
last_updated = 0s;
|
2021-04-06 14:25:46 +02:00
|
|
|
srvRecords.clear();
|
2022-05-26 17:59:44 +02:00
|
|
|
version = llarp::constants::proto_version;
|
2018-08-31 15:51:24 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 18:14:43 +01:00
|
|
|
util::StatusObject
|
|
|
|
RouterContact::ExtractStatus() const
|
2019-02-08 20:43:25 +01:00
|
|
|
{
|
2021-03-05 18:31:52 +01:00
|
|
|
util::StatusObject obj{
|
|
|
|
{"lastUpdated", last_updated.count()},
|
|
|
|
{"publicRouter", IsPublicRouter()},
|
|
|
|
{"identity", pubkey.ToString()},
|
|
|
|
{"addresses", addrs}};
|
2019-08-20 00:25:40 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (HasNick())
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2019-08-19 11:33:26 +02:00
|
|
|
obj["nickname"] = Nick();
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2020-05-20 21:46:08 +02:00
|
|
|
if (routerVersion)
|
2020-01-25 17:28:07 +01:00
|
|
|
{
|
|
|
|
obj["routerVersion"] = routerVersion->ToString();
|
|
|
|
}
|
2021-04-06 14:25:46 +02:00
|
|
|
std::vector<util::StatusObject> srv;
|
|
|
|
for (const auto& record : srvRecords)
|
|
|
|
{
|
|
|
|
srv.emplace_back(record.ExtractStatus());
|
|
|
|
}
|
|
|
|
obj["srvRecords"] = srv;
|
2019-02-11 18:14:43 +01:00
|
|
|
return obj;
|
2019-02-08 20:43:25 +01:00
|
|
|
}
|
|
|
|
|
2020-09-25 20:05:28 +02:00
|
|
|
bool
|
|
|
|
RouterContact::BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
2020-09-28 17:15:07 +02:00
|
|
|
if (*buf->cur == 'd') // old format
|
2020-09-25 20:05:28 +02:00
|
|
|
{
|
|
|
|
return DecodeVersion_0(buf);
|
|
|
|
}
|
2020-09-28 17:15:07 +02:00
|
|
|
else if (*buf->cur != 'l') // if not dict, should be new format and start with list
|
2020-09-25 20:05:28 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-10-02 16:06:08 +02:00
|
|
|
std::string_view buf_view(reinterpret_cast<char*>(buf->cur), buf->size_left());
|
2022-02-17 19:44:31 +01:00
|
|
|
oxenc::bt_list_consumer btlist(buf_view);
|
2020-09-25 20:05:28 +02:00
|
|
|
|
|
|
|
uint64_t outer_version = btlist.consume_integer<uint64_t>();
|
|
|
|
|
|
|
|
if (outer_version == 1)
|
|
|
|
{
|
|
|
|
bool decode_result = DecodeVersion_1(btlist);
|
|
|
|
|
|
|
|
// advance the llarp_buffer_t since lokimq serialization is unaware of it.
|
|
|
|
buf->cur += btlist.current_buffer().data() - buf_view.data() + 1;
|
|
|
|
|
|
|
|
return decode_result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::warning(logcat, "Received RouterContact with unkown version ({})", outer_version);
|
2020-09-25 20:05:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::debug(logcat, "RouterContact::BDecode failed: {}", e.what());
|
2020-09-25 20:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::DecodeVersion_0(llarp_buffer_t* buf)
|
|
|
|
{
|
2020-10-02 17:41:21 +02:00
|
|
|
return bencode_decode_dict(*this, buf);
|
2020-09-25 20:05:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-02-17 19:44:31 +01:00
|
|
|
RouterContact::DecodeVersion_1(oxenc::bt_list_consumer& btlist)
|
2020-09-25 20:05:28 +02:00
|
|
|
{
|
|
|
|
auto signature_string = btlist.consume_string_view();
|
|
|
|
signed_bt_dict = btlist.consume_dict_data();
|
|
|
|
|
|
|
|
if (not btlist.is_finished())
|
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::debug(logcat, "RouterContact serialized list too long for specified version.");
|
2020-09-25 20:05:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_buffer_t sigbuf(signature_string.data(), signature_string.size());
|
|
|
|
if (not signature.FromBytestring(&sigbuf))
|
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::debug(logcat, "RouterContact serialized signature had invalid length.");
|
2020-09-25 20:05:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_buffer_t data_dict_buf(signed_bt_dict.data(), signed_bt_dict.size());
|
|
|
|
return bencode_decode_dict(*this, &data_dict_buf);
|
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
RouterContact::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2018-08-09 17:36:58 +02:00
|
|
|
{
|
2018-08-30 20:48:43 +02:00
|
|
|
bool read = false;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictList("a", addrs, read, key, buf))
|
2018-08-09 17:36:58 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictEntry("i", netID, read, key, buf))
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
|
2018-08-09 17:36:58 +02:00
|
|
|
return false;
|
2018-08-02 02:48:43 +02:00
|
|
|
|
2022-09-09 23:48:38 +02:00
|
|
|
if (key.startswith("r"))
|
2020-01-25 17:28:07 +01:00
|
|
|
{
|
|
|
|
RouterVersion r;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (not r.BDecode(buf))
|
2020-01-25 17:28:07 +01:00
|
|
|
return false;
|
|
|
|
routerVersion = r;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-09 23:48:38 +02:00
|
|
|
if (key.startswith("n"))
|
2018-08-31 15:51:24 +02:00
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2020-04-07 20:38:56 +02:00
|
|
|
if (strbuf.sz > llarp::AlignedBuffer<(32)>::size())
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-31 15:51:24 +02:00
|
|
|
nickname.Zero();
|
2019-01-02 23:21:29 +01:00
|
|
|
std::copy(strbuf.base, strbuf.base + strbuf.sz, nickname.begin());
|
2018-08-31 15:51:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-12 14:49:23 +02:00
|
|
|
|
2021-04-06 14:25:46 +02:00
|
|
|
if (not BEncodeMaybeReadDictList("s", srvRecords, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-21 14:44:50 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
|
2018-08-31 15:51:24 +02:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
2018-08-30 20:48:43 +02:00
|
|
|
return false;
|
2018-05-11 01:32:46 +02:00
|
|
|
|
2022-09-09 23:48:38 +02:00
|
|
|
if (key.startswith("x") and serializeExit)
|
2020-08-19 21:10:11 +02:00
|
|
|
{
|
|
|
|
return bencode_discard(buf);
|
|
|
|
}
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
|
2018-05-22 17:54:19 +02:00
|
|
|
return false;
|
2018-08-30 20:48:43 +02:00
|
|
|
|
2021-04-06 14:25:46 +02:00
|
|
|
return read or bencode_discard(buf);
|
2018-05-11 01:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
bool
|
|
|
|
RouterContact::IsPublicRouter() const
|
|
|
|
{
|
2020-05-20 21:46:08 +02:00
|
|
|
if (not routerVersion)
|
2020-02-19 21:23:46 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
return !addrs.empty();
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::HasNick() const
|
|
|
|
{
|
|
|
|
return nickname[0] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-05-01 21:51:15 +02:00
|
|
|
RouterContact::SetNick(std::string_view nick)
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
|
|
|
nickname.Zero();
|
2020-04-07 20:38:56 +02:00
|
|
|
std::copy(
|
|
|
|
nick.begin(), nick.begin() + std::min(nick.size(), nickname.size()), nickname.begin());
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:17:41 +01:00
|
|
|
bool
|
|
|
|
RouterContact::IsExpired(llarp_time_t now) const
|
2019-07-15 18:56:09 +02:00
|
|
|
{
|
2022-07-18 19:11:57 +02:00
|
|
|
return Age(now) >= rc_expire_age;
|
2019-07-15 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_time_t
|
|
|
|
RouterContact::TimeUntilExpires(llarp_time_t now) const
|
2018-12-19 17:17:41 +01:00
|
|
|
{
|
2019-06-04 15:19:45 +02:00
|
|
|
const auto expiresAt = last_updated + Lifetime;
|
2020-02-24 20:40:45 +01:00
|
|
|
return now < expiresAt ? expiresAt - now : 0s;
|
2019-07-15 18:56:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
llarp_time_t
|
|
|
|
RouterContact::Age(llarp_time_t now) const
|
|
|
|
{
|
2020-02-24 20:40:45 +01:00
|
|
|
return now > last_updated ? now - last_updated : 0s;
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::ExpiresSoon(llarp_time_t now, llarp_time_t dlt) const
|
|
|
|
{
|
2019-07-15 18:56:09 +02:00
|
|
|
return TimeUntilExpires(now) <= dlt;
|
2018-12-19 17:17:41 +01:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
std::string
|
|
|
|
RouterContact::Nick() const
|
|
|
|
{
|
2019-01-02 02:03:53 +01:00
|
|
|
auto term = std::find(nickname.begin(), nickname.end(), '\0');
|
|
|
|
return std::string(nickname.begin(), term);
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 20:38:56 +02:00
|
|
|
RouterContact::Sign(const SecretKey& secretkey)
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2019-02-03 00:12:42 +01:00
|
|
|
pubkey = llarp::seckey_topublic(secretkey);
|
2020-04-07 20:38:56 +02:00
|
|
|
std::array<byte_t, MAX_RC_SIZE> tmp;
|
2019-02-03 00:12:42 +01:00
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-31 14:46:54 +02:00
|
|
|
signature.Zero();
|
2018-11-19 23:45:37 +01:00
|
|
|
last_updated = time_now_ms();
|
2020-09-25 20:05:28 +02:00
|
|
|
|
|
|
|
if (!BEncodeSignedSection(&buf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 14:46:54 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2020-04-07 20:38:56 +02:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2018-08-31 14:46:54 +02:00
|
|
|
buf.cur = buf.base;
|
2020-09-25 20:05:28 +02:00
|
|
|
|
2020-09-28 17:15:07 +02:00
|
|
|
signed_bt_dict = std::string(reinterpret_cast<char*>(buf.base), buf.sz);
|
2020-09-25 20:05:28 +02:00
|
|
|
|
|
|
|
if (version == 0 or version == 1)
|
|
|
|
{
|
|
|
|
return CryptoManager::instance()->sign(signature, secretkey, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:02:32 +02:00
|
|
|
bool
|
2019-06-04 15:19:45 +02:00
|
|
|
RouterContact::Verify(llarp_time_t now, bool allowExpired) const
|
2018-10-15 14:02:32 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
if (netID != NetID::DefaultValue())
|
2018-12-28 16:04:05 +01:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(
|
|
|
|
logcat, "netid mismatch: '{}' (theirs) != '{}' (ours)", netID, NetID::DefaultValue());
|
2018-12-19 17:17:41 +01:00
|
|
|
return false;
|
2018-12-28 16:04:05 +01:00
|
|
|
}
|
2022-07-26 15:45:58 +02:00
|
|
|
|
|
|
|
if (IsExpired(now) and not allowExpired)
|
|
|
|
return false;
|
|
|
|
|
2022-07-28 18:07:38 +02:00
|
|
|
// TODO: make net* overridable
|
|
|
|
const auto* net = net::Platform::Default_ptr();
|
2020-04-07 20:38:56 +02:00
|
|
|
for (const auto& a : addrs)
|
2018-10-15 14:02:32 +02:00
|
|
|
{
|
2022-07-28 18:07:38 +02:00
|
|
|
if (net->IsBogon(a.ip) && BlockBogons)
|
2018-10-15 14:02:32 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(logcat, "invalid address info: {}", a);
|
2018-10-15 14:02:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!VerifySignature())
|
2018-12-28 16:04:05 +01:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(logcat, "invalid signature: {}", *this);
|
2018-12-28 16:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-10-15 14:02:32 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
bool
|
2019-05-28 21:45:08 +02:00
|
|
|
RouterContact::VerifySignature() const
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2020-09-25 20:05:28 +02:00
|
|
|
if (version == 0)
|
2018-08-31 15:51:24 +02:00
|
|
|
{
|
2020-09-25 20:05:28 +02:00
|
|
|
RouterContact copy;
|
|
|
|
copy = *this;
|
|
|
|
copy.signature.Zero();
|
|
|
|
std::array<byte_t, MAX_RC_SIZE> tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
|
|
|
if (!copy.BEncode(&buf))
|
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(logcat, "bencode failed");
|
2020-09-25 20:05:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
return CryptoManager::instance()->verify(pubkey, buf, signature);
|
2018-08-31 15:51:24 +02:00
|
|
|
}
|
2020-09-25 20:05:28 +02:00
|
|
|
/* else */
|
|
|
|
if (version == 1)
|
|
|
|
{
|
2021-04-06 14:25:46 +02:00
|
|
|
llarp_buffer_t buf{signed_bt_dict};
|
2020-09-25 20:05:28 +02:00
|
|
|
return CryptoManager::instance()->verify(pubkey, buf, signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 19:00:27 +02:00
|
|
|
static constexpr std::array obsolete_bootstraps = {
|
2022-11-18 21:00:12 +01:00
|
|
|
"7a16ac0b85290bcf69b2f3b52456d7e989ac8913b4afbb980614e249a3723218"sv,
|
|
|
|
"e6b3a6fe5e32c379b64212c72232d65b0b88ddf9bbaed4997409d329f8519e0b"sv,
|
|
|
|
};
|
2022-09-27 19:00:27 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::IsObsoleteBootstrap() const
|
|
|
|
{
|
|
|
|
for (const auto& k : obsolete_bootstraps)
|
|
|
|
{
|
|
|
|
if (pubkey.ToHex() == k)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:46:54 +02:00
|
|
|
bool
|
2020-05-27 05:36:46 +02:00
|
|
|
RouterContact::Write(const fs::path& fname) const
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
std::array<byte_t, MAX_RC_SIZE> tmp;
|
2019-02-03 00:12:42 +01:00
|
|
|
llarp_buffer_t buf(tmp);
|
2020-04-07 20:38:56 +02:00
|
|
|
if (!BEncode(&buf))
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2018-08-31 14:46:54 +02:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2022-10-05 20:05:25 +02:00
|
|
|
try
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
util::dump_file(fname, tmp.data(), buf.cur - buf.base);
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2022-10-05 20:05:25 +02:00
|
|
|
catch (const std::exception& e)
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(logcat, "Failed to write RC to {}: {}", fname, e.what());
|
2019-01-11 02:44:45 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-31 14:46:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-05-27 05:36:46 +02:00
|
|
|
RouterContact::Read(const fs::path& fname)
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2020-04-07 20:38:56 +02:00
|
|
|
std::array<byte_t, MAX_RC_SIZE> tmp;
|
2019-02-03 00:12:42 +01:00
|
|
|
llarp_buffer_t buf(tmp);
|
2022-10-05 20:05:25 +02:00
|
|
|
try
|
2018-08-31 14:46:54 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
util::slurp_file(fname, tmp.data(), tmp.size());
|
2018-08-31 14:46:54 +02:00
|
|
|
}
|
2022-10-05 20:05:25 +02:00
|
|
|
catch (const std::exception& e)
|
2019-08-27 01:29:17 +02:00
|
|
|
{
|
2022-10-05 20:05:25 +02:00
|
|
|
log::error(logcat, "Failed to read RC from {}: {}", fname, e.what());
|
2019-01-24 16:13:41 +01:00
|
|
|
return false;
|
2019-08-27 01:29:17 +02:00
|
|
|
}
|
2018-08-31 14:46:54 +02:00
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
2022-07-16 02:41:14 +02:00
|
|
|
std::string
|
|
|
|
RouterContact::ToString() const
|
|
|
|
{
|
2022-07-18 19:56:09 +02:00
|
|
|
return fmt::format(
|
|
|
|
"[RC k={} updated={} netid={} v={} ai={{{}}} e={} z={}]",
|
|
|
|
pubkey,
|
|
|
|
last_updated.count(),
|
|
|
|
netID,
|
|
|
|
version,
|
|
|
|
fmt::format("{}", fmt::join(addrs, ",")),
|
|
|
|
enckey,
|
|
|
|
signature);
|
2022-07-16 02:41:14 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 20:48:43 +02:00
|
|
|
} // namespace llarp
|