diff --git a/td/mtproto/AuthData.cpp b/td/mtproto/AuthData.cpp index c34e0c16b..3bb0049cf 100644 --- a/td/mtproto/AuthData.cpp +++ b/td/mtproto/AuthData.cpp @@ -110,12 +110,13 @@ int64 AuthData::next_message_id(double now) { return result; } -bool AuthData::is_valid_outbound_msg_id(int64 id, double now) { +bool AuthData::is_valid_outbound_msg_id(int64 id, double now) const { double server_time = get_server_time(now); auto id_time = static_cast(id / (1ll << 32)); return server_time - 300 / 2 < id_time && id_time < server_time + 60 / 2; } -bool AuthData::is_valid_inbound_msg_id(int64 id, double now) { + +bool AuthData::is_valid_inbound_msg_id(int64 id, double now) const { double server_time = get_server_time(now); auto id_time = static_cast(id / (1ll << 32)); return server_time - 300 < id_time && id_time < server_time + 30; diff --git a/td/mtproto/AuthData.h b/td/mtproto/AuthData.h index 334a8e4c2..bf1f50d9c 100644 --- a/td/mtproto/AuthData.h +++ b/td/mtproto/AuthData.h @@ -57,7 +57,6 @@ class AuthData { bool is_ready(double now); - uint64 session_id_; void set_main_auth_key(AuthKey auth_key) { main_auth_key_ = std::move(auth_key); } @@ -148,7 +147,7 @@ class AuthData { tmp_auth_key_.set_auth_flag(true); } - Slice get_header() { + Slice get_header() const { if (use_pfs()) { return tmp_auth_key_.need_header() ? Slice(header_) : Slice(); } else { @@ -170,7 +169,11 @@ class AuthData { } } + void set_session_id(uint64 session_id) { + session_id_ = session_id; + } uint64 get_session_id() const { + CHECK(session_id_ != 0); return session_id_; } @@ -204,7 +207,7 @@ class AuthData { future_salts_.clear(); } - bool is_server_salt_valid(double now) { + bool is_server_salt_valid(double now) const { return server_salt_.valid_until > get_server_time(now) + 60; } @@ -224,9 +227,9 @@ class AuthData { int64 next_message_id(double now); - bool is_valid_outbound_msg_id(int64 id, double now); + bool is_valid_outbound_msg_id(int64 id, double now) const; - bool is_valid_inbound_msg_id(int64 id, double now); + bool is_valid_inbound_msg_id(int64 id, double now) const; Status check_packet(int64 session_id, int64 message_id, double now, bool &time_difference_was_updated); @@ -264,6 +267,7 @@ class AuthData { int64 last_message_id_ = 0; int32 seq_no_ = 0; std::string header_; + uint64 session_id_ = 0; std::vector future_salts_; diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index f835e2503..9ddc54fd8 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -499,7 +499,7 @@ Status SessionConnection::on_slice_packet(const MsgInfo &info, Slice packet) { return Status::OK(); } else { VLOG(mtproto) << "Got update from " << get_name() << " created in " << (Time::now() - created_at_) - << " in container " << container_id_ << " from session " << auth_data_->session_id_ + << " in container " << container_id_ << " from session " << auth_data_->get_session_id() << " with message_id " << info.message_id << ", main_message_id = " << main_message_id_ << ", seq_no = " << info.seq_no << " and original size " << info.size; return callback_->on_message_result_ok(0, as_buffer_slice(packet), info.size); @@ -789,7 +789,7 @@ void SessionConnection::destroy_key() { std::pair SessionConnection::encrypted_bind(int64 perm_key, int64 nonce, int32 expire_at) { int64 temp_key = auth_data_->get_tmp_auth_key().id(); - mtproto_api::bind_auth_key_inner object(nonce, temp_key, perm_key, auth_data_->session_id_, expire_at); + mtproto_api::bind_auth_key_inner object(nonce, temp_key, perm_key, auth_data_->get_session_id(), expire_at); auto object_storer = create_storer(object); auto size = object_storer.size(); auto object_packet = BufferWriter{size, 0, 0}; diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index 8fa5ed87a..14c33dc4f 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -115,7 +115,6 @@ Session::Session(unique_ptr callback, std::shared_ptr bool is_main, bool use_pfs, bool is_cdn, bool need_destroy, const mtproto::AuthKey &tmp_auth_key, std::vector server_salts) : dc_id_(dc_id), is_main_(is_main), is_cdn_(is_cdn) { - LOG(INFO) << "Open session: "; VLOG(dc) << "Start connection"; need_destroy_ = need_destroy; if (need_destroy) { @@ -134,7 +133,7 @@ Session::Session(unique_ptr callback, std::shared_ptr } uint64 session_id = 0; Random::secure_bytes(reinterpret_cast(&session_id), sizeof(session_id)); - auth_data_.session_id_ = session_id; + auth_data_.set_session_id(session_id); LOG(WARNING) << "Generate new session_id " << session_id << " for " << (use_pfs ? "temp " : "") << (is_cdn ? "CDN " : "") << "auth key " << auth_data_.get_auth_key().id() << " for DC" << dc_id; @@ -222,7 +221,7 @@ void Session::send(NetQueryPtr &&query) { last_activity_timestamp_ = Time::now(); query->debug("Session: received from SessionProxy"); - query->set_session_id(auth_data_.session_id_); + query->set_session_id(auth_data_.get_session_id()); VLOG(net_query) << "got query " << query; if (query->update_is_ready()) { return_query(std::move(query)); @@ -657,7 +656,7 @@ void Session::on_message_result_error(uint64 id, int error_code, BufferSlice mes // TODO: some errors shouldn't cause loss of authorizations. Especially when PFS will be used if (error_code == 401 && message.as_slice() != CSlice("SESSION_PASSWORD_NEEDED")) { if (auth_data_.use_pfs() && message.as_slice() == CSlice("AUTH_KEY_PERM_EMPTY")) { - LOG(ERROR) << "Receive AUTH_KEY_PERM_EMPTY in session " << auth_data_.session_id_ << " for auth key " + LOG(ERROR) << "Receive AUTH_KEY_PERM_EMPTY in session " << auth_data_.get_session_id() << " for auth key " << auth_data_.get_tmp_auth_key().id(); auth_data_.drop_tmp_auth_key(); on_tmp_auth_key_updated(); @@ -822,7 +821,7 @@ void Session::connection_send_query(ConnectionInfo *info, NetQueryPtr &&net_quer NetQueryRef invoke_after = net_query->invoke_after(); if (!invoke_after.empty()) { invoke_after_id = invoke_after->message_id(); - if (invoke_after->session_id() != auth_data_.session_id_ || invoke_after_id == 0) { + if (invoke_after->session_id() != auth_data_.get_session_id() || invoke_after_id == 0) { net_query->set_error_resend_invoke_after(); return return_query(std::move(net_query)); } @@ -1061,7 +1060,7 @@ void Session::on_handshake_ready(Result> r auth_data_.set_tmp_auth_key(std::move(handshake->auth_key)); on_tmp_auth_key_updated(); } - LOG(WARNING) << "Update auth key in session_id " << auth_data_.session_id_ << " to " + LOG(WARNING) << "Update auth key in session_id " << auth_data_.get_session_id() << " to " << auth_data_.get_auth_key().id(); connection_close(&main_connection_); connection_close(&long_poll_connection_);