Changing listen/dht port changes the listening/dht ports.

This commit is contained in:
Jari Sundell
2026-07-12 10:19:33 +02:00
committed by GitHub
parent 2354c9cdb5
commit 86fa0d195f
7 changed files with 73 additions and 55 deletions
+30 -8
View File
@@ -9,7 +9,9 @@
#include <torrent/download/resource_manager.h> #include <torrent/download/resource_manager.h>
#include <torrent/net/http_stack.h> #include <torrent/net/http_stack.h>
#include <torrent/net/socket_address.h> #include <torrent/net/socket_address.h>
#include <torrent/runtime/client_config.h>
#include <torrent/runtime/network_config.h> #include <torrent/runtime/network_config.h>
#include <torrent/runtime/network_manager.h>
#include <torrent/runtime/proxy_manager.h> #include <torrent/runtime/proxy_manager.h>
#include <torrent/runtime/runtime.h> #include <torrent/runtime/runtime.h>
#include <torrent/runtime/socket_manager.h> #include <torrent/runtime/socket_manager.h>
@@ -32,6 +34,26 @@
#include <systemd/sd-daemon.h> #include <systemd/sd-daemon.h>
#endif #endif
torrent::Object
listen_port_range() {
auto port_range = torrent::runtime::client_config()->listen_port_range();
return std::to_string(port_range.first) + "-" + std::to_string(port_range.second);
}
void
set_listen_port_range(const std::string& arg) {
unsigned int port_first{}, port_last{};
if (std::sscanf(arg.c_str(), "%i-%i", &port_first, &port_last) != 2)
throw torrent::input_error("Invalid port_range argument.");
if (port_first >= (1 << 16) || port_last >= (1 << 16))
throw torrent::input_error("Port range out-of-bounds.");
torrent::runtime::client_config()->set_listen_port_range(port_first, port_last);
}
torrent::Object torrent::Object
apply_encryption(const torrent::Object::list_type& args) { apply_encryption(const torrent::Object::list_type& args) {
uint32_t options_mask = torrent::runtime::NetworkConfig::encryption_none; uint32_t options_mask = torrent::runtime::NetworkConfig::encryption_none;
@@ -220,14 +242,14 @@ initialize_command_network() {
auto http_stack = torrent::net_thread::http_stack(); auto http_stack = torrent::net_thread::http_stack();
auto nw_config = torrent::runtime::network_config(); auto nw_config = torrent::runtime::network_config();
// Isn't port_open used? CMD_ANY ("network.listen.port", [](auto, auto) { return torrent::runtime::network_manager()->listen_port(); });
CMD_VAR_BOOL ("network.port_open", true); CMD_ANY_VALUE_V ("network.listen.port.set", [](auto, auto& value) { return torrent::runtime::network_manager()->set_listen_port(value); });
CMD_VAR_BOOL ("network.port_random", true); CMD_ANY ("network.listen.port.random", [](auto, auto) { return torrent::runtime::client_config()->listen_port_random(); });
CMD_VAR_STRING ("network.port_range", "6881-6999"); CMD_ANY_VALUE_V ("network.listen.port.random.set", [](auto, auto& value) { return torrent::runtime::client_config()->set_listen_port_random(value); });
CMD_ANY ("network.listen.port.range", [](auto, auto) { return listen_port_range(); });
CMD_ANY ("network.listen.port", [](auto, auto) { return torrent::runtime::listen_port(); }); CMD_ANY_STRING_V("network.listen.port.range.set", [](auto, auto& value) { return set_listen_port_range(value); });
CMD_ANY ("network.listen.backlog", [nw_config](auto, auto) { return nw_config->listen_backlog(); }); CMD_ANY ("network.listen.backlog", [](auto, auto) { return torrent::runtime::network_config()->listen_backlog(); });
CMD_ANY_VALUE_V ("network.listen.backlog.set", [nw_config](auto, auto& value) { return nw_config->set_listen_backlog(value); }); CMD_ANY_VALUE_V ("network.listen.backlog.set", [](auto, auto& value) { return torrent::runtime::network_config()->set_listen_backlog(value); });
CMD_VAR_BOOL ("protocol.pex", true); CMD_VAR_BOOL ("protocol.pex", true);
CMD_ANY_LIST ("protocol.encryption.set", [](auto, auto& args) { return apply_encryption(args); }); CMD_ANY_LIST ("protocol.encryption.set", [](auto, auto& args) { return apply_encryption(args); });
+1 -1
View File
@@ -142,7 +142,7 @@ initialize_command_tracker() {
lt_log_print(torrent::LOG_DHT_ERROR, "dht.port.set is no longer supported, use dht.override_port.set", 0); lt_log_print(torrent::LOG_DHT_ERROR, "dht.port.set is no longer supported, use dht.override_port.set", 0);
}); });
CMD2_ANY ("dht.override_port", [](auto, auto) { return torrent::runtime::network_config()->override_dht_port(); }); CMD2_ANY ("dht.override_port", [](auto, auto) { return torrent::runtime::network_config()->override_dht_port(); });
CMD2_ANY_VALUE_V ("dht.override_port.set", [](auto, auto& value) { return torrent::runtime::network_config()->set_override_dht_port(value); }); CMD2_ANY_VALUE_V ("dht.override_port.set", [](auto, auto& value) { return torrent::runtime::network_manager()->set_dht_port(value); });
CMD2_ANY_STRING ("dht.add_node", [](auto, auto& str) { return apply_dht_add_node(str); }); CMD2_ANY_STRING ("dht.add_node", [](auto, auto& str) { return apply_dht_add_node(str); });
CMD2_ANY ("dht.statistics", [](auto, auto) { return control->dht_manager()->dht_statistics(); }); CMD2_ANY ("dht.statistics", [](auto, auto) { return control->dht_manager()->dht_statistics(); });
-3
View File
@@ -70,9 +70,6 @@ Control::initialize() {
display::Window::slot_unschedule([this](display::Window* w) { m_display->unschedule(w); }); display::Window::slot_unschedule([this](display::Window* w) { m_display->unschedule(w); });
display::Window::slot_adjust([this]() { m_display->adjust_layout(); }); display::Window::slot_adjust([this]() { m_display->adjust_layout(); });
torrent::net_thread::http_stack()->set_user_agent(USER_AGENT);
m_core->listen_open();
m_core->set_hashing_view(*m_view_manager->find_throw("hashing")); m_core->set_hashing_view(*m_view_manager->find_throw("hashing"));
m_ui->init(this); m_ui->init(this);
+21 -7
View File
@@ -8,6 +8,7 @@
#include <torrent/object_stream.h> #include <torrent/object_stream.h>
#include <torrent/rate.h> #include <torrent/rate.h>
#include <torrent/runtime/network_manager.h> #include <torrent/runtime/network_manager.h>
#include <torrent/runtime/runtime.h>
#include <torrent/tracker/dht_controller.h> #include <torrent/tracker/dht_controller.h>
#include <torrent/utils/log.h> #include <torrent/utils/log.h>
@@ -141,18 +142,29 @@ DhtManager::save_dht_cache() {
void void
DhtManager::set_mode_by_user(const std::string& arg) { DhtManager::set_mode_by_user(const std::string& arg) {
for (int i = 0; i < dht_settings_num; i++) { unsigned int mode = [arg]() {
if (arg == dht_settings[i]) { for (int i = 0; i < dht_settings_num; i++) {
m_set_by_user = true; if (arg == dht_settings[i])
return set_mode_directly(i); return i;
} }
throw torrent::input_error("Invalid dht mode: " + arg);
}();
m_set_by_user = true;
if (!torrent::runtime::is_network_initialized()) {
m_start = mode;
return;
} }
set_mode_directly(mode);
} }
void void
DhtManager::set_mode_directly(unsigned int mode) { DhtManager::set_mode_directly(unsigned int mode) {
if (mode >= dht_settings_num) if (mode >= dht_settings_num)
throw torrent::input_error("Invalid argument."); throw torrent::input_error("Invalid dht mode.");
m_start = mode; m_start = mode;
@@ -164,8 +176,10 @@ DhtManager::set_mode_directly(unsigned int mode) {
void void
DhtManager::set_auto_if_untouched_and_has_session() { DhtManager::set_auto_if_untouched_and_has_session() {
if (m_set_by_user) if (m_set_by_user) {
set_mode_directly(m_start);
return; return;
}
if (rpc::call_command_string("session.path").empty()) { if (rpc::call_command_string("session.path").empty()) {
LT_LOG("DHT auto-start disabled, session path not set.", 0); LT_LOG("DHT auto-start disabled, session path not set.", 0);
+2 -34
View File
@@ -33,6 +33,8 @@
#include "core/http_queue.h" #include "core/http_queue.h"
#include "core/view.h" #include "core/view.h"
#include <torrent/runtime/client_config.h>
namespace core { namespace core {
const int Manager::create_start; const int Manager::create_start;
@@ -160,40 +162,6 @@ Manager::shutdown(bool force) {
} }
} }
void
Manager::listen_open() {
// This stuff really should be moved outside of manager, make it
// part of the init script.
if (!rpc::call_command_value("network.port_open"))
return;
int portFirst, portLast;
torrent::Object portRange = rpc::call_command("network.port_range");
if (!portRange.is_string())
throw torrent::input_error("Invalid port_range argument type.");
if (std::sscanf(portRange.as_string().c_str(), "%i-%i", &portFirst, &portLast) != 2)
throw torrent::input_error("Invalid port_range argument.");
if (portFirst > portLast || portLast >= (1 << 16))
throw torrent::input_error("Invalid port range.");
if (rpc::call_command_value("network.port_random")) {
int boundary = portFirst + random() % (portLast - portFirst + 1);
if (torrent::runtime::network_manager()->listen_open(boundary, portLast) ||
torrent::runtime::network_manager()->listen_open(portFirst, boundary))
return;
} else {
if (torrent::runtime::network_manager()->listen_open(portFirst, portLast))
return;
}
throw torrent::input_error("Could not open/bind port for listening: " + std::string(std::strerror(errno)));
}
void void
Manager::receive_http_failed(std::string msg) { Manager::receive_http_failed(std::string msg) {
push_log_std("Http download error: \"" + msg + "\""); push_log_std("Http download error: \"" + msg + "\"");
-2
View File
@@ -55,8 +55,6 @@ public:
void cleanup(); void cleanup();
void listen_open();
const std::string& magnet_path(); const std::string& magnet_path();
void set_magnet_path(const std::string& path); void set_magnet_path(const std::string& path);
+19
View File
@@ -11,7 +11,9 @@
#include <torrent/exceptions.h> #include <torrent/exceptions.h>
#include <torrent/data/chunk_utils.h> #include <torrent/data/chunk_utils.h>
#include <torrent/net/fd.h> #include <torrent/net/fd.h>
#include <torrent/net/http_stack.h>
#include <torrent/runtime/memory_manager.h> #include <torrent/runtime/memory_manager.h>
#include <torrent/runtime/runtime.h>
#include <torrent/utils/chrono.h> #include <torrent/utils/chrono.h>
#include <torrent/utils/log.h> #include <torrent/utils/log.h>
@@ -418,6 +420,20 @@ main(int argc, char** argv) {
lt_log_print(torrent::LOG_WARN, "The 'throttle.ip' command is deprecated and does nothing."); lt_log_print(torrent::LOG_WARN, "The 'throttle.ip' command is deprecated and does nothing.");
return torrent::Object(); return torrent::Object();
}); });
CMD_ANY("network.port_open", [](auto, auto) {
lt_log_print(torrent::LOG_WARN, "The 'network.port_open' command is deprecated and does nothing.");
return torrent::Object();
});
CMD_ANY("network.port_open.set", [](auto, auto) {
lt_log_print(torrent::LOG_WARN, "The 'network.port_open.set' command is deprecated and does nothing.");
return torrent::Object();
});
CMD_REDIRECT("network.port_random", "network.listen.port.random");
CMD_REDIRECT("network.port_random.set", "network.listen.port.random.set");
CMD_REDIRECT("network.port_range", "network.listen.port.range");
CMD_REDIRECT("network.port_range.set", "network.listen.port.range.set");
} }
{ {
@@ -450,6 +466,9 @@ main(int argc, char** argv) {
control->initialize(); control->initialize();
control->ui()->load_input_history(); control->ui()->load_input_history();
torrent::net_thread::http_stack()->set_user_agent(USER_AGENT);
torrent::runtime::initialize_network();
// Load session torrents and perform scheduled tasks to ensure session torrents are loaded // Load session torrents and perform scheduled tasks to ensure session torrents are loaded
// before arg torrents. // before arg torrents.
control->dht_manager()->set_auto_if_untouched_and_has_session(); control->dht_manager()->set_auto_if_untouched_and_has_session();