Better logic for time difference in tls obfuscation

GitOrigin-RevId: bbd69762a4700571ed7c67b4afbf30950b79b638
This commit is contained in:
Arseny Smirnov 2019-07-04 18:21:07 +02:00
parent 6dc06b5b18
commit a695b0823c
4 changed files with 35 additions and 5 deletions

View File

@ -455,7 +455,7 @@ class ConfigRecoverer : public Actor {
VLOG(config_recoverer) << "Config has expired at " << config->expires_;
}
G()->update_server_time_difference(config->date_ - Time::now());
G()->update_dns_time_difference(config->date_ - Time::now());
simple_config_expires_at_ = get_config_expire_time();
simple_config_at_ = Time::now_cached();
for (size_t i = 1; i < simple_config_.dc_options.size(); i++) {

View File

@ -71,17 +71,20 @@ Status Global::init(const TdParameters &parameters, ActorId<Td> td, unique_ptr<T
td_db_ = std::move(td_db_ptr);
string save_diff_str = td_db()->get_binlog_pmc()->get("server_time_difference");
auto default_time_difference = Clocks::system() - Time::now();
if (save_diff_str.empty()) {
server_time_difference_ = Clocks::system() - Time::now();
server_time_difference_ = default_time_difference;
server_time_difference_was_updated_ = false;
} else {
double save_diff;
unserialize(save_diff, save_diff_str).ensure();
double diff = save_diff + Clocks::system() - Time::now();
double diff = save_diff + default_time_difference;
LOG(DEBUG) << "LOAD: " << tag("server_time_difference", diff);
server_time_difference_ = diff;
server_time_difference_was_updated_ = false;
}
dns_time_difference_ = default_time_difference;
dns_time_difference_was_updated_ = false;
return Status::OK();
}
@ -99,6 +102,27 @@ void Global::update_server_time_difference(double diff) {
}
}
void Global::update_dns_time_difference(double diff) {
dns_time_difference_ = diff;
dns_time_difference_was_updated_ = true;
}
double Global::get_dns_time_difference() const {
// rely that was updated flag is monotonic. Currenly it is true. If it stops being monitonic at some point it won't
// lead to problems anyway.
bool dns_flag = dns_time_difference_was_updated_;
double dns_diff = dns_time_difference_;
bool server_flag = server_time_difference_was_updated_;
double server_diff = server_time_difference_;
if (dns_flag != server_flag) {
return dns_flag ? dns_diff : server_diff;
}
if (dns_flag) {
return std::max(dns_diff, server_diff);
}
return server_diff;
}
DcId Global::get_webfile_dc_id() const {
CHECK(shared_config_ != nullptr);
int32 dc_id = shared_config_->get_option_integer("webfile_dc_id");

View File

@ -137,6 +137,10 @@ class Global : public ActorContext {
return server_time_difference_.load(std::memory_order_relaxed);
}
void update_dns_time_difference(double diff);
double get_dns_time_difference() const;
ActorId<StateManager> state_manager() const {
return state_manager_;
}
@ -383,6 +387,8 @@ class Global : public ActorContext {
std::atomic<double> server_time_difference_{0.0};
std::atomic<bool> server_time_difference_was_updated_{false};
std::atomic<double> dns_time_difference_{0.0};
std::atomic<bool> dns_time_difference_was_updated_{false};
std::atomic<bool> close_flag_{false};
std::vector<std::shared_ptr<NetStatsCallback>> net_stats_file_callbacks_;

View File

@ -544,7 +544,7 @@ void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address
children_[token] = {
false, create_actor<TlsInit>("PingTlsInit", std::move(socket_fd), extra.mtproto_ip, "www.google.com",
hex_decode(proxy.proxy().secret().substr(2)).move_as_ok(), std::move(callback),
create_reference(token), G()->get_server_time_difference())};
create_reference(token), G()->get_dns_time_difference())};
} else {
UNREACHABLE();
}
@ -1033,7 +1033,7 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
children_[token] = {
true, create_actor<TlsInit>("TlsInit", std::move(socket_fd), extra.mtproto_ip, "www.google.com",
hex_decode(proxy.proxy().secret().substr(2)).move_as_ok(), std::move(callback),
create_reference(token), G()->get_server_time_difference())};
create_reference(token), G()->get_dns_time_difference())};
} else {
UNREACHABLE();
}