2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
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.
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.
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.
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.
29 #include "proxyconfigurationmanager.h"
31 #define SETTINGS_KEY(name) ("Network/Proxy/" name)
33 bool Net::operator==(const ProxyConfiguration
&left
, const ProxyConfiguration
&right
)
35 return (left
.type
== right
.type
)
36 && (left
.ip
== right
.ip
)
37 && (left
.port
== right
.port
)
38 && (left
.username
== right
.username
)
39 && (left
.password
== right
.password
);
42 bool Net::operator!=(const ProxyConfiguration
&left
, const ProxyConfiguration
&right
)
44 return !(left
== right
);
49 ProxyConfigurationManager
*ProxyConfigurationManager::m_instance
= nullptr;
51 ProxyConfigurationManager::ProxyConfigurationManager(QObject
*parent
)
53 , m_storeProxyOnlyForTorrents
{SETTINGS_KEY("OnlyForTorrents")}
54 , m_storeProxyType
{SETTINGS_KEY("Type")}
55 , m_storeProxyIP
{SETTINGS_KEY("IP")}
56 , m_storeProxyPort
{SETTINGS_KEY("Port")}
57 , m_storeProxyUsername
{SETTINGS_KEY("Username")}
58 , m_storeProxyPassword
{SETTINGS_KEY("Password")}
60 m_config
.type
= m_storeProxyType
.get(ProxyType::None
);
61 if ((m_config
.type
< ProxyType::None
) || (m_config
.type
> ProxyType::SOCKS4
))
62 m_config
.type
= ProxyType::None
;
63 m_config
.ip
= m_storeProxyIP
.get(QLatin1String("0.0.0.0"));
64 m_config
.port
= m_storeProxyPort
.get(8080);
65 m_config
.username
= m_storeProxyUsername
;
66 m_config
.password
= m_storeProxyPassword
;
70 void ProxyConfigurationManager::initInstance()
73 m_instance
= new ProxyConfigurationManager
;
76 void ProxyConfigurationManager::freeInstance()
82 ProxyConfigurationManager
*ProxyConfigurationManager::instance()
87 ProxyConfiguration
ProxyConfigurationManager::proxyConfiguration() const
92 void ProxyConfigurationManager::setProxyConfiguration(const ProxyConfiguration
&config
)
94 if (m_config
!= config
)
97 m_storeProxyType
= config
.type
;
98 m_storeProxyIP
= config
.ip
;
99 m_storeProxyPort
= config
.port
;
100 m_storeProxyUsername
= config
.username
;
101 m_storeProxyPassword
= config
.password
;
104 emit
proxyConfigurationChanged();
108 bool ProxyConfigurationManager::isProxyOnlyForTorrents() const
110 return m_storeProxyOnlyForTorrents
|| (m_config
.type
== ProxyType::SOCKS4
);
113 void ProxyConfigurationManager::setProxyOnlyForTorrents(const bool onlyForTorrents
)
115 m_storeProxyOnlyForTorrents
= onlyForTorrents
;
118 bool ProxyConfigurationManager::isAuthenticationRequired() const
120 return m_config
.type
== ProxyType::SOCKS5_PW
121 || m_config
.type
== ProxyType::HTTP_PW
;
124 void ProxyConfigurationManager::configureProxy()
126 // Define environment variables for urllib in search engine plugins
127 QString proxyStrHTTP
, proxyStrSOCK
;
128 if (!isProxyOnlyForTorrents())
130 switch (m_config
.type
)
132 case ProxyType::HTTP_PW
:
133 proxyStrHTTP
= QString::fromLatin1("http://%1:%2@%3:%4").arg(m_config
.username
134 , m_config
.password
, m_config
.ip
, QString::number(m_config
.port
));
136 case ProxyType::HTTP
:
137 proxyStrHTTP
= QString::fromLatin1("http://%1:%2").arg(m_config
.ip
, QString::number(m_config
.port
));
139 case ProxyType::SOCKS5
:
140 proxyStrSOCK
= QString::fromLatin1("%1:%2").arg(m_config
.ip
, QString::number(m_config
.port
));
142 case ProxyType::SOCKS5_PW
:
143 proxyStrSOCK
= QString::fromLatin1("%1:%2@%3:%4").arg(m_config
.username
144 , m_config
.password
, m_config
.ip
, QString::number(m_config
.port
));
147 qDebug("Disabling HTTP communications proxy");
150 qDebug("HTTP communications proxy string: %s"
151 , qUtf8Printable((m_config
.type
== ProxyType::SOCKS5
) || (m_config
.type
== ProxyType::SOCKS5_PW
)
152 ? proxyStrSOCK
: proxyStrHTTP
));
155 qputenv("http_proxy", proxyStrHTTP
.toLocal8Bit());
156 qputenv("https_proxy", proxyStrHTTP
.toLocal8Bit());
157 qputenv("sock_proxy", proxyStrSOCK
.toLocal8Bit());