Skip to content

Commit

Permalink
Don't drop recently valid auth keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Apr 13, 2021
1 parent f8d6d0e commit a78fe2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions td/telegram/net/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Session::Session(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared>
auth_data_.set_header(G()->mtproto_header().get_default_header().str());
}
last_activity_timestamp_ = Time::now();
last_success_timestamp_ = Time::now() - 366 * 86400;
}

bool Session::can_destroy_auth_key() const {
Expand Down Expand Up @@ -273,8 +274,9 @@ void Session::on_bind_result(NetQueryPtr query) {
if (query->is_error()) {
status = std::move(query->error());
if (status.code() == 400 && status.message() == "ENCRYPTED_MESSAGE_INVALID") {
bool has_immunity =
!G()->is_server_time_reliable() || G()->server_time() - auth_data_.get_main_auth_key().created_at() < 60;
auto auth_key_age = G()->server_time() - auth_data_.get_main_auth_key().created_at();
bool has_immunity = !G()->is_server_time_reliable() || auth_key_age < 60 ||
(auth_key_age > 86400 && last_success_timestamp_ > Time::now() - 86400);
if (!use_pfs_) {
if (has_immunity) {
LOG(WARNING) << "Do not drop main key, because it was created too recently";
Expand Down Expand Up @@ -559,8 +561,8 @@ void Session::on_closed(Status status) {

void Session::on_session_created(uint64 unique_id, uint64 first_id) {
// TODO: use unique_id
// send updatesTooLong to force getDifference
LOG(INFO) << "New session " << unique_id << " created with first message_id " << first_id;
last_success_timestamp_ = Time::now();
if (is_main_) {
LOG(DEBUG) << "Sending updatesTooLong to force getDifference";
BufferSlice packet(4);
Expand Down Expand Up @@ -711,9 +713,11 @@ Status Session::on_message_result_ok(uint64 id, BufferSlice packet, size_t origi
if (is_cdn_) {
return Status::Error("Got update from CDN connection");
}
last_success_timestamp_ = Time::now();
return_query(G()->net_query_creator().create_update(std::move(packet)));
return Status::OK();
}
last_success_timestamp_ = Time::now();

TlParser parser(packet.as_slice());
int32 ID = parser.fetch_int();
Expand Down
1 change: 1 addition & 0 deletions td/telegram/net/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class Session final
uint64 last_bind_query_id_ = 0;
uint64 last_check_query_id_ = 0;
double last_activity_timestamp_ = 0;
double last_success_timestamp_ = 0; // time when auth_key and Session definitely was valid
size_t dropped_size_ = 0;

std::unordered_set<uint64> unknown_queries_;
Expand Down

0 comments on commit a78fe2d

Please sign in to comment.