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/net/MtprotoHeader.h"
|
|
|
|
|
2019-07-15 20:37:45 +02:00
|
|
|
#include "td/telegram/JsonValue.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/telegram/LanguagePackManager.h"
|
2020-04-08 23:09:55 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2019-08-15 17:16:09 +02:00
|
|
|
#include "td/telegram/Version.h"
|
2019-07-15 20:37:45 +02:00
|
|
|
|
|
|
|
#include "td/tl/tl_object_store.h"
|
2018-08-06 16:22:22 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
namespace {
|
2018-07-02 22:36:45 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class HeaderStorer {
|
|
|
|
public:
|
|
|
|
HeaderStorer(const MtprotoHeader::Options &options, bool is_anonymous)
|
|
|
|
: options(options), is_anonymous(is_anonymous) {
|
|
|
|
}
|
|
|
|
template <class StorerT>
|
|
|
|
void store(StorerT &storer) const {
|
|
|
|
using td::store;
|
|
|
|
// invokeWithLayer#da9b0d0d {X:Type} layer:int query:!X = X;
|
|
|
|
store(static_cast<int32>(0xda9b0d0d), storer);
|
2019-08-15 17:16:09 +02:00
|
|
|
store(MTPROTO_LAYER, storer);
|
2018-05-08 14:50:33 +02:00
|
|
|
// initConnection#785188b8 {X:Type} flags:# api_id:int device_model:string system_version:string app_version:string
|
|
|
|
// system_lang_code:string lang_pack:string lang_code:string proxy:flags.0?InputClientProxy query:!X = X;
|
|
|
|
store(static_cast<int32>(0x785188b8), storer);
|
|
|
|
int32 flags = 0;
|
2018-05-22 13:09:58 +02:00
|
|
|
bool have_proxy = !is_anonymous && options.proxy.type() == Proxy::Type::Mtproto;
|
|
|
|
if (have_proxy) {
|
2018-05-08 14:50:33 +02:00
|
|
|
flags |= 1 << 0;
|
|
|
|
}
|
2020-04-08 23:09:55 +02:00
|
|
|
if (!is_anonymous) {
|
2019-07-15 20:37:45 +02:00
|
|
|
flags |= 1 << 1;
|
|
|
|
}
|
2018-05-22 13:09:58 +02:00
|
|
|
if (options.is_emulator) {
|
|
|
|
flags |= 1 << 10;
|
|
|
|
}
|
2018-05-08 14:50:33 +02:00
|
|
|
store(flags, storer);
|
2018-12-31 20:04:05 +01:00
|
|
|
store(options.api_id, storer);
|
|
|
|
if (is_anonymous) {
|
|
|
|
store(Slice("n/a"), storer);
|
|
|
|
store(Slice("n/a"), storer);
|
|
|
|
} else {
|
|
|
|
store(options.device_model, storer);
|
|
|
|
store(options.system_version, storer);
|
|
|
|
}
|
|
|
|
store(options.application_version, storer);
|
|
|
|
store(options.system_language_code, storer);
|
2018-08-06 16:22:22 +02:00
|
|
|
if (is_anonymous || options.language_pack.empty() ||
|
|
|
|
LanguagePackManager::is_custom_language_code(options.language_code)) {
|
2018-07-02 22:44:23 +02:00
|
|
|
store(Slice(), storer);
|
|
|
|
store(Slice(), storer);
|
|
|
|
} else {
|
|
|
|
store(options.language_pack, storer);
|
|
|
|
if (options.language_code.empty()) {
|
|
|
|
store(Slice("en"), storer);
|
|
|
|
} else {
|
|
|
|
store(options.language_code, storer);
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 13:09:58 +02:00
|
|
|
if (have_proxy) {
|
2018-05-08 16:48:30 +02:00
|
|
|
// inputClientProxy#75588b3f address:string port:int = InputClientProxy;
|
|
|
|
store(static_cast<int32>(0x75588b3f), storer);
|
|
|
|
store(Slice(options.proxy.server()), storer);
|
|
|
|
store(options.proxy.port(), storer);
|
2018-05-08 14:50:33 +02:00
|
|
|
}
|
2020-04-08 23:09:55 +02:00
|
|
|
if (!is_anonymous) {
|
|
|
|
telegram_api::object_ptr<telegram_api::JSONValue> json_value;
|
|
|
|
if (options.parameters.empty()) {
|
|
|
|
json_value = make_tl_object<telegram_api::jsonObject>(vector<tl_object_ptr<telegram_api::jsonObjectValue>>());
|
|
|
|
} else {
|
|
|
|
auto parameters_copy = options.parameters;
|
|
|
|
json_value = get_input_json_value(parameters_copy).move_as_ok();
|
|
|
|
}
|
2019-07-15 20:37:45 +02:00
|
|
|
CHECK(json_value != nullptr);
|
2020-04-08 23:09:55 +02:00
|
|
|
if (json_value->get_id() == telegram_api::jsonObject::ID) {
|
|
|
|
auto &values = static_cast<telegram_api::jsonObject *>(json_value.get())->value_;
|
|
|
|
bool has_tz_offset = false;
|
|
|
|
for (auto &value : values) {
|
|
|
|
if (value->key_ == "tz_offset") {
|
|
|
|
has_tz_offset = true;
|
2021-11-01 20:32:49 +01:00
|
|
|
value->value_ = make_tl_object<telegram_api::jsonNumber>(options.tz_offset);
|
2020-04-08 23:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!has_tz_offset) {
|
|
|
|
values.push_back(make_tl_object<telegram_api::jsonObjectValue>(
|
2021-11-01 20:32:49 +01:00
|
|
|
"tz_offset", make_tl_object<telegram_api::jsonNumber>(options.tz_offset)));
|
2020-04-08 23:09:55 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-15 20:37:45 +02:00
|
|
|
TlStoreBoxedUnknown<TlStoreObject>::store(json_value, storer);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const MtprotoHeader::Options &options;
|
|
|
|
bool is_anonymous;
|
|
|
|
};
|
2018-07-02 22:36:45 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
string MtprotoHeader::gen_header(const MtprotoHeader::Options &options, bool is_anonymous) {
|
|
|
|
HeaderStorer storer(options, is_anonymous);
|
|
|
|
return serialize(storer);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|