2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/mtproto/AuthKey.h"
|
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2021-08-22 21:08:46 +02:00
|
|
|
#include <array>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
namespace mtproto {
|
|
|
|
|
|
|
|
struct ServerSalt {
|
|
|
|
int64 salt;
|
|
|
|
double valid_since;
|
|
|
|
double valid_until;
|
|
|
|
};
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(const ServerSalt &salt, StorerT &storer) {
|
|
|
|
storer.template store_binary<int64>(salt.salt);
|
|
|
|
storer.template store_binary<double>(salt.valid_since);
|
|
|
|
storer.template store_binary<double>(salt.valid_until);
|
|
|
|
}
|
2018-02-12 10:01:11 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(ServerSalt &salt, ParserT &parser) {
|
|
|
|
salt.salt = parser.fetch_long();
|
|
|
|
salt.valid_since = parser.fetch_double();
|
|
|
|
salt.valid_until = parser.fetch_double();
|
|
|
|
}
|
|
|
|
|
2021-08-22 21:08:46 +02:00
|
|
|
Status check_message_id_duplicates(int64 *saved_message_ids, size_t max_size, size_t &end_pos, int64 message_id);
|
|
|
|
|
|
|
|
template <size_t max_size>
|
2018-12-31 20:04:05 +01:00
|
|
|
class MessageIdDuplicateChecker {
|
|
|
|
public:
|
2021-08-22 21:08:46 +02:00
|
|
|
Status check(int64 message_id) {
|
|
|
|
return check_message_id_duplicates(&saved_message_ids_[0], max_size, end_pos_, message_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2021-08-22 21:08:46 +02:00
|
|
|
std::array<int64, 2 * max_size> saved_message_ids_;
|
|
|
|
size_t end_pos_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class AuthData {
|
|
|
|
public:
|
2018-02-12 10:28:14 +01:00
|
|
|
AuthData();
|
2019-05-06 20:53:39 +02:00
|
|
|
AuthData(const AuthData &) = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
AuthData &operator=(const AuthData &) = delete;
|
2018-02-12 10:28:14 +01:00
|
|
|
AuthData(AuthData &&) = delete;
|
|
|
|
AuthData &operator=(AuthData &&) = delete;
|
|
|
|
~AuthData() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
bool is_ready(double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_main_auth_key(AuthKey auth_key) {
|
|
|
|
main_auth_key_ = std::move(auth_key);
|
|
|
|
}
|
2020-01-07 13:42:04 +01:00
|
|
|
void break_main_auth_key() {
|
|
|
|
main_auth_key_.break_key();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
const AuthKey &get_main_auth_key() const {
|
|
|
|
// CHECK(has_main_auth_key());
|
|
|
|
return main_auth_key_;
|
|
|
|
}
|
|
|
|
bool has_main_auth_key() const {
|
|
|
|
return !main_auth_key_.empty();
|
|
|
|
}
|
|
|
|
bool need_main_auth_key() const {
|
|
|
|
return !has_main_auth_key();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_tmp_auth_key(AuthKey auth_key) {
|
|
|
|
CHECK(!auth_key.empty());
|
|
|
|
tmp_auth_key_ = std::move(auth_key);
|
|
|
|
}
|
|
|
|
const AuthKey &get_tmp_auth_key() const {
|
|
|
|
// CHECK(has_tmp_auth_key());
|
|
|
|
return tmp_auth_key_;
|
|
|
|
}
|
|
|
|
bool was_tmp_auth_key() const {
|
|
|
|
return use_pfs() && !tmp_auth_key_.empty();
|
|
|
|
}
|
|
|
|
bool need_tmp_auth_key(double now) const {
|
|
|
|
if (!use_pfs()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tmp_auth_key_.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
if (now > tmp_auth_key_.expires_at() - 60 * 60 * 2 /*2 hours*/) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!has_tmp_auth_key(now)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void drop_main_auth_key() {
|
|
|
|
main_auth_key_ = AuthKey();
|
|
|
|
}
|
|
|
|
void drop_tmp_auth_key() {
|
|
|
|
tmp_auth_key_ = AuthKey();
|
|
|
|
}
|
|
|
|
bool has_tmp_auth_key(double now) const {
|
|
|
|
if (!use_pfs()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tmp_auth_key_.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
if (now > tmp_auth_key_.expires_at() - 60 * 60 /*1 hour*/) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const AuthKey &get_auth_key() const {
|
|
|
|
if (use_pfs()) {
|
|
|
|
return get_tmp_auth_key();
|
|
|
|
}
|
|
|
|
return get_main_auth_key();
|
|
|
|
}
|
|
|
|
bool has_auth_key(double now) const {
|
|
|
|
if (use_pfs()) {
|
|
|
|
return has_tmp_auth_key(now);
|
|
|
|
}
|
|
|
|
return has_main_auth_key();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_auth_flag() const {
|
|
|
|
return main_auth_key_.auth_flag();
|
|
|
|
}
|
|
|
|
void set_auth_flag(bool auth_flag) {
|
|
|
|
main_auth_key_.set_auth_flag(auth_flag);
|
|
|
|
if (!auth_flag) {
|
2017-12-29 21:34:39 +01:00
|
|
|
drop_tmp_auth_key();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_bind_flag() const {
|
|
|
|
return !use_pfs() || tmp_auth_key_.auth_flag();
|
|
|
|
}
|
|
|
|
void on_bind() {
|
|
|
|
CHECK(use_pfs());
|
|
|
|
tmp_auth_key_.set_auth_flag(true);
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:44:29 +01:00
|
|
|
Slice get_header() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (use_pfs()) {
|
|
|
|
return tmp_auth_key_.need_header() ? Slice(header_) : Slice();
|
|
|
|
} else {
|
|
|
|
return main_auth_key_.need_header() ? Slice(header_) : Slice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void set_header(std::string header) {
|
|
|
|
header_ = std::move(header);
|
|
|
|
}
|
|
|
|
void on_api_response() {
|
|
|
|
if (use_pfs()) {
|
|
|
|
if (tmp_auth_key_.auth_flag()) {
|
|
|
|
tmp_auth_key_.set_need_header(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (main_auth_key_.auth_flag()) {
|
|
|
|
main_auth_key_.set_need_header(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:44:29 +01:00
|
|
|
void set_session_id(uint64 session_id) {
|
|
|
|
session_id_ = session_id;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
uint64 get_session_id() const {
|
2019-02-04 16:44:29 +01:00
|
|
|
CHECK(session_id_ != 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
return session_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_server_time(double now) const {
|
|
|
|
return server_time_difference_ + now;
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_server_time_difference() const {
|
|
|
|
return server_time_difference_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// diff == msg_id / 2^32 - now == old_server_now - now <= server_now - now
|
|
|
|
// server_time_difference >= max{diff}
|
2018-02-12 10:28:14 +01:00
|
|
|
bool update_server_time_difference(double diff);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void set_server_time_difference(double diff) {
|
|
|
|
server_time_difference_was_updated_ = false;
|
|
|
|
server_time_difference_ = diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 get_server_salt(double now) {
|
|
|
|
update_salt(now);
|
|
|
|
return server_salt_.salt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_server_salt(uint64 salt, double now) {
|
|
|
|
server_salt_.salt = salt;
|
|
|
|
double server_time = get_server_time(now);
|
|
|
|
server_salt_.valid_since = server_time;
|
|
|
|
server_salt_.valid_until = server_time + 60 * 10;
|
|
|
|
future_salts_.clear();
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:44:29 +01:00
|
|
|
bool is_server_salt_valid(double now) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return server_salt_.valid_until > get_server_time(now) + 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_salt(double now) {
|
|
|
|
update_salt(now);
|
|
|
|
return is_server_salt_valid(now);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_future_salts(double now) {
|
|
|
|
update_salt(now);
|
|
|
|
return future_salts_.empty() || !is_server_salt_valid(now);
|
|
|
|
}
|
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
void set_future_salts(const std::vector<ServerSalt> &salts, double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
std::vector<ServerSalt> get_future_salts() const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
int64 next_message_id(double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-04 16:44:29 +01:00
|
|
|
bool is_valid_outbound_msg_id(int64 id, double now) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-04 16:44:29 +01:00
|
|
|
bool is_valid_inbound_msg_id(int64 id, double now) const;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
Status check_packet(int64 session_id, int64 message_id, double now, bool &time_difference_was_updated);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Status check_update(int64 message_id) {
|
|
|
|
return updates_duplicate_checker_.check(message_id);
|
|
|
|
}
|
|
|
|
|
2021-08-22 23:08:05 +02:00
|
|
|
Status recheck_update(int64 message_id) {
|
|
|
|
return updates_duplicate_rechecker_.check(message_id);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 next_seq_no(bool is_content_related) {
|
|
|
|
int32 res = seq_no_;
|
|
|
|
if (is_content_related) {
|
|
|
|
res |= 1;
|
|
|
|
seq_no_ += 2;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_seq_no() {
|
|
|
|
seq_no_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_use_pfs(bool use_pfs) {
|
|
|
|
use_pfs_ = use_pfs;
|
|
|
|
}
|
|
|
|
bool use_pfs() const {
|
|
|
|
return use_pfs_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool use_pfs_ = true;
|
|
|
|
AuthKey main_auth_key_;
|
|
|
|
AuthKey tmp_auth_key_;
|
|
|
|
bool server_time_difference_was_updated_ = false;
|
|
|
|
double server_time_difference_ = 0;
|
|
|
|
ServerSalt server_salt_;
|
|
|
|
int64 last_message_id_ = 0;
|
|
|
|
int32 seq_no_ = 0;
|
|
|
|
std::string header_;
|
2019-02-04 16:44:29 +01:00
|
|
|
uint64 session_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::vector<ServerSalt> future_salts_;
|
|
|
|
|
2021-08-22 21:08:46 +02:00
|
|
|
MessageIdDuplicateChecker<1000> duplicate_checker_;
|
|
|
|
MessageIdDuplicateChecker<1000> updates_duplicate_checker_;
|
2021-08-22 23:08:05 +02:00
|
|
|
MessageIdDuplicateChecker<100> updates_duplicate_rechecker_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-12 10:28:14 +01:00
|
|
|
void update_salt(double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mtproto
|
|
|
|
} // namespace td
|