1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019  Vladimir Golovnev <glassez@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL".  If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "portforwarderimpl.h"
30 
31 #include <libtorrent/session.hpp>
32 
33 #include <QDebug>
34 
35 #include "base/logger.h"
36 #include "base/settingsstorage.h"
37 
38 const QString KEY_ENABLED = QStringLiteral("Network/PortForwardingEnabled");
39 
PortForwarderImpl(lt::session * provider,QObject * parent)40 PortForwarderImpl::PortForwarderImpl(lt::session *provider, QObject *parent)
41     : Net::PortForwarder {parent}
42     , m_active {SettingsStorage::instance()->loadValue(KEY_ENABLED, true)}
43     , m_provider {provider}
44 {
45     if (m_active)
46         start();
47 }
48 
~PortForwarderImpl()49 PortForwarderImpl::~PortForwarderImpl()
50 {
51     stop();
52 }
53 
isEnabled() const54 bool PortForwarderImpl::isEnabled() const
55 {
56     return m_active;
57 }
58 
setEnabled(const bool enabled)59 void PortForwarderImpl::setEnabled(const bool enabled)
60 {
61     if (m_active != enabled)
62     {
63         if (enabled)
64             start();
65         else
66             stop();
67 
68         m_active = enabled;
69         SettingsStorage::instance()->storeValue(KEY_ENABLED, enabled);
70     }
71 }
72 
addPort(const quint16 port)73 void PortForwarderImpl::addPort(const quint16 port)
74 {
75     if (!m_mappedPorts.contains(port))
76     {
77         m_mappedPorts.insert(port, {});
78         if (isEnabled())
79             m_mappedPorts[port] = {m_provider->add_port_mapping(lt::session::tcp, port, port)};
80     }
81 }
82 
deletePort(const quint16 port)83 void PortForwarderImpl::deletePort(const quint16 port)
84 {
85     if (m_mappedPorts.contains(port))
86     {
87         if (isEnabled())
88         {
89             for (const lt::port_mapping_t &portMapping : m_mappedPorts[port])
90             m_provider->delete_port_mapping(portMapping);
91         }
92         m_mappedPorts.remove(port);
93     }
94 }
95 
start()96 void PortForwarderImpl::start()
97 {
98     qDebug("Enabling UPnP / NAT-PMP");
99     lt::settings_pack settingsPack = m_provider->get_settings();
100     settingsPack.set_bool(lt::settings_pack::enable_upnp, true);
101     settingsPack.set_bool(lt::settings_pack::enable_natpmp, true);
102     m_provider->apply_settings(settingsPack);
103     for (auto i = m_mappedPorts.begin(); i != m_mappedPorts.end(); ++i)
104     {
105         // quint16 port = i.key();
106         i.value() = {m_provider->add_port_mapping(lt::session::tcp, i.key(), i.key())};
107     }
108     LogMsg(tr("UPnP / NAT-PMP support [ON]"), Log::INFO);
109 }
110 
stop()111 void PortForwarderImpl::stop()
112 {
113     qDebug("Disabling UPnP / NAT-PMP");
114     lt::settings_pack settingsPack = m_provider->get_settings();
115     settingsPack.set_bool(lt::settings_pack::enable_upnp, false);
116     settingsPack.set_bool(lt::settings_pack::enable_natpmp, false);
117     m_provider->apply_settings(settingsPack);
118     LogMsg(tr("UPnP / NAT-PMP support [OFF]"), Log::INFO);
119 }
120