-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProxyDataService.cpp
More file actions
126 lines (104 loc) · 2.83 KB
/
ProxyDataService.cpp
File metadata and controls
126 lines (104 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "ProxyDataService.h"
#include "Settings.h"
#include "Exceptions.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTimer>
#include <QEventLoop>
ProxyDataService::ProxyDataService()
{
//test no proxy
if(test_connection(QNetworkProxy(QNetworkProxy::NoProxy)))
{
proxy_ = QNetworkProxy(QNetworkProxy::NoProxy);
return;
}
//test system proxy
QNetworkProxyQuery npq(QUrl("http://www.google.com"));
QList<QNetworkProxy> system_proxies = QNetworkProxyFactory::systemProxyForQuery(npq);
if(system_proxies.size() > 0)
{
foreach(const QNetworkProxy system_proxy, system_proxies)
{
//debug
if(test_connection(system_proxy))
{
proxy_ = system_proxy;
return;
}
}
}
//test proxy from ini
QNetworkProxy tmp_proxy;
tmp_proxy.setType(QNetworkProxy::HttpProxy);
tmp_proxy.setHostName(Settings::string("proxy_host"));
tmp_proxy.setPort(Settings::integer("proxy_port"));
tmp_proxy.setUser(Settings::string("proxy_user", true));
tmp_proxy.setPassword(Settings::string("proxy_password", true));
if(test_connection(tmp_proxy))
{
proxy_ = tmp_proxy;
return;
}
if(tmp_proxy.password().isEmpty() || tmp_proxy.user().isEmpty())
{
proxy_ = tmp_proxy;
return;
}
THROW(NetworkException, "Proxy settings provided, but couldn't connect to the internet.")
}
bool ProxyDataService::setCredentials(QString user, QString password)
{
ProxyDataService& service = instance();
QNetworkProxy tmp_proxy;
tmp_proxy.setType(QNetworkProxy::HttpProxy);
tmp_proxy.setHostName(service.proxy_.hostName());
tmp_proxy.setPort(service.proxy_.port());
tmp_proxy.setUser(user);
tmp_proxy.setPassword(password);
if(test_connection(tmp_proxy))
{
service.proxy_ = tmp_proxy;
return true;
}
return false;
}
const QNetworkProxy& ProxyDataService::getProxy()
{
ProxyDataService& service = instance();
return service.proxy_;
}
bool ProxyDataService::isConnected()
{
QNetworkProxy proxy = getProxy();
return test_connection(proxy);
}
ProxyDataService& ProxyDataService::instance()
{
static ProxyDataService service;
return service;
}
bool ProxyDataService::test_connection(QNetworkProxy proxy)
{
QNetworkAccessManager network_manager;
network_manager.setProxy(proxy);
QNetworkReply* reply = network_manager.get(QNetworkRequest(QUrl("https://www.google.com")));
//make the loop process the reply immediately
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
// add shorter time out for NoProxy check
QTimer timeout_timer;
if(proxy.type() == QNetworkProxy::NoProxy)
{
timeout_timer.setSingleShot(true);
connect(&timeout_timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timeout_timer.start(3000);
}
loop.exec();
if((proxy.type() == QNetworkProxy::NoProxy) && !timeout_timer.isActive())
{
// reply ran in time out
return false;
}
return (reply->error() == QNetworkReply::NoError);
}