2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
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/ConfigManager.h"
|
|
|
|
|
2019-12-19 21:00:28 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/ConfigShared.h"
|
|
|
|
#include "td/telegram/Global.h"
|
2019-12-19 02:23:14 +01:00
|
|
|
#include "td/telegram/JsonValue.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
2019-07-23 01:14:34 +02:00
|
|
|
#include "td/telegram/net/AuthDataShared.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/ConnectionCreator.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/DcId.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/DcOptions.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2018-06-26 01:43:11 +02:00
|
|
|
#include "td/telegram/net/NetType.h"
|
|
|
|
#include "td/telegram/net/PublicRsaKeyShared.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/net/Session.h"
|
2018-10-28 18:30:47 +01:00
|
|
|
#include "td/telegram/StateManager.h"
|
2019-12-19 21:00:28 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2019-01-31 03:05:40 +01:00
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/mtproto/AuthData.h"
|
|
|
|
#include "td/mtproto/AuthKey.h"
|
|
|
|
#include "td/mtproto/crypto.h"
|
|
|
|
#include "td/mtproto/RawConnection.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/mtproto/TransportType.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#if !TD_EMSCRIPTEN //FIXME
|
|
|
|
#include "td/net/HttpQuery.h"
|
2018-08-15 14:41:42 +02:00
|
|
|
#include "td/net/SslStream.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/net/Wget.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
|
|
|
#include "td/utils/base64.h"
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/crypto.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/JsonBuilder.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/Parser.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/Clocks.h"
|
|
|
|
#include "td/utils/Random.h"
|
|
|
|
#include "td/utils/Time.h"
|
2018-06-05 19:44:41 +02:00
|
|
|
#include "td/utils/tl_helpers.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_parsers.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-11-26 19:57:48 +01:00
|
|
|
#include <functional>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2018-12-26 21:58:18 +01:00
|
|
|
int VERBOSITY_NAME(config_recoverer) = VERBOSITY_NAME(INFO);
|
2019-07-11 22:32:45 +02:00
|
|
|
|
|
|
|
Result<int32> HttpDate::to_unix_time(int32 year, int32 month, int32 day, int32 hour, int32 minute, int32 second) {
|
2019-07-11 13:25:20 +02:00
|
|
|
if (year < 1970 || year > 2037) {
|
2019-07-11 22:32:45 +02:00
|
|
|
return Status::Error("Invalid year");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
|
|
|
if (month < 1 || month > 12) {
|
2019-07-11 22:32:45 +02:00
|
|
|
return Status::Error("Invalid month");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
|
|
|
if (day < 1 || day > days_in_month(year, month)) {
|
2019-07-11 22:32:45 +02:00
|
|
|
return Status::Error("Invalid day");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
2019-07-11 22:32:45 +02:00
|
|
|
if (hour < 0 || hour >= 24) {
|
|
|
|
return Status::Error("Invalid hour");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
2019-07-11 22:32:45 +02:00
|
|
|
if (minute < 0 || minute >= 60) {
|
|
|
|
return Status::Error("Invalid minute");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
|
|
|
if (second < 0 || second > 60) {
|
2019-07-11 22:32:45 +02:00
|
|
|
return Status::Error("Invalid second");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
2019-07-11 22:32:45 +02:00
|
|
|
|
|
|
|
int32 res = 0;
|
|
|
|
for (int32 y = 1970; y < year; y++) {
|
2019-07-11 13:25:20 +02:00
|
|
|
res += (is_leap(y) + 365) * seconds_in_day();
|
|
|
|
}
|
2019-07-11 22:32:45 +02:00
|
|
|
for (int32 m = 1; m < month; m++) {
|
2019-07-11 13:25:20 +02:00
|
|
|
res += days_in_month(year, m) * seconds_in_day();
|
|
|
|
}
|
|
|
|
res += (day - 1) * seconds_in_day();
|
|
|
|
res += hour * 60 * 60;
|
|
|
|
res += minute * 60;
|
|
|
|
res += second;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-07-11 22:32:45 +02:00
|
|
|
Result<int32> HttpDate::parse_http_date(std::string slice) {
|
|
|
|
Parser p(slice);
|
2019-07-11 13:25:20 +02:00
|
|
|
p.read_till(','); // ignore week day
|
|
|
|
p.skip(',');
|
|
|
|
p.skip_whitespaces();
|
2019-07-11 22:32:45 +02:00
|
|
|
p.skip_nofail('0');
|
2019-07-11 13:25:20 +02:00
|
|
|
TRY_RESULT(day, to_integer_safe<int32>(p.read_word()));
|
|
|
|
auto month_name = p.read_word();
|
|
|
|
to_lower_inplace(month_name);
|
|
|
|
TRY_RESULT(year, to_integer_safe<int32>(p.read_word()));
|
|
|
|
p.skip_whitespaces();
|
|
|
|
p.skip_nofail('0');
|
|
|
|
TRY_RESULT(hour, to_integer_safe<int32>(p.read_till(':')));
|
|
|
|
p.skip(':');
|
|
|
|
p.skip_nofail('0');
|
|
|
|
TRY_RESULT(minute, to_integer_safe<int32>(p.read_till(':')));
|
|
|
|
p.skip(':');
|
|
|
|
p.skip_nofail('0');
|
|
|
|
TRY_RESULT(second, to_integer_safe<int32>(p.read_word()));
|
|
|
|
auto gmt = p.read_word();
|
|
|
|
TRY_STATUS(std::move(p.status()));
|
|
|
|
if (gmt != "GMT") {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("Timezone must be GMT");
|
2019-07-11 13:25:20 +02:00
|
|
|
}
|
|
|
|
|
2019-07-11 22:32:45 +02:00
|
|
|
static Slice month_names[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
|
2019-07-11 13:25:20 +02:00
|
|
|
|
|
|
|
int month = 0;
|
|
|
|
|
|
|
|
for (int m = 1; m <= 12; m++) {
|
|
|
|
if (month_names[m - 1] == month_name) {
|
|
|
|
month = m;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (month == 0) {
|
|
|
|
return Status::Error("Unknown month name");
|
|
|
|
}
|
|
|
|
|
|
|
|
return HttpDate::to_unix_time(year, month, day, hour, minute, second);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<SimpleConfig> decode_config(Slice input) {
|
2019-07-11 22:32:45 +02:00
|
|
|
static auto rsa = RSA::from_pem(
|
2018-12-31 20:04:05 +01:00
|
|
|
"-----BEGIN RSA PUBLIC KEY-----\n"
|
|
|
|
"MIIBCgKCAQEAyr+18Rex2ohtVy8sroGP\n"
|
|
|
|
"BwXD3DOoKCSpjDqYoXgCqB7ioln4eDCFfOBUlfXUEvM/fnKCpF46VkAftlb4VuPD\n"
|
|
|
|
"eQSS/ZxZYEGqHaywlroVnXHIjgqoxiAd192xRGreuXIaUKmkwlM9JID9WS2jUsTp\n"
|
|
|
|
"zQ91L8MEPLJ/4zrBwZua8W5fECwCCh2c9G5IzzBm+otMS/YKwmR1olzRCyEkyAEj\n"
|
|
|
|
"XWqBI9Ftv5eG8m0VkBzOG655WIYdyV0HfDK/NWcvGqa0w/nriMD6mDjKOryamw0O\n"
|
|
|
|
"P9QuYgMN0C9xMW9y8SmP4h92OAWodTYgY1hZCxdv6cs5UnW9+PWvS+WIbkh+GaWY\n"
|
|
|
|
"xwIDAQAB\n"
|
|
|
|
"-----END RSA PUBLIC KEY-----\n")
|
|
|
|
.move_as_ok();
|
|
|
|
|
|
|
|
if (input.size() < 344 || input.size() > 1024) {
|
|
|
|
return Status::Error(PSLICE() << "Invalid " << tag("length", input.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data_base64 = base64_filter(input);
|
|
|
|
if (data_base64.size() != 344) {
|
|
|
|
return Status::Error(PSLICE() << "Invalid " << tag("length", data_base64.size()) << " after base64_filter");
|
|
|
|
}
|
|
|
|
TRY_RESULT(data_rsa, base64_decode(data_base64));
|
|
|
|
if (data_rsa.size() != 256) {
|
|
|
|
return Status::Error(PSLICE() << "Invalid " << tag("length", data_rsa.size()) << " after base64_decode");
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice data_rsa_slice(data_rsa);
|
|
|
|
rsa.decrypt(data_rsa_slice, data_rsa_slice);
|
|
|
|
|
|
|
|
MutableSlice data_cbc = data_rsa_slice.substr(32);
|
|
|
|
UInt256 key;
|
|
|
|
UInt128 iv;
|
2018-11-06 17:00:03 +01:00
|
|
|
as_slice(key).copy_from(data_rsa_slice.substr(0, 32));
|
|
|
|
as_slice(iv).copy_from(data_rsa_slice.substr(16, 16));
|
2019-08-12 15:53:17 +02:00
|
|
|
aes_cbc_decrypt(as_slice(key), as_slice(iv), data_cbc, data_cbc);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
CHECK(data_cbc.size() == 224);
|
|
|
|
string hash(32, ' ');
|
|
|
|
sha256(data_cbc.substr(0, 208), MutableSlice(hash));
|
|
|
|
if (data_cbc.substr(208) != Slice(hash).substr(0, 16)) {
|
2019-08-26 03:53:22 +02:00
|
|
|
return Status::Error("SHA256 mismatch");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TlParser len_parser{data_cbc};
|
|
|
|
int len = len_parser.fetch_int();
|
2019-07-11 02:12:27 +02:00
|
|
|
if (len < 8 || len > 208) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Status::Error(PSLICE() << "Invalid " << tag("data length", len) << " after aes_cbc_decrypt");
|
|
|
|
}
|
|
|
|
int constructor_id = len_parser.fetch_int();
|
|
|
|
if (constructor_id != telegram_api::help_configSimple::ID) {
|
|
|
|
return Status::Error(PSLICE() << "Wrong " << tag("constructor", format::as_hex(constructor_id)));
|
|
|
|
}
|
2019-07-11 02:12:27 +02:00
|
|
|
BufferSlice raw_config(data_cbc.substr(8, len - 8));
|
2018-12-31 20:04:05 +01:00
|
|
|
TlBufferParser parser{&raw_config};
|
|
|
|
auto config = telegram_api::help_configSimple::fetch(parser);
|
2019-07-11 02:12:27 +02:00
|
|
|
parser.fetch_end();
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_STATUS(parser.get_status());
|
|
|
|
return std::move(config);
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:25:20 +02:00
|
|
|
static ActorOwn<> get_simple_config_impl(Promise<SimpleConfigResult> promise, int32 scheduler_id, string url,
|
2019-11-19 16:12:51 +01:00
|
|
|
string host, std::vector<std::pair<string, string>> headers, bool prefer_ipv6,
|
2019-11-19 15:58:24 +01:00
|
|
|
std::function<Result<string>(HttpQuery &)> get_config,
|
|
|
|
string content = string(), string content_type = string()) {
|
2018-03-12 19:04:03 +01:00
|
|
|
VLOG(config_recoverer) << "Request simple config from " << url;
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_EMSCRIPTEN // FIXME
|
|
|
|
return ActorOwn<>();
|
|
|
|
#else
|
2018-07-01 03:45:25 +02:00
|
|
|
const int timeout = 10;
|
|
|
|
const int ttl = 3;
|
2019-11-19 16:12:51 +01:00
|
|
|
headers.emplace_back("Host", std::move(host));
|
|
|
|
headers.emplace_back("User-Agent",
|
|
|
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
|
|
"Chrome/77.0.3865.90 Safari/537.36");
|
2018-12-31 20:04:05 +01:00
|
|
|
return ActorOwn<>(create_actor_on_scheduler<Wget>(
|
|
|
|
"Wget", scheduler_id,
|
2019-11-19 15:58:24 +01:00
|
|
|
PromiseCreator::lambda([get_config = std::move(get_config),
|
|
|
|
promise = std::move(promise)](Result<unique_ptr<HttpQuery>> r_query) mutable {
|
2019-07-11 13:25:20 +02:00
|
|
|
promise.set_result([&]() -> Result<SimpleConfigResult> {
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(http_query, std::move(r_query));
|
2019-07-11 13:25:20 +02:00
|
|
|
SimpleConfigResult res;
|
2019-07-11 22:32:45 +02:00
|
|
|
res.r_http_date = HttpDate::parse_http_date(http_query->get_header("date").str());
|
2019-11-19 15:58:24 +01:00
|
|
|
auto r_config = get_config(*http_query);
|
|
|
|
if (r_config.is_error()) {
|
|
|
|
res.r_config = r_config.move_as_error();
|
|
|
|
} else {
|
|
|
|
res.r_config = decode_config(r_config.ok());
|
|
|
|
}
|
2019-07-22 06:05:50 +02:00
|
|
|
return std::move(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
}());
|
|
|
|
}),
|
2019-11-19 16:12:51 +01:00
|
|
|
std::move(url), std::move(headers), timeout, ttl, prefer_ipv6, SslStream::VerifyPeer::Off, std::move(content),
|
|
|
|
std::move(content_type)));
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:25:20 +02:00
|
|
|
ActorOwn<> get_simple_config_azure(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config, bool is_test,
|
2018-06-30 15:35:37 +02:00
|
|
|
int32 scheduler_id) {
|
2018-05-24 17:09:27 +02:00
|
|
|
string url = PSTRING() << "https://software-download.microsoft.com/" << (is_test ? "test" : "prod")
|
|
|
|
<< "v2/config.txt";
|
2018-07-02 03:21:05 +02:00
|
|
|
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
|
2019-11-19 16:12:51 +01:00
|
|
|
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "tcdnb.azureedge.net", {},
|
|
|
|
prefer_ipv6, [](HttpQuery &http_query) { return http_query.content_.str(); });
|
2018-03-12 19:04:03 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:20:15 +02:00
|
|
|
static ActorOwn<> get_simple_config_dns(Slice address, Slice host, Promise<SimpleConfigResult> promise,
|
|
|
|
const ConfigShared *shared_config, bool is_test, int32 scheduler_id) {
|
2018-06-30 15:35:37 +02:00
|
|
|
string name = shared_config == nullptr ? string() : shared_config->get_option_string("dc_txt_domain_name");
|
2018-07-02 03:21:05 +02:00
|
|
|
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
|
2019-11-12 20:21:37 +01:00
|
|
|
if (name.empty()) {
|
2019-07-11 13:25:20 +02:00
|
|
|
name = is_test ? "tapv3.stel.com" : "apv3.stel.com";
|
2018-06-30 15:35:37 +02:00
|
|
|
}
|
2019-11-19 16:12:51 +01:00
|
|
|
auto get_config = [](HttpQuery &http_query) -> Result<string> {
|
|
|
|
TRY_RESULT(json, json_decode(http_query.content_));
|
|
|
|
if (json.type() != JsonValue::Type::Object) {
|
2019-11-19 17:26:39 +01:00
|
|
|
return Status::Error("Expected JSON object");
|
2019-11-19 16:12:51 +01:00
|
|
|
}
|
|
|
|
auto &answer_object = json.get_object();
|
|
|
|
TRY_RESULT(answer, get_json_object_field(answer_object, "Answer", JsonValue::Type::Array, false));
|
|
|
|
auto &answer_array = answer.get_array();
|
|
|
|
vector<string> parts;
|
|
|
|
for (auto &v : answer_array) {
|
|
|
|
if (v.type() != JsonValue::Type::Object) {
|
2019-11-19 17:26:39 +01:00
|
|
|
return Status::Error("Expected JSON object");
|
2019-11-19 16:12:51 +01:00
|
|
|
}
|
|
|
|
auto &data_object = v.get_object();
|
|
|
|
TRY_RESULT(part, get_json_object_string_field(data_object, "data", false));
|
|
|
|
parts.push_back(std::move(part));
|
|
|
|
}
|
|
|
|
if (parts.size() != 2) {
|
|
|
|
return Status::Error("Expected data in two parts");
|
|
|
|
}
|
|
|
|
string data;
|
|
|
|
if (parts[0].size() < parts[1].size()) {
|
|
|
|
data = parts[1] + parts[0];
|
|
|
|
} else {
|
|
|
|
data = parts[0] + parts[1];
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
return get_simple_config_impl(std::move(promise), scheduler_id,
|
|
|
|
PSTRING() << "https://" << address << "?name=" << url_encode(name) << "&type=16",
|
|
|
|
host.str(), {{"Accept", "application/dns-json"}}, prefer_ipv6, std::move(get_config));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-03 21:20:15 +02:00
|
|
|
ActorOwn<> get_simple_config_google_dns(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config,
|
|
|
|
bool is_test, int32 scheduler_id) {
|
|
|
|
return get_simple_config_dns("www.google.com/resolve", "dns.google.com", std::move(promise), shared_config, is_test,
|
|
|
|
scheduler_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorOwn<> get_simple_config_mozilla_dns(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config,
|
|
|
|
bool is_test, int32 scheduler_id) {
|
|
|
|
return get_simple_config_dns("mozilla.cloudflare-dns.com/dns-query", "mozilla.cloudflare-dns.com", std::move(promise),
|
|
|
|
shared_config, is_test, scheduler_id);
|
|
|
|
}
|
|
|
|
|
2019-11-19 15:58:24 +01:00
|
|
|
static string generate_firebase_remote_config_payload() {
|
|
|
|
unsigned char buf[17];
|
|
|
|
Random::secure_bytes(buf, sizeof(buf));
|
|
|
|
buf[0] = static_cast<unsigned char>((buf[0] & 0xF0) | 0x07);
|
|
|
|
auto app_instance_id = base64url_encode(Slice(buf, sizeof(buf)));
|
|
|
|
app_instance_id.resize(22);
|
|
|
|
return PSTRING() << "{\"app_id\":\"1:560508485281:web:4ee13a6af4e84d49e67ae0\",\"app_instance_id\":\""
|
|
|
|
<< app_instance_id << "\"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
ActorOwn<> get_simple_config_firebase_remote_config(Promise<SimpleConfigResult> promise,
|
|
|
|
const ConfigShared *shared_config, bool is_test,
|
|
|
|
int32 scheduler_id) {
|
|
|
|
if (is_test) {
|
|
|
|
promise.set_error(Status::Error(400, "Test config is not supported"));
|
|
|
|
return ActorOwn<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
static const string payload = generate_firebase_remote_config_payload();
|
|
|
|
string url =
|
|
|
|
"https://firebaseremoteconfig.googleapis.com/v1/projects/peak-vista-421/namespaces/"
|
|
|
|
"firebase:fetch?key=AIzaSyC2-kAkpDsroixRXw-sTw-Wfqo4NxjMwwM";
|
|
|
|
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
|
|
|
|
auto get_config = [](HttpQuery &http_query) -> Result<string> {
|
|
|
|
TRY_RESULT(json, json_decode(http_query.get_arg("entries")));
|
|
|
|
if (json.type() != JsonValue::Type::Object) {
|
2019-11-19 17:26:39 +01:00
|
|
|
return Status::Error("Expected JSON object");
|
2019-11-19 15:58:24 +01:00
|
|
|
}
|
|
|
|
auto &entries_object = json.get_object();
|
|
|
|
TRY_RESULT(config, get_json_object_string_field(entries_object, "ipconfigv3", false));
|
|
|
|
return std::move(config);
|
|
|
|
};
|
|
|
|
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firebaseremoteconfig.googleapis.com",
|
2019-11-19 16:12:51 +01:00
|
|
|
{}, prefer_ipv6, std::move(get_config), payload, "application/json");
|
2019-11-19 15:58:24 +01:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:01:12 +01:00
|
|
|
ActorOwn<> get_simple_config_firebase_realtime(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config,
|
|
|
|
bool is_test, int32 scheduler_id) {
|
|
|
|
if (is_test) {
|
|
|
|
promise.set_error(Status::Error(400, "Test config is not supported"));
|
|
|
|
return ActorOwn<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
string url = "https://reserve-5a846.firebaseio.com/ipconfigv3.json";
|
|
|
|
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
|
|
|
|
auto get_config = [](HttpQuery &http_query) -> Result<string> {
|
|
|
|
return http_query.get_arg("content").str();
|
|
|
|
};
|
|
|
|
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "reserve-5a846.firebaseio.com", {},
|
|
|
|
prefer_ipv6, std::move(get_config));
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:26:39 +01:00
|
|
|
ActorOwn<> get_simple_config_firebase_firestore(Promise<SimpleConfigResult> promise, const ConfigShared *shared_config,
|
|
|
|
bool is_test, int32 scheduler_id) {
|
|
|
|
if (is_test) {
|
|
|
|
promise.set_error(Status::Error(400, "Test config is not supported"));
|
|
|
|
return ActorOwn<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
string url = "https://www.google.com/v1/projects/reserve-5a846/databases/(default)/documents/ipconfig/v3";
|
|
|
|
const bool prefer_ipv6 = shared_config == nullptr ? false : shared_config->get_option_boolean("prefer_ipv6");
|
|
|
|
auto get_config = [](HttpQuery &http_query) -> Result<string> {
|
|
|
|
TRY_RESULT(json, json_decode(http_query.get_arg("fields")));
|
|
|
|
if (json.type() != JsonValue::Type::Object) {
|
|
|
|
return Status::Error("Expected JSON object");
|
|
|
|
}
|
|
|
|
TRY_RESULT(data, get_json_object_field(json.get_object(), "data", JsonValue::Type::Object, false));
|
|
|
|
TRY_RESULT(config, get_json_object_string_field(data.get_object(), "stringValue", false));
|
|
|
|
return std::move(config);
|
|
|
|
};
|
|
|
|
return get_simple_config_impl(std::move(promise), scheduler_id, std::move(url), "firestore.googleapis.com", {},
|
|
|
|
prefer_ipv6, std::move(get_config));
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:14:07 +02:00
|
|
|
ActorOwn<> get_full_config(DcOption option, Promise<FullConfig> promise, ActorShared<> parent) {
|
2018-12-31 20:04:05 +01:00
|
|
|
class SessionCallback : public Session::Callback {
|
|
|
|
public:
|
2019-07-11 13:25:20 +02:00
|
|
|
SessionCallback(ActorShared<> parent, DcOption option) : parent_(std::move(parent)), option_(std::move(option)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void on_failed() final {
|
|
|
|
}
|
|
|
|
void on_closed() final {
|
|
|
|
}
|
2019-05-06 20:53:39 +02:00
|
|
|
void request_raw_connection(unique_ptr<mtproto::AuthData> auth_data,
|
|
|
|
Promise<unique_ptr<mtproto::RawConnection>> promise) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
request_raw_connection_cnt_++;
|
2019-07-11 13:25:20 +02:00
|
|
|
VLOG(config_recoverer) << "Request full config from " << option_.get_ip_address()
|
|
|
|
<< ", try = " << request_raw_connection_cnt_;
|
2018-05-18 13:38:49 +02:00
|
|
|
if (request_raw_connection_cnt_ <= 2) {
|
2019-07-11 13:25:20 +02:00
|
|
|
send_closure(G()->connection_creator(), &ConnectionCreator::request_raw_connection_by_ip,
|
|
|
|
option_.get_ip_address(),
|
|
|
|
mtproto::TransportType{mtproto::TransportType::ObfuscatedTcp,
|
|
|
|
narrow_cast<int16>(option_.get_dc_id().get_raw_id()), option_.get_secret()},
|
|
|
|
std::move(promise));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-02-07 21:48:40 +01:00
|
|
|
// Delay all queries except first forever
|
2018-12-31 20:04:05 +01:00
|
|
|
delay_forever_.push_back(std::move(promise));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void on_tmp_auth_key_updated(mtproto::AuthKey auth_key) final {
|
2018-02-07 21:48:40 +01:00
|
|
|
// nop
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-03-15 10:00:18 +01:00
|
|
|
void on_result(NetQueryPtr net_query) final {
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(net_query));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<> parent_;
|
2019-07-11 13:25:20 +02:00
|
|
|
DcOption option_;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t request_raw_connection_cnt_{0};
|
2018-09-27 03:19:03 +02:00
|
|
|
std::vector<Promise<unique_ptr<mtproto::RawConnection>>> delay_forever_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SimpleAuthData : public AuthDataShared {
|
|
|
|
public:
|
2018-05-18 13:38:49 +02:00
|
|
|
explicit SimpleAuthData(DcId dc_id) : dc_id_(dc_id) {
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
DcId dc_id() const override {
|
2018-05-18 13:38:49 +02:00
|
|
|
return dc_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
const std::shared_ptr<PublicRsaKeyShared> &public_rsa_key() override {
|
|
|
|
return public_rsa_key_;
|
|
|
|
}
|
|
|
|
mtproto::AuthKey get_auth_key() override {
|
2018-05-18 13:38:49 +02:00
|
|
|
string dc_key = G()->td_db()->get_binlog_pmc()->get(auth_key_key());
|
|
|
|
|
|
|
|
mtproto::AuthKey res;
|
|
|
|
if (!dc_key.empty()) {
|
|
|
|
unserialize(res, dc_key).ensure();
|
|
|
|
}
|
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-23 01:14:34 +02:00
|
|
|
std::pair<AuthKeyState, bool> get_auth_key_state() override {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto auth_key = get_auth_key();
|
2019-07-23 01:14:34 +02:00
|
|
|
AuthKeyState state = AuthDataShared::get_auth_key_state(auth_key);
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::make_pair(state, auth_key.was_auth_flag());
|
|
|
|
}
|
|
|
|
void set_auth_key(const mtproto::AuthKey &auth_key) override {
|
2018-05-18 13:38:49 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set(auth_key_key(), serialize(auth_key));
|
|
|
|
|
|
|
|
//notify();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void update_server_time_difference(double diff) override {
|
2018-05-18 13:38:49 +02:00
|
|
|
G()->update_server_time_difference(diff);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
double get_server_time_difference() override {
|
2018-05-18 13:38:49 +02:00
|
|
|
return G()->get_server_time_difference();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void add_auth_key_listener(unique_ptr<Listener> listener) override {
|
|
|
|
if (listener->notify()) {
|
|
|
|
auth_key_listeners_.push_back(std::move(listener));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_future_salts(const std::vector<mtproto::ServerSalt> &future_salts) override {
|
2018-05-18 13:38:49 +02:00
|
|
|
G()->td_db()->get_binlog_pmc()->set(future_salts_key(), serialize(future_salts));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-18 13:38:49 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::vector<mtproto::ServerSalt> get_future_salts() override {
|
2018-05-18 13:38:49 +02:00
|
|
|
string future_salts = G()->td_db()->get_binlog_pmc()->get(future_salts_key());
|
|
|
|
std::vector<mtproto::ServerSalt> res;
|
|
|
|
if (!future_salts.empty()) {
|
|
|
|
unserialize(res, future_salts).ensure();
|
|
|
|
}
|
|
|
|
return res;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-05-18 13:38:49 +02:00
|
|
|
DcId dc_id_;
|
2019-01-02 00:43:37 +01:00
|
|
|
std::shared_ptr<PublicRsaKeyShared> public_rsa_key_ =
|
|
|
|
std::make_shared<PublicRsaKeyShared>(DcId::empty(), G()->is_test_dc());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
std::vector<unique_ptr<Listener>> auth_key_listeners_;
|
2018-12-31 20:04:05 +01:00
|
|
|
void notify() {
|
2019-10-21 15:25:56 +02:00
|
|
|
td::remove_if(auth_key_listeners_, [&](auto &listener) { return !listener->notify(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-05-18 13:38:49 +02:00
|
|
|
|
2018-05-18 18:44:46 +02:00
|
|
|
string auth_key_key() const {
|
2018-05-18 13:38:49 +02:00
|
|
|
return PSTRING() << "config_recovery_auth" << dc_id().get_raw_id();
|
|
|
|
}
|
2018-05-18 18:44:46 +02:00
|
|
|
string future_salts_key() const {
|
2018-05-18 13:38:49 +02:00
|
|
|
return PSTRING() << "config_recovery_salt" << dc_id().get_raw_id();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class GetConfigActor : public NetQueryCallback {
|
|
|
|
public:
|
2019-08-09 14:14:07 +02:00
|
|
|
GetConfigActor(DcOption option, Promise<FullConfig> promise, ActorShared<> parent)
|
|
|
|
: option_(std::move(option)), promise_(std::move(promise)), parent_(std::move(parent)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void start_up() override {
|
2019-07-11 13:25:20 +02:00
|
|
|
auto auth_data = std::make_shared<SimpleAuthData>(option_.get_dc_id());
|
2019-12-18 01:47:51 +01:00
|
|
|
int32 raw_dc_id = option_.get_dc_id().get_raw_id();
|
2019-07-11 13:25:20 +02:00
|
|
|
auto session_callback = make_unique<SessionCallback>(actor_shared(this, 1), std::move(option_));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-12-18 01:47:51 +01:00
|
|
|
int32 int_dc_id = raw_dc_id;
|
2018-05-23 18:49:05 +02:00
|
|
|
if (G()->is_test_dc()) {
|
|
|
|
int_dc_id += 10000;
|
|
|
|
}
|
2019-12-18 01:47:51 +01:00
|
|
|
session_ = create_actor<Session>("ConfigSession", std::move(session_callback), std::move(auth_data), raw_dc_id,
|
|
|
|
int_dc_id, false /*is_main*/, true /*use_pfs*/, false /*is_cdn*/,
|
|
|
|
false /*need_destroy_auth_key*/, mtproto::AuthKey(),
|
|
|
|
std::vector<mtproto::ServerSalt>());
|
2018-12-31 20:04:05 +01:00
|
|
|
auto query = G()->net_query_creator().create(create_storer(telegram_api::help_getConfig()), DcId::empty(),
|
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off,
|
|
|
|
NetQuery::GzipFlag::On, 60 * 60 * 24);
|
|
|
|
query->set_callback(actor_shared(this));
|
|
|
|
query->dispatch_ttl = 0;
|
|
|
|
send_closure(session_, &Session::send, std::move(query));
|
|
|
|
set_timeout_in(10);
|
|
|
|
}
|
|
|
|
void on_result(NetQueryPtr query) override {
|
|
|
|
promise_.set_result(fetch_result<telegram_api::help_getConfig>(std::move(query)));
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
void hangup_shared() override {
|
|
|
|
if (get_link_token() == 1) {
|
2018-05-18 13:38:49 +02:00
|
|
|
if (promise_) {
|
|
|
|
promise_.set_error(Status::Error("Failed"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void hangup() override {
|
|
|
|
session_.reset();
|
|
|
|
}
|
|
|
|
void timeout_expired() override {
|
|
|
|
promise_.set_error(Status::Error("Timeout expired"));
|
2018-05-18 13:38:49 +02:00
|
|
|
session_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:25:20 +02:00
|
|
|
DcOption option_;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorOwn<Session> session_;
|
|
|
|
Promise<FullConfig> promise_;
|
2019-08-09 14:14:07 +02:00
|
|
|
ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2019-08-09 14:14:07 +02:00
|
|
|
return ActorOwn<>(create_actor<GetConfigActor>("GetConfigActor", option, std::move(promise), std::move(parent)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConfigRecoverer : public Actor {
|
|
|
|
public:
|
|
|
|
explicit ConfigRecoverer(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_dc_options_update(DcOptions dc_options) {
|
|
|
|
dc_options_update_ = dc_options;
|
|
|
|
update_dc_options();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void on_network(bool has_network, uint32 network_generation) {
|
|
|
|
has_network_ = has_network;
|
|
|
|
if (network_generation_ != network_generation) {
|
|
|
|
if (has_network_) {
|
|
|
|
has_network_since_ = Time::now_cached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
void on_online(bool is_online) {
|
2018-08-04 23:35:50 +02:00
|
|
|
if (is_online_ == is_online) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
is_online_ = is_online;
|
2018-08-04 23:35:50 +02:00
|
|
|
if (is_online) {
|
|
|
|
if (simple_config_.dc_options.empty()) {
|
2019-02-10 01:45:15 +01:00
|
|
|
simple_config_expires_at_ = 0;
|
2018-08-04 23:35:50 +02:00
|
|
|
}
|
|
|
|
if (full_config_ == nullptr) {
|
2019-02-10 01:45:15 +01:00
|
|
|
full_config_expires_at_ = 0;
|
2018-08-04 23:35:50 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
void on_connecting(bool is_connecting) {
|
|
|
|
VLOG(config_recoverer) << "ON CONNECTING " << is_connecting;
|
|
|
|
if (is_connecting && !is_connecting_) {
|
|
|
|
connecting_since_ = Time::now_cached();
|
|
|
|
}
|
|
|
|
is_connecting_ = is_connecting;
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:09:27 +02:00
|
|
|
static bool check_phone_number_rules(Slice phone_number, Slice rules) {
|
2018-05-24 18:40:00 +02:00
|
|
|
if (rules.empty() || phone_number.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:09:27 +02:00
|
|
|
bool found = false;
|
|
|
|
for (auto prefix : full_split(rules, ',')) {
|
|
|
|
if (prefix.empty()) {
|
|
|
|
found = true;
|
|
|
|
} else if (prefix[0] == '+' && begins_with(phone_number, prefix.substr(1))) {
|
|
|
|
found = true;
|
|
|
|
} else if (prefix[0] == '-' && begins_with(phone_number, prefix.substr(1))) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Invalid prefix rule " << prefix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:25:20 +02:00
|
|
|
void on_simple_config(Result<SimpleConfigResult> r_simple_config_result, bool dummy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
simple_config_query_.reset();
|
|
|
|
dc_options_i_ = 0;
|
2019-07-11 13:25:20 +02:00
|
|
|
|
|
|
|
SimpleConfigResult cfg;
|
|
|
|
if (r_simple_config_result.is_error()) {
|
|
|
|
cfg.r_http_date = r_simple_config_result.error().clone();
|
|
|
|
cfg.r_config = r_simple_config_result.move_as_error();
|
|
|
|
} else {
|
|
|
|
cfg = r_simple_config_result.move_as_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.r_http_date.is_ok() && (date_option_i_ == 0 || cfg.r_config.is_error())) {
|
|
|
|
G()->update_dns_time_difference(cfg.r_http_date.ok() - Time::now());
|
|
|
|
} else if (cfg.r_config.is_ok()) {
|
|
|
|
G()->update_dns_time_difference(cfg.r_config.ok()->date_ - Time::now());
|
|
|
|
}
|
|
|
|
date_option_i_ = (date_option_i_ + 1) % 2;
|
|
|
|
|
|
|
|
do_on_simple_config(std::move(cfg.r_config));
|
|
|
|
update_dc_options();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_on_simple_config(Result<SimpleConfig> r_simple_config) {
|
2018-05-24 17:09:27 +02:00
|
|
|
if (r_simple_config.is_ok()) {
|
|
|
|
auto config = r_simple_config.move_as_ok();
|
2018-11-17 22:24:19 +01:00
|
|
|
VLOG(config_recoverer) << "Receive raw " << to_string(config);
|
2018-05-24 17:09:27 +02:00
|
|
|
if (config->expires_ >= G()->unix_time()) {
|
|
|
|
string phone_number = G()->shared_config().get_option_string("my_phone_number");
|
|
|
|
simple_config_.dc_options.clear();
|
|
|
|
|
|
|
|
for (auto &rule : config->rules_) {
|
|
|
|
if (check_phone_number_rules(phone_number, rule->phone_prefix_rules_) && DcId::is_valid(rule->dc_id_)) {
|
|
|
|
DcId dc_id = DcId::internal(rule->dc_id_);
|
|
|
|
for (auto &ip_port : rule->ips_) {
|
|
|
|
DcOption option(dc_id, *ip_port);
|
|
|
|
if (option.is_valid()) {
|
|
|
|
simple_config_.dc_options.push_back(std::move(option));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VLOG(config_recoverer) << "Got SimpleConfig " << simple_config_;
|
2019-01-23 04:13:51 +01:00
|
|
|
} else {
|
|
|
|
VLOG(config_recoverer) << "Config has expired at " << config->expires_;
|
2018-05-24 17:09:27 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
simple_config_expires_at_ = get_config_expire_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
simple_config_at_ = Time::now_cached();
|
|
|
|
for (size_t i = 1; i < simple_config_.dc_options.size(); i++) {
|
|
|
|
std::swap(simple_config_.dc_options[i], simple_config_.dc_options[Random::fast(0, static_cast<int>(i))]);
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-24 17:09:27 +02:00
|
|
|
VLOG(config_recoverer) << "Get SimpleConfig error " << r_simple_config.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
simple_config_ = DcOptions();
|
2019-02-10 01:45:15 +01:00
|
|
|
simple_config_expires_at_ = get_failed_config_expire_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_full_config(Result<FullConfig> r_full_config, bool dummy) {
|
|
|
|
full_config_query_.reset();
|
|
|
|
if (r_full_config.is_ok()) {
|
|
|
|
full_config_ = r_full_config.move_as_ok();
|
|
|
|
VLOG(config_recoverer) << "Got FullConfig " << to_string(full_config_);
|
2019-02-10 01:45:15 +01:00
|
|
|
full_config_expires_at_ = get_config_expire_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->connection_creator(), &ConnectionCreator::on_dc_options, DcOptions(full_config_->dc_options_));
|
|
|
|
} else {
|
|
|
|
VLOG(config_recoverer) << "Get FullConfig error " << r_full_config.error();
|
|
|
|
full_config_ = FullConfig();
|
2019-02-10 01:45:15 +01:00
|
|
|
full_config_expires_at_ = get_failed_config_expire_time();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:18:07 +02:00
|
|
|
bool expect_blocking() const {
|
|
|
|
return G()->shared_config().get_option_boolean("expect_blocking", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_config_expire_time() const {
|
2018-08-04 23:35:50 +02:00
|
|
|
auto offline_delay = is_online_ ? 0 : 5 * 60;
|
|
|
|
auto expire_time = expect_blocking() ? Random::fast(2 * 60, 3 * 60) : Random::fast(20 * 60, 30 * 60);
|
|
|
|
return Time::now() + offline_delay + expire_time;
|
2018-05-03 12:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double get_failed_config_expire_time() const {
|
2018-08-04 23:35:50 +02:00
|
|
|
auto offline_delay = is_online_ ? 0 : 5 * 60;
|
|
|
|
auto expire_time = expect_blocking() ? Random::fast(5, 7) : Random::fast(15, 30);
|
|
|
|
return Time::now() + offline_delay + expire_time;
|
2018-05-03 12:18:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_connecting_{false};
|
|
|
|
double connecting_since_{0};
|
|
|
|
|
|
|
|
bool is_online_{false};
|
|
|
|
|
|
|
|
bool has_network_{false};
|
|
|
|
double has_network_since_{0};
|
|
|
|
uint32 network_generation_{0};
|
|
|
|
|
|
|
|
DcOptions simple_config_;
|
2019-02-10 01:45:15 +01:00
|
|
|
double simple_config_expires_at_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
double simple_config_at_{0};
|
|
|
|
ActorOwn<> simple_config_query_;
|
|
|
|
|
|
|
|
DcOptions dc_options_update_;
|
|
|
|
|
|
|
|
DcOptions dc_options_; // dc_options_update_ + simple_config_
|
|
|
|
double dc_options_at_{0};
|
|
|
|
size_t dc_options_i_;
|
|
|
|
|
2019-07-11 13:25:20 +02:00
|
|
|
size_t date_option_i_{0};
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
FullConfig full_config_;
|
2019-02-10 01:45:15 +01:00
|
|
|
double full_config_expires_at_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorOwn<> full_config_query_;
|
|
|
|
|
|
|
|
uint32 ref_cnt_{1};
|
|
|
|
bool close_flag_{false};
|
|
|
|
uint8 simple_config_turn_{0};
|
|
|
|
|
|
|
|
ActorShared<> parent_;
|
|
|
|
|
|
|
|
void hangup_shared() override {
|
|
|
|
ref_cnt_--;
|
|
|
|
try_stop();
|
|
|
|
}
|
|
|
|
void hangup() override {
|
|
|
|
ref_cnt_--;
|
|
|
|
close_flag_ = true;
|
|
|
|
full_config_query_.reset();
|
|
|
|
simple_config_query_.reset();
|
|
|
|
try_stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void try_stop() {
|
|
|
|
if (ref_cnt_ == 0) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double max_connecting_delay() const {
|
2018-05-03 12:18:07 +02:00
|
|
|
return expect_blocking() ? 5 : 20;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void loop() override {
|
|
|
|
if (close_flag_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 21:48:40 +01:00
|
|
|
if (is_connecting_) {
|
|
|
|
VLOG(config_recoverer) << "Failed to connect for " << Time::now_cached() - connecting_since_;
|
|
|
|
} else {
|
|
|
|
VLOG(config_recoverer) << "Successfully connected";
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Timestamp wakeup_timestamp;
|
|
|
|
auto check_timeout = [&](Timestamp timestamp) {
|
|
|
|
if (timestamp.at() < Time::now_cached()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
wakeup_timestamp.relax(timestamp);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool has_connecting_problem =
|
|
|
|
is_connecting_ && check_timeout(Timestamp::at(connecting_since_ + max_connecting_delay()));
|
2019-02-10 01:45:15 +01:00
|
|
|
bool is_valid_simple_config = !check_timeout(Timestamp::at(simple_config_expires_at_));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_valid_simple_config && !simple_config_.dc_options.empty()) {
|
|
|
|
simple_config_ = DcOptions();
|
|
|
|
update_dc_options();
|
|
|
|
}
|
|
|
|
bool need_simple_config = has_connecting_problem && !is_valid_simple_config && simple_config_query_.empty();
|
|
|
|
bool has_dc_options = !dc_options_.dc_options.empty();
|
2019-02-10 01:45:15 +01:00
|
|
|
bool is_valid_full_config = !check_timeout(Timestamp::at(full_config_expires_at_));
|
2018-12-31 20:04:05 +01:00
|
|
|
bool need_full_config = has_connecting_problem && has_dc_options && !is_valid_full_config &&
|
2018-05-03 12:18:07 +02:00
|
|
|
full_config_query_.empty() &&
|
|
|
|
check_timeout(Timestamp::at(dc_options_at_ + (expect_blocking() ? 5 : 10)));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (need_simple_config) {
|
|
|
|
ref_cnt_++;
|
|
|
|
VLOG(config_recoverer) << "ASK SIMPLE CONFIG";
|
2019-07-11 13:25:20 +02:00
|
|
|
auto promise =
|
|
|
|
PromiseCreator::lambda([actor_id = actor_shared(this)](Result<SimpleConfigResult> r_simple_config) {
|
|
|
|
send_closure(actor_id, &ConfigRecoverer::on_simple_config, std::move(r_simple_config), false);
|
|
|
|
});
|
2018-04-24 18:21:47 +02:00
|
|
|
auto get_simple_config = [&]() {
|
2019-11-19 15:58:24 +01:00
|
|
|
switch (simple_config_turn_ % 4) {
|
2019-10-03 21:28:01 +02:00
|
|
|
case 2:
|
2018-05-24 17:09:27 +02:00
|
|
|
return get_simple_config_azure;
|
2019-11-19 15:58:24 +01:00
|
|
|
case 3:
|
|
|
|
return get_simple_config_firebase_remote_config;
|
2019-11-19 17:01:12 +01:00
|
|
|
case 4:
|
|
|
|
return get_simple_config_firebase_realtime;
|
2019-11-19 17:26:39 +01:00
|
|
|
case 5:
|
|
|
|
return get_simple_config_firebase_firestore;
|
2018-05-24 17:09:27 +02:00
|
|
|
case 0:
|
2018-03-12 19:04:03 +01:00
|
|
|
return get_simple_config_google_dns;
|
2019-10-03 21:28:01 +02:00
|
|
|
case 1:
|
|
|
|
default:
|
|
|
|
return get_simple_config_mozilla_dns;
|
2018-03-12 19:04:03 +01:00
|
|
|
}
|
|
|
|
}();
|
2018-06-30 15:35:37 +02:00
|
|
|
simple_config_query_ =
|
|
|
|
get_simple_config(std::move(promise), &G()->shared_config(), G()->is_test_dc(), G()->get_gc_scheduler_id());
|
2018-12-31 20:04:05 +01:00
|
|
|
simple_config_turn_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_full_config) {
|
|
|
|
ref_cnt_++;
|
|
|
|
VLOG(config_recoverer) << "ASK FULL CONFIG";
|
2019-07-11 13:25:20 +02:00
|
|
|
full_config_query_ =
|
|
|
|
get_full_config(dc_options_.dc_options[dc_options_i_],
|
2019-08-09 14:14:07 +02:00
|
|
|
PromiseCreator::lambda([actor_id = actor_id(this)](Result<FullConfig> r_full_config) {
|
2019-07-11 13:25:20 +02:00
|
|
|
send_closure(actor_id, &ConfigRecoverer::on_full_config, std::move(r_full_config), false);
|
2019-08-09 14:14:07 +02:00
|
|
|
}),
|
|
|
|
actor_shared(this));
|
2018-12-31 20:04:05 +01:00
|
|
|
dc_options_i_ = (dc_options_i_ + 1) % dc_options_.dc_options.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wakeup_timestamp) {
|
|
|
|
VLOG(config_recoverer) << "Wakeup in " << format::as_time(wakeup_timestamp.in());
|
|
|
|
set_timeout_at(wakeup_timestamp.at());
|
|
|
|
} else {
|
|
|
|
VLOG(config_recoverer) << "Wakeup NEVER";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_up() override {
|
|
|
|
class StateCallback : public StateManager::Callback {
|
|
|
|
public:
|
|
|
|
explicit StateCallback(ActorId<ConfigRecoverer> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
bool on_state(StateManager::State state) override {
|
|
|
|
send_closure(parent_, &ConfigRecoverer::on_connecting, state == StateManager::State::Connecting);
|
|
|
|
return parent_.is_alive();
|
|
|
|
}
|
|
|
|
bool on_network(NetType network_type, uint32 network_generation) override {
|
|
|
|
send_closure(parent_, &ConfigRecoverer::on_network, network_type != NetType::None, network_generation);
|
|
|
|
return parent_.is_alive();
|
|
|
|
}
|
|
|
|
bool on_online(bool online_flag) override {
|
|
|
|
send_closure(parent_, &ConfigRecoverer::on_online, online_flag);
|
|
|
|
return parent_.is_alive();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorId<ConfigRecoverer> parent_;
|
|
|
|
};
|
|
|
|
send_closure(G()->state_manager(), &StateManager::add_callback, make_unique<StateCallback>(actor_id(this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_dc_options() {
|
|
|
|
auto v = simple_config_.dc_options;
|
|
|
|
v.insert(v.begin(), dc_options_update_.dc_options.begin(), dc_options_update_.dc_options.end());
|
|
|
|
if (v != dc_options_.dc_options) {
|
|
|
|
dc_options_.dc_options = std::move(v);
|
|
|
|
dc_options_i_ = 0;
|
|
|
|
dc_options_at_ = Time::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConfigManager::ConfigManager(ActorShared<> parent) : parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::start_up() {
|
|
|
|
ref_cnt_++;
|
|
|
|
config_recoverer_ = create_actor<ConfigRecoverer>("Recoverer", actor_shared());
|
|
|
|
send_closure(config_recoverer_, &ConfigRecoverer::on_dc_options_update, load_dc_options_update());
|
2019-12-19 02:23:14 +01:00
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
auto expire_time = load_config_expire_time();
|
|
|
|
if (expire_time.is_in_past()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
request_config();
|
|
|
|
} else {
|
2019-02-10 01:45:15 +01:00
|
|
|
expire_time_ = expire_time;
|
|
|
|
set_timeout_in(expire_time_.in());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::hangup_shared() {
|
|
|
|
ref_cnt_--;
|
|
|
|
try_stop();
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::hangup() {
|
|
|
|
ref_cnt_--;
|
|
|
|
config_recoverer_.reset();
|
|
|
|
try_stop();
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::loop() {
|
2019-02-10 01:45:15 +01:00
|
|
|
if (expire_time_ && expire_time_.is_in_past()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
request_config();
|
2019-02-10 01:45:15 +01:00
|
|
|
expire_time_ = {};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::try_stop() {
|
|
|
|
if (ref_cnt_ == 0) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::request_config() {
|
2019-12-19 21:00:28 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
if (config_sent_cnt_ != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
request_config_from_dc_impl(DcId::main());
|
|
|
|
}
|
|
|
|
|
2019-12-19 02:23:14 +01:00
|
|
|
void ConfigManager::get_app_config(Promise<td_api::object_ptr<td_api::JsonValue>> &&promise) {
|
2019-12-19 21:00:28 +01:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request aborted"));
|
|
|
|
}
|
|
|
|
|
2019-12-19 02:23:14 +01:00
|
|
|
get_app_config_queries_.push_back(std::move(promise));
|
|
|
|
if (get_app_config_queries_.size() == 1) {
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::help_getAppConfig()), DcId::main(),
|
|
|
|
NetQuery::Type::Common, NetQuery::AuthFlag::Off, NetQuery::GzipFlag::On,
|
|
|
|
60 * 60 * 24),
|
|
|
|
actor_shared(this, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 21:00:28 +01:00
|
|
|
void ConfigManager::get_content_settings(Promise<Unit> &&promise) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request aborted"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto auth_manager = G()->td().get_actor_unsafe()->auth_manager_.get();
|
2019-12-20 14:16:30 +01:00
|
|
|
if (auth_manager == nullptr || !auth_manager->is_authorized() || auth_manager->is_bot()) {
|
2019-12-19 21:00:28 +01:00
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
get_content_settings_queries_.push_back(std::move(promise));
|
|
|
|
if (get_content_settings_queries_.size() == 1) {
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::account_getContentSettings())),
|
|
|
|
actor_shared(this, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 00:58:41 +01:00
|
|
|
void ConfigManager::set_content_settings(bool ignore_sensitive_content_restrictions, Promise<Unit> &&promise) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return promise.set_error(Status::Error(500, "Request aborted"));
|
|
|
|
}
|
|
|
|
|
2019-12-20 02:06:38 +01:00
|
|
|
last_set_content_settings_ = ignore_sensitive_content_restrictions;
|
2019-12-20 00:58:41 +01:00
|
|
|
auto &queries = set_content_settings_queries_[ignore_sensitive_content_restrictions];
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (!is_set_content_settings_request_sent_) {
|
|
|
|
is_set_content_settings_request_sent_ = true;
|
|
|
|
int32 flags = 0;
|
|
|
|
if (ignore_sensitive_content_restrictions) {
|
|
|
|
flags |= telegram_api::account_setContentSettings::SENSITIVE_ENABLED_MASK;
|
|
|
|
}
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(
|
|
|
|
G()->net_query_creator().create(
|
|
|
|
create_storer(telegram_api::account_setContentSettings(flags, false /*ignored*/))),
|
|
|
|
actor_shared(this, 3 + static_cast<uint64>(ignore_sensitive_content_restrictions)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::on_dc_options_update(DcOptions dc_options) {
|
|
|
|
save_dc_options_update(dc_options);
|
|
|
|
send_closure(config_recoverer_, &ConfigRecoverer::on_dc_options_update, std::move(dc_options));
|
|
|
|
if (dc_options.dc_options.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
expire_time_ = Timestamp::now();
|
|
|
|
save_config_expire(expire_time_);
|
|
|
|
set_timeout_in(expire_time_.in());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::request_config_from_dc_impl(DcId dc_id) {
|
|
|
|
config_sent_cnt_++;
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(
|
|
|
|
G()->net_query_creator().create(create_storer(telegram_api::help_getConfig()), dc_id, NetQuery::Type::Common,
|
|
|
|
NetQuery::AuthFlag::Off, NetQuery::GzipFlag::On, 60 * 60 * 24),
|
2019-12-19 02:23:14 +01:00
|
|
|
actor_shared(this, 0));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-12-20 02:19:40 +01:00
|
|
|
void ConfigManager::set_ignore_sensitive_content_restrictions(bool ignore_sensitive_content_restrictions) {
|
2019-12-20 14:11:58 +01:00
|
|
|
G()->shared_config().set_option_boolean("ignore_sensitive_content_restrictions",
|
|
|
|
ignore_sensitive_content_restrictions);
|
2019-12-20 02:19:40 +01:00
|
|
|
bool have_ignored_restriction_reasons = G()->shared_config().have_option("ignored_restriction_reasons");
|
|
|
|
if (have_ignored_restriction_reasons != ignore_sensitive_content_restrictions) {
|
|
|
|
get_app_config(Auto());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void ConfigManager::on_result(NetQueryPtr res) {
|
2019-12-19 02:23:14 +01:00
|
|
|
auto token = get_link_token();
|
2019-12-20 00:58:41 +01:00
|
|
|
if (token == 3 || token == 4) {
|
|
|
|
is_set_content_settings_request_sent_ = false;
|
|
|
|
bool ignore_sensitive_content_restrictions = (token == 4);
|
|
|
|
auto promises = std::move(set_content_settings_queries_[ignore_sensitive_content_restrictions]);
|
|
|
|
set_content_settings_queries_[ignore_sensitive_content_restrictions].clear();
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_setContentSettings>(std::move(res));
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_error(result_ptr.error().clone());
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-20 02:19:40 +01:00
|
|
|
if (G()->shared_config().get_option_boolean("can_ignore_sensitive_content_restrictions") &&
|
2019-12-20 02:06:38 +01:00
|
|
|
last_set_content_settings_ == ignore_sensitive_content_restrictions) {
|
2019-12-20 02:19:40 +01:00
|
|
|
set_ignore_sensitive_content_restrictions(ignore_sensitive_content_restrictions);
|
2019-12-20 00:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!set_content_settings_queries_[!ignore_sensitive_content_restrictions].empty()) {
|
2019-12-20 02:06:38 +01:00
|
|
|
if (ignore_sensitive_content_restrictions == last_set_content_settings_) {
|
|
|
|
promises = std::move(set_content_settings_queries_[!ignore_sensitive_content_restrictions]);
|
|
|
|
set_content_settings_queries_[!ignore_sensitive_content_restrictions].clear();
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set_content_settings(!ignore_sensitive_content_restrictions, Auto());
|
|
|
|
}
|
2019-12-20 00:58:41 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-12-19 21:00:28 +01:00
|
|
|
if (token == 2) {
|
|
|
|
auto promises = std::move(get_content_settings_queries_);
|
|
|
|
get_content_settings_queries_.clear();
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
auto result_ptr = fetch_result<telegram_api::account_getContentSettings>(std::move(res));
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_error(result_ptr.error().clone());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.move_as_ok();
|
2019-12-20 02:19:40 +01:00
|
|
|
set_ignore_sensitive_content_restrictions(result->sensitive_enabled_);
|
|
|
|
G()->shared_config().set_option_boolean("can_ignore_sensitive_content_restrictions", result->sensitive_can_change_);
|
2019-12-19 21:00:28 +01:00
|
|
|
|
|
|
|
for (auto &promise : promises) {
|
|
|
|
promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
if (token == 1) {
|
|
|
|
auto promises = std::move(get_app_config_queries_);
|
|
|
|
get_app_config_queries_.clear();
|
|
|
|
CHECK(!promises.empty());
|
|
|
|
auto result_ptr = fetch_result<telegram_api::help_getAppConfig>(std::move(res));
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
for (auto &promise : promises) {
|
2019-12-19 02:58:03 +01:00
|
|
|
if (!promise) {
|
|
|
|
promise.set_value(nullptr);
|
|
|
|
} else {
|
|
|
|
promise.set_error(result_ptr.error().clone());
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
}
|
2019-12-19 21:00:28 +01:00
|
|
|
return;
|
2019-12-19 02:23:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = result_ptr.move_as_ok();
|
2019-12-19 02:58:03 +01:00
|
|
|
process_app_config(result);
|
2019-12-19 02:23:14 +01:00
|
|
|
for (auto &promise : promises) {
|
2019-12-19 02:58:03 +01:00
|
|
|
if (!promise) {
|
|
|
|
promise.set_value(nullptr);
|
|
|
|
} else {
|
|
|
|
promise.set_value(convert_json_value_object(result));
|
|
|
|
}
|
2019-12-19 02:23:14 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(token == 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(config_sent_cnt_ > 0);
|
|
|
|
config_sent_cnt_--;
|
|
|
|
auto r_config = fetch_result<telegram_api::help_getConfig>(std::move(res));
|
|
|
|
if (r_config.is_error()) {
|
2018-04-19 19:21:26 +02:00
|
|
|
if (!G()->close_flag()) {
|
|
|
|
LOG(ERROR) << "TODO: getConfig failed: " << r_config.error();
|
2019-02-10 01:45:15 +01:00
|
|
|
expire_time_ = Timestamp::in(60.0); // try again in a minute
|
|
|
|
set_timeout_in(expire_time_.in());
|
2018-04-19 19:21:26 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
on_dc_options_update(DcOptions());
|
|
|
|
process_config(r_config.move_as_ok());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::save_dc_options_update(DcOptions dc_options) {
|
|
|
|
if (dc_options.dc_options.empty()) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("dc_options_update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("dc_options_update", log_event_store(dc_options).as_slice().str());
|
|
|
|
}
|
|
|
|
|
|
|
|
DcOptions ConfigManager::load_dc_options_update() {
|
|
|
|
auto log_event_dc_options = G()->td_db()->get_binlog_pmc()->get("dc_options_update");
|
|
|
|
DcOptions dc_options;
|
|
|
|
if (!log_event_dc_options.empty()) {
|
|
|
|
log_event_parse(dc_options, log_event_dc_options).ensure();
|
|
|
|
}
|
|
|
|
return dc_options;
|
|
|
|
}
|
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
Timestamp ConfigManager::load_config_expire_time() {
|
|
|
|
auto expires_in = to_integer<int32>(G()->td_db()->get_binlog_pmc()->get("config_expire")) - Clocks::system();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-02-10 01:45:15 +01:00
|
|
|
if (expires_in < 0 || expires_in > 60 * 60 /* 1 hour */) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return Timestamp::now();
|
|
|
|
} else {
|
2019-02-10 01:45:15 +01:00
|
|
|
return Timestamp::in(expires_in);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::save_config_expire(Timestamp timestamp) {
|
2019-02-10 01:45:15 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->set("config_expire",
|
|
|
|
to_string(static_cast<int>(Clocks::system() + expire_time_.in())));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::process_config(tl_object_ptr<telegram_api::config> config) {
|
|
|
|
bool is_from_main_dc = G()->net_query_dispatcher().main_dc_id().get_value() == config->this_dc_;
|
|
|
|
|
|
|
|
LOG(INFO) << to_string(config);
|
2019-12-19 02:58:03 +01:00
|
|
|
auto reload_in = clamp(config->expires_ - config->date_, 60, 86400);
|
2018-12-31 20:04:05 +01:00
|
|
|
save_config_expire(Timestamp::in(reload_in));
|
|
|
|
reload_in -= Random::fast(0, reload_in / 5);
|
|
|
|
if (!is_from_main_dc) {
|
|
|
|
reload_in = 0;
|
|
|
|
}
|
2019-02-10 01:45:15 +01:00
|
|
|
expire_time_ = Timestamp::in(reload_in);
|
|
|
|
set_timeout_at(expire_time_.at());
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG_IF(ERROR, config->test_mode_ != G()->is_test_dc()) << "Wrong parameter is_test";
|
|
|
|
|
|
|
|
ConfigShared &shared_config = G()->shared_config();
|
|
|
|
|
|
|
|
// Do not save dc_options in config, because it will be interpreted and saved by ConnectionCreator.
|
|
|
|
send_closure(G()->connection_creator(), &ConnectionCreator::on_dc_options, DcOptions(config->dc_options_));
|
|
|
|
|
2018-03-08 14:49:45 +01:00
|
|
|
shared_config.set_option_integer("recent_stickers_limit", config->stickers_recent_limit_);
|
2018-12-31 20:04:05 +01:00
|
|
|
shared_config.set_option_integer("favorite_stickers_limit", config->stickers_faved_limit_);
|
|
|
|
shared_config.set_option_integer("saved_animations_limit", config->saved_gifs_limit_);
|
|
|
|
shared_config.set_option_integer("channels_read_media_period", config->channels_read_media_period_);
|
|
|
|
|
|
|
|
shared_config.set_option_boolean("test_mode", config->test_mode_);
|
|
|
|
shared_config.set_option_integer("forwarded_message_count_max", config->forwarded_count_max_);
|
|
|
|
shared_config.set_option_integer("basic_group_size_max", config->chat_size_max_);
|
|
|
|
shared_config.set_option_integer("supergroup_size_max", config->megagroup_size_max_);
|
|
|
|
shared_config.set_option_integer("pinned_chat_count_max", config->pinned_dialogs_count_max_);
|
2019-08-22 17:24:02 +02:00
|
|
|
shared_config.set_option_integer("pinned_archived_chat_count_max", config->pinned_infolder_count_max_);
|
2018-05-03 12:18:07 +02:00
|
|
|
if (is_from_main_dc || !shared_config.have_option("expect_blocking")) {
|
2018-05-03 14:36:05 +02:00
|
|
|
shared_config.set_option_boolean("expect_blocking",
|
2018-05-03 12:18:07 +02:00
|
|
|
(config->flags_ & telegram_api::config::BLOCKED_MODE_MASK) != 0);
|
|
|
|
}
|
2018-06-26 22:46:15 +02:00
|
|
|
if (is_from_main_dc || !shared_config.have_option("dc_txt_domain_name")) {
|
|
|
|
shared_config.set_option_string("dc_txt_domain_name", config->dc_txt_domain_name_);
|
|
|
|
}
|
2018-03-08 16:01:45 +01:00
|
|
|
if (is_from_main_dc || !shared_config.have_option("t_me_url")) {
|
2018-05-17 20:08:51 +02:00
|
|
|
auto url = config->me_url_prefix_;
|
|
|
|
if (!url.empty()) {
|
|
|
|
if (url.back() != '/') {
|
|
|
|
url.push_back('/');
|
|
|
|
}
|
|
|
|
shared_config.set_option_string("t_me_url", url);
|
|
|
|
}
|
2018-03-08 16:01:45 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_from_main_dc) {
|
2018-06-26 22:46:15 +02:00
|
|
|
shared_config.set_option_integer("webfile_dc_id", config->webfile_dc_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((config->flags_ & telegram_api::config::TMP_SESSIONS_MASK) != 0) {
|
2018-09-04 03:29:26 +02:00
|
|
|
shared_config.set_option_integer("session_count", config->tmp_sessions_);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2018-09-04 03:29:26 +02:00
|
|
|
shared_config.set_option_empty("session_count");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-03 00:52:43 +02:00
|
|
|
if ((config->flags_ & telegram_api::config::SUGGESTED_LANG_CODE_MASK) != 0) {
|
2018-09-04 03:29:26 +02:00
|
|
|
shared_config.set_option_string("suggested_language_pack_id", config->suggested_lang_code_);
|
|
|
|
shared_config.set_option_integer("language_pack_version", config->lang_pack_version_);
|
2019-02-11 01:45:09 +01:00
|
|
|
shared_config.set_option_integer("base_language_pack_version", config->base_lang_pack_version_);
|
2018-07-03 00:52:43 +02:00
|
|
|
} else {
|
2018-09-04 03:29:26 +02:00
|
|
|
shared_config.set_option_empty("suggested_language_pack_id");
|
|
|
|
shared_config.set_option_empty("language_pack_version");
|
2019-02-11 01:45:09 +01:00
|
|
|
shared_config.set_option_empty("base_language_pack_version");
|
2018-07-03 00:52:43 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 16:01:45 +01:00
|
|
|
if (is_from_main_dc) {
|
|
|
|
shared_config.set_option_integer("edit_time_limit", config->edit_time_limit_);
|
|
|
|
shared_config.set_option_boolean("revoke_pm_inbox",
|
|
|
|
(config->flags_ & telegram_api::config::REVOKE_PM_INBOX_MASK) != 0);
|
|
|
|
shared_config.set_option_integer("revoke_time_limit", config->revoke_time_limit_);
|
|
|
|
shared_config.set_option_integer("revoke_pm_time_limit", config->revoke_pm_time_limit_);
|
2018-03-04 21:50:38 +01:00
|
|
|
|
2018-03-08 16:01:45 +01:00
|
|
|
shared_config.set_option_integer("rating_e_decay", config->rating_e_decay_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
shared_config.set_option_boolean("calls_enabled", config->phonecalls_enabled_);
|
|
|
|
}
|
|
|
|
shared_config.set_option_integer("call_ring_timeout_ms", config->call_ring_timeout_ms_);
|
|
|
|
shared_config.set_option_integer("call_connect_timeout_ms", config->call_connect_timeout_ms_);
|
|
|
|
shared_config.set_option_integer("call_packet_timeout_ms", config->call_packet_timeout_ms_);
|
|
|
|
shared_config.set_option_integer("call_receive_timeout_ms", config->call_receive_timeout_ms_);
|
|
|
|
|
2018-06-27 01:28:49 +02:00
|
|
|
shared_config.set_option_integer("message_text_length_max", config->message_length_max_);
|
|
|
|
shared_config.set_option_integer("message_caption_length_max", config->caption_length_max_);
|
|
|
|
|
2018-06-27 01:35:32 +02:00
|
|
|
if (config->gif_search_username_.empty()) {
|
|
|
|
shared_config.set_option_empty("animation_search_bot_username");
|
|
|
|
} else {
|
|
|
|
shared_config.set_option_string("animation_search_bot_username", config->gif_search_username_);
|
|
|
|
}
|
|
|
|
if (config->venue_search_username_.empty()) {
|
|
|
|
shared_config.set_option_empty("venue_search_bot_username");
|
|
|
|
} else {
|
|
|
|
shared_config.set_option_string("venue_search_bot_username", config->venue_search_username_);
|
|
|
|
}
|
|
|
|
if (config->img_search_username_.empty()) {
|
|
|
|
shared_config.set_option_empty("photo_search_bot_username");
|
|
|
|
} else {
|
|
|
|
shared_config.set_option_string("photo_search_bot_username", config->img_search_username_);
|
|
|
|
}
|
|
|
|
|
2019-09-28 04:14:21 +02:00
|
|
|
auto fix_timeout_ms = [](int32 timeout_ms) {
|
|
|
|
return clamp(timeout_ms, 1000, 86400 * 1000);
|
|
|
|
};
|
2018-11-15 23:03:04 +01:00
|
|
|
|
|
|
|
shared_config.set_option_integer("online_update_period_ms", fix_timeout_ms(config->online_update_period_ms_));
|
|
|
|
|
|
|
|
shared_config.set_option_integer("online_cloud_timeout_ms", fix_timeout_ms(config->online_cloud_timeout_ms_));
|
|
|
|
shared_config.set_option_integer("notification_cloud_delay_ms", fix_timeout_ms(config->notify_cloud_delay_ms_));
|
|
|
|
shared_config.set_option_integer("notification_default_delay_ms", fix_timeout_ms(config->notify_default_delay_ms_));
|
2018-11-08 14:06:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// delete outdated options
|
2018-09-04 03:29:26 +02:00
|
|
|
shared_config.set_option_empty("suggested_language_code");
|
2018-12-31 20:04:05 +01:00
|
|
|
shared_config.set_option_empty("chat_big_size");
|
|
|
|
shared_config.set_option_empty("group_size_max");
|
|
|
|
shared_config.set_option_empty("saved_gifs_limit");
|
|
|
|
shared_config.set_option_empty("sessions_count");
|
|
|
|
shared_config.set_option_empty("forwarded_messages_count_max");
|
|
|
|
shared_config.set_option_empty("broadcast_size_max");
|
|
|
|
shared_config.set_option_empty("group_chat_size_max");
|
|
|
|
shared_config.set_option_empty("chat_size_max");
|
|
|
|
shared_config.set_option_empty("megagroup_size_max");
|
|
|
|
shared_config.set_option_empty("offline_blur_timeout_ms");
|
|
|
|
shared_config.set_option_empty("offline_idle_timeout_ms");
|
|
|
|
shared_config.set_option_empty("notify_cloud_delay_ms");
|
|
|
|
shared_config.set_option_empty("notify_default_delay_ms");
|
2018-03-05 16:34:33 +01:00
|
|
|
shared_config.set_option_empty("large_chat_size");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-02-22 01:36:40 +01:00
|
|
|
if (is_from_main_dc) {
|
|
|
|
for (auto &feature : shared_config.get_options("disabled_")) {
|
|
|
|
shared_config.set_option_empty(feature.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO implement online status updates
|
|
|
|
// shared_config.set_option_integer("offline_blur_timeout_ms", config->offline_blur_timeout_ms_);
|
|
|
|
// shared_config.set_option_integer("offline_idle_timeout_ms", config->offline_idle_timeout_ms_);
|
2018-11-08 14:06:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
// shared_config.set_option_integer("push_chat_period_ms", config->push_chat_period_ms_);
|
|
|
|
// shared_config.set_option_integer("push_chat_limit", config->push_chat_limit_);
|
2019-12-19 02:58:03 +01:00
|
|
|
|
|
|
|
if (is_from_main_dc) {
|
|
|
|
get_app_config(Auto());
|
2019-12-19 21:00:28 +01:00
|
|
|
if (!shared_config.have_option("can_ignore_sensitive_content_restrictions") ||
|
|
|
|
!shared_config.have_option("ignore_sensitive_content_restrictions")) {
|
|
|
|
get_content_settings(Auto());
|
|
|
|
}
|
2019-12-19 02:58:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigManager::process_app_config(tl_object_ptr<telegram_api::JSONValue> &config) {
|
|
|
|
CHECK(config != nullptr);
|
|
|
|
LOG(INFO) << "Receive app config " << to_string(config);
|
|
|
|
|
2019-12-19 17:18:42 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::jsonObjectValue>> new_values;
|
|
|
|
string wallet_blockchain_name;
|
|
|
|
string wallet_config;
|
2019-12-19 17:58:33 +01:00
|
|
|
string ignored_restriction_reasons;
|
2019-12-19 02:58:03 +01:00
|
|
|
if (config->get_id() == telegram_api::jsonObject::ID) {
|
2019-12-19 17:18:42 +01:00
|
|
|
for (auto &key_value : static_cast<telegram_api::jsonObject *>(config.get())->value_) {
|
|
|
|
Slice key = key_value->key_;
|
|
|
|
telegram_api::JSONValue *value = key_value->value_.get();
|
|
|
|
if (key == "test" || key == "wallet_enabled") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "wallet_blockchain_name") {
|
|
|
|
if (value->get_id() == telegram_api::jsonString::ID) {
|
|
|
|
wallet_blockchain_name = std::move(static_cast<telegram_api::jsonString *>(value)->value_);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive unexpected wallet_blockchain_name " << to_string(*value);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "wallet_config") {
|
|
|
|
if (value->get_id() == telegram_api::jsonString::ID) {
|
|
|
|
wallet_config = std::move(static_cast<telegram_api::jsonString *>(value)->value_);
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive unexpected wallet_config " << to_string(*value);
|
|
|
|
}
|
2019-12-19 02:58:03 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-19 17:58:33 +01:00
|
|
|
if (key == "ignore_restriction_reasons") {
|
|
|
|
if (value->get_id() == telegram_api::jsonArray::ID) {
|
|
|
|
auto reasons = std::move(static_cast<telegram_api::jsonArray *>(value)->value_);
|
|
|
|
for (auto &reason : reasons) {
|
|
|
|
if (reason->get_id() == telegram_api::jsonString::ID) {
|
|
|
|
Slice reason_name = static_cast<telegram_api::jsonString *>(reason.get())->value_;
|
|
|
|
if (!reason_name.empty() && reason_name.find(',') == Slice::npos) {
|
|
|
|
if (!ignored_restriction_reasons.empty()) {
|
|
|
|
ignored_restriction_reasons += ',';
|
|
|
|
}
|
|
|
|
ignored_restriction_reasons.append(reason_name.begin(), reason_name.end());
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive unexpected restriction reason " << reason_name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive unexpected restriction reason " << to_string(reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive unexpected ignore_restriction_reasons " << to_string(*value);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-19 02:58:03 +01:00
|
|
|
|
2019-12-19 17:18:42 +01:00
|
|
|
new_values.push_back(std::move(key_value));
|
2019-12-19 02:58:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive wrong app config " << to_string(config);
|
|
|
|
}
|
2019-12-19 17:18:42 +01:00
|
|
|
config = make_tl_object<telegram_api::jsonObject>(std::move(new_values));
|
|
|
|
|
|
|
|
ConfigShared &shared_config = G()->shared_config();
|
|
|
|
if (wallet_config.empty()) {
|
|
|
|
shared_config.set_option_empty("default_ton_blockchain_config");
|
|
|
|
shared_config.set_option_empty("default_ton_blockchain_name");
|
|
|
|
} else {
|
|
|
|
shared_config.set_option_string("default_ton_blockchain_name", wallet_blockchain_name);
|
|
|
|
shared_config.set_option_string("default_ton_blockchain_config", wallet_config);
|
|
|
|
}
|
2019-12-20 14:11:58 +01:00
|
|
|
|
2019-12-19 17:58:33 +01:00
|
|
|
if (ignored_restriction_reasons.empty()) {
|
|
|
|
shared_config.set_option_empty("ignored_restriction_reasons");
|
2019-12-19 21:00:28 +01:00
|
|
|
|
|
|
|
if (shared_config.get_option_boolean("ignore_sensitive_content_restrictions", true)) {
|
|
|
|
get_content_settings(Auto());
|
|
|
|
}
|
2019-12-19 17:58:33 +01:00
|
|
|
} else {
|
|
|
|
shared_config.set_option_string("ignored_restriction_reasons", ignored_restriction_reasons);
|
2019-12-19 21:00:28 +01:00
|
|
|
|
|
|
|
if (!shared_config.get_option_boolean("can_ignore_sensitive_content_restrictions")) {
|
|
|
|
get_content_settings(Auto());
|
|
|
|
}
|
2019-12-19 17:58:33 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|