2014-01-16 15:06:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* 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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-01-16 15:06:45 +04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "clientproxy.h"
|
|
|
|
|
2014-11-10 01:25:57 +03:00
|
|
|
#include "configfile.h"
|
2017-05-09 15:24:11 +03:00
|
|
|
#include <QLoggingCategory>
|
2014-01-27 15:36:52 +04:00
|
|
|
#include <QUrl>
|
2015-03-24 23:30:42 +03:00
|
|
|
#include <QThreadPool>
|
2014-01-16 15:06:45 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-01-16 15:06:45 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcClientProxy, "nextcloud.sync.clientproxy", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2014-01-16 15:06:45 +04:00
|
|
|
ClientProxy::ClientProxy(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:30:42 +03:00
|
|
|
static QNetworkProxy proxyFromConfig(const ConfigFile &cfg)
|
2014-01-16 15:06:45 +04:00
|
|
|
{
|
|
|
|
QNetworkProxy proxy;
|
|
|
|
|
|
|
|
if (cfg.proxyHostName().isEmpty())
|
|
|
|
return QNetworkProxy();
|
|
|
|
|
|
|
|
proxy.setHostName(cfg.proxyHostName());
|
|
|
|
proxy.setPort(cfg.proxyPort());
|
|
|
|
if (cfg.proxyNeedsAuth()) {
|
|
|
|
proxy.setUser(cfg.proxyUser());
|
|
|
|
proxy.setPassword(cfg.proxyPassword());
|
|
|
|
}
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:30:42 +03:00
|
|
|
bool ClientProxy::isUsingSystemDefault()
|
|
|
|
{
|
|
|
|
OCC::ConfigFile cfg;
|
|
|
|
|
|
|
|
// if there is no config file, default to system proxy.
|
|
|
|
if (cfg.exists()) {
|
|
|
|
return cfg.proxyType() == QNetworkProxy::DefaultProxy;
|
|
|
|
}
|
|
|
|
|
2020-01-21 13:59:07 +03:00
|
|
|
return true;
|
2015-03-24 23:30:42 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 22:05:11 +03:00
|
|
|
const char *ClientProxy::proxyTypeToCStr(QNetworkProxy::ProxyType type)
|
2020-06-18 21:13:57 +03:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case QNetworkProxy::NoProxy:
|
|
|
|
return "NoProxy";
|
|
|
|
case QNetworkProxy::DefaultProxy:
|
|
|
|
return "DefaultProxy";
|
|
|
|
case QNetworkProxy::Socks5Proxy:
|
|
|
|
return "Socks5Proxy";
|
|
|
|
case QNetworkProxy::HttpProxy:
|
|
|
|
return "HttpProxy";
|
|
|
|
case QNetworkProxy::HttpCachingProxy:
|
|
|
|
return "HttpCachingProxy";
|
|
|
|
case QNetworkProxy::FtpCachingProxy:
|
|
|
|
return "FtpCachingProxy";
|
|
|
|
default:
|
|
|
|
return "NoProxy";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:05:11 +03:00
|
|
|
QString ClientProxy::printQNetworkProxy(const QNetworkProxy &proxy)
|
2015-03-26 17:17:02 +03:00
|
|
|
{
|
2020-06-18 21:13:57 +03:00
|
|
|
return QString("%1://%2:%3").arg(proxyTypeToCStr(proxy.type())).arg(proxy.hostName()).arg(proxy.port());
|
2015-03-26 17:17:02 +03:00
|
|
|
}
|
|
|
|
|
2014-01-16 15:06:45 +04:00
|
|
|
void ClientProxy::setupQtProxyFromConfig()
|
|
|
|
{
|
2014-11-10 00:34:07 +03:00
|
|
|
OCC::ConfigFile cfg;
|
2014-01-16 15:47:22 +04:00
|
|
|
int proxyType = QNetworkProxy::DefaultProxy;
|
|
|
|
QNetworkProxy proxy;
|
|
|
|
|
|
|
|
// if there is no config file, default to system proxy.
|
|
|
|
if (cfg.exists()) {
|
|
|
|
proxyType = cfg.proxyType();
|
|
|
|
proxy = proxyFromConfig(cfg);
|
|
|
|
}
|
2014-01-16 15:06:45 +04:00
|
|
|
|
|
|
|
switch (proxyType) {
|
2019-01-31 16:14:31 +03:00
|
|
|
case QNetworkProxy::NoProxy:
|
|
|
|
qCInfo(lcClientProxy) << "Set proxy configuration to use NO proxy";
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(false);
|
|
|
|
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::DefaultProxy:
|
2020-05-03 23:54:50 +03:00
|
|
|
qCInfo(lcClientProxy) << "Set proxy configuration to use the preferred system proxy for http tcp connections";
|
2019-01-31 16:14:31 +03:00
|
|
|
{
|
|
|
|
QNetworkProxyQuery query;
|
|
|
|
query.setProtocolTag("http");
|
|
|
|
query.setQueryType(QNetworkProxyQuery::TcpSocket);
|
|
|
|
auto proxies = QNetworkProxyFactory::proxyForQuery(query);
|
|
|
|
proxy = proxies.first();
|
|
|
|
}
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(false);
|
|
|
|
QNetworkProxy::setApplicationProxy(proxy);
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::Socks5Proxy:
|
|
|
|
proxy.setType(QNetworkProxy::Socks5Proxy);
|
|
|
|
qCInfo(lcClientProxy) << "Set proxy configuration to SOCKS5" << printQNetworkProxy(proxy);
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(false);
|
|
|
|
QNetworkProxy::setApplicationProxy(proxy);
|
|
|
|
break;
|
|
|
|
case QNetworkProxy::HttpProxy:
|
|
|
|
proxy.setType(QNetworkProxy::HttpProxy);
|
|
|
|
qCInfo(lcClientProxy) << "Set proxy configuration to HTTP" << printQNetworkProxy(proxy);
|
|
|
|
QNetworkProxyFactory::setUseSystemConfiguration(false);
|
|
|
|
QNetworkProxy::setApplicationProxy(proxy);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-01-16 15:06:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-24 23:30:42 +03:00
|
|
|
void ClientProxy::lookupSystemProxyAsync(const QUrl &url, QObject *dst, const char *slot)
|
|
|
|
{
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *runnable = new SystemProxyRunnable(url);
|
2015-03-24 23:30:42 +03:00
|
|
|
QObject::connect(runnable, SIGNAL(systemProxyLookedUp(QNetworkProxy)), dst, slot);
|
|
|
|
QThreadPool::globalInstance()->start(runnable); // takes ownership and deletes
|
2014-01-16 15:06:45 +04:00
|
|
|
}
|
|
|
|
|
2015-03-24 23:30:42 +03:00
|
|
|
SystemProxyRunnable::SystemProxyRunnable(const QUrl &url)
|
|
|
|
: QObject()
|
|
|
|
, QRunnable()
|
|
|
|
, _url(url)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemProxyRunnable::run()
|
|
|
|
{
|
2015-04-29 15:54:08 +03:00
|
|
|
qRegisterMetaType<QNetworkProxy>("QNetworkProxy");
|
2015-03-24 23:30:42 +03:00
|
|
|
QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(QNetworkProxyQuery(_url));
|
|
|
|
|
|
|
|
if (proxies.isEmpty()) {
|
|
|
|
emit systemProxyLookedUp(QNetworkProxy(QNetworkProxy::NoProxy));
|
|
|
|
} else {
|
|
|
|
emit systemProxyLookedUp(proxies.first());
|
|
|
|
// FIXME Would we really ever return more?
|
|
|
|
}
|
|
|
|
}
|
2014-01-16 15:06:45 +04:00
|
|
|
}
|