2019-03-06 08:58:07 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2019 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portforwarderimpl.h"
|
|
|
|
|
|
|
|
#include <libtorrent/session.hpp>
|
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
#include "base/algorithm.h"
|
2019-03-06 08:58:07 +03:00
|
|
|
#include "base/logger.h"
|
|
|
|
|
2019-05-09 12:45:52 +08:00
|
|
|
PortForwarderImpl::PortForwarderImpl(lt::session *provider, QObject *parent)
|
2019-03-06 08:58:07 +03:00
|
|
|
: Net::PortForwarder {parent}
|
2022-03-23 23:56:47 +08:00
|
|
|
, m_storeActive {u"Network/PortForwardingEnabled"_qs, true}
|
2019-03-06 08:58:07 +03:00
|
|
|
, m_provider {provider}
|
|
|
|
{
|
2021-11-08 13:23:33 +08:00
|
|
|
if (isEnabled())
|
2019-03-06 08:58:07 +03:00
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
PortForwarderImpl::~PortForwarderImpl()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PortForwarderImpl::isEnabled() const
|
|
|
|
{
|
2021-11-08 13:23:33 +08:00
|
|
|
return m_storeActive;
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarderImpl::setEnabled(const bool enabled)
|
|
|
|
{
|
2022-10-18 23:47:05 +08:00
|
|
|
if (m_storeActive == enabled)
|
|
|
|
return;
|
2019-03-06 08:58:07 +03:00
|
|
|
|
2022-10-18 23:47:05 +08:00
|
|
|
if (enabled)
|
|
|
|
start();
|
|
|
|
else
|
|
|
|
stop();
|
|
|
|
m_storeActive = enabled;
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
void PortForwarderImpl::setPorts(const QString &profile, QSet<quint16> ports)
|
2019-03-06 08:58:07 +03:00
|
|
|
{
|
2022-11-05 11:33:21 +08:00
|
|
|
PortMapping &portMapping = m_portProfiles[profile];
|
|
|
|
Algorithm::removeIf(portMapping, [this, &ports](const quint16 port, const std::vector<lt::port_mapping_t> &handles)
|
|
|
|
{
|
|
|
|
// keep existing forwardings
|
|
|
|
const bool isAlreadyMapped = ports.remove(port);
|
|
|
|
if (isAlreadyMapped)
|
|
|
|
return false;
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
// remove outdated forwardings
|
|
|
|
for (const lt::port_mapping_t &handle : handles)
|
|
|
|
m_provider->delete_port_mapping(handle);
|
|
|
|
m_forwardedPorts.remove(port);
|
2019-03-06 08:58:07 +03:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
return true;
|
|
|
|
});
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
// add new forwardings
|
|
|
|
for (const quint16 port : ports)
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2022-11-05 11:33:21 +08:00
|
|
|
// port already forwarded/taken by other profile, don't do anything
|
|
|
|
if (m_forwardedPorts.contains(port))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isEnabled())
|
|
|
|
portMapping.insert(port, m_provider->add_port_mapping(lt::session::tcp, port, port));
|
|
|
|
else
|
|
|
|
portMapping.insert(port, {});
|
|
|
|
m_forwardedPorts.insert(port);
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
if (portMapping.isEmpty())
|
|
|
|
m_portProfiles.remove(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarderImpl::removePorts(const QString &profile)
|
|
|
|
{
|
|
|
|
setPorts(profile, {});
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarderImpl::start()
|
|
|
|
{
|
2022-11-09 08:02:34 +03:00
|
|
|
lt::settings_pack settingsPack;
|
2019-03-06 08:58:07 +03:00
|
|
|
settingsPack.set_bool(lt::settings_pack::enable_upnp, true);
|
|
|
|
settingsPack.set_bool(lt::settings_pack::enable_natpmp, true);
|
2022-11-09 08:02:34 +03:00
|
|
|
m_provider->apply_settings(std::move(settingsPack));
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter)
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2022-11-05 11:33:21 +08:00
|
|
|
PortMapping &portMapping = profileIter.value();
|
|
|
|
for (auto iter = portMapping.begin(); iter != portMapping.end(); ++iter)
|
|
|
|
{
|
|
|
|
Q_ASSERT(iter.value().empty());
|
|
|
|
|
|
|
|
const quint16 port = iter.key();
|
|
|
|
iter.value() = m_provider->add_port_mapping(lt::session::tcp, port, port);
|
|
|
|
}
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-02-21 13:07:04 +08:00
|
|
|
LogMsg(tr("UPnP/NAT-PMP support: ON"), Log::INFO);
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortForwarderImpl::stop()
|
|
|
|
{
|
2022-11-09 08:02:34 +03:00
|
|
|
lt::settings_pack settingsPack;
|
2019-03-06 08:58:07 +03:00
|
|
|
settingsPack.set_bool(lt::settings_pack::enable_upnp, false);
|
|
|
|
settingsPack.set_bool(lt::settings_pack::enable_natpmp, false);
|
2022-11-09 08:02:34 +03:00
|
|
|
m_provider->apply_settings(std::move(settingsPack));
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-11-05 11:33:21 +08:00
|
|
|
// don't clear m_portProfiles so a later `start()` call can restore the port forwardings
|
|
|
|
for (auto profileIter = m_portProfiles.begin(); profileIter != m_portProfiles.end(); ++profileIter)
|
|
|
|
{
|
|
|
|
PortMapping &portMapping = profileIter.value();
|
|
|
|
for (auto iter = portMapping.begin(); iter != portMapping.end(); ++iter)
|
|
|
|
iter.value().clear();
|
|
|
|
}
|
2022-10-18 23:47:05 +08:00
|
|
|
|
2022-02-21 13:07:04 +08:00
|
|
|
LogMsg(tr("UPnP/NAT-PMP support: OFF"), Log::INFO);
|
2019-03-06 08:58:07 +03:00
|
|
|
}
|