From 4ac7313ff17fcfc2ad4f8d692bd0d0d423076e20 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Sun, 23 Feb 2025 11:00:53 +0100 Subject: [PATCH] Pass tracker key at download creation. --- src/core/download.h | 52 ++++++------------------------------ src/core/download_factory.cc | 46 +++++++++++++++++-------------- src/core/download_list.cc | 48 +++++---------------------------- src/core/download_list.h | 41 +++------------------------- 4 files changed, 43 insertions(+), 144 deletions(-) diff --git a/src/core/download.h b/src/core/download.h index f2b51941..5de5d693 100644 --- a/src/core/download.h +++ b/src/core/download.h @@ -1,39 +1,3 @@ -// rTorrent - BitTorrent client -// Copyright (C) 2005-2011, Jari Sundell -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - #ifndef RTORRENT_CORE_DOWNLOAD_H #define RTORRENT_CORE_DOWNLOAD_H @@ -70,10 +34,10 @@ public: Download(download_type d); ~Download(); - const torrent::DownloadInfo* info() const { return m_download.info(); } - const torrent::download_data* data() const { return m_download.data(); } + auto info() const { return m_download.info(); } + auto data() const { return m_download.data(); } - torrent::DownloadMain* main() { return m_download.main(); } + auto main() { return m_download.main(); } bool is_open() const { return m_download.info()->is_open(); } bool is_active() const { return m_download.info()->is_active(); } @@ -101,12 +65,12 @@ public: torrent::Object* bencode() { return m_download.bencode(); } - torrent::TrackerControllerWrapper tracker_controller() { return m_download.tracker_controller(); } - tracker_list_type* tracker_list() { return m_download.tracker_list(); } - uint32_t tracker_list_size() const { return m_download.tracker_list()->size(); } + auto tracker_controller() { return m_download.tracker_controller(); } + tracker_list_type* tracker_list() { return m_download.tracker_list(); } + uint32_t tracker_list_size() const { return m_download.tracker_list()->size(); } - connection_list_type* connection_list() { return m_download.connection_list(); } - uint32_t connection_list_size() const; + auto connection_list() { return m_download.connection_list(); } + uint32_t connection_list_size() const; const std::string& message() const { return m_message; } void set_message(const std::string& msg) { m_message = msg; } diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index 8b5fedb1..4fd02848 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -37,21 +37,20 @@ is_network_uri(const std::string& uri) { std::strncmp(uri.c_str(), "ftp://", 6) == 0; } -static bool -download_factory_add_stream(torrent::Object* root, const char* key, const char* filename) { +static std::unique_ptr +download_factory_load_stream(const char* filename) { std::fstream stream(filename, std::ios::in | std::ios::binary); if (!stream.is_open()) - return false; + return std::unique_ptr(); - torrent::Object obj; - stream >> obj; + auto obj = std::make_unique(); + stream >> *obj; if (!stream.good()) - return false; + return std::unique_ptr(); - root->insert_key_move(key, obj); - return true; + return obj; } bool @@ -173,9 +172,19 @@ DownloadFactory::receive_commit() { void DownloadFactory::receive_success() { + auto rtorrent_object = download_factory_load_stream((rak::path_expand(m_uri) + ".rtorrent").c_str()); + auto libtorrent_resume_object = download_factory_load_stream((rak::path_expand(m_uri) + ".libtorrent_resume").c_str()); + + uint32_t tracker_key; + + if (rtorrent_object && rtorrent_object->has_key_value("key")) + tracker_key = rtorrent_object->get_key_value("key"); + else + tracker_key = random() % (std::numeric_limits::max() - 1) + 1; + Download* download = m_stream != NULL ? - m_manager->download_list()->create(m_stream, m_printLog) : - m_manager->download_list()->create(m_object, m_printLog); + m_manager->download_list()->create(m_stream, tracker_key, m_printLog) : + m_manager->download_list()->create(m_object, tracker_key, m_printLog); m_object = NULL; @@ -200,8 +209,11 @@ DownloadFactory::receive_success() { } if (m_session) { - download_factory_add_stream(root, "rtorrent", (rak::path_expand(m_uri) + ".rtorrent").c_str()); - download_factory_add_stream(root, "libtorrent_resume", (rak::path_expand(m_uri) + ".libtorrent_resume").c_str()); + if (rtorrent_object) + root->insert_key_move("rtorrent", *rtorrent_object); + + if (libtorrent_resume_object) + root->insert_key_move("libtorrent_resume", *libtorrent_resume_object); } else { // We only allow session torrents to keep their @@ -213,6 +225,8 @@ DownloadFactory::receive_success() { torrent::Object* rtorrent = &root->insert_preserve_copy("rtorrent", torrent::Object::create_map()).first->second; torrent::Object& resumeObject = root->insert_preserve_copy("libtorrent_resume", torrent::Object::create_map()).first->second; + rtorrent->insert_key("key", download->tracker_list()->key()); + initialize_rtorrent(download, rtorrent); if (!rtorrent->has_key_string("custom1")) rtorrent->insert_key("custom1", std::string()); @@ -378,14 +392,6 @@ DownloadFactory::initialize_rtorrent(Download* download, torrent::Object* rtorre else rpc::call_command("d.priority.set", (int64_t)2, rpc::make_target(download)); - if (rtorrent->has_key_value("key")) { - download->tracker_list()->set_key(rtorrent->get_key_value("key")); - - } else { - download->tracker_list()->set_key(random() % (std::numeric_limits::max() - 1) + 1); - rtorrent->insert_key("key", download->tracker_list()->key()); - } - if (rtorrent->has_key_value("total_uploaded")) download->info()->mutable_up_rate()->set_total(rtorrent->get_key_value("total_uploaded")); diff --git a/src/core/download_list.cc b/src/core/download_list.cc index e47b379c..1a3f1e02 100644 --- a/src/core/download_list.cc +++ b/src/core/download_list.cc @@ -1,39 +1,3 @@ -// rTorrent - BitTorrent client -// Copyright (C) 2005-2011, Jari Sundell -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - #include "config.h" #include @@ -70,7 +34,7 @@ namespace core { inline void -DownloadList::check_contains(Download* d) { +DownloadList::check_contains([[maybe_unused]] Download* d) { #ifdef USE_EXTRA_DEBUG if (std::find(begin(), end(), d) == end()) throw torrent::internal_error("DownloadList::check_contains(...) failed."); @@ -119,11 +83,11 @@ DownloadList::find_hex_ptr(const char* hash) { } Download* -DownloadList::create(torrent::Object* obj, bool printLog) { +DownloadList::create(torrent::Object* obj, uint32_t tracker_key, bool printLog) { torrent::Download download; try { - download = torrent::download_add(obj); + download = torrent::download_add(obj, tracker_key); } catch (torrent::local_error& e) { delete obj; @@ -140,13 +104,13 @@ DownloadList::create(torrent::Object* obj, bool printLog) { } Download* -DownloadList::create(std::istream* str, bool printLog) { +DownloadList::create(std::istream* str, uint32_t tracker_key, bool printLog) { torrent::Object* object = new torrent::Object; torrent::Download download; try { *str >> *object; - + // Don't throw input_error from here as gcc-3.3.5 produces bad // code. if (str->fail()) { @@ -158,7 +122,7 @@ DownloadList::create(std::istream* str, bool printLog) { return NULL; } - download = torrent::download_add(object); + download = torrent::download_add(object, tracker_key); } catch (torrent::local_error& e) { delete object; diff --git a/src/core/download_list.h b/src/core/download_list.h index 25996e21..0e73d00c 100644 --- a/src/core/download_list.h +++ b/src/core/download_list.h @@ -1,39 +1,3 @@ -// rTorrent - BitTorrent client -// Copyright (C) 2005-2011, Jari Sundell -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// In addition, as a special exception, the copyright holders give -// permission to link the code of portions of this program with the -// OpenSSL library under certain conditions as described in each -// individual source file, and distribute linked combinations -// including the two. -// -// You must obey the GNU General Public License in all respects for -// all of the code used other than OpenSSL. If you modify file(s) -// with this exception, you may extend this exception to your version -// of the file(s), but you are not obligated to do so. If you do not -// wish to do so, delete this exception statement from your version. -// If you delete this exception statement from all source files in the -// program, then also delete it here. -// -// Contact: Jari Sundell -// -// Skomakerveien 33 -// 3185 Skoppum, NORWAY - #ifndef RTORRENT_CORE_DOWNLOAD_LIST_H #define RTORRENT_CORE_DOWNLOAD_LIST_H @@ -43,6 +7,7 @@ namespace torrent { class HashString; + class Object; } namespace core { @@ -86,8 +51,8 @@ public: Download* find_hex_ptr(const char* hash); // Might move this to DownloadFactory. - Download* create(std::istream* str, bool printLog); - Download* create(torrent::Object* obj, bool printLog); + Download* create(std::istream* str, uint32_t tracker_key, bool printLog); + Download* create(torrent::Object* obj, uint32_t tracker_key, bool printLog); iterator insert(Download* d);