From 02bb1251d256b7c40f1321fc6227101e7b9f20dd Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Mar 2017 01:56:45 -0400 Subject: [PATCH] removed (broken) support for incoming connections over socks5 --- ChangeLog | 1 + include/libtorrent/aux_/session_impl.hpp | 11 -- include/libtorrent/socks5_stream.hpp | 63 +--------- simulation/test_socks5.cpp | 142 +---------------------- simulation/test_transfer.cpp | 25 +--- src/session_impl.cpp | 110 ------------------ src/socks5_stream.cpp | 111 ++---------------- 7 files changed, 14 insertions(+), 449 deletions(-) diff --git a/ChangeLog b/ChangeLog index ca760b7bd..942b3eab0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * removed (broken) support for incoming connections over socks5 * restore announce_entry's timestamp fields to posix time in python binding * deprecate torrent_added_alert (in favor of add_torrent_alert) * fix python binding for parse_magnet_uri diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 123182726..b028168e5 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -242,10 +242,6 @@ namespace libtorrent void async_accept(boost::shared_ptr const& listener, bool ssl); void on_accept_connection(boost::shared_ptr const& s , boost::weak_ptr listener, error_code const& e, bool ssl); - void on_socks_listen(boost::shared_ptr const& s - , error_code const& e); - void on_socks_accept(boost::shared_ptr const& s - , error_code const& e); void incoming_connection(boost::shared_ptr const& s); @@ -895,16 +891,9 @@ namespace libtorrent void ssl_handshake(error_code const& ec, boost::shared_ptr s); #endif - // when as a socks proxy is used for peers, also - // listen for incoming connections on a socks connection - boost::shared_ptr m_socks_listen_socket; - boost::uint16_t m_socks_listen_port; - // round-robin index into m_net_interfaces mutable boost::uint8_t m_interface_index; - void open_new_incoming_socks_connection(); - enum listen_on_flags_t { open_ssl_socket = 0x10 diff --git a/include/libtorrent/socks5_stream.hpp b/include/libtorrent/socks5_stream.hpp index c0dd810f7..e3979b350 100644 --- a/include/libtorrent/socks5_stream.hpp +++ b/include/libtorrent/socks5_stream.hpp @@ -90,7 +90,6 @@ public: // commands enum { socks5_connect = 1, - socks5_bind = 2, socks5_udp_associate = 3 }; @@ -98,14 +97,13 @@ public: : proxy_base(io_service) , m_version(5) , m_command(socks5_connect) - , m_listen(0) {} void set_version(int v) { m_version = v; } void set_command(int c) { - TORRENT_ASSERT(c >= socks5_connect && c <= socks5_udp_associate); + TORRENT_ASSERT(c == socks5_connect || c == socks5_udp_associate); m_command = c; } @@ -116,40 +114,6 @@ public: m_password = password; } - template - void async_accept(Handler const& handler) - { - TORRENT_ASSERT(m_listen == 1); - TORRENT_ASSERT(m_command == socks5_bind); - - // to avoid unnecessary copying of the handler, - // store it in a shaed_ptr - error_code e; -#if defined TORRENT_ASIO_DEBUGGING - add_outstanding_async("socks5_stream::connect1"); -#endif - connect1(e, boost::make_shared(handler)); - } - - template - void async_listen(tcp::endpoint const& ep, Handler const& handler) - { - m_command = socks5_bind; - - m_remote_endpoint = ep; - - // to avoid unnecessary copying of the handler, - // store it in a shaed_ptr - boost::shared_ptr h(new handler_type(handler)); - -#if defined TORRENT_ASIO_DEBUGGING - add_outstanding_async("socks5_stream::name_lookup"); -#endif - tcp::resolver::query q(m_hostname, to_string(m_port).elems); - m_resolver.async_resolve(q, boost::bind( - &socks5_stream::name_lookup, this, _1, _2, h)); - } - void set_dst_name(std::string const& host) { // if this assert trips, set_dst_name() is called wth an IP address rather @@ -175,26 +139,12 @@ public: } #endif -#ifndef BOOST_NO_EXCEPTIONS - endpoint_type local_endpoint() const - { - return m_local_endpoint; - } -#endif - - endpoint_type local_endpoint(error_code&) const - { - return m_local_endpoint; - } - - // TODO: 2 add async_connect() that takes a hostname and port as well template void async_connect(endpoint_type const& endpoint, Handler const& handler) { // make sure we don't try to connect to INADDR_ANY. binding is fine, // and using a hostname is fine on SOCKS version 5. - TORRENT_ASSERT(m_command != socks5_bind); TORRENT_ASSERT(endpoint.address() != address() || (!m_dst_name.empty() && m_version == 5)); @@ -242,19 +192,10 @@ private: std::string m_password; std::string m_dst_name; - // when listening via a socks proxy, this is the IP and port our listen - // socket bound to - endpoint_type m_local_endpoint; - int m_version; - // the socks command to send for this connection (connect, bind, - // udp associate) + // the socks command to send for this connection (connect or udp associate) int m_command; - - // set to one when we're waiting for the - // second message to accept an incoming connection - int m_listen; }; } diff --git a/simulation/test_socks5.cpp b/simulation/test_socks5.cpp index ae19e56a1..a0a3982b4 100644 --- a/simulation/test_socks5.cpp +++ b/simulation/test_socks5.cpp @@ -108,143 +108,6 @@ void run_test(Setup const& setup test(sim, *ses, params.ti); } -TORRENT_TEST(socks5_tcp_accept) -{ - using namespace libtorrent; - bool incoming_connection = false; - run_test( - [](lt::session& ses) - { - set_proxy(ses, settings_pack::socks5); - }, - [&](lt::session& ses, lt::alert const* alert) { - if (auto* a = lt::alert_cast(alert)) - { - TEST_EQUAL(a->socket_type, 2); - incoming_connection = true; - } - }, - [](sim::simulation& sim, lt::session& ses - , boost::shared_ptr ti) - { - // test connecting to the client via its socks5 listen port - // TODO: maybe we could use peer_conn here instead - fake_peer peer1(sim, "60.0.0.0"); - fake_peer peer2(sim, "60.0.0.1"); - - sim::timer t1(sim, lt::seconds(2), [&](boost::system::error_code const& ec) - { - peer1.connect_to(tcp::endpoint(addr("50.50.50.50"), 6881), ti->info_hash()); - }); - - sim::timer t2(sim, lt::seconds(3), [&](boost::system::error_code const& ec) - { - peer2.connect_to(tcp::endpoint(addr("50.50.50.50"), 6881), ti->info_hash()); - }); - - sim.run(); - } - ); - - TEST_EQUAL(incoming_connection, true); -} - -TORRENT_TEST(socks4_tcp_accept) -{ - using namespace libtorrent; - bool incoming_connection = false; - run_test( - [](lt::session& ses) - { - set_proxy(ses, settings_pack::socks4); - }, - [&](lt::session& ses, lt::alert const* alert) { - if (auto* a = lt::alert_cast(alert)) - { - TEST_EQUAL(a->socket_type, 2); - TEST_EQUAL(a->ip.address(), addr("60.0.0.0")) - incoming_connection = true; - } - }, - [](sim::simulation& sim, lt::session& ses - , boost::shared_ptr ti) - { - fake_peer peer1(sim, "60.0.0.0"); - - sim::timer t1(sim, lt::seconds(2), [&](boost::system::error_code const& ec) - { - peer1.connect_to(tcp::endpoint(addr("50.50.50.50"), 6881), ti->info_hash()); - }); - - sim.run(); - } - ); - - TEST_EQUAL(incoming_connection, true); -} - -// make sure a listen_succeeded_alert is issued when successfully listening on -// incoming connections via a socks5 proxy -TORRENT_TEST(socks4_tcp_listen_alert) -{ - using namespace libtorrent; - bool listen_alert = false; - run_test( - [](lt::session& ses) - { - set_proxy(ses, settings_pack::socks4); - }, - [&](lt::session& ses, lt::alert const* alert) { - if (auto* a = lt::alert_cast(alert)) - { - if (a->sock_type == listen_succeeded_alert::socks5) - { - TEST_EQUAL(a->endpoint.address(), addr("50.50.50.50")); - TEST_EQUAL(a->endpoint.port(), 6881); - listen_alert = true; - } - } - }, - [](sim::simulation& sim, lt::session& ses - , boost::shared_ptr ti) - { - sim.run(); - } - ); - - TEST_EQUAL(listen_alert, true); -} - -TORRENT_TEST(socks5_tcp_listen_alert) -{ - using namespace libtorrent; - bool listen_alert = false; - run_test( - [](lt::session& ses) - { - set_proxy(ses, settings_pack::socks5); - }, - [&](lt::session& ses, lt::alert const* alert) { - if (auto* a = lt::alert_cast(alert)) - { - if (a->sock_type == listen_succeeded_alert::socks5) - { - TEST_EQUAL(a->endpoint.address(), addr("50.50.50.50")); - TEST_EQUAL(a->endpoint.port(), 6881); - listen_alert = true; - } - } - }, - [](sim::simulation& sim, lt::session& ses - , boost::shared_ptr ti) - { - sim.run(); - } - ); - - TEST_EQUAL(listen_alert, true); -} - TORRENT_TEST(socks5_tcp_announce) { using namespace libtorrent; @@ -264,7 +127,7 @@ TORRENT_TEST(socks5_tcp_announce) [&alert_port](lt::session& ses, lt::alert const* alert) { if (auto* a = lt::alert_cast(alert)) { - if (a->sock_type == listen_succeeded_alert::socks5) + if (a->sock_type == listen_succeeded_alert::udp) { alert_port = a->endpoint.port(); } @@ -298,7 +161,8 @@ TORRENT_TEST(socks5_tcp_announce) } ); - TEST_EQUAL(alert_port, tracker_port); + // since force_proxy is enabled, don't send the port + TEST_EQUAL(tracker_port, 0); TEST_CHECK(alert_port != -1); TEST_CHECK(tracker_port != -1); } diff --git a/simulation/test_transfer.cpp b/simulation/test_transfer.cpp index 3752faedc..566b777e6 100644 --- a/simulation/test_transfer.cpp +++ b/simulation/test_transfer.cpp @@ -51,8 +51,6 @@ using namespace sim; namespace lt = libtorrent; -const int connect_socks = 2; - template void run_test( Setup const& setup @@ -112,8 +110,7 @@ void run_test( print_alerts(*ses[0], [=](lt::session& ses, lt::alert const* a) { if (auto ta = alert_cast(a)) { - ta->handle.connect_peer(lt::tcp::endpoint( - (flags & connect_socks) ? proxy : peer1, 6881)); + ta->handle.connect_peer(lt::tcp::endpoint(peer1, 6881)); } on_alert(ses, a); }); @@ -179,26 +176,6 @@ TORRENT_TEST(socks5_tcp_connect) ); } -TORRENT_TEST(socks5_tcp_accept) -{ - using namespace libtorrent; - run_test( - [](lt::session& ses0, lt::session& ses1) - { - // this time, the session accepting the connection is listening on a - // socks5 BIND session - set_proxy(ses1, settings_pack::socks5); - filter_ips(ses0); - }, - [](lt::session& ses, lt::alert const* alert) {}, - [](std::shared_ptr ses[2]) { - TEST_EQUAL(is_seed(*ses[0]), true); - }, - connect_socks - ); -} - - TORRENT_TEST(encryption_tcp) { using namespace libtorrent; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index f74867ce6..0a7d47d12 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -368,7 +368,6 @@ namespace aux { #if TORRENT_USE_I2P , m_i2p_conn(m_io_service) #endif - , m_socks_listen_port(0) , m_interface_index(0) , m_unchoke_time_scaler(0) , m_auto_manage_time_scaler(0) @@ -1110,12 +1109,6 @@ namespace aux { TORRENT_ASSERT(!ec); } m_listen_sockets.clear(); - if (m_socks_listen_socket && m_socks_listen_socket->is_open()) - { - m_socks_listen_socket->close(ec); - TORRENT_ASSERT(!ec); - } - m_socks_listen_socket.reset(); #if TORRENT_USE_I2P if (m_i2p_listen_socket && m_i2p_listen_socket->is_open()) @@ -2223,7 +2216,6 @@ retry: , end(m_listen_sockets.end()); i != end; ++i) async_accept(i->sock, i->ssl); - open_new_incoming_socks_connection(); #if TORRENT_USE_I2P open_new_incoming_i2p_connection(); #endif @@ -2270,93 +2262,6 @@ retry: } } - void session_impl::open_new_incoming_socks_connection() - { - int const proxy_type = m_settings.get_int(settings_pack::proxy_type); - - if (proxy_type != settings_pack::socks5 - && proxy_type != settings_pack::socks5_pw - && proxy_type != settings_pack::socks4) - return; - - if (m_socks_listen_socket) return; - - m_socks_listen_socket = boost::make_shared(boost::ref(m_io_service)); - bool const ret = instantiate_connection(m_io_service, proxy() - , *m_socks_listen_socket, NULL, NULL, false, false); - TORRENT_ASSERT_VAL(ret, ret); - TORRENT_UNUSED(ret); - -#if defined TORRENT_ASIO_DEBUGGING - add_outstanding_async("session_impl::on_socks_listen"); -#endif - socks5_stream& s = *m_socks_listen_socket->get(); - - m_socks_listen_port = m_listen_interface.port(); - if (m_socks_listen_port == 0) m_socks_listen_port = 2000 + random() % 60000; - s.async_listen(tcp::endpoint(address_v4::any(), m_socks_listen_port) - , boost::bind(&session_impl::on_socks_listen, this - , m_socks_listen_socket, _1)); - } - - void session_impl::on_socks_listen(boost::shared_ptr const& sock - , error_code const& e) - { -#if defined TORRENT_ASIO_DEBUGGING - complete_async("session_impl::on_socks_listen"); -#endif - - TORRENT_ASSERT(sock == m_socks_listen_socket || !m_socks_listen_socket); - - if (e) - { - m_socks_listen_socket.reset(); - if (e == boost::asio::error::operation_aborted) return; - if (m_alerts.should_post()) - m_alerts.emplace_alert("socks5" - , -1, listen_failed_alert::accept, e - , listen_failed_alert::socks5); - return; - } - - error_code ec; - tcp::endpoint ep = sock->local_endpoint(ec); - TORRENT_ASSERT(!ec); - TORRENT_UNUSED(ec); - - if (m_alerts.should_post()) - m_alerts.emplace_alert( - ep, listen_succeeded_alert::socks5); - -#if defined TORRENT_ASIO_DEBUGGING - add_outstanding_async("session_impl::on_socks_accept"); -#endif - socks5_stream& s = *m_socks_listen_socket->get(); - s.async_accept(boost::bind(&session_impl::on_socks_accept, this - , m_socks_listen_socket, _1)); - } - - void session_impl::on_socks_accept(boost::shared_ptr const& s - , error_code const& e) - { -#if defined TORRENT_ASIO_DEBUGGING - complete_async("session_impl::on_socks_accept"); -#endif - TORRENT_ASSERT(s == m_socks_listen_socket || !m_socks_listen_socket); - m_socks_listen_socket.reset(); - if (e == boost::asio::error::operation_aborted) return; - if (e) - { - if (m_alerts.should_post()) - m_alerts.emplace_alert("socks5" - , -1, listen_failed_alert::accept, e - , listen_failed_alert::socks5); - return; - } - open_new_incoming_socks_connection(); - incoming_connection(s); - } - void session_impl::update_i2p_bridge() { // we need this socket to be open before we @@ -5467,9 +5372,6 @@ retry: void session_impl::update_proxy() { - // in case we just set a socks proxy, we might have to - // open the socks incoming connection - if (!m_socks_listen_socket) open_new_incoming_socks_connection(); m_udp_socket.set_proxy_settings(proxy()); #ifdef TORRENT_USE_OPENSSL @@ -5561,12 +5463,6 @@ retry: boost::uint16_t session_impl::listen_port() const { - // if peer connections are set up to be received over a socks - // proxy, and it's the same one as we're using for the tracker - // just tell the tracker the socks5 port we're listening on - if (m_socks_listen_socket && m_socks_listen_socket->is_open()) - return m_socks_listen_socket->local_endpoint().port(); - // if not, don't tell the tracker anything if we're in force_proxy // mode. We don't want to leak our listen port since it can // potentially identify us if it is leaked elsewere @@ -5578,12 +5474,6 @@ retry: boost::uint16_t session_impl::ssl_listen_port() const { #ifdef TORRENT_USE_OPENSSL - // if peer connections are set up to be received over a socks - // proxy, and it's the same one as we're using for the tracker - // just tell the tracker the socks5 port we're listening on - if (m_socks_listen_socket && m_socks_listen_socket->is_open()) - return m_socks_listen_port; - // if not, don't tell the tracker anything if we're in force_proxy // mode. We don't want to leak our listen port since it can // potentially identify us if it is leaked elsewere diff --git a/src/socks5_stream.cpp b/src/socks5_stream.cpp index afaf6a089..eaf0a12c7 100644 --- a/src/socks5_stream.cpp +++ b/src/socks5_stream.cpp @@ -84,56 +84,6 @@ namespace libtorrent { return socks_category(); } #endif - namespace - { - // parse out the endpoint from a SOCKS response - tcp::endpoint parse_endpoint(std::vector const& buffer - , int const version) - { - using namespace libtorrent::detail; - char const* p = &buffer[0]; - p += 2; // version & response code - if (version == 5) - { - ++p; // reserved byte - int const atyp = read_uint8(p); - - if (atyp == 1) - { - tcp::endpoint ret; - ret.address(read_v4_address(p)); - ret.port(read_uint16(p)); - return ret; - } - else if (atyp == 3) - { - // we don't support resolving the endpoint address - // if we receive a domain name, just set the remote - // endpoint to INADDR_ANY - return tcp::endpoint(); - } - else if (atyp == 4) - { - tcp::endpoint ret; -#if TORRENT_USE_IPV6 - ret.address(read_v6_address(p)); - ret.port(read_uint16(p)); -#endif - return ret; - } - } - else if (version == 4) - { - tcp::endpoint ret; - ret.port(read_uint16(p)); - ret.address(read_v4_address(p)); - return ret; - } - TORRENT_ASSERT(false); - return tcp::endpoint(); - } - } - void socks5_stream::name_lookup(error_code const& e, tcp::resolver::iterator i , boost::shared_ptr h) { @@ -325,7 +275,7 @@ namespace libtorrent :(m_remote_endpoint.address().is_v4()?4:16))); char* p = &m_buffer[0]; write_uint8(5, p); // SOCKS VERSION 5 - write_uint8(m_command, p); // CONNECT/BIND command + write_uint8(m_command, p); // CONNECT command write_uint8(0, p); // reserved if (!m_dst_name.empty()) { @@ -338,8 +288,7 @@ namespace libtorrent else { // we either need a hostname or a valid endpoint - TORRENT_ASSERT(m_command == socks5_bind - || m_remote_endpoint.address() != address()); + TORRENT_ASSERT(m_remote_endpoint.address() != address()); write_uint8(m_remote_endpoint.address().is_v4()?1:4, p); // address type write_address(m_remote_endpoint.address(), p); @@ -357,7 +306,7 @@ namespace libtorrent m_buffer.resize(m_user.size() + 9); char* p = &m_buffer[0]; write_uint8(4, p); // SOCKS VERSION 4 - write_uint8(m_command, p); // CONNECT/BIND command + write_uint8(m_command, p); // CONNECT command write_uint16(m_remote_endpoint.port(), p); write_uint32(m_remote_endpoint.address().to_v4().to_ulong(), p); std::copy(m_user.begin(), m_user.end(), p); @@ -438,25 +387,8 @@ namespace libtorrent // on address type) if (atyp == 1) { - if (m_command == socks5_bind) - { - if (m_listen == 0) - { - m_local_endpoint = parse_endpoint(m_buffer, m_version); - m_listen = 1; - } - else - { - m_remote_endpoint = parse_endpoint(m_buffer, m_version); - } - std::vector().swap(m_buffer); - (*h)(e); - } - else - { - std::vector().swap(m_buffer); - (*h)(e); - } + std::vector().swap(m_buffer); + (*h)(e); return; } int extra_bytes = 0; @@ -495,25 +427,8 @@ namespace libtorrent // access granted if (response == 90) { - if (m_command == socks5_bind) - { - if (m_listen == 0) - { - m_local_endpoint = parse_endpoint(m_buffer, m_version); - m_listen = 1; - } - else - { - m_remote_endpoint = parse_endpoint(m_buffer, m_version); - } - std::vector().swap(m_buffer); - (*h)(e); - } - else - { - std::vector().swap(m_buffer); - (*h)(e); - } + std::vector().swap(m_buffer); + (*h)(e); return; } @@ -537,18 +452,6 @@ namespace libtorrent if (handle_error(e, h)) return; - if (m_command == socks5_bind) - { - if (m_listen == 0) - { - m_local_endpoint = parse_endpoint(m_buffer, m_version); - m_listen = 1; - } - else - { - m_remote_endpoint = parse_endpoint(m_buffer, m_version); - } - } std::vector().swap(m_buffer); (*h)(e); }