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/net/SessionProxy.h"
|
|
|
|
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#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/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/net/Session.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
2018-02-03 13:58:18 +01:00
|
|
|
#include <functional>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace mtproto {
|
|
|
|
class RawConnection;
|
|
|
|
} // namespace mtproto
|
|
|
|
|
|
|
|
class SessionCallback : public Session::Callback {
|
|
|
|
public:
|
|
|
|
SessionCallback(ActorShared<SessionProxy> parent, DcId dc_id, bool allow_media_only, bool is_media, size_t hash)
|
|
|
|
: parent_(std::move(parent))
|
|
|
|
, dc_id_(dc_id)
|
|
|
|
, allow_media_only_(allow_media_only)
|
|
|
|
, is_media_(is_media)
|
|
|
|
, hash_(hash) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_failed() override {
|
|
|
|
send_closure(parent_, &SessionProxy::on_failed);
|
|
|
|
}
|
|
|
|
void on_closed() override {
|
|
|
|
send_closure(parent_, &SessionProxy::on_closed);
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
void request_raw_connection(Promise<unique_ptr<mtproto::RawConnection>> promise) override {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(G()->connection_creator(), &ConnectionCreator::request_raw_connection, dc_id_, allow_media_only_,
|
|
|
|
is_media_, std::move(promise), hash_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_tmp_auth_key_updated(mtproto::AuthKey auth_key) override {
|
|
|
|
send_closure(parent_, &SessionProxy::on_tmp_auth_key_updated, std::move(auth_key));
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:17:27 +02:00
|
|
|
void on_server_salt_updated(std::vector<mtproto::ServerSalt> server_salts) override {
|
|
|
|
send_closure(parent_, &SessionProxy::on_server_salt_updated, std::move(server_salts));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
ActorShared<SessionProxy> parent_;
|
|
|
|
DcId dc_id_;
|
|
|
|
bool allow_media_only_ = false;
|
|
|
|
bool is_media_ = false;
|
|
|
|
size_t hash_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
SessionProxy::SessionProxy(std::shared_ptr<AuthDataShared> shared_auth_data, bool is_main, bool allow_media_only,
|
2018-12-11 21:43:52 +01:00
|
|
|
bool is_media, bool use_pfs, bool is_cdn, bool need_destroy)
|
2018-12-31 20:04:05 +01:00
|
|
|
: auth_data_(std::move(shared_auth_data))
|
|
|
|
, is_main_(is_main)
|
|
|
|
, allow_media_only_(allow_media_only)
|
|
|
|
, is_media_(is_media)
|
|
|
|
, use_pfs_(use_pfs)
|
2017-12-29 21:34:39 +01:00
|
|
|
, is_cdn_(is_cdn)
|
|
|
|
, need_destroy_(need_destroy) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::start_up() {
|
|
|
|
class Listener : public AuthDataShared::Listener {
|
|
|
|
public:
|
|
|
|
explicit Listener(ActorShared<SessionProxy> session_proxy) : session_proxy_(std::move(session_proxy)) {
|
|
|
|
}
|
|
|
|
bool notify() override {
|
|
|
|
if (!session_proxy_.is_alive()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
send_closure(session_proxy_, &SessionProxy::update_auth_state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ActorShared<SessionProxy> session_proxy_;
|
|
|
|
};
|
|
|
|
auth_state_ = auth_data_->get_auth_state().first;
|
2018-09-27 03:19:03 +02:00
|
|
|
auth_data_->add_auth_key_listener(make_unique<Listener>(actor_shared(this)));
|
2017-12-29 21:34:39 +01:00
|
|
|
open_session();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::tear_down() {
|
|
|
|
for (auto &query : pending_queries_) {
|
|
|
|
query->resend();
|
|
|
|
G()->net_query_dispatcher().dispatch(std::move(query));
|
|
|
|
}
|
|
|
|
pending_queries_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::send(NetQueryPtr query) {
|
|
|
|
if (query->auth_flag() == NetQuery::AuthFlag::On && auth_state_ != AuthState::OK) {
|
|
|
|
query->debug(PSTRING() << get_name() << ": wait for auth");
|
|
|
|
pending_queries_.emplace_back(std::move(query));
|
|
|
|
return;
|
|
|
|
}
|
2017-12-29 21:34:39 +01:00
|
|
|
open_session(true);
|
2018-12-31 20:04:05 +01:00
|
|
|
query->debug(PSTRING() << get_name() << ": sent to session");
|
|
|
|
send_closure(session_, &Session::send, std::move(query));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::update_main_flag(bool is_main) {
|
|
|
|
if (is_main_ == is_main) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Update " << get_name() << " is_main to " << is_main;
|
|
|
|
is_main_ = is_main;
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2017-12-29 21:34:39 +01:00
|
|
|
void SessionProxy::update_destroy(bool need_destroy) {
|
|
|
|
need_destroy_ = need_destroy;
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionProxy::on_failed() {
|
|
|
|
if (session_generation_ != get_link_token()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:17:27 +02:00
|
|
|
void SessionProxy::update_mtproto_header() {
|
|
|
|
close_session();
|
|
|
|
open_session();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void SessionProxy::on_closed() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::close_session() {
|
|
|
|
send_closure(std::move(session_), &Session::close);
|
|
|
|
session_generation_++;
|
|
|
|
}
|
|
|
|
void SessionProxy::open_session(bool force) {
|
2017-12-29 21:34:39 +01:00
|
|
|
if (!session_.empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-11-03 07:33:22 +01:00
|
|
|
// There are several assumption that make this code OK
|
|
|
|
// 1. All unauthorized query will be sent into the same SessionProxy
|
|
|
|
// 2. All authorized query are delayed before we have authorization
|
|
|
|
// So only one SessionProxy will be active before we have authorization key
|
|
|
|
auto should_open = [&]() {
|
|
|
|
if (force) {
|
|
|
|
return true;
|
2018-11-01 17:34:18 +01:00
|
|
|
}
|
2018-11-03 07:33:22 +01:00
|
|
|
if (need_destroy_) {
|
2018-11-04 11:53:57 +01:00
|
|
|
return auth_state_ != AuthState::Empty;
|
2018-11-01 17:34:18 +01:00
|
|
|
}
|
2018-11-06 12:06:08 +01:00
|
|
|
if (auth_state_ != AuthState::OK) {
|
|
|
|
return false;
|
2018-11-03 07:33:22 +01:00
|
|
|
}
|
2018-11-06 12:06:08 +01:00
|
|
|
return is_main_ || !pending_queries_.empty();
|
2018-11-03 07:33:22 +01:00
|
|
|
}();
|
|
|
|
if (!should_open) {
|
|
|
|
return;
|
2017-12-29 21:34:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(session_.empty());
|
2018-05-23 18:49:05 +02:00
|
|
|
auto dc_id = auth_data_->dc_id();
|
2018-12-31 20:04:05 +01:00
|
|
|
string name = PSTRING() << "Session" << get_name().substr(Slice("SessionProxy").size());
|
2018-05-23 18:49:05 +02:00
|
|
|
string hash_string = PSTRING() << name << " " << dc_id.get_raw_id() << " " << allow_media_only_;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto hash = std::hash<std::string>()(hash_string);
|
2018-05-23 18:49:05 +02:00
|
|
|
int32 int_dc_id = dc_id.get_raw_id();
|
|
|
|
if (G()->is_test_dc()) {
|
|
|
|
int_dc_id += 10000;
|
|
|
|
}
|
2018-06-04 00:17:59 +02:00
|
|
|
if (allow_media_only_ && !is_cdn_) {
|
2018-05-23 18:49:05 +02:00
|
|
|
int_dc_id = -int_dc_id;
|
|
|
|
}
|
|
|
|
session_ = create_actor<Session>(
|
|
|
|
name,
|
|
|
|
make_unique<SessionCallback>(actor_shared(this, session_generation_), dc_id, allow_media_only_, is_media_, hash),
|
2017-12-29 21:34:39 +01:00
|
|
|
auth_data_, int_dc_id, is_main_, use_pfs_, is_cdn_, need_destroy_, tmp_auth_key_, server_salts_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::update_auth_state() {
|
2018-11-06 12:06:08 +01:00
|
|
|
auto old_auth_state = auth_state_;
|
2018-12-31 20:04:05 +01:00
|
|
|
auth_state_ = auth_data_->get_auth_state().first;
|
2018-11-06 12:06:08 +01:00
|
|
|
if (auth_state_ != old_auth_state && old_auth_state == AuthState::OK) {
|
|
|
|
close_session();
|
|
|
|
}
|
2018-11-02 16:55:55 +01:00
|
|
|
open_session();
|
2018-11-03 07:33:22 +01:00
|
|
|
if (session_.empty() || auth_state_ != AuthState::OK) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &query : pending_queries_) {
|
|
|
|
query->debug(PSTRING() << get_name() << ": sent to session");
|
|
|
|
send_closure(session_, &Session::send, std::move(query));
|
|
|
|
}
|
|
|
|
pending_queries_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SessionProxy::on_tmp_auth_key_updated(mtproto::AuthKey auth_key) {
|
2019-02-21 16:58:20 +01:00
|
|
|
Slice state;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (auth_key.empty()) {
|
2019-02-21 16:58:20 +01:00
|
|
|
state = Slice("Empty");
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (auth_key.auth_flag()) {
|
2019-02-21 16:58:20 +01:00
|
|
|
state = Slice("OK");
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-02-21 16:58:20 +01:00
|
|
|
state = Slice("NoAuth");
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(WARNING) << "Have tmp_auth_key " << auth_key.id() << ": " << state;
|
2018-12-31 20:04:05 +01:00
|
|
|
tmp_auth_key_ = std::move(auth_key);
|
|
|
|
}
|
2018-06-13 17:17:27 +02:00
|
|
|
void SessionProxy::on_server_salt_updated(std::vector<mtproto::ServerSalt> server_salts) {
|
|
|
|
server_salts_ = std::move(server_salts);
|
|
|
|
}
|
2018-04-24 18:21:47 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|