new mthod ping and called from GUI-detection, refs #5
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#include <QtNetwork/QNetworkAccessManager>
|
#include <QtNetwork/QNetworkAccessManager>
|
||||||
#include <QtNetwork/QNetworkReply>
|
#include <QtNetwork/QNetworkReply>
|
||||||
#include <QtNetwork/QSslError>
|
#include <QtNetwork/QSslError>
|
||||||
|
#include <QtNetwork/QHostInfo>
|
||||||
#include <QtCore/QTimer>
|
#include <QtCore/QTimer>
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
#ifndef Q_OS_WIN32
|
#ifndef Q_OS_WIN32
|
||||||
@@ -111,6 +112,32 @@ namespace proxy {
|
|||||||
//! Get list of proxies for a given URL.
|
//! Get list of proxies for a given URL.
|
||||||
virtual List proxies(const std::string& url) = 0;
|
virtual List proxies(const std::string& url) = 0;
|
||||||
#ifdef QT_NETWORK_LIB
|
#ifdef QT_NETWORK_LIB
|
||||||
|
//! Reset, stop all outstanding checks
|
||||||
|
void reset() {
|
||||||
|
_timeout1.stop();
|
||||||
|
_timeout2.stop();
|
||||||
|
for (Requests::iterator it(_requests.begin());
|
||||||
|
it!=_requests.end(); ++it)
|
||||||
|
clean(it->second.first);
|
||||||
|
_requests.clear();
|
||||||
|
}
|
||||||
|
//! Network Ping: Check access to a given address using default proxy.
|
||||||
|
void ping(const std::string& url, int timeout2=30000) {
|
||||||
|
reset();
|
||||||
|
_timeout2.setSingleShot(true);
|
||||||
|
_timeout2.setInterval(timeout2);
|
||||||
|
_url = url;
|
||||||
|
_direct = false; // simulate second try
|
||||||
|
QNetworkProxy defaultProxy;
|
||||||
|
setupProxyCheck(defaultProxy, url);
|
||||||
|
_timeout2.start();
|
||||||
|
}
|
||||||
|
void ping(const QUrl& url, int timeout2=30000) {
|
||||||
|
ping(url.toString().toStdString(), timeout2);
|
||||||
|
}
|
||||||
|
void ping(const QString& url, int timeout2=30000) {
|
||||||
|
ping(url.toStdString(), timeout2);
|
||||||
|
}
|
||||||
//! If Qt Network is available you may check the proxies found
|
//! If Qt Network is available you may check the proxies found
|
||||||
/*! First checks for direct access to the target. If this is not
|
/*! First checks for direct access to the target. If this is not
|
||||||
possible, starts scan for proxies.
|
possible, starts scan for proxies.
|
||||||
@@ -127,11 +154,8 @@ namespace proxy {
|
|||||||
PROXYFACE_LOG;
|
PROXYFACE_LOG;
|
||||||
qDebug()<<"Search proxy for URL, direct and default"
|
qDebug()<<"Search proxy for URL, direct and default"
|
||||||
<<"url="<<url.data()<<"timeout1="<<timeout1;
|
<<"url="<<url.data()<<"timeout1="<<timeout1;
|
||||||
|
reset();
|
||||||
_url = url;
|
_url = url;
|
||||||
for (Requests::iterator it(_requests.begin());
|
|
||||||
it!=_requests.end(); ++it)
|
|
||||||
clean(it->second.first);
|
|
||||||
_requests.clear();
|
|
||||||
_direct = true; // first try direct access
|
_direct = true; // first try direct access
|
||||||
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
QNetworkProxy directProxy(QNetworkProxy::NoProxy);
|
||||||
setupProxyCheck(directProxy, url);
|
setupProxyCheck(directProxy, url);
|
||||||
@@ -238,21 +262,12 @@ namespace proxy {
|
|||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void timeout() {
|
void timeout() {
|
||||||
PROXYFACE_LOG;
|
PROXYFACE_LOG;
|
||||||
_timeout1.stop();
|
reset();
|
||||||
_timeout2.stop();
|
qDebug()<<"Proxy detection timed out"<<"url="<<_url.data();
|
||||||
std::string url;
|
if (_direct) {
|
||||||
for (Requests::iterator it(_requests.begin());
|
|
||||||
it!=_requests.end(); ++it) {
|
|
||||||
url = it->first->url().toString().toStdString();
|
|
||||||
clean(it->second.first);
|
|
||||||
}
|
|
||||||
_requests.clear();
|
|
||||||
qDebug()<<"Proxy detection timed out"<<"url="<<url.data();
|
|
||||||
if (_direct && url.size()) {
|
|
||||||
qDebug()<<"Direct or preconfigured proxy not available,"
|
qDebug()<<"Direct or preconfigured proxy not available,"
|
||||||
" try autoproxy negotiation";
|
" try autoproxy negotiation";
|
||||||
_direct = false;
|
_direct = false;
|
||||||
_url = url;
|
|
||||||
#ifndef Q_OS_WIN32
|
#ifndef Q_OS_WIN32
|
||||||
start(); // autoproxy detection in own thread
|
start(); // autoproxy detection in own thread
|
||||||
#else
|
#else
|
||||||
@@ -272,14 +287,9 @@ namespace proxy {
|
|||||||
toString(_requests[reply].second));
|
toString(_requests[reply].second));
|
||||||
return; // wait for timeout
|
return; // wait for timeout
|
||||||
}
|
}
|
||||||
_timeout1.stop();
|
|
||||||
_timeout2.stop();
|
|
||||||
QNetworkProxy prxy(_requests[reply].second);
|
QNetworkProxy prxy(_requests[reply].second);
|
||||||
QUrl url(reply->url());
|
QUrl url(reply->url());
|
||||||
for (Requests::iterator it(_requests.begin());
|
reset();
|
||||||
it!=_requests.end(); ++it)
|
|
||||||
clean(it->second.first);
|
|
||||||
_requests.clear();
|
|
||||||
qDebug()<<"SUCCESS - Valid proxy found for url:"
|
qDebug()<<"SUCCESS - Valid proxy found for url:"
|
||||||
<<"url="<<url<<"proxy="<<toString(prxy);
|
<<"url="<<url<<"proxy="<<toString(prxy);
|
||||||
proxyFound(url, prxy);
|
proxyFound(url, prxy);
|
||||||
|
@@ -42,34 +42,47 @@ namespace gui {
|
|||||||
QString, QString)),
|
QString, QString)),
|
||||||
SIGNAL(temporaryError(QNetworkReply::NetworkError,
|
SIGNAL(temporaryError(QNetworkReply::NetworkError,
|
||||||
QString, QString)));
|
QString, QString)));
|
||||||
accept();
|
acceptValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkUrl(const QString& url) {
|
void checkUrl(const QString& url) {
|
||||||
_testUrl = url;
|
_testUrl = url;
|
||||||
accept();
|
acceptValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
void acceptValues() {
|
void retry() {
|
||||||
|
acceptValues(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void acceptValues(bool retry=false) {
|
||||||
|
_auto.reset();
|
||||||
QNetworkProxy::ProxyType type(QNetworkProxy::NoProxy);
|
QNetworkProxy::ProxyType type(QNetworkProxy::NoProxy);
|
||||||
switch (_type->currentIndex()) {
|
switch (_type->currentIndex()) {
|
||||||
case 0: {
|
case 0: try {
|
||||||
_url->setEnabled(false);
|
_url->setEnabled(false);
|
||||||
_port->setEnabled(false);
|
_port->setEnabled(false);
|
||||||
type=QNetworkProxy::NoProxy;
|
type=QNetworkProxy::NoProxy;
|
||||||
|
_auto.proxy(_testUrl);
|
||||||
|
} catch (...) {} break;
|
||||||
|
case 1: {
|
||||||
|
type=QNetworkProxy::HttpProxy;
|
||||||
|
QNetworkProxy::setApplicationProxy
|
||||||
|
(QNetworkProxy(type, _url->text(), _port->value()));
|
||||||
|
_auto.ping(_testUrl);
|
||||||
|
} break;
|
||||||
|
case 2: {
|
||||||
|
type=QNetworkProxy::Socks5Proxy;
|
||||||
|
QNetworkProxy::setApplicationProxy
|
||||||
|
(QNetworkProxy(type, _url->text(), _port->value()));
|
||||||
|
_auto.ping(_testUrl);
|
||||||
} break;
|
} break;
|
||||||
case 1: type=QNetworkProxy::HttpProxy; break;
|
|
||||||
case 2: type=QNetworkProxy::Socks5Proxy; break;
|
|
||||||
}
|
}
|
||||||
QNetworkProxy::setApplicationProxy
|
if (!retry) detecting();
|
||||||
(QNetworkProxy(type, _url->text(), _port->value()));
|
|
||||||
try {
|
|
||||||
_auto.proxy(_testUrl);
|
|
||||||
} catch (...) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
|
|
||||||
|
void detecting();
|
||||||
void proxyFound(const QUrl&, const QNetworkProxy&);
|
void proxyFound(const QUrl&, const QNetworkProxy&);
|
||||||
void proxyError(QNetworkReply::NetworkError);
|
void proxyError(QNetworkReply::NetworkError);
|
||||||
void temporaryError(QNetworkReply::NetworkError, QString, QString);
|
void temporaryError(QNetworkReply::NetworkError, QString, QString);
|
||||||
@@ -77,9 +90,13 @@ namespace gui {
|
|||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
|
|
||||||
void proxyFoundSlot(const QUrl&, const QNetworkProxy& p) {
|
void proxyFoundSlot(const QUrl&, const QNetworkProxy& p) {
|
||||||
QNetworkProxy::setApplicationProxy(p);
|
QNetworkProxy prx(p.type()==QNetworkProxy::DefaultProxy
|
||||||
_url->setText(p.hostName());
|
?QNetworkProxy::applicationProxy():p);
|
||||||
_port->setValue(p.port());
|
if (prx.type()==QNetworkProxy::DefaultProxy) return;
|
||||||
|
_url->setText(prx.hostName());
|
||||||
|
_port->setValue(prx.port());
|
||||||
|
if (p.type()==QNetworkProxy::DefaultProxy) return;
|
||||||
|
QNetworkProxy::setApplicationProxy(prx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void on__type_currentIndexChanged(int index) {
|
void on__type_currentIndexChanged(int index) {
|
||||||
|
Reference in New Issue
Block a user