From d5a6256b51e05c513560fcd312a88eaae1563e82 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 27 Mar 2017 20:47:12 -0400 Subject: [PATCH 1/5] improve documentation on peer classes --- docs/manual.rst | 58 +++++++++++++++++++++++ examples/client_test.cpp | 3 +- include/libtorrent/add_torrent_params.hpp | 3 ++ include/libtorrent/settings_pack.hpp | 3 ++ include/libtorrent/torrent_handle.hpp | 2 + 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/docs/manual.rst b/docs/manual.rst index 46921d645..8cca26821 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -815,6 +815,64 @@ Custom peer classes can be assigned to torrents, with the ??? call, in which case all its peers will belong to the class. They can also be assigned based on the peer's IP address. See set_peer_class_filter() for more information. +peer class examples +------------------- + +Here are a few examples of common peer class operations. + +To make the global rate limit apply to local peers as well, update the IP-filter +based peer class assignment: + +.. code:: c++ + + std::uint32_t const mask = 1 << lt::session::global_peer_class_id; + ip_filter f; + + // for every IPv4 address, assign the global peer class + f.add_rule(address_v4::from_string("0.0.0.0") + , address_v4::from_string("255.255.255.255") + , mask); + + // for every IPv6 address, assign the global peer class + f.add_rule(address_v6::from_string("::") + , address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") + , mask); + ses.set_peer_class_filter(f); + +To make uTP sockets exempt from rate limiting: + +.. code:: c++ + + peer_class_type_filter flt; + // filter out the global and local peer class for uTP sockets, if these + // classes are set by the IP filter + flt.disallow(peer_class_type_filter::utp_socket, session::global_peer_class_id); + flt.disallow(peer_class_type_filter::utp_socket, session::local_peer_class_id); + + // this filter should not add the global or local peer class to utp sockets + flt.remove(peer_class_type_filter::utp_socket, session::global_peer_class_id); + flt.remove(peer_class_type_filter::utp_socket, session::local_peer_class_id); + + ses.set_peer_class_type_filter(flt); + +To make all peers on the internal network unthrottled: + +.. code:: c++ + + std::uint32_t const mask = 1 << lt::session::global_peer_class_id; + ip_filter f; + + // for every IPv4 address, assign the global peer class + f.add_rule(address_v4::from_string("0.0.0.0") + , address_v4::from_string("255.255.255.255") + , mask); + + // for every address on the local metwork, set the mastk to 0 + f.add_rule(address_v4::from_string("10.0.0.0") + , address_v4::from_string("10.255.255.255") + , 0); + ses.set_peer_class_filter(f); + SSL torrents ============ diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 5ae070191..dd3b70a00 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -1553,9 +1553,8 @@ int main(int argc, char* argv[]) if (rate_limit_locals) { ip_filter pcf; - // 1 is the global peer class. This should be done properly in the future pcf.add_rule(address_v4::from_string("0.0.0.0") - , address_v4::from_string("255.255.255.255"), 1); + , address_v4::from_string("255.255.255.255"), 1 << lt::session::global_peer_class_id); #if TORRENT_USE_IPV6 pcf.add_rule(address_v6::from_string("::") , address_v6::from_string("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), 1); diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index 55b1f620d..a21b20981 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -406,6 +406,9 @@ namespace libtorrent // // -1 means unlimited on these settings just like their counterpart // functions on torrent_handle + // + // For fine grained control over rate limits, including making them apply + // to local peers, see peer-classes_. int max_uploads; int max_connections; int upload_limit; diff --git a/include/libtorrent/settings_pack.hpp b/include/libtorrent/settings_pack.hpp index d7fce6165..20bae3ab5 100644 --- a/include/libtorrent/settings_pack.hpp +++ b/include/libtorrent/settings_pack.hpp @@ -1279,6 +1279,9 @@ namespace libtorrent // limited. // // A value of 0 means unlimited. + // + // For fine grained control over rate limits, including making them apply + // to local peers, see peer-classes_. upload_rate_limit, download_rate_limit, #ifndef TORRENT_NO_DEPRECATE diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index fc0c6da77..b667118ff 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -1119,6 +1119,8 @@ namespace libtorrent // // ``upload_limit`` and ``download_limit`` will return the current limit // setting, for upload and download, respectively. + // + // Local peers are not rate limited by default. see peer-classes_. void set_upload_limit(int limit) const; int upload_limit() const; void set_download_limit(int limit) const; From 412325ba464e7f83d91440e50423c4a54229cb23 Mon Sep 17 00:00:00 2001 From: arvidn Date: Mon, 27 Mar 2017 20:33:28 -0400 Subject: [PATCH 2/5] fix python binding byte encoding of info_hash in parse_magnet_uri --- bindings/python/src/magnet_uri.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bindings/python/src/magnet_uri.cpp b/bindings/python/src/magnet_uri.cpp index 52e763b17..de0797993 100644 --- a/bindings/python/src/magnet_uri.cpp +++ b/bindings/python/src/magnet_uri.cpp @@ -7,6 +7,7 @@ #include #include #include "gil.hpp" +#include "bytes.hpp" using namespace boost::python; using namespace libtorrent; @@ -56,9 +57,9 @@ namespace { list nodes_list; for (std::vector >::const_iterator i = p.dht_nodes.begin() , end(p.dht_nodes.end()); i != end; ++i) - tracker_list.append(boost::python::make_tuple(i->first, i->second)); + nodes_list.append(boost::python::make_tuple(i->first, i->second)); ret["dht_nodes"] = nodes_list; - ret["info_hash"] = p.info_hash.to_string(); + ret["info_hash"] = bytes(p.info_hash.to_string()); ret["name"] = p.name; ret["save_path"] = p.save_path; ret["storage_mode"] = p.storage_mode; From 852fada77266903998ddf7a0ac07b5ba5edbd6e7 Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Mar 2017 20:22:27 -0400 Subject: [PATCH 3/5] restore announce_entry's timestamp fields to posix time in python binding --- ChangeLog | 1 + bindings/python/src/torrent_handle.cpp | 29 ++++++++++++++++++++++++-- bindings/python/test.py | 5 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 84217751a..ca760b7bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * 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 * fix minor robustness issue in DHT bootstrap logic diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index ea16d66b8..da69100d3 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -231,6 +231,21 @@ void add_tracker(torrent_handle& h, dict d) h.add_tracker(ae); } +namespace +{ +#if defined BOOST_ASIO_HAS_STD_CHRONO + using std::chrono::system_clock; +#else + using boost::chrono::system_clock; +#endif + + time_t to_ptime(time_point tpt) + { + return system_clock::to_time_t(system_clock::now() + + duration_cast(tpt - clock_type::now())); + } +} + list trackers(torrent_handle& h) { list ret; @@ -245,8 +260,18 @@ list trackers(torrent_handle& h) last_error["value"] = i->last_error.value(); last_error["category"] = i->last_error.category().name(); d["last_error"] = last_error; - d["next_announce"] = i->next_announce; - d["min_announce"] = i->min_announce; + if (i->next_announce > min_time()) { + d["next_announce"] = to_ptime(i->next_announce); + } + else { + d["next_announce"] = object(); + } + if (i->min_announce > min_time()) { + d["min_announce"] = to_ptime(i->min_announce); + } + else { + d["min_announce"] = object(); + } d["scrape_incomplete"] = i->scrape_incomplete; d["scrape_complete"] = i->scrape_complete; d["scrape_downloaded"] = i->scrape_downloaded; diff --git a/bindings/python/test.py b/bindings/python/test.py index 5abc13355..63b494936 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -109,6 +109,11 @@ class test_torrent_handle(unittest.TestCase): """Test to ensure the dict contains only python built-in types""" self.setup() self.h.add_tracker({'url':'udp://tracker1.com'}) + tr = self.h.trackers()[0] + # wait a bit until a valid timestamp appears + while tr['next_announce'] == None: + time.sleep(0.1) + tr = self.h.trackers()[0] import json print(json.dumps(self.h.trackers()[0])) From 02bb1251d256b7c40f1321fc6227101e7b9f20dd Mon Sep 17 00:00:00 2001 From: arvidn Date: Tue, 28 Mar 2017 01:56:45 -0400 Subject: [PATCH 4/5] 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); } From d896e9bab372f205f2a98ea94f3a24c3dd9f60b5 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 29 Mar 2017 19:19:23 -0400 Subject: [PATCH 5/5] replace use of add_torrent_params::url in some examples (#1866) replace use of add_torrent_params::url in some examples --- docs/tutorial.rst | 4 +++- examples/bt-get.cpp | 9 ++++++++- examples/bt-get2.cpp | 9 ++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index a7433ee5b..8b6c1c5b7 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -29,6 +29,7 @@ For example: #include #include #include + #include namespace lt = libtorrent; int main(int argc, char const* argv[]) @@ -40,7 +41,8 @@ For example: lt::session ses; lt::add_torrent_params atp; - atp.url = argv[1]; + lt::error_code ec; + lt::parse_magnet_uri(argv[1], atp, ec); atp.save_path = "."; // save in current dir lt::torrent_handle h = ses.add_torrent(atp); diff --git a/examples/bt-get.cpp b/examples/bt-get.cpp index 0029a81bf..6609a5214 100644 --- a/examples/bt-get.cpp +++ b/examples/bt-get.cpp @@ -38,6 +38,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include namespace lt = libtorrent; int main(int argc, char const* argv[]) @@ -49,7 +51,12 @@ int main(int argc, char const* argv[]) lt::session ses; lt::add_torrent_params atp; - atp.url = argv[1]; + lt::error_code ec; + lt::parse_magnet_uri(argv[1], atp, ec); + if (ec) { + std::cerr << "invalid magnet URI: " << ec.message() << std::endl; + return 1; + } atp.save_path = "."; // save in current dir lt::torrent_handle h = ses.add_torrent(atp); diff --git a/examples/bt-get2.cpp b/examples/bt-get2.cpp index 93389788b..68a7fe5b1 100644 --- a/examples/bt-get2.cpp +++ b/examples/bt-get2.cpp @@ -41,6 +41,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include namespace lt = libtorrent; using clk = std::chrono::steady_clock; @@ -82,7 +84,12 @@ int main(int argc, char const* argv[]) ifs.unsetf(std::ios_base::skipws); atp.resume_data.assign(std::istream_iterator(ifs) , std::istream_iterator()); - atp.url = argv[1]; + lt::error_code ec; + lt::parse_magnet_uri(argv[1], atp, ec); + if (ec) { + std::cerr << "invalid magnet URI: " << ec.message() << std::endl; + return 1; + } atp.save_path = "."; // save in current dir ses.async_add_torrent(atp);