2015-04-19 18:17:47 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. 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), 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.
|
|
|
|
*/
|
|
|
|
|
2016-06-03 17:03:17 +03:00
|
|
|
#include "portforwarder.h"
|
|
|
|
|
2015-04-19 18:17:47 +03:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <libtorrent/session.hpp>
|
2016-06-03 17:03:17 +03:00
|
|
|
#include <libtorrent/version.hpp>
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/logger.h"
|
2016-06-03 17:03:17 +03:00
|
|
|
#include "base/settingsstorage.h"
|
|
|
|
|
2016-10-30 00:11:52 +03:00
|
|
|
static const QString KEY_ENABLED = QLatin1String("Network/PortForwardingEnabled");
|
2015-04-19 18:17:47 +03:00
|
|
|
|
|
|
|
namespace libt = libtorrent;
|
|
|
|
using namespace Net;
|
|
|
|
|
|
|
|
PortForwarder::PortForwarder(libtorrent::session *provider, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_active(false)
|
|
|
|
, m_provider(provider)
|
|
|
|
{
|
2016-06-03 17:03:17 +03:00
|
|
|
if (SettingsStorage::instance()->loadValue(KEY_ENABLED, true).toBool())
|
|
|
|
start();
|
2015-04-19 18:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PortForwarder::~PortForwarder()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::initInstance(libtorrent::session *const provider)
|
|
|
|
{
|
|
|
|
if (!m_instance)
|
|
|
|
m_instance = new PortForwarder(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::freeInstance()
|
|
|
|
{
|
|
|
|
if (m_instance) {
|
|
|
|
delete m_instance;
|
|
|
|
m_instance = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PortForwarder *PortForwarder::instance()
|
|
|
|
{
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
2016-06-03 17:03:17 +03:00
|
|
|
bool PortForwarder::isEnabled() const
|
|
|
|
{
|
|
|
|
return m_active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::setEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (m_active != enabled) {
|
|
|
|
if (enabled)
|
|
|
|
start();
|
|
|
|
else
|
|
|
|
stop();
|
2016-10-30 00:11:52 +03:00
|
|
|
|
|
|
|
SettingsStorage::instance()->storeValue(KEY_ENABLED, enabled);
|
2016-06-03 17:03:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 23:09:30 +03:00
|
|
|
void PortForwarder::addPort(quint16 port)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
if (!m_mappedPorts.contains(port)) {
|
|
|
|
m_mappedPorts.insert(port, 0);
|
2015-07-25 15:40:15 +03:00
|
|
|
if (m_active)
|
2015-04-19 18:17:47 +03:00
|
|
|
m_mappedPorts[port] = m_provider->add_port_mapping(libt::session::tcp, port, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-11 23:09:30 +03:00
|
|
|
void PortForwarder::deletePort(quint16 port)
|
2015-04-19 18:17:47 +03:00
|
|
|
{
|
|
|
|
if (m_mappedPorts.contains(port)) {
|
2015-07-25 15:40:15 +03:00
|
|
|
if (m_active)
|
2015-04-19 18:17:47 +03:00
|
|
|
m_provider->delete_port_mapping(m_mappedPorts[port]);
|
|
|
|
m_mappedPorts.remove(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::start()
|
|
|
|
{
|
|
|
|
qDebug("Enabling UPnP / NAT-PMP");
|
2016-06-03 17:03:17 +03:00
|
|
|
#if LIBTORRENT_VERSION_NUM < 10100
|
2015-04-19 18:17:47 +03:00
|
|
|
m_provider->start_upnp();
|
|
|
|
m_provider->start_natpmp();
|
2016-06-03 17:03:17 +03:00
|
|
|
#else
|
|
|
|
libt::settings_pack settingsPack = m_provider->get_settings();
|
|
|
|
settingsPack.set_bool(libt::settings_pack::enable_upnp, true);
|
|
|
|
settingsPack.set_bool(libt::settings_pack::enable_natpmp, true);
|
|
|
|
m_provider->apply_settings(settingsPack);
|
|
|
|
#endif
|
2016-09-11 23:09:30 +03:00
|
|
|
foreach (quint16 port, m_mappedPorts.keys())
|
2015-04-19 18:17:47 +03:00
|
|
|
m_mappedPorts[port] = m_provider->add_port_mapping(libt::session::tcp, port, port);
|
|
|
|
m_active = true;
|
|
|
|
Logger::instance()->addMessage(tr("UPnP / NAT-PMP support [ON]"), Log::INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarder::stop()
|
|
|
|
{
|
|
|
|
qDebug("Disabling UPnP / NAT-PMP");
|
2016-06-03 17:03:17 +03:00
|
|
|
#if LIBTORRENT_VERSION_NUM < 10100
|
2015-04-19 18:17:47 +03:00
|
|
|
m_provider->stop_upnp();
|
|
|
|
m_provider->stop_natpmp();
|
2016-06-03 17:03:17 +03:00
|
|
|
#else
|
|
|
|
libt::settings_pack settingsPack = m_provider->get_settings();
|
|
|
|
settingsPack.set_bool(libt::settings_pack::enable_upnp, false);
|
|
|
|
settingsPack.set_bool(libt::settings_pack::enable_natpmp, false);
|
|
|
|
m_provider->apply_settings(settingsPack);
|
|
|
|
#endif
|
2015-04-19 18:17:47 +03:00
|
|
|
m_active = false;
|
|
|
|
Logger::instance()->addMessage(tr("UPnP / NAT-PMP support [OFF]"), Log::INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
PortForwarder *PortForwarder::m_instance = 0;
|