strawberry-audio-player-win.../src/core/networkproxyfactory.cpp

146 lines
3.7 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QtGlobal>
#include <QMutex>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QStringList>
#include <QUrl>
#include <QNetworkProxy>
#include <QSettings>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
#include "networkproxyfactory.h"
2018-02-27 18:06:05 +01:00
NetworkProxyFactory *NetworkProxyFactory::sInstance = nullptr;
const char *NetworkProxyFactory::kSettingsGroup = "NetworkProxy";
NetworkProxyFactory::NetworkProxyFactory()
2023-02-18 14:09:27 +01:00
: mode_(Mode::System),
2018-02-27 18:06:05 +01:00
type_(QNetworkProxy::HttpProxy),
port_(8080),
use_authentication_(false) {
2021-06-20 19:04:08 +02:00
2018-02-27 18:06:05 +01:00
#ifdef Q_OS_LINUX
// Linux uses environment variables to pass proxy configuration information, which systemProxyForQuery doesn't support for some reason.
2018-02-27 18:06:05 +01:00
QStringList urls;
2021-06-20 19:04:08 +02:00
urls << QString::fromLocal8Bit(qgetenv("HTTP_PROXY"));
urls << QString::fromLocal8Bit(qgetenv("http_proxy"));
urls << QString::fromLocal8Bit(qgetenv("ALL_PROXY"));
urls << QString::fromLocal8Bit(qgetenv("all_proxy"));
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Detected system proxy URLs:" << urls;
for (const QString &url_str : urls) {
if (url_str.isEmpty()) continue;
env_url_ = QUrl(url_str);
break;
}
#endif
ReloadSettings();
}
NetworkProxyFactory *NetworkProxyFactory::Instance() {
if (!sInstance) {
sInstance = new NetworkProxyFactory;
}
return sInstance;
}
void NetworkProxyFactory::ReloadSettings() {
QMutexLocker l(&mutex_);
QSettings s;
s.beginGroup(kSettingsGroup);
2023-02-18 14:09:27 +01:00
mode_ = static_cast<Mode>(s.value("mode", static_cast<int>(Mode::System)).toInt());
2018-02-27 18:06:05 +01:00
type_ = QNetworkProxy::ProxyType(s.value("type", QNetworkProxy::HttpProxy).toInt());
hostname_ = s.value("hostname").toString();
port_ = s.value("port", 8080).toInt();
use_authentication_ = s.value("use_authentication", false).toBool();
username_ = s.value("username").toString();
password_ = s.value("password").toString();
2021-06-20 19:04:08 +02:00
s.endGroup();
2018-02-27 18:06:05 +01:00
}
QList<QNetworkProxy> NetworkProxyFactory::queryProxy(const QNetworkProxyQuery &query) {
QMutexLocker l(&mutex_);
QNetworkProxy ret;
switch (mode_) {
2023-02-18 14:09:27 +01:00
case Mode::System:
2018-02-27 18:06:05 +01:00
#ifdef Q_OS_LINUX
Q_UNUSED(query);
if (env_url_.isEmpty()) {
ret.setType(QNetworkProxy::NoProxy);
}
else {
ret.setHostName(env_url_.host());
ret.setPort(env_url_.port());
ret.setUser(env_url_.userName());
ret.setPassword(env_url_.password());
2021-08-23 21:21:08 +02:00
if (env_url_.scheme().startsWith("http")) {
2018-02-27 18:06:05 +01:00
ret.setType(QNetworkProxy::HttpProxy);
2021-08-23 21:21:08 +02:00
}
else {
2018-02-27 18:06:05 +01:00
ret.setType(QNetworkProxy::Socks5Proxy);
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
qLog(Debug) << "Using proxy URL:" << env_url_;
}
break;
#else
return systemProxyForQuery(query);
#endif
2023-02-18 14:09:27 +01:00
case Mode::Direct:
2018-02-27 18:06:05 +01:00
ret.setType(QNetworkProxy::NoProxy);
break;
2023-02-18 14:09:27 +01:00
case Mode::Manual:
2018-02-27 18:06:05 +01:00
ret.setType(type_);
ret.setHostName(hostname_);
ret.setPort(port_);
if (use_authentication_) {
ret.setUser(username_);
ret.setPassword(password_);
}
break;
}
return QList<QNetworkProxy>() << ret;
}