Ping proxy fixes.

GitOrigin-RevId: cf6e8a2585fdfe7a7491109b8facf0dcad379d2c
This commit is contained in:
levlam 2018-05-19 18:05:38 +03:00
parent 2216998d74
commit 8fb6edab2f
2 changed files with 37 additions and 31 deletions

View File

@ -157,7 +157,7 @@ class PingActor : public Actor {
class ConnectionCreator::ProxyInfo { class ConnectionCreator::ProxyInfo {
public: public:
explicit ProxyInfo(Proxy *proxy, IPAddress ip_address) : proxy_(proxy), ip_address_(std::move(ip_address)) { ProxyInfo(Proxy *proxy, IPAddress ip_address) : proxy_(proxy), ip_address_(std::move(ip_address)) {
} }
bool use_proxy() const { bool use_proxy() const {
return proxy_ != nullptr; return proxy_ != nullptr;
@ -171,11 +171,11 @@ class ConnectionCreator::ProxyInfo {
bool use_mtproto_proxy() const { bool use_mtproto_proxy() const {
return proxy_type() == Proxy::Type::Mtproto; return proxy_type() == Proxy::Type::Mtproto;
} }
Proxy &proxy() { const Proxy &proxy() const {
CHECK(use_proxy()); CHECK(use_proxy());
return *proxy_; return *proxy_;
} }
IPAddress &ip_address() { const IPAddress &ip_address() const {
return ip_address_; return ip_address_;
} }
@ -394,12 +394,12 @@ void ConnectionCreator::ping_proxy(int32 proxy_id, Promise<double> promise) {
if (it == proxies_.end()) { if (it == proxies_.end()) {
return promise.set_error(Status::Error(400, "Unknown proxy identifier")); return promise.set_error(Status::Error(400, "Unknown proxy identifier"));
} }
Proxy &proxy = it->second; const Proxy &proxy = it->second;
send_closure(get_host_by_name_actor_, &GetHostByNameActor::run, proxy.server().str(), proxy.port(), send_closure(get_host_by_name_actor_, &GetHostByNameActor::run, proxy.server().str(), proxy.port(),
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise), PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise),
proxy_id](Result<IPAddress> result) mutable { proxy_id](Result<IPAddress> result) mutable {
if (result.is_error()) { if (result.is_error()) {
return promise.set_error(result.move_as_error()); return promise.set_error(Status::Error(400, result.error().message()));
} }
send_closure(actor_id, &ConnectionCreator::ping_proxy_resolved, proxy_id, result.move_as_ok(), send_closure(actor_id, &ConnectionCreator::ping_proxy_resolved, proxy_id, result.move_as_ok(),
std::move(promise)); std::move(promise));
@ -416,7 +416,7 @@ void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address
FindConnectionExtra extra; FindConnectionExtra extra;
auto r_socket_fd = find_connection(proxy, main_dc_id, false, extra); auto r_socket_fd = find_connection(proxy, main_dc_id, false, extra);
if (r_socket_fd.is_error()) { if (r_socket_fd.is_error()) {
return promise.set_error(r_socket_fd.move_as_error()); return promise.set_error(Status::Error(400, r_socket_fd.error().message()));
} }
auto socket_fd = r_socket_fd.move_as_ok(); auto socket_fd = r_socket_fd.move_as_ok();
@ -424,7 +424,7 @@ void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address
PromiseCreator::lambda([promise = std::move(promise), actor_id = actor_id(this), PromiseCreator::lambda([promise = std::move(promise), actor_id = actor_id(this),
transport_type = std::move(extra.transport_type)](Result<SocketFd> r_socket_fd) mutable { transport_type = std::move(extra.transport_type)](Result<SocketFd> r_socket_fd) mutable {
if (r_socket_fd.is_error()) { if (r_socket_fd.is_error()) {
return promise.set_error(r_socket_fd.move_as_error()); return promise.set_error(Status::Error(400, r_socket_fd.error().message()));
} }
send_closure(actor_id, &ConnectionCreator::ping_proxy_socket_fd, r_socket_fd.move_as_ok(), send_closure(actor_id, &ConnectionCreator::ping_proxy_socket_fd, r_socket_fd.move_as_ok(),
std::move(transport_type), std::move(promise)); std::move(transport_type), std::move(promise));
@ -446,9 +446,10 @@ void ConnectionCreator::ping_proxy_resolved(int32 proxy_id, IPAddress ip_address
}; };
LOG(INFO) << "Start socks5: " << extra.debug_str; LOG(INFO) << "Start socks5: " << extra.debug_str;
auto token = next_token(); auto token = next_token();
children_[token] = create_actor<Socks5>( children_[token] = {
"Socks5", std::move(socket_fd), extra.mtproto_ip, proxy.proxy().user().str(), proxy.proxy().password().str(), false, create_actor<Socks5>("Socks5", std::move(socket_fd), extra.mtproto_ip, proxy.proxy().user().str(),
std::make_unique<Callback>(std::move(socket_fd_promise)), create_reference(token)); proxy.proxy().password().str(),
std::make_unique<Callback>(std::move(socket_fd_promise)), create_reference(token))};
} else { } else {
socket_fd_promise.set_value(std::move(socket_fd)); socket_fd_promise.set_value(std::move(socket_fd));
} }
@ -459,16 +460,17 @@ void ConnectionCreator::ping_proxy_socket_fd(SocketFd socket_fd, mtproto::Transp
auto token = next_token(); auto token = next_token();
auto raw_connection = auto raw_connection =
std::make_unique<mtproto::RawConnection>(std::move(socket_fd), std::move(transport_type), nullptr); std::make_unique<mtproto::RawConnection>(std::move(socket_fd), std::move(transport_type), nullptr);
children_[token] = create_actor<detail::PingActor>( children_[token] = {false,
"PingActor", std::move(raw_connection), create_actor<detail::PingActor>(
PromiseCreator::lambda([start = Time::now(), promise = std::move(promise)]( "PingActor", std::move(raw_connection),
Result<std::unique_ptr<mtproto::RawConnection>> result) mutable { PromiseCreator::lambda([start = Time::now(), promise = std::move(promise)](
if (result.is_error()) { Result<std::unique_ptr<mtproto::RawConnection>> result) mutable {
return promise.set_error(result.move_as_error()); if (result.is_error()) {
} return promise.set_error(Status::Error(400, result.error().message()));
promise.set_value(Time::now() - start); }
}), promise.set_value(Time::now() - start);
create_reference(token)); }),
create_reference(token))};
} }
void ConnectionCreator::enable_proxy_impl(int32 proxy_id) { void ConnectionCreator::enable_proxy_impl(int32 proxy_id) {
@ -511,7 +513,9 @@ void ConnectionCreator::on_proxy_changed(bool from_db) {
if (!from_db) { if (!from_db) {
for (auto &child : children_) { for (auto &child : children_) {
child.second.reset(); if (child.second.first) {
child.second.second.reset();
}
} }
} }
@ -664,7 +668,7 @@ void ConnectionCreator::request_raw_connection_by_ip(IPAddress ip_address,
promise.set_value(std::move(raw_connection)); promise.set_value(std::move(raw_connection));
} }
Result<SocketFd> ConnectionCreator::find_connection(ConnectionCreator::ProxyInfo &proxy, DcId dc_id, Result<SocketFd> ConnectionCreator::find_connection(const ConnectionCreator::ProxyInfo &proxy, DcId dc_id,
bool allow_media_only, FindConnectionExtra &extra) { bool allow_media_only, FindConnectionExtra &extra) {
TRY_RESULT(info, dc_options_set_.find_connection(dc_id, allow_media_only, proxy.use_proxy())); TRY_RESULT(info, dc_options_set_.find_connection(dc_id, allow_media_only, proxy.use_proxy()));
extra.stat = info.stat; extra.stat = info.stat;
@ -777,7 +781,6 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
} }
// Create new RawConnection // Create new RawConnection
//TODO
// sync part // sync part
FindConnectionExtra extra; FindConnectionExtra extra;
auto r_socket_fd = find_connection(proxy, client.dc_id, client.allow_media_only, extra); auto r_socket_fd = find_connection(proxy, client.dc_id, client.allow_media_only, extra);
@ -853,9 +856,11 @@ void ConnectionCreator::client_loop(ClientInfo &client) {
}; };
LOG(INFO) << "Start socks5: " << extra.debug_str; LOG(INFO) << "Start socks5: " << extra.debug_str;
auto token = next_token(); auto token = next_token();
children_[token] = create_actor<Socks5>( children_[token] = {
"Socks5", std::move(socket_fd), extra.mtproto_ip, proxy.proxy().user().str(), proxy.proxy().password().str(), true, create_actor<Socks5>("Socks5", std::move(socket_fd), extra.mtproto_ip, proxy.proxy().user().str(),
std::make_unique<Callback>(std::move(promise), std::move(stats_callback)), create_reference(token)); proxy.proxy().password().str(),
std::make_unique<Callback>(std::move(promise), std::move(stats_callback)),
create_reference(token))};
} else { } else {
ConnectionData data; ConnectionData data;
data.socket_fd = std::move(socket_fd); data.socket_fd = std::move(socket_fd);
@ -890,8 +895,8 @@ void ConnectionCreator::client_create_raw_connection(Result<ConnectionData> r_co
if (check_mode) { if (check_mode) {
VLOG(connections) << "Start check: " << debug_str; VLOG(connections) << "Start check: " << debug_str;
auto token = next_token(); auto token = next_token();
children_[token] = create_actor<detail::PingActor>("PingActor", std::move(raw_connection), std::move(promise), children_[token] = {true, create_actor<detail::PingActor>("PingActor", std::move(raw_connection),
create_reference(token)); std::move(promise), create_reference(token))};
} else { } else {
promise.set_value(std::move(raw_connection)); promise.set_value(std::move(raw_connection));
} }
@ -1074,7 +1079,7 @@ void ConnectionCreator::hangup() {
save_proxy_last_used_date(0); save_proxy_last_used_date(0);
ref_cnt_guard_.reset(); ref_cnt_guard_.reset();
for (auto &child : children_) { for (auto &child : children_) {
child.second.reset(); child.second.second.reset();
} }
} }

View File

@ -222,7 +222,7 @@ class ConnectionCreator : public NetQueryCallback {
ActorShared<ConnectionCreator> create_reference(int64 token); ActorShared<ConnectionCreator> create_reference(int64 token);
bool close_flag_{false}; bool close_flag_{false};
uint64 current_token_ = 0; uint64 current_token_ = 0;
std::map<int64, ActorShared<>> children_; std::map<int64, std::pair<bool, ActorShared<>>> children_;
uint64 next_token() { uint64 next_token() {
return ++current_token_; return ++current_token_;
@ -285,7 +285,8 @@ class ConnectionCreator : public NetQueryCallback {
bool check_mode{false}; bool check_mode{false};
}; };
class ProxyInfo; class ProxyInfo;
Result<SocketFd> find_connection(ProxyInfo &proxy, DcId dc_id, bool allow_media_only, FindConnectionExtra &extra); Result<SocketFd> find_connection(const ProxyInfo &proxy, DcId dc_id, bool allow_media_only,
FindConnectionExtra &extra);
void ping_proxy_resolved(int32 proxy_id, IPAddress ip_address, Promise<double> promise); void ping_proxy_resolved(int32 proxy_id, IPAddress ip_address, Promise<double> promise);
void ping_proxy_socket_fd(SocketFd socket_fd, mtproto::TransportType transport_type, Promise<double> promise); void ping_proxy_socket_fd(SocketFd socket_fd, mtproto::TransportType transport_type, Promise<double> promise);
}; };