Add prefer_ipv6 option.

GitOrigin-RevId: b20e8c699e20c4fb6ec76cd53aeaeee509cd2115
This commit is contained in:
levlam 2018-07-01 17:19:59 +03:00
parent 5a11dd6c58
commit ac1d7a9657
9 changed files with 41 additions and 14 deletions

View File

@ -46,6 +46,10 @@ void StateManager::on_synchronized(bool is_synchronized) {
}
}
void StateManager::on_network_updated() {
do_on_network(network_type_, true /*inc_generation*/);
}
void StateManager::on_network(NetType new_network_type) {
do_on_network(new_network_type, true /*inc_generation*/);
}

View File

@ -38,6 +38,8 @@ class StateManager final : public Actor {
void on_synchronized(bool is_synchronized);
void on_network_updated();
void on_network(NetType new_network_type);
void on_online(bool is_online);

View File

@ -5998,6 +5998,12 @@ void Td::on_request(uint64 id, td_api::setOption &request) {
return send_closure(actor_id(this), &Td::send_result, id, make_tl_object<td_api::ok>());
}
break;
case 'p':
if (set_boolean_option("prefer_ipv6")) {
send_closure(state_manager_, &StateManager::on_network_updated);
return;
}
break;
case 's':
if (set_integer_option("session_count", 0, 50)) {
return;

View File

@ -411,7 +411,8 @@ void ConnectionCreator::ping_proxy(int32 proxy_id, Promise<double> promise) {
if (proxy_id == 0) {
ProxyInfo proxy{nullptr, IPAddress()};
auto main_dc_id = G()->net_query_dispatcher().main_dc_id();
auto infos = dc_options_set_.find_all_connections(main_dc_id, false, false);
bool prefer_ipv6 = G()->shared_config().get_option_boolean("prefer_ipv6");
auto infos = dc_options_set_.find_all_connections(main_dc_id, false, false, prefer_ipv6);
if (infos.empty()) {
return promise.set_error(Status::Error(400, "Can't find valid DC address"));
}
@ -754,9 +755,10 @@ Result<mtproto::TransportType> ConnectionCreator::get_transport_type(const Proxy
Result<SocketFd> ConnectionCreator::find_connection(const ProxyInfo &proxy, DcId dc_id, bool allow_media_only,
FindConnectionExtra &extra) {
extra.debug_str = PSTRING() << "Failed to find valid IP for " << dc_id;
TRY_RESULT(info, dc_options_set_.find_connection(
dc_id, allow_media_only,
proxy.use_proxy() && proxy.use_socks5_proxy() && proxy.ip_address().is_ipv4()));
bool prefer_ipv6 =
G()->shared_config().get_option_boolean("prefer_ipv6") || (proxy.use_proxy() && proxy.ip_address().is_ipv6());
TRY_RESULT(info, dc_options_set_.find_connection(dc_id, allow_media_only,
proxy.use_proxy() && proxy.use_socks5_proxy(), prefer_ipv6));
extra.stat = info.stat;
TRY_RESULT(transport_type, get_transport_type(proxy, info));
extra.transport_type = std::move(transport_type);

View File

@ -44,10 +44,14 @@ DcOptions DcOptionsSet::get_dc_options() const {
}
vector<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_all_connections(DcId dc_id, bool allow_media_only,
bool use_static) {
bool use_static, bool prefer_ipv6) {
std::vector<ConnectionInfo> options;
std::vector<ConnectionInfo> static_options;
if (prefer_ipv6) {
use_static = false;
}
for (auto &option_info : options_) {
auto &option = option_info->option;
if (option.get_dc_id() != dc_id) {
@ -98,6 +102,15 @@ vector<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_all_connections(DcId dc_
options = std::move(static_options);
}
}
if (prefer_ipv6) {
bool have_ipv6 = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_ipv6(); });
if (have_ipv6) {
options.erase(std::remove_if(options.begin(), options.end(), [](auto &v) { return !v.option->is_ipv6(); }),
options.end());
}
}
bool have_media_only = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_media_only(); });
if (have_media_only) {
options.erase(std::remove_if(options.begin(), options.end(), [](auto &v) { return !v.option->is_media_only(); }),
@ -107,12 +120,14 @@ vector<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_all_connections(DcId dc_
return options;
}
Result<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_connection(DcId dc_id, bool allow_media_only, bool use_static) {
auto options = find_all_connections(dc_id, allow_media_only, use_static);
Result<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_connection(DcId dc_id, bool allow_media_only, bool use_static,
bool prefer_ipv6) {
auto options = find_all_connections(dc_id, allow_media_only, use_static, prefer_ipv6);
if (options.empty()) {
return Status::Error(PSLICE() << "No such connection: " << tag("dc_id", dc_id)
<< tag("allow_media_only", allow_media_only) << tag("use_static", use_static));
<< tag("allow_media_only", allow_media_only) << tag("use_static", use_static)
<< tag("prefer_ipv6", prefer_ipv6));
}
auto last_error_at = std::min_element(options.begin(), options.end(),

View File

@ -61,9 +61,9 @@ class DcOptionsSet {
Stat *stat{nullptr};
};
vector<ConnectionInfo> find_all_connections(DcId dc_id, bool allow_media_only, bool use_static);
vector<ConnectionInfo> find_all_connections(DcId dc_id, bool allow_media_only, bool use_static, bool prefer_ipv6);
Result<ConnectionInfo> find_connection(DcId dc_id, bool allow_media_only, bool use_static);
Result<ConnectionInfo> find_connection(DcId dc_id, bool allow_media_only, bool use_static, bool prefer_ipv6);
void reset();
private:

View File

@ -94,7 +94,7 @@ struct BinlogEvent {
//explicit BinlogEvent(BufferSlice &&raw_event) {
//init(std::move(raw_event), false).ensure();
//}
explicit BinlogEvent(BufferSlice &&raw_event, BinlogDebugInfo info) {
BinlogEvent(BufferSlice &&raw_event, BinlogDebugInfo info) {
debug_info_ = info;
init(std::move(raw_event), false).ensure();
}

View File

@ -8,7 +8,6 @@
#include "td/utils/common.h"
#include <cstddef>
#include <type_traits>
namespace td {

View File

@ -11,10 +11,10 @@
#include "td/utils/logging.h"
#include <cstring>
#include <type_traits>
namespace td {
/*** MutableSlice ***/
inline MutableSlice::MutableSlice() : s_(const_cast<char *>("")), len_(0) {
}
@ -141,7 +141,6 @@ inline char &MutableSlice::operator[](size_t i) {
return s_[i];
}
/*** Slice ***/
inline Slice::Slice() : s_(""), len_(0) {
}