2019-04-22 18:35:19 +00:00
|
|
|
#include <service/intro_set.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <path/path.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
IntroSet::ExtractStatus() const
|
2019-02-08 19:43:25 +00:00
|
|
|
{
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject obj{{"published", T}};
|
|
|
|
std::vector< util::StatusObject > introsObjs;
|
|
|
|
std::transform(I.begin(), I.end(), std::back_inserter(introsObjs),
|
|
|
|
[](const auto& intro) -> util::StatusObject {
|
|
|
|
return intro.ExtractStatus();
|
|
|
|
});
|
|
|
|
obj.Put("intros", introsObjs);
|
2019-02-08 19:43:25 +00:00
|
|
|
if(!topic.IsZero())
|
2019-02-11 17:14:43 +00:00
|
|
|
obj.Put("topic", topic.ToString());
|
|
|
|
return obj;
|
2019-02-08 19:43:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 00:12:43 +00:00
|
|
|
bool
|
2019-02-02 23:12:42 +00:00
|
|
|
IntroSet::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "i")
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
|
|
|
return BEncodeReadList(I, buf);
|
|
|
|
}
|
|
|
|
if(!BEncodeMaybeReadDictEntry("k", K, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("n", topic, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictInt("t", T, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "w")
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-04-23 09:25:03 +00:00
|
|
|
W = absl::make_optional< PoW >();
|
2019-01-11 00:12:43 +00:00
|
|
|
return W->BDecode(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IntroSet::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("a", A, buf))
|
|
|
|
return false;
|
|
|
|
// start introduction list
|
|
|
|
if(!bencode_write_bytestring(buf, "i", 1))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteList(I.begin(), I.end(), buf))
|
|
|
|
return false;
|
|
|
|
// end introduction list
|
|
|
|
|
|
|
|
// pq pubkey
|
|
|
|
if(!BEncodeWriteDictEntry("k", K, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// topic tag
|
|
|
|
if(topic.ToString().size())
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("n", topic, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Timestamp published
|
|
|
|
if(!BEncodeWriteDictInt("t", T, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// write version
|
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
|
|
|
return false;
|
|
|
|
if(W)
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("w", *W, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!BEncodeWriteDictEntry("z", Z, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IntroSet::HasExpiredIntros(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
for(const auto& i : I)
|
|
|
|
if(now >= i.expiresAt)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IntroSet::IsExpired(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
return GetNewestIntroExpiration() < now;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-04-22 18:35:19 +00:00
|
|
|
IntroSet::Verify(Crypto* crypto, llarp_time_t now) const
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, MAX_INTROSET_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-11 00:12:43 +00:00
|
|
|
IntroSet copy;
|
|
|
|
copy = *this;
|
|
|
|
copy.Z.Zero();
|
|
|
|
if(!copy.BEncode(&buf))
|
2019-01-26 12:33:34 +00:00
|
|
|
{
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
2019-01-26 12:33:34 +00:00
|
|
|
}
|
2019-01-11 00:12:43 +00:00
|
|
|
// rewind and resize buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
if(!A.Verify(crypto, buf, Z))
|
2019-01-26 12:33:34 +00:00
|
|
|
{
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
2019-01-26 12:33:34 +00:00
|
|
|
}
|
2019-01-11 00:12:43 +00:00
|
|
|
// validate PoW
|
2019-01-26 15:40:58 +00:00
|
|
|
using namespace std::placeholders;
|
|
|
|
if(W && !W->IsValid(std::bind(&Crypto::shorthash, crypto, _1, _2), now))
|
2019-01-26 12:33:34 +00:00
|
|
|
{
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
2019-01-26 12:33:34 +00:00
|
|
|
}
|
2019-01-11 00:12:43 +00:00
|
|
|
// valid timestamps
|
|
|
|
// add max clock skew
|
|
|
|
now += MAX_INTROSET_TIME_DELTA;
|
|
|
|
for(const auto& intro : I)
|
|
|
|
{
|
|
|
|
if(intro.expiresAt > now
|
2019-04-05 14:58:22 +00:00
|
|
|
&& intro.expiresAt - now > path::default_lifetime)
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-04-05 14:58:22 +00:00
|
|
|
if(W
|
|
|
|
&& intro.expiresAt - W->extendedLifetime > path::default_lifetime)
|
2019-04-22 17:55:04 +00:00
|
|
|
{
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
2019-04-22 17:55:04 +00:00
|
|
|
}
|
2019-04-23 09:25:03 +00:00
|
|
|
else if(!W.has_value())
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-04-22 18:35:19 +00:00
|
|
|
LogWarn("intro has too high expire time");
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(IsExpired(now))
|
|
|
|
{
|
2019-04-22 18:35:19 +00:00
|
|
|
LogWarn("introset expired: ", *this);
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp_time_t
|
|
|
|
IntroSet::GetNewestIntroExpiration() const
|
|
|
|
{
|
|
|
|
llarp_time_t t = 0;
|
|
|
|
for(const auto& intro : I)
|
|
|
|
t = std::max(intro.expiresAt, t);
|
|
|
|
return t;
|
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
IntroSet::print(std::ostream& stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printAttribute("A", A);
|
|
|
|
printer.printAttribute("I", I);
|
|
|
|
printer.printAttribute("K", K);
|
|
|
|
|
|
|
|
std::string _topic = topic.ToString();
|
|
|
|
|
|
|
|
if(!_topic.empty())
|
|
|
|
{
|
|
|
|
printer.printAttribute("topic", _topic);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printer.printAttribute("topic", topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
printer.printAttribute("T", T);
|
2019-04-23 09:25:03 +00:00
|
|
|
if(W)
|
|
|
|
{
|
|
|
|
printer.printAttribute("W", W.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printer.printAttribute("W", "NULL");
|
|
|
|
}
|
2019-02-24 23:46:37 +00:00
|
|
|
printer.printAttribute("V", version);
|
|
|
|
printer.printAttribute("Z", Z);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
2019-01-11 00:12:43 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|