2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/DeviceTokenManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/misc.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/td_api.hpp"
|
2021-09-03 11:27:59 +02:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2021-09-03 11:27:59 +02:00
|
|
|
#include "td/telegram/UserId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-01-31 12:26:06 +01:00
|
|
|
#include "td/mtproto/DhHandshake.h"
|
2019-01-31 01:20:27 +01:00
|
|
|
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
#include "td/utils/algorithm.h"
|
2018-01-19 16:04:16 +01:00
|
|
|
#include "td/utils/base64.h"
|
2018-03-06 17:27:52 +01:00
|
|
|
#include "td/utils/buffer.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
2018-01-19 16:04:16 +01:00
|
|
|
#include "td/utils/JsonBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2022-04-12 15:21:42 +02:00
|
|
|
#include "td/utils/misc.h"
|
2018-12-24 16:33:39 +01:00
|
|
|
#include "td/utils/Random.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
2018-01-10 01:18:03 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace td {
|
2018-01-10 01:18:03 +01:00
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void DeviceTokenManager::TokenInfo::store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
bool has_other_user_ids = !other_user_ids.empty();
|
|
|
|
bool is_sync = state == State::Sync;
|
|
|
|
bool is_unregister = state == State::Unregister;
|
|
|
|
bool is_register = state == State::Register;
|
2020-01-01 17:38:54 +01:00
|
|
|
CHECK(state != State::Reregister);
|
2018-01-10 01:18:03 +01:00
|
|
|
BEGIN_STORE_FLAGS();
|
2021-09-03 11:27:59 +02:00
|
|
|
STORE_FLAG(false);
|
2018-01-10 01:18:03 +01:00
|
|
|
STORE_FLAG(is_sync);
|
|
|
|
STORE_FLAG(is_unregister);
|
|
|
|
STORE_FLAG(is_register);
|
2018-01-10 13:11:35 +01:00
|
|
|
STORE_FLAG(is_app_sandbox);
|
2018-12-24 16:33:39 +01:00
|
|
|
STORE_FLAG(encrypt);
|
2021-09-03 11:27:59 +02:00
|
|
|
STORE_FLAG(has_other_user_ids);
|
2018-01-10 01:18:03 +01:00
|
|
|
END_STORE_FLAGS();
|
|
|
|
store(token, storer);
|
|
|
|
if (has_other_user_ids) {
|
|
|
|
store(other_user_ids, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-24 16:33:39 +01:00
|
|
|
if (encrypt) {
|
|
|
|
store(encryption_key, storer);
|
2018-12-28 23:48:32 +01:00
|
|
|
store(encryption_key_id, storer);
|
2018-12-24 16:33:39 +01:00
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-10 01:18:03 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void DeviceTokenManager::TokenInfo::parse(ParserT &parser) {
|
|
|
|
using td::parse;
|
2021-09-03 11:27:59 +02:00
|
|
|
bool has_other_user_ids_legacy;
|
2018-01-10 01:18:03 +01:00
|
|
|
bool has_other_user_ids;
|
|
|
|
bool is_sync;
|
|
|
|
bool is_unregister;
|
|
|
|
bool is_register;
|
|
|
|
BEGIN_PARSE_FLAGS();
|
2021-09-03 11:27:59 +02:00
|
|
|
PARSE_FLAG(has_other_user_ids_legacy);
|
2018-01-10 01:18:03 +01:00
|
|
|
PARSE_FLAG(is_sync);
|
|
|
|
PARSE_FLAG(is_unregister);
|
|
|
|
PARSE_FLAG(is_register);
|
2018-01-10 13:11:35 +01:00
|
|
|
PARSE_FLAG(is_app_sandbox);
|
2018-12-24 16:33:39 +01:00
|
|
|
PARSE_FLAG(encrypt);
|
2021-09-03 11:27:59 +02:00
|
|
|
PARSE_FLAG(has_other_user_ids);
|
2019-08-01 02:40:28 +02:00
|
|
|
END_PARSE_FLAGS();
|
2018-01-10 01:18:03 +01:00
|
|
|
CHECK(is_sync + is_unregister + is_register == 1);
|
|
|
|
if (is_sync) {
|
|
|
|
state = State::Sync;
|
|
|
|
} else if (is_unregister) {
|
|
|
|
state = State::Unregister;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-01-10 01:18:03 +01:00
|
|
|
state = State::Register;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
parse(token, parser);
|
2021-09-03 11:27:59 +02:00
|
|
|
if (has_other_user_ids_legacy) {
|
|
|
|
vector<int32> other_user_ids_legacy;
|
|
|
|
parse(other_user_ids_legacy, parser);
|
|
|
|
other_user_ids = transform(other_user_ids_legacy, [](int32 user_id) { return static_cast<int64>(user_id); });
|
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
if (has_other_user_ids) {
|
|
|
|
parse(other_user_ids, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-12-24 16:33:39 +01:00
|
|
|
if (encrypt) {
|
|
|
|
parse(encryption_key, parser);
|
2018-12-28 23:48:32 +01:00
|
|
|
parse(encryption_key_id, parser);
|
2018-12-24 16:33:39 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-01 17:38:54 +01:00
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const DeviceTokenManager::TokenInfo::State &state) {
|
|
|
|
switch (state) {
|
2018-01-10 01:18:03 +01:00
|
|
|
case DeviceTokenManager::TokenInfo::State::Sync:
|
2020-01-01 17:38:54 +01:00
|
|
|
return string_builder << "Synchronized";
|
2018-01-10 01:18:03 +01:00
|
|
|
case DeviceTokenManager::TokenInfo::State::Unregister:
|
2020-01-01 17:38:54 +01:00
|
|
|
return string_builder << "Unregister";
|
2018-01-10 01:18:03 +01:00
|
|
|
case DeviceTokenManager::TokenInfo::State::Register:
|
2020-01-01 17:38:54 +01:00
|
|
|
return string_builder << "Register";
|
|
|
|
case DeviceTokenManager::TokenInfo::State::Reregister:
|
|
|
|
return string_builder << "Reregister";
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2020-01-01 17:38:54 +01:00
|
|
|
return string_builder;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-01 17:38:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const DeviceTokenManager::TokenInfo &token_info) {
|
|
|
|
string_builder << token_info.state << " token \"" << format::escaped(token_info.token) << "\"";
|
2018-01-10 01:18:03 +01:00
|
|
|
if (!token_info.other_user_ids.empty()) {
|
|
|
|
string_builder << ", with other users " << token_info.other_user_ids;
|
|
|
|
}
|
2018-01-10 13:11:35 +01:00
|
|
|
if (token_info.is_app_sandbox) {
|
|
|
|
string_builder << ", sandboxed";
|
|
|
|
}
|
2018-12-24 16:33:39 +01:00
|
|
|
if (token_info.encrypt) {
|
2019-12-30 02:34:59 +01:00
|
|
|
string_builder << ", encrypted with ID " << token_info.encryption_key_id;
|
2018-12-24 16:33:39 +01:00
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
return string_builder;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
|
|
|
|
void DeviceTokenManager::register_device(tl_object_ptr<td_api::DeviceToken> device_token_ptr,
|
2021-10-19 17:11:16 +02:00
|
|
|
const vector<UserId> &other_user_ids,
|
2018-12-28 23:48:32 +01:00
|
|
|
Promise<td_api::object_ptr<td_api::pushReceiverId>> promise) {
|
2018-01-10 01:18:03 +01:00
|
|
|
CHECK(device_token_ptr != nullptr);
|
|
|
|
TokenType token_type;
|
|
|
|
string token;
|
2018-01-10 13:11:35 +01:00
|
|
|
bool is_app_sandbox = false;
|
2018-12-24 16:33:39 +01:00
|
|
|
bool encrypt = false;
|
2018-01-10 01:18:03 +01:00
|
|
|
switch (device_token_ptr->get_id()) {
|
|
|
|
case td_api::deviceTokenApplePush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenApplePush *>(device_token_ptr.get());
|
2018-01-19 16:04:16 +01:00
|
|
|
token = std::move(device_token->device_token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::Apns;
|
2018-01-10 13:11:35 +01:00
|
|
|
is_app_sandbox = device_token->is_app_sandbox_;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-04-18 01:23:02 +02:00
|
|
|
case td_api::deviceTokenFirebaseCloudMessaging::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenFirebaseCloudMessaging *>(device_token_ptr.get());
|
2018-01-10 01:18:03 +01:00
|
|
|
token = std::move(device_token->token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::Fcm;
|
2018-12-24 16:33:39 +01:00
|
|
|
encrypt = device_token->encrypt_;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenMicrosoftPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenMicrosoftPush *>(device_token_ptr.get());
|
2018-01-19 16:04:16 +01:00
|
|
|
token = std::move(device_token->channel_uri_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::Mpns;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenSimplePush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenSimplePush *>(device_token_ptr.get());
|
2018-01-19 16:04:16 +01:00
|
|
|
token = std::move(device_token->endpoint_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::SimplePush;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenUbuntuPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenUbuntuPush *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::UbuntuPhone;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-02-17 13:32:14 +01:00
|
|
|
case td_api::deviceTokenBlackBerryPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenBlackBerryPush *>(device_token_ptr.get());
|
2018-01-10 01:18:03 +01:00
|
|
|
token = std::move(device_token->token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::BlackBerry;
|
2018-01-19 16:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenWindowsPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenWindowsPush *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->access_token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::Wns;
|
2018-01-19 16:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenApplePushVoIP::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenApplePushVoIP *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->device_token_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::ApnsVoip;
|
2018-01-19 16:04:16 +01:00
|
|
|
is_app_sandbox = device_token->is_app_sandbox_;
|
2018-12-24 16:33:39 +01:00
|
|
|
encrypt = device_token->encrypt_;
|
2018-01-19 16:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenWebPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenWebPush *>(device_token_ptr.get());
|
|
|
|
if (device_token->endpoint_.find(',') != string::npos) {
|
|
|
|
return promise.set_error(Status::Error(400, "Illegal endpoint value"));
|
|
|
|
}
|
|
|
|
if (!is_base64url(device_token->p256dh_base64url_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Public key must be base64url-encoded"));
|
|
|
|
}
|
|
|
|
if (!is_base64url(device_token->auth_base64url_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Authentication secret must be base64url-encoded"));
|
|
|
|
}
|
|
|
|
if (!clean_input_string(device_token->endpoint_)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Endpoint must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!device_token->endpoint_.empty()) {
|
2018-04-11 12:49:04 +02:00
|
|
|
token = json_encode<string>(json_object([&device_token](auto &o) {
|
|
|
|
o("endpoint", device_token->endpoint_);
|
|
|
|
o("keys", json_object([&device_token](auto &o) {
|
2018-04-11 16:21:24 +02:00
|
|
|
o("p256dh", device_token->p256dh_base64url_);
|
2018-04-11 12:49:04 +02:00
|
|
|
o("auth", device_token->auth_base64url_);
|
|
|
|
}));
|
|
|
|
}));
|
2018-01-19 16:04:16 +01:00
|
|
|
}
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::WebPush;
|
2018-01-19 16:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenMicrosoftPushVoIP::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenMicrosoftPushVoIP *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->channel_uri_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::MpnsVoip;
|
2018-01-19 16:04:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case td_api::deviceTokenTizenPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenTizenPush *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->reg_id_);
|
2021-07-05 18:17:26 +02:00
|
|
|
token_type = TokenType::Tizen;
|
2018-01-10 01:18:03 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-01-30 13:42:15 +01:00
|
|
|
case td_api::deviceTokenHuaweiPush::ID: {
|
|
|
|
auto device_token = static_cast<td_api::deviceTokenHuaweiPush *>(device_token_ptr.get());
|
|
|
|
token = std::move(device_token->token_);
|
|
|
|
token_type = TokenType::Huawei;
|
|
|
|
encrypt = device_token->encrypt_;
|
|
|
|
break;
|
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-19 16:04:16 +01:00
|
|
|
|
2018-01-10 01:18:03 +01:00
|
|
|
if (!clean_input_string(token)) {
|
|
|
|
return promise.set_error(Status::Error(400, "Device token must be encoded in UTF-8"));
|
|
|
|
}
|
|
|
|
for (auto &other_user_id : other_user_ids) {
|
2021-03-28 23:50:15 +02:00
|
|
|
if (!other_user_id.is_valid()) {
|
2018-01-10 01:18:03 +01:00
|
|
|
return promise.set_error(Status::Error(400, "Invalid user_id among other user_ids"));
|
|
|
|
}
|
|
|
|
}
|
2021-12-22 12:38:49 +01:00
|
|
|
auto input_user_ids = UserId::get_input_user_ids(other_user_ids);
|
2018-01-10 01:18:03 +01:00
|
|
|
|
|
|
|
auto &info = tokens_[token_type];
|
|
|
|
if (token.empty()) {
|
|
|
|
if (info.token.empty()) {
|
|
|
|
// already unregistered
|
2018-12-28 23:48:32 +01:00
|
|
|
return promise.set_value(td_api::make_object<td_api::pushReceiverId>());
|
2018-01-10 01:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
info.state = TokenInfo::State::Unregister;
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2021-12-22 12:38:49 +01:00
|
|
|
if ((info.state == TokenInfo::State::Reregister || info.state == TokenInfo::State::Sync) && info.token == token &&
|
|
|
|
info.other_user_ids == input_user_ids && info.is_app_sandbox == is_app_sandbox && encrypt == info.encrypt) {
|
2022-08-18 19:21:40 +02:00
|
|
|
int64 push_token_id = encrypt ? info.encryption_key_id : G()->get_option_integer("my_id");
|
2021-12-22 12:38:49 +01:00
|
|
|
return promise.set_value(td_api::make_object<td_api::pushReceiverId>(push_token_id));
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:18:03 +01:00
|
|
|
info.state = TokenInfo::State::Register;
|
|
|
|
info.token = std::move(token);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-12-22 12:38:49 +01:00
|
|
|
info.net_query_id = 0;
|
|
|
|
info.other_user_ids = std::move(input_user_ids);
|
2018-01-10 13:11:35 +01:00
|
|
|
info.is_app_sandbox = is_app_sandbox;
|
2018-12-24 16:33:39 +01:00
|
|
|
if (encrypt != info.encrypt) {
|
|
|
|
if (encrypt) {
|
|
|
|
constexpr size_t ENCRYPTION_KEY_LENGTH = 256;
|
2021-10-19 17:11:16 +02:00
|
|
|
constexpr auto MIN_ENCRYPTION_KEY_ID = static_cast<int64>(10000000000000ll);
|
2018-12-24 16:33:39 +01:00
|
|
|
info.encryption_key.resize(ENCRYPTION_KEY_LENGTH);
|
2018-12-28 23:48:32 +01:00
|
|
|
while (true) {
|
|
|
|
Random::secure_bytes(info.encryption_key);
|
2021-07-05 20:04:23 +02:00
|
|
|
info.encryption_key_id = mtproto::DhHandshake::calc_key_id(info.encryption_key);
|
2018-12-28 23:48:32 +01:00
|
|
|
if (info.encryption_key_id <= -MIN_ENCRYPTION_KEY_ID || info.encryption_key_id >= MIN_ENCRYPTION_KEY_ID) {
|
|
|
|
// ensure that encryption key ID never collide with anything
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-24 16:33:39 +01:00
|
|
|
} else {
|
|
|
|
info.encryption_key.clear();
|
2018-12-28 23:48:32 +01:00
|
|
|
info.encryption_key_id = 0;
|
2018-12-24 16:33:39 +01:00
|
|
|
}
|
|
|
|
info.encrypt = encrypt;
|
|
|
|
}
|
2018-12-28 23:48:32 +01:00
|
|
|
info.promise.set_value(td_api::make_object<td_api::pushReceiverId>());
|
2018-01-10 01:18:03 +01:00
|
|
|
info.promise = std::move(promise);
|
|
|
|
save_info(token_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-11-17 20:43:10 +01:00
|
|
|
void DeviceTokenManager::reregister_device() {
|
2021-07-05 18:17:26 +02:00
|
|
|
for (int32 token_type = 1; token_type < TokenType::Size; token_type++) {
|
2019-11-17 20:43:10 +01:00
|
|
|
auto &token = tokens_[token_type];
|
|
|
|
if (token.state == TokenInfo::State::Sync && !token.token.empty()) {
|
2020-01-01 17:38:54 +01:00
|
|
|
token.state = TokenInfo::State::Reregister;
|
2019-11-17 20:43:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:48:32 +01:00
|
|
|
vector<std::pair<int64, Slice>> DeviceTokenManager::get_encryption_keys() const {
|
|
|
|
vector<std::pair<int64, Slice>> result;
|
2021-07-05 18:17:26 +02:00
|
|
|
for (int32 token_type = 1; token_type < TokenType::Size; token_type++) {
|
2018-12-24 16:33:39 +01:00
|
|
|
auto &info = tokens_[token_type];
|
2018-12-28 23:48:32 +01:00
|
|
|
if (!info.token.empty() && info.state != TokenInfo::State::Unregister) {
|
|
|
|
if (info.encrypt) {
|
|
|
|
result.emplace_back(info.encryption_key_id, info.encryption_key);
|
|
|
|
} else {
|
2022-08-18 19:21:40 +02:00
|
|
|
result.emplace_back(G()->get_option_integer("my_id"), Slice());
|
2018-12-28 23:48:32 +01:00
|
|
|
}
|
2018-12-24 16:33:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:18:03 +01:00
|
|
|
string DeviceTokenManager::get_database_key(int32 token_type) {
|
|
|
|
return PSTRING() << "device_token" << token_type;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceTokenManager::start_up() {
|
2021-07-05 18:17:26 +02:00
|
|
|
for (int32 token_type = 1; token_type < TokenType::Size; token_type++) {
|
2018-01-10 01:18:03 +01:00
|
|
|
auto serialized = G()->td_db()->get_binlog_pmc()->get(get_database_key(token_type));
|
|
|
|
if (serialized.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &token = tokens_[token_type];
|
|
|
|
char c = serialized[0];
|
|
|
|
if (c == '*') {
|
2018-12-28 23:48:32 +01:00
|
|
|
auto status = unserialize(token, serialized.substr(1));
|
|
|
|
if (status.is_error()) {
|
|
|
|
token = TokenInfo();
|
|
|
|
LOG(ERROR) << "Invalid serialized TokenInfo: " << format::escaped(serialized) << ' ' << status;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
} else {
|
|
|
|
// legacy
|
|
|
|
if (c == '+') {
|
|
|
|
token.state = TokenInfo::State::Register;
|
|
|
|
} else if (c == '-') {
|
|
|
|
token.state = TokenInfo::State::Unregister;
|
|
|
|
} else if (c == '=') {
|
|
|
|
token.state = TokenInfo::State::Sync;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Invalid serialized TokenInfo: " << format::escaped(serialized);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
token.token = serialized.substr(1);
|
|
|
|
}
|
2019-12-30 02:34:59 +01:00
|
|
|
LOG(INFO) << "Have device token " << token_type << "--->" << token;
|
2019-11-17 20:43:10 +01:00
|
|
|
if (token.state == TokenInfo::State::Sync && !token.token.empty()) {
|
2020-01-01 17:38:54 +01:00
|
|
|
token.state = TokenInfo::State::Reregister;
|
2019-08-29 02:40:18 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
2018-01-10 01:18:03 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void DeviceTokenManager::save_info(int32 token_type) {
|
2018-01-10 01:18:03 +01:00
|
|
|
LOG(INFO) << "SET device token " << token_type << "--->" << tokens_[token_type];
|
|
|
|
if (tokens_[token_type].token.empty()) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase(get_database_key(token_type));
|
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(get_database_key(token_type), "*" + serialize(tokens_[token_type]));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
sync_cnt_++;
|
|
|
|
G()->td_db()->get_binlog_pmc()->force_sync(
|
2022-06-27 01:57:56 +02:00
|
|
|
create_event_promise(self_closure(this, &DeviceTokenManager::dec_sync_cnt)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceTokenManager::dec_sync_cnt() {
|
|
|
|
sync_cnt_--;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceTokenManager::loop() {
|
2020-01-01 17:38:54 +01:00
|
|
|
if (sync_cnt_ != 0 || G()->close_flag()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-07-05 18:17:26 +02:00
|
|
|
for (int32 token_type = 1; token_type < TokenType::Size; token_type++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &info = tokens_[token_type];
|
|
|
|
if (info.state == TokenInfo::State::Sync) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (info.net_query_id != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// have to send query
|
|
|
|
NetQueryPtr net_query;
|
|
|
|
if (info.state == TokenInfo::State::Unregister) {
|
|
|
|
net_query = G()->net_query_creator().create(
|
2021-09-03 11:27:59 +02:00
|
|
|
telegram_api::account_unregisterDevice(token_type, info.token, vector<int64>(info.other_user_ids)));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-10-22 15:24:55 +02:00
|
|
|
int32 flags = telegram_api::account_registerDevice::NO_MUTED_MASK;
|
2021-09-03 11:27:59 +02:00
|
|
|
net_query = G()->net_query_creator().create(
|
|
|
|
telegram_api::account_registerDevice(flags, false /*ignored*/, token_type, info.token, info.is_app_sandbox,
|
|
|
|
BufferSlice(info.encryption_key), vector<int64>(info.other_user_ids)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
info.net_query_id = net_query->id();
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this, token_type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceTokenManager::on_result(NetQueryPtr net_query) {
|
|
|
|
auto token_type = static_cast<TokenType>(get_link_token());
|
2021-07-05 18:17:26 +02:00
|
|
|
CHECK(token_type >= 1 && token_type < TokenType::Size);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &info = tokens_[token_type];
|
|
|
|
if (info.net_query_id != net_query->id()) {
|
|
|
|
net_query->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info.net_query_id = 0;
|
2020-01-01 17:38:54 +01:00
|
|
|
CHECK(info.state != TokenInfo::State::Sync);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static_assert(std::is_same<telegram_api::account_registerDevice::ReturnType,
|
|
|
|
telegram_api::account_unregisterDevice::ReturnType>::value,
|
|
|
|
"");
|
|
|
|
auto r_flag = fetch_result<telegram_api::account_registerDevice>(std::move(net_query));
|
|
|
|
if (r_flag.is_ok() && r_flag.ok()) {
|
|
|
|
if (info.promise) {
|
2018-12-28 23:48:32 +01:00
|
|
|
int64 push_token_id = 0;
|
|
|
|
if (info.state == TokenInfo::State::Register) {
|
|
|
|
if (info.encrypt) {
|
|
|
|
push_token_id = info.encryption_key_id;
|
|
|
|
} else {
|
2022-08-18 19:21:40 +02:00
|
|
|
push_token_id = G()->get_option_integer("my_id");
|
2018-12-28 23:48:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
info.promise.set_value(td_api::make_object<td_api::pushReceiverId>(push_token_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (info.state == TokenInfo::State::Unregister) {
|
2018-12-24 16:33:39 +01:00
|
|
|
info.token.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
info.state = TokenInfo::State::Sync;
|
|
|
|
} else {
|
2022-04-12 15:21:42 +02:00
|
|
|
int32 retry_after = 0;
|
2020-01-01 17:38:54 +01:00
|
|
|
if (r_flag.is_error()) {
|
2022-04-12 15:21:42 +02:00
|
|
|
auto &error = r_flag.error();
|
|
|
|
if (!G()->is_expected_error(error)) {
|
|
|
|
LOG(ERROR) << "Failed to " << info.state << " device: " << error;
|
|
|
|
} else {
|
|
|
|
retry_after = Global::get_retry_after(error.code(), error.message());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-01 17:38:54 +01:00
|
|
|
info.promise.set_error(r_flag.move_as_error());
|
|
|
|
} else {
|
2021-09-24 09:59:51 +02:00
|
|
|
info.promise.set_error(Status::Error(400, "Got false as result of registerDevice server request"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-01-01 17:38:54 +01:00
|
|
|
if (info.state == TokenInfo::State::Reregister) {
|
|
|
|
// keep trying to reregister the token
|
2022-04-12 15:21:42 +02:00
|
|
|
return set_timeout_in(clamp(retry_after, 1, 3600));
|
2020-01-01 17:38:54 +01:00
|
|
|
} else if (info.state == TokenInfo::State::Register) {
|
2018-12-31 20:04:05 +01:00
|
|
|
info.state = TokenInfo::State::Unregister;
|
|
|
|
} else {
|
2020-01-01 17:38:54 +01:00
|
|
|
CHECK(info.state == TokenInfo::State::Unregister);
|
2018-12-31 20:04:05 +01:00
|
|
|
info.state = TokenInfo::State::Sync;
|
2018-12-24 16:33:39 +01:00
|
|
|
info.token.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 13:11:35 +01:00
|
|
|
save_info(token_type);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-01-10 13:11:35 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|