2018-07-26 02:28:02 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-07-26 02:28:02 +02: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/net/TransparentProxy.h"
|
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2018-07-26 02:28:02 +02:00
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
int VERBOSITY_NAME(proxy) = VERBOSITY_NAME(DEBUG);
|
|
|
|
|
|
|
|
TransparentProxy::TransparentProxy(SocketFd socket_fd, IPAddress ip_address, string username, string password,
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<Callback> callback, ActorShared<> parent)
|
2018-07-26 02:28:02 +02:00
|
|
|
: fd_(std::move(socket_fd))
|
|
|
|
, ip_address_(std::move(ip_address))
|
|
|
|
, username_(std::move(username))
|
|
|
|
, password_(std::move(password))
|
|
|
|
, callback_(std::move(callback))
|
|
|
|
, parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::on_error(Status status) {
|
|
|
|
CHECK(status.is_error());
|
|
|
|
VLOG(proxy) << "Receive " << status;
|
|
|
|
if (callback_) {
|
|
|
|
callback_->set_result(std::move(status));
|
|
|
|
callback_.reset();
|
|
|
|
}
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::tear_down() {
|
|
|
|
VLOG(proxy) << "Finish to connect to proxy";
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::unsubscribe(fd_.get_poll_info().get_pollable_fd_ref());
|
2018-07-26 02:28:02 +02:00
|
|
|
if (callback_) {
|
|
|
|
if (!fd_.input_buffer().empty()) {
|
|
|
|
LOG(ERROR) << "Have " << fd_.input_buffer().size() << " unread bytes";
|
2022-03-17 21:33:02 +01:00
|
|
|
callback_->set_result(Status::Error("Proxy has sent too many data"));
|
2018-07-26 02:28:02 +02:00
|
|
|
} else {
|
|
|
|
callback_->set_result(std::move(fd_));
|
|
|
|
}
|
|
|
|
callback_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::hangup() {
|
2021-05-31 20:51:48 +02:00
|
|
|
on_error(Status::Error("Canceled"));
|
2018-07-26 02:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::start_up() {
|
|
|
|
VLOG(proxy) << "Begin to connect to proxy";
|
2018-09-11 00:20:41 +02:00
|
|
|
Scheduler::subscribe(fd_.get_poll_info().extract_pollable_fd(this));
|
2018-07-26 02:28:02 +02:00
|
|
|
set_timeout_in(10);
|
2020-07-21 17:29:39 +02:00
|
|
|
sync_with_poll(fd_);
|
|
|
|
if (can_write_local(fd_)) {
|
2018-07-26 02:28:02 +02:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::loop() {
|
2020-07-21 17:29:39 +02:00
|
|
|
sync_with_poll(fd_);
|
2018-07-26 02:28:02 +02:00
|
|
|
auto status = [&] {
|
|
|
|
TRY_STATUS(fd_.flush_read());
|
|
|
|
TRY_STATUS(loop_impl());
|
|
|
|
TRY_STATUS(fd_.flush_write());
|
2023-01-18 15:10:08 +01:00
|
|
|
if (can_close_local(fd_)) {
|
|
|
|
return Status::Error("Connection closed");
|
|
|
|
}
|
2018-07-26 02:28:02 +02:00
|
|
|
return Status::OK();
|
|
|
|
}();
|
|
|
|
if (status.is_error()) {
|
|
|
|
on_error(std::move(status));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransparentProxy::timeout_expired() {
|
2018-12-07 10:25:22 +01:00
|
|
|
on_error(Status::Error("Connection timeout expired"));
|
2018-07-26 02:28:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|