2
* UGENE - Integrated Bioinformatics Tools.
3
* Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
4
* http://ugene.unipro.ru
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22
#include <QtCore/QUrl>
23
#include <U2Core/AppContext.h>
24
#include <U2Core/Settings.h>
25
#include "NetworkConfiguration.h"
30
static const char * SETTINGS_HTTP_PROXY_HOST = "network_settings/http_proxy/host";
31
static const char * SETTINGS_HTTP_PROXY_PORT = "network_settings/http_proxy/port";
32
static const char * SETTINGS_HTTP_PROXY_USER = "network_settings/http_proxy/user";
33
static const char * SETTINGS_HTTP_PROXY_PASSWORD = "network_settings/http_proxy/password";
34
static const char * SETTINGS_HTTP_PROXY_ENABLED = "network_settings/http_proxy/enabled";
35
static const char * SETTINGS_PROXY_EXCEPTED_URLS = "network_settings/proxy_exc_urls";
36
static const char * SETTINGS_PROXY_EXCEPTED_URLS_ENABLED = "network_settings/proxy_exc_urls_enabled";
37
static const char * SETTINGS_SSL_PROTOCOL = "network_settings/ssl_protocol";
38
static const char * SETTINGS_REMOTE_REQUEST_TIMEOUT = "network_settings/remote_request/timeout";
40
const QString SslConfig::TLSV1 = "TlsV1";
41
const QString SslConfig::SSLV2 = "SslV2";
42
const QString SslConfig::SSLV3 = "SslV3";
44
const int RemoteRequestConfig::DEFAULT_REMOTE_REQUEST_TIMEOUT_SECONDS = 60;
47
NetworkConfiguration::NetworkConfiguration() {
48
Settings * s = AppContext::getSettings();
49
pc.excepted_addr_enabled = s->getValue( SETTINGS_PROXY_EXCEPTED_URLS_ENABLED ).toBool();
50
pc.excepted_addr = s->getValue( SETTINGS_PROXY_EXCEPTED_URLS ).toStringList();
52
QString httpProxyHost = s->getValue( SETTINGS_HTTP_PROXY_HOST ).toString();
53
int httpProxyPort = s->getValue( SETTINGS_HTTP_PROXY_PORT ).toInt();
55
if( !httpProxyHost.isEmpty() && httpProxyPort ) {
56
QNetworkProxy httpProxy( QNetworkProxy::HttpProxy, httpProxyHost, httpProxyPort );
58
QString user = s->getValue( SETTINGS_HTTP_PROXY_USER ).toString();
59
if (!user.isEmpty()) {
60
QByteArray passwordEncoded = s->getValue( SETTINGS_HTTP_PROXY_PASSWORD ).toByteArray();
61
QString passwordDecoded = QByteArray::fromBase64(passwordEncoded);
62
httpProxy.setUser(user);
63
httpProxy.setPassword(passwordDecoded);
66
pc.proxyz[QNetworkProxy::HttpProxy] = httpProxy;
67
pc.proxyz_usage[QNetworkProxy::HttpProxy] = s->getValue( SETTINGS_HTTP_PROXY_ENABLED ).toBool();
70
sslConfig.currentProtocol = s->getValue(SETTINGS_SSL_PROTOCOL, SslConfig::SSLV3).toString();
72
rrConfig.remoteRequestTimeout = s->getValue(SETTINGS_REMOTE_REQUEST_TIMEOUT, RemoteRequestConfig::DEFAULT_REMOTE_REQUEST_TIMEOUT_SECONDS).toInt();
76
NetworkConfiguration::~NetworkConfiguration() {
77
Settings * s = AppContext::getSettings();
78
s->setValue( SETTINGS_PROXY_EXCEPTED_URLS_ENABLED, pc.excepted_addr_enabled );
79
s->setValue( SETTINGS_PROXY_EXCEPTED_URLS, pc.excepted_addr );
80
s->setValue( SETTINGS_SSL_PROTOCOL, sslConfig.currentProtocol );
81
s->setValue( SETTINGS_REMOTE_REQUEST_TIMEOUT, rrConfig.remoteRequestTimeout );
83
QNetworkProxy httpP = getProxy( QNetworkProxy::HttpProxy );
85
if( !httpP.hostName().isEmpty() ) {
86
s->setValue( SETTINGS_HTTP_PROXY_HOST, httpP.hostName() );
87
s->setValue( SETTINGS_HTTP_PROXY_PORT, httpP.port() );
88
s->setValue( SETTINGS_HTTP_PROXY_USER, httpP.user());
89
s->setValue( SETTINGS_HTTP_PROXY_PASSWORD, httpP.password().toAscii().toBase64());
90
s->setValue( SETTINGS_HTTP_PROXY_ENABLED, isProxyUsed(QNetworkProxy::HttpProxy) );
95
int NetworkConfiguration::addProxy( const QNetworkProxy & p )
97
int ret = !pc.proxyz.contains( p.type() );
98
pc.proxyz.insert( p.type(), p );
99
pc.proxyz_usage.insert( p.type(), false ); //needs explicit enabling
103
QNetworkProxy NetworkConfiguration::getProxyByUrl( const QUrl & url ) const
105
Proxy_t prtype = url2type( url );
106
if( pc.proxyz.contains( prtype ) ) {
107
assert( pc.proxyz_usage.contains(prtype) );
108
if( pc.proxyz_usage[prtype] ) {
109
return ( pc.excepted_addr_enabled && pc.excepted_addr.contains( url.toString() ) ?
110
QNetworkProxy() : pc.proxyz[prtype] );
113
return QNetworkProxy();
116
QNetworkProxy NetworkConfiguration::getProxy( Proxy_t prtype ) const {
117
return (pc.proxyz.contains( prtype ) ? pc.proxyz[prtype] : QNetworkProxy() );
120
void NetworkConfiguration::removeProxy( Proxy_t prtype ) {
121
pc.proxyz.remove( prtype );
124
bool NetworkConfiguration::isProxyUsed( Proxy_t prtype ) const {
125
return pc.proxyz_usage.contains( prtype ) ? pc.proxyz_usage[prtype] : false;
128
void NetworkConfiguration::setExceptionsList( const QStringList & exc_addr ) {
129
pc.excepted_addr = exc_addr;
132
void NetworkConfiguration::setProxyUsed( Proxy_t prtype, bool flag ) {
133
if( pc.proxyz_usage.contains(prtype) ) {
134
pc.proxyz_usage[prtype] = flag;
138
Proxy_t NetworkConfiguration::url2type( const QUrl & url ) {
139
if( "http" == url.scheme() || "https" == url.scheme() ) {
140
return QNetworkProxy::HttpProxy;
142
if( "ftp" == url.scheme() ) {
143
return QNetworkProxy::FtpCachingProxy;
146
return QNetworkProxy::NoProxy;
149
void NetworkConfiguration::copyFrom(const NetworkConfiguration& image) {
151
sslConfig = image.sslConfig;
152
rrConfig = image.rrConfig;
155
#ifndef QT_NO_OPENSSL
157
QSsl::SslProtocol NetworkConfiguration::getSslProtocol() const
159
if (sslConfig.currentProtocol == SslConfig::SSLV2) {
161
} else if (sslConfig.currentProtocol == SslConfig::SSLV3) {
163
} else if (sslConfig.currentProtocol == SslConfig::TLSV1) {
172
QString NetworkConfiguration::getSslProtocolName() const
174
if (sslConfig.currentProtocol.isEmpty()) {
175
return SslConfig::SSLV3;
177
return sslConfig.currentProtocol;
181
void NetworkConfiguration::setSslProtocol( const QString& name )
183
sslConfig.currentProtocol = name;
186
void NetworkConfiguration::setRequestTimeout( const int timeout )
188
rrConfig.remoteRequestTimeout = timeout;