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