Reset server time difference after a "MessageId is too high" error.

This commit is contained in:
levlam 2023-05-14 21:58:54 +03:00
parent edf0aaaa2a
commit c9f83caf9f
12 changed files with 20 additions and 18 deletions

View File

@ -203,7 +203,7 @@ class AuthData {
// server_time_difference >= max{diff} // server_time_difference >= max{diff}
bool update_server_time_difference(double diff); bool update_server_time_difference(double diff);
void set_server_time_difference(double diff) { void reset_server_time_difference(double diff) {
server_time_difference_was_updated_ = false; server_time_difference_was_updated_ = false;
server_time_difference_ = diff; server_time_difference_ = diff;
} }

View File

@ -119,7 +119,7 @@ class PingConnectionPingPong final
} }
void on_server_salt_updated() final { void on_server_salt_updated() final {
} }
void on_server_time_difference_updated() final { void on_server_time_difference_updated(bool force) final {
} }
void on_session_created(uint64 unique_id, uint64 first_id) final { void on_session_created(uint64 unique_id, uint64 first_id) final {

View File

@ -330,6 +330,8 @@ Status SessionConnection::on_packet(const MsgInfo &info,
LOG(WARNING) << bad_info << ": MessageId is too high. Session will be closed"; LOG(WARNING) << bad_info << ": MessageId is too high. Session will be closed";
// All this queries will be re-sent by parent // All this queries will be re-sent by parent
to_send_.clear(); to_send_.clear();
auth_data_->reset_server_time_difference(static_cast<uint32>(info.message_id >> 32) - Time::now());
callback_->on_server_time_difference_updated(true);
callback_->on_session_failed(Status::Error("MessageId is too high")); callback_->on_session_failed(Status::Error("MessageId is too high"));
return Status::Error("MessageId is too high"); return Status::Error("MessageId is too high");
} }
@ -692,7 +694,7 @@ Status SessionConnection::on_raw_packet(const PacketInfo &info, BufferSlice pack
auto status = auto status =
auth_data_->check_packet(info.session_id, info.message_id, Time::now_cached(), time_difference_was_updated); auth_data_->check_packet(info.session_id, info.message_id, Time::now_cached(), time_difference_was_updated);
if (time_difference_was_updated) { if (time_difference_was_updated) {
callback_->on_server_time_difference_updated(); callback_->on_server_time_difference_updated(false);
} }
if (status.is_error()) { if (status.is_error()) {
if (status.code() == 1) { if (status.code() == 1) {

View File

@ -106,7 +106,7 @@ class SessionConnection final
virtual void on_auth_key_updated() = 0; virtual void on_auth_key_updated() = 0;
virtual void on_tmp_auth_key_updated() = 0; virtual void on_tmp_auth_key_updated() = 0;
virtual void on_server_salt_updated() = 0; virtual void on_server_salt_updated() = 0;
virtual void on_server_time_difference_updated() = 0; virtual void on_server_time_difference_updated(bool force) = 0;
virtual void on_session_created(uint64 unique_id, uint64 first_id) = 0; virtual void on_session_created(uint64 unique_id, uint64 first_id) = 0;
virtual void on_session_failed(Status status) = 0; virtual void on_session_failed(Status status) = 0;

View File

@ -459,8 +459,8 @@ static ActorOwn<> get_full_config(DcOption option, Promise<tl_object_ptr<telegra
//notify(); //notify();
} }
void update_server_time_difference(double diff) final { void update_server_time_difference(double diff, bool force) final {
G()->update_server_time_difference(diff); G()->update_server_time_difference(diff, force);
} }
double get_server_time_difference() final { double get_server_time_difference() final {
return G()->get_server_time_difference(); return G()->get_server_time_difference();

View File

@ -185,8 +185,8 @@ int32 Global::to_unix_time(double server_time) const {
return static_cast<int32>(server_time); return static_cast<int32>(server_time);
} }
void Global::update_server_time_difference(double diff) { void Global::update_server_time_difference(double diff, bool force) {
if (!server_time_difference_was_updated_ || server_time_difference_ < diff) { if (force || !server_time_difference_was_updated_ || server_time_difference_ < diff) {
server_time_difference_ = diff; server_time_difference_ = diff;
server_time_difference_was_updated_ = true; server_time_difference_was_updated_ = true;
do_save_server_time_difference(); do_save_server_time_difference();

View File

@ -159,7 +159,7 @@ class Global final : public ActorContext {
return to_unix_time(server_time_cached()); return to_unix_time(server_time_cached());
} }
void update_server_time_difference(double diff); void update_server_time_difference(double diff, bool force);
void save_server_time(); void save_server_time();

View File

@ -51,8 +51,8 @@ class AuthDataSharedImpl final : public AuthDataShared {
} }
// TODO: extract it from G() // TODO: extract it from G()
void update_server_time_difference(double diff) final { void update_server_time_difference(double diff, bool force) final {
G()->update_server_time_difference(diff); G()->update_server_time_difference(diff, force);
} }
double get_server_time_difference() final { double get_server_time_difference() final {

View File

@ -37,7 +37,7 @@ class AuthDataShared {
virtual const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() = 0; virtual const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() = 0;
virtual mtproto::AuthKey get_auth_key() = 0; virtual mtproto::AuthKey get_auth_key() = 0;
virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0; virtual void set_auth_key(const mtproto::AuthKey &auth_key) = 0;
virtual void update_server_time_difference(double diff) = 0; virtual void update_server_time_difference(double diff, bool force) = 0;
virtual double get_server_time_difference() = 0; virtual double get_server_time_difference() = 0;
virtual void add_auth_key_listener(unique_ptr<Listener> listener) = 0; virtual void add_auth_key_listener(unique_ptr<Listener> listener) = 0;

View File

@ -250,7 +250,7 @@ Session::Session(unique_ptr<Callback> callback, std::shared_ptr<AuthDataShared>
auth_data_.set_use_pfs(use_pfs); auth_data_.set_use_pfs(use_pfs);
auth_data_.set_main_auth_key(shared_auth_data_->get_auth_key()); auth_data_.set_main_auth_key(shared_auth_data_->get_auth_key());
// auth_data_.break_main_auth_key(); // auth_data_.break_main_auth_key();
auth_data_.set_server_time_difference(shared_auth_data_->get_server_time_difference()); auth_data_.reset_server_time_difference(shared_auth_data_->get_server_time_difference());
auto now = Time::now(); auto now = Time::now();
auth_data_.set_future_salts(shared_auth_data_->get_future_salts(), now); auth_data_.set_future_salts(shared_auth_data_->get_future_salts(), now);
if (use_pfs && !tmp_auth_key.empty()) { if (use_pfs && !tmp_auth_key.empty()) {
@ -612,8 +612,8 @@ void Session::on_server_salt_updated() {
shared_auth_data_->set_future_salts(auth_data_.get_future_salts()); shared_auth_data_->set_future_salts(auth_data_.get_future_salts());
} }
void Session::on_server_time_difference_updated() { void Session::on_server_time_difference_updated(bool force) {
shared_auth_data_->update_server_time_difference(auth_data_.get_server_time_difference()); shared_auth_data_->update_server_time_difference(auth_data_.get_server_time_difference(), force);
} }
void Session::on_closed(Status status) { void Session::on_closed(Status status) {
@ -1434,7 +1434,7 @@ void Session::on_handshake_ready(Result<unique_ptr<mtproto::AuthKeyHandshake>> r
on_server_salt_updated(); on_server_salt_updated();
} }
if (auth_data_.update_server_time_difference(handshake->get_server_time_diff())) { if (auth_data_.update_server_time_difference(handshake->get_server_time_diff())) {
on_server_time_difference_updated(); on_server_time_difference_updated(true);
} }
} }
} }

View File

@ -207,7 +207,7 @@ class Session final
void on_auth_key_updated() final; void on_auth_key_updated() final;
void on_tmp_auth_key_updated() final; void on_tmp_auth_key_updated() final;
void on_server_salt_updated() final; void on_server_salt_updated() final;
void on_server_time_difference_updated() final; void on_server_time_difference_updated(bool force) final;
void on_session_created(uint64 unique_id, uint64 first_message_id) final; void on_session_created(uint64 unique_id, uint64 first_message_id) final;
void on_session_failed(Status status) final; void on_session_failed(Status status) final;

View File

@ -604,7 +604,7 @@ class FastPingTestActor final : public td::Actor {
if (iteration_ % 2 == 0) { if (iteration_ % 2 == 0) {
auth_data = td::make_unique<td::mtproto::AuthData>(); auth_data = td::make_unique<td::mtproto::AuthData>();
auth_data->set_tmp_auth_key(handshake_->get_auth_key()); auth_data->set_tmp_auth_key(handshake_->get_auth_key());
auth_data->set_server_time_difference(handshake_->get_server_time_diff()); auth_data->reset_server_time_difference(handshake_->get_server_time_diff());
auth_data->set_server_salt(handshake_->get_server_salt(), td::Time::now()); auth_data->set_server_salt(handshake_->get_server_salt(), td::Time::now());
auth_data->set_future_salts({td::mtproto::ServerSalt{0u, 1e20, 1e30}}, td::Time::now()); auth_data->set_future_salts({td::mtproto::ServerSalt{0u, 1e20, 1e30}}, td::Time::now());
auth_data->set_use_pfs(true); auth_data->set_use_pfs(true);