Skip to content

Commit

Permalink
Add option to change DHT/LSD/PeX (#540)
Browse files Browse the repository at this point in the history
Closes #532
  • Loading branch information
vktr committed Mar 6, 2018
1 parent eb1f9d8 commit 8d4f9d2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 11 deletions.
7 changes: 6 additions & 1 deletion lang/1033.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@
"add_trackers": "Add trackers",
"tracker_urls": "Tracker URLs",
"force_reannounce": "Force reannounce",
"copy_url": "Copy URL"
"copy_url": "Copy URL",
"enable_dht": "Enable DHT",
"enable_lsd": "Enable LSD",
"enable_pex": "Enable PeX",
"changing_pex_settings_requires_restart": "Changing the PeX setting requires a restart",
"restart_required": "Restart required"
}
}
3 changes: 3 additions & 0 deletions src/picotorrent/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ namespace pt
int ActiveTrackerLimit();

bool EnableAnonymousMode();
void EnableAnonymousMode(bool value);
bool EnableDht();
void EnableDht(bool value);
bool EnableLsd();
void EnableLsd(bool value);
bool EnablePex();
void EnablePex(bool value);

bool RequireIncomingEncryption();
void RequireIncomingEncryption(bool value);
Expand Down
15 changes: 15 additions & 0 deletions src/picotorrent/config_sessionsection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ bool Configuration::SessionSection::EnableAnonymousMode()
return Get("session", "anonymous_mode", false);
}

void Configuration::SessionSection::EnableAnonymousMode(bool value)
{
Set("session", "anonymous_mode", value);
}

bool Configuration::SessionSection::EnableDht()
{
return Get("session", "enable_dht", true);
Expand All @@ -82,6 +87,16 @@ void Configuration::SessionSection::EnableLsd(bool value)
Set("session", "enable_lsd", value);
}

bool Configuration::SessionSection::EnablePex()
{
return Get("session", "enable_pex", true);
}

void Configuration::SessionSection::EnablePex(bool value)
{
Set("session", "enable_pex", value);
}

bool Configuration::SessionSection::EnableDownloadRateLimit()
{
return Get("session", "enable_download_rate_limit", false);
Expand Down
51 changes: 44 additions & 7 deletions src/picotorrent/connectionpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ using pt::ConnectionPage;

ConnectionPage::ConnectionPage(wxWindow* parent, std::shared_ptr<pt::Configuration> config, std::shared_ptr<pt::Translator> tr)
: wxPanel(parent, wxID_ANY),
m_cfg(config)
m_parent(parent),
m_cfg(config),
m_translator(tr)
{
wxStaticBoxSizer* listenSizer = new wxStaticBoxSizer(wxVERTICAL, this, i18n(tr, "listen_interface"));
wxFlexGridSizer* listenGrid = new wxFlexGridSizer(1, 10, 10);
Expand All @@ -26,23 +28,45 @@ ConnectionPage::ConnectionPage(wxWindow* parent, std::shared_ptr<pt::Configurati
listenGrid->Add(m_listenInterfaces, 1, wxEXPAND);
listenSizer->Add(listenGrid, 1, wxEXPAND | wxALL, 5);

wxStaticBoxSizer* privacySizer = new wxStaticBoxSizer(wxVERTICAL, this, i18n(tr, "encryption"));
wxFlexGridSizer* privacyGrid = new wxFlexGridSizer(1, 10, 10);
wxStaticBoxSizer* encryptionSizer = new wxStaticBoxSizer(wxVERTICAL, this, i18n(tr, "encryption"));
wxFlexGridSizer* encryptionGrid = new wxFlexGridSizer(1, 10, 10);

m_incomingEncryption = new wxCheckBox(privacySizer->GetStaticBox(), wxID_ANY, i18n(tr, "require_encryption_incoming"));
m_incomingEncryption = new wxCheckBox(encryptionSizer->GetStaticBox(), wxID_ANY, i18n(tr, "require_encryption_incoming"));
m_incomingEncryption->SetValue(m_cfg->Session()->RequireIncomingEncryption());

m_outgoingEncryption = new wxCheckBox(privacySizer->GetStaticBox(), wxID_ANY, i18n(tr, "require_encryption_outgoing"));
m_outgoingEncryption = new wxCheckBox(encryptionSizer->GetStaticBox(), wxID_ANY, i18n(tr, "require_encryption_outgoing"));
m_outgoingEncryption->SetValue(m_cfg->Session()->RequireOutgoingEncryption());

encryptionGrid->AddGrowableCol(0, 1);
encryptionGrid->Add(m_incomingEncryption, 1, wxEXPAND);
encryptionGrid->Add(m_outgoingEncryption, 1, wxEXPAND);
encryptionSizer->Add(encryptionGrid, 1, wxEXPAND | wxALL, 5);

wxStaticBoxSizer* privacySizer = new wxStaticBoxSizer(wxVERTICAL, this, i18n(tr, "privacy"));
wxFlexGridSizer* privacyGrid = new wxFlexGridSizer(3, 10, 10);

m_enableDht = new wxCheckBox(privacySizer->GetStaticBox(), wxID_ANY, i18n(tr, "enable_dht"));
m_enableDht->SetValue(m_cfg->Session()->EnableDht());

m_enableLsd = new wxCheckBox(privacySizer->GetStaticBox(), wxID_ANY, i18n(tr, "enable_lsd"));
m_enableLsd->SetValue(m_cfg->Session()->EnableLsd());

m_enablePex = new wxCheckBox(privacySizer->GetStaticBox(), wxID_ANY, i18n(tr, "enable_pex"));
m_enablePex->SetValue(m_cfg->Session()->EnablePex());

privacyGrid->AddGrowableCol(0, 1);
privacyGrid->Add(m_incomingEncryption, 1, wxEXPAND);
privacyGrid->Add(m_outgoingEncryption, 1, wxEXPAND);
privacyGrid->AddGrowableCol(1, 1);
privacyGrid->AddGrowableCol(2, 1);
privacyGrid->Add(m_enableDht, 1, wxEXPAND);
privacyGrid->Add(m_enableLsd, 1, wxEXPAND);
privacyGrid->Add(m_enablePex, 1, wxEXPAND);
privacySizer->Add(privacyGrid, 1, wxEXPAND | wxALL, 5);

wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(listenSizer, 0, wxEXPAND);
sizer->AddSpacer(10);
sizer->Add(encryptionSizer, 0, wxEXPAND);
sizer->AddSpacer(10);
sizer->Add(privacySizer, 0, wxEXPAND);
sizer->AddStretchSpacer();

Expand All @@ -68,9 +92,22 @@ void ConnectionPage::ApplyConfiguration()
m_cfg->ListenInterfaces(ifs);
m_cfg->Session()->RequireIncomingEncryption(m_incomingEncryption->GetValue());
m_cfg->Session()->RequireOutgoingEncryption(m_outgoingEncryption->GetValue());

m_cfg->Session()->EnableDht(m_enableDht->GetValue());
m_cfg->Session()->EnableLsd(m_enableLsd->GetValue());
m_cfg->Session()->EnablePex(m_enablePex->GetValue());
}

bool ConnectionPage::ValidateConfiguration(wxString& error)
{
if (m_enablePex->GetValue() != m_cfg->Session()->EnablePex())
{
wxMessageBox(
i18n(m_translator, "changing_pex_settings_requires_restart"),
i18n(m_translator, "restart_required"),
wxOK | wxCENTRE,
m_parent);
}

return true;
}
7 changes: 7 additions & 0 deletions src/picotorrent/connectionpage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ namespace pt
bool ValidateConfiguration(wxString& error);

private:
wxWindow* m_parent;

wxTextCtrl* m_listenInterfaces;
wxCheckBox* m_incomingEncryption;
wxCheckBox* m_outgoingEncryption;

wxCheckBox* m_enableDht;
wxCheckBox* m_enableLsd;
wxCheckBox* m_enablePex;

std::shared_ptr<Configuration> m_cfg;
std::shared_ptr<Translator> m_translator;
};
}
6 changes: 5 additions & 1 deletion src/picotorrent/mainframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ void MainFrame::OnSessionAlert()
lt::span<const int64_t> counters = ssa->counters();
int idx = -1;

if ((idx = lt::find_metric_idx("dht.dht_nodes")) >= 0)
if (!m_config->Session()->EnableDht())
{
m_status->UpdateDhtNodesCount(-1);
}
else if ((idx = lt::find_metric_idx("dht.dht_nodes")) >= 0)
{
m_status->UpdateDhtNodesCount(counters[idx]);
}
Expand Down
16 changes: 15 additions & 1 deletion src/picotorrent/sessionloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <filesystem>
#include <fstream>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/extensions/smart_ban.hpp>
#include <libtorrent/extensions/ut_metadata.hpp>
#include <libtorrent/extensions/ut_pex.hpp>
#include <libtorrent/magnet_uri.hpp>
#include <libtorrent/read_resume_data.hpp>
#include <libtorrent/session.hpp>
Expand Down Expand Up @@ -43,7 +46,18 @@ std::shared_ptr<pt::SessionState> SessionLoader::Load(std::shared_ptr<pt::Enviro
lt::settings_pack settings = SessionSettings::Get(cfg);

std::shared_ptr<SessionState> state = std::make_shared<SessionState>();
state->session = std::make_unique<lt::session>(settings);
state->session = std::make_unique<lt::session>(
settings,
lt::session_flags_t{ lt::session_handle::start_default_features });

// Enable plugins
state->session->add_extension(lt::create_smart_ban_plugin);
state->session->add_extension(lt::create_ut_metadata_plugin);

if (cfg->Session()->EnablePex())
{
state->session->add_extension(lt::create_ut_pex_plugin);
}

// Load state
if (fs::exists(stateFile))
Expand Down
9 changes: 8 additions & 1 deletion src/picotorrent/statusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ StatusBar::StatusBar(wxWindow* parent)

void StatusBar::UpdateDhtNodesCount(int64_t nodes)
{
SetStatusText(wxString::Format("DHT: %I64d node(s)", nodes), 1);
if (nodes < 0)
{
SetStatusText("DHT: disabled", 1);
}
else
{
SetStatusText(wxString::Format("DHT: %I64d node(s)", nodes), 1);
}
}

void StatusBar::UpdateTorrentCount(int64_t torrents)
Expand Down

0 comments on commit 8d4f9d2

Please sign in to comment.