Skip to content

Commit

Permalink
merged RC_1_2 into RC_2_0
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Nov 6, 2021
2 parents d281301 + c83139e commit 139131e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
* libtorrent now requires C++14 to build
* added support for GnuTLS for HTTPS and torrents over SSL

* cache DNS lookups for SOCKS5 proxy
* fix stalled pieces on disk-full errors
* fix build configuration issue on NetBSD, OpenBSD and DragonFly
* make UTF-8 sanitization a bit stricter. This will re-write invalid UTF-8
Expand Down
3 changes: 2 additions & 1 deletion include/libtorrent/udp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/span.hpp"
#include "libtorrent/flags.hpp"
#include "libtorrent/aux_/listen_socket_handle.hpp"
#include "libtorrent/aux_/resolver_interface.hpp"

#include <array>
#include <memory>
Expand Down Expand Up @@ -104,7 +105,7 @@ namespace aux { struct alert_manager; }
int local_port() const { return m_bind_port; }

void set_proxy_settings(aux::proxy_settings const& ps, aux::alert_manager& alerts
, bool send_local_ep);
, aux::resolver_interface& resolver, bool send_local_ep);
aux::proxy_settings const& get_proxy_settings() { return m_proxy_settings; }

bool is_closed() const { return m_abort; }
Expand Down
4 changes: 2 additions & 2 deletions src/session_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,7 @@ namespace {
// change after the session is up and listening, at no other point
// set_proxy_settings is called with the correct proxy configuration,
// internally, this method handle the SOCKS5's connection logic
ret->udp_sock->sock.set_proxy_settings(proxy(), m_alerts
ret->udp_sock->sock.set_proxy_settings(proxy(), m_alerts, get_resolver()
, settings().get_bool(settings_pack::socks5_udp_send_local_ep));

ADD_OUTSTANDING_ASYNC("session_impl::on_udp_packet");
Expand Down Expand Up @@ -5361,7 +5361,7 @@ namespace {
void session_impl::update_proxy()
{
for (auto& i : m_listen_sockets)
i->udp_sock->sock.set_proxy_settings(proxy(), m_alerts
i->udp_sock->sock.set_proxy_settings(proxy(), m_alerts, get_resolver()
, settings().get_bool(settings_pack::socks5_udp_send_local_ep));
}

Expand Down
54 changes: 25 additions & 29 deletions src/udp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/alert_manager.hpp"
#include "libtorrent/socks5_stream.hpp" // for socks_error
#include "libtorrent/aux_/keepalive.hpp"
#include "libtorrent/aux_/resolver_interface.hpp"

#include <cstdlib>
#include <functional>
Expand Down Expand Up @@ -80,9 +81,9 @@ std::size_t const max_header_size = 255;
struct socks5 : std::enable_shared_from_this<socks5>
{
explicit socks5(io_context& ios, aux::listen_socket_handle ls
, aux::alert_manager& alerts, bool const send_local_ep)
, aux::alert_manager& alerts, aux::resolver_interface& res, bool const send_local_ep)
: m_socks5_sock(ios)
, m_resolver(ios)
, m_resolver(res)
, m_timer(ios)
, m_retry_timer(ios)
, m_alerts(alerts)
Expand All @@ -100,7 +101,7 @@ struct socks5 : std::enable_shared_from_this<socks5>

std::shared_ptr<socks5> self() { return shared_from_this(); }

void on_name_lookup(error_code const& e, tcp::resolver::results_type ips);
void on_name_lookup(error_code const& e, std::vector<address> const& ips);
void on_connect_timeout(error_code const& e);
void on_connected(error_code const& e);
void handshake1(error_code const& e);
Expand All @@ -116,7 +117,7 @@ struct socks5 : std::enable_shared_from_this<socks5>
void retry_connection();

tcp::socket m_socks5_sock;
tcp::resolver m_resolver;
aux::resolver_interface& m_resolver;
deadline_timer m_timer;
deadline_timer m_retry_timer;
aux::alert_manager& m_alerts;
Expand Down Expand Up @@ -506,7 +507,7 @@ void udp_socket::bind(udp::endpoint const& ep, error_code& ec)
}

void udp_socket::set_proxy_settings(aux::proxy_settings const& ps
, aux::alert_manager& alerts, bool const send_local_ep)
, aux::alert_manager& alerts, aux::resolver_interface& resolver, bool const send_local_ep)
{
TORRENT_ASSERT(is_single_thread());

Expand All @@ -526,7 +527,7 @@ void udp_socket::set_proxy_settings(aux::proxy_settings const& ps
// connect to socks5 server and open up the UDP tunnel

m_socks5_connection = std::make_shared<socks5>(m_ioc
, m_listen_socket, alerts, send_local_ep);
, m_listen_socket, alerts, resolver, send_local_ep);
m_socks5_connection->start(ps);
}
}
Expand All @@ -537,13 +538,13 @@ void socks5::start(aux::proxy_settings const& ps)
{
m_proxy_settings = ps;

// TODO: use the system resolver_interface here
ADD_OUTSTANDING_ASYNC("socks5::on_name_lookup");
m_resolver.async_resolve(ps.hostname, to_string(ps.port).data(), std::bind(
&socks5::on_name_lookup, self(), _1, _2));
m_proxy_addr.port(ps.port);
m_resolver.async_resolve(ps.hostname, aux::resolver_interface::abort_on_shutdown
, std::bind(&socks5::on_name_lookup, self(), _1, _2));
}

void socks5::on_name_lookup(error_code const& e, tcp::resolver::results_type ips)
void socks5::on_name_lookup(error_code const& e, std::vector<address> const& ips)
{
COMPLETE_ASYNC("socks5::on_name_lookup");

Expand All @@ -561,31 +562,27 @@ void socks5::on_name_lookup(error_code const& e, tcp::resolver::results_type ips
return;
}

auto i = ips.begin();
// only set up a SOCKS5 tunnel for sockets with the same address family
// as the proxy
// this is a hack to mitigate excessive SOCKS5 tunnels, until this can get
// fixed properly.
for (;;)
{
if (i == ips.end())
{
if (m_alerts.should_post<socks5_alert>())
m_alerts.emplace_alert<socks5_alert>(m_listen_socket.get_local_endpoint()
, operation_t::hostname_lookup
, error_code(boost::system::errc::host_unreachable, generic_category()));
++m_failures;
retry_connection();
return;
}
auto const i = std::find_if(ips.begin(), ips.end()
, [&](address const& a) {
return m_listen_socket.can_route(a);
});

// we found a match
if (m_listen_socket.can_route(i->endpoint().address()))
break;
++i;
if (i == ips.end())
{
if (m_alerts.should_post<socks5_alert>())
m_alerts.emplace_alert<socks5_alert>(m_listen_socket.get_local_endpoint()
, operation_t::hostname_lookup
, error_code(boost::system::errc::host_unreachable, generic_category()));
++m_failures;
retry_connection();
return;
}

m_proxy_addr = i->endpoint();
m_proxy_addr.address(*i);

error_code ec;
m_socks5_sock.open(aux::is_v4(m_proxy_addr) ? tcp::v4() : tcp::v6(), ec);
Expand Down Expand Up @@ -991,7 +988,6 @@ void socks5::close()
m_abort = true;
error_code ec;
m_socks5_sock.close(ec);
m_resolver.cancel();
m_timer.cancel();
m_retry_timer.cancel();
}
Expand Down

0 comments on commit 139131e

Please sign in to comment.