networksettings.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file networksettings.cpp
00013 ** \version $Id: networksettings.cpp 3299 2008-11-15 02:59:57Z edmanm $
00014 ** \brief Settings for configuring an HTTP/HTTPS proxy or bridges
00015 */
00016 
00017 #include "networksettings.h"
00018 
00019 #define SETTING_FASCIST_FIREWALL    "FascistFirewall"
00020 #define SETTING_REACHABLE_ADDRESSES "ReachableAddresses"
00021 #define SETTING_USE_HTTP_PROXY      "UseHttpProxy"
00022 #define SETTING_HTTP_PROXY          "HttpProxy"
00023 #define SETTING_HTTP_PROXY_AUTH     "HttpProxyAuthenticator"
00024 #define SETTING_USE_HTTPS_PROXY     "UseHttpsProxy"
00025 #define SETTING_HTTPS_PROXY         "HttpsProxy"
00026 #define SETTING_HTTPS_PROXY_AUTH    "HttpsProxyAuthenticator"
00027 #define SETTING_USE_BRIDGES         "UseBridges"
00028 #define SETTING_BRIDGE_LIST         "Bridge"
00029 #define SETTING_UPDATE_BRIDGES      "UpdateBridgesFromAuthority"
00030 #define SETTING_TUNNEL_DIR_CONNS    "TunnelDirConns"
00031 #define SETTING_PREFER_TUNNELED_DIR_CONNS "PreferTunneledDirConns"
00032 
00033 
00034 /** Default constructor */
00035 NetworkSettings::NetworkSettings(TorControl *torControl)
00036 : AbstractTorSettings("Network", torControl)
00037 {
00038   setDefault(SETTING_USE_HTTP_PROXY,    false);
00039   setDefault(SETTING_HTTP_PROXY,        "");
00040   setDefault(SETTING_HTTP_PROXY_AUTH,   "");
00041   setDefault(SETTING_USE_HTTPS_PROXY,   true);
00042   setDefault(SETTING_HTTPS_PROXY,       "");
00043   setDefault(SETTING_HTTPS_PROXY_AUTH,  "");
00044   setDefault(SETTING_USE_BRIDGES,       false);
00045   setDefault(SETTING_BRIDGE_LIST,       QStringList());
00046   setDefault(SETTING_FASCIST_FIREWALL,  false);
00047   setDefault(SETTING_TUNNEL_DIR_CONNS,  true);
00048   setDefault(SETTING_REACHABLE_ADDRESSES,
00049     QStringList() << "*:80" << "*:443");
00050 }
00051 
00052 /** Applies the current network configuration settings to Tor. If
00053  * <b>errmsg</b> is specified and an error occurs while applying the settings,
00054  * it will be set to a string describing the error. */
00055 bool
00056 NetworkSettings::apply(QString *errmsg)
00057 {
00058   QMultiHash<QString, QString> conf;
00059   quint32 torVersion = torControl()->getTorVersion();
00060 
00061   conf.insert(SETTING_REACHABLE_ADDRESSES,
00062     (getFascistFirewall() ? 
00063       localValue(SETTING_REACHABLE_ADDRESSES).toStringList().join(",") : ""));
00064 
00065   if (getUseHttpProxy())
00066     conf.insert(SETTING_HTTP_PROXY, localValue(SETTING_HTTP_PROXY).toString());
00067   else
00068     conf.insert(SETTING_HTTP_PROXY,  "");
00069   conf.insert(SETTING_HTTP_PROXY_AUTH,
00070               localValue(SETTING_HTTP_PROXY_AUTH).toString());
00071 
00072   if (getUseHttpProxy() && getUseHttpsProxy())
00073     conf.insert(SETTING_HTTPS_PROXY, localValue(SETTING_HTTPS_PROXY).toString());
00074   else
00075     conf.insert(SETTING_HTTPS_PROXY, "");
00076   conf.insert(SETTING_HTTPS_PROXY_AUTH,
00077               localValue(SETTING_HTTPS_PROXY_AUTH).toString());
00078 
00079   if (getUseBridges()) {
00080     /* We want to always enable TunnelDirConns and friends when using
00081      * bridge relays. */
00082     conf.insert(SETTING_TUNNEL_DIR_CONNS, "1");
00083     conf.insert(SETTING_PREFER_TUNNELED_DIR_CONNS, "1");
00084   } else if (torVersion <= 0x020021) {
00085     /* TunnelDirConns is enabled by default on Tor >= 0.2.0.22-rc, so don't
00086      * disable it if our Tor is recent enough. */
00087     conf.insert(SETTING_TUNNEL_DIR_CONNS, "0");
00088     conf.insert(SETTING_PREFER_TUNNELED_DIR_CONNS, "0");
00089   }
00090 
00091   if (torVersion >= 0x020003) {
00092     /* Do the bridge stuff only on Tor >= 0.2.0.3-alpha */
00093     QStringList bridges = localValue(SETTING_BRIDGE_LIST).toStringList();
00094     if (getUseBridges() && !bridges.isEmpty()) {
00095       conf.insert(SETTING_USE_BRIDGES, "1");
00096       conf.insert(SETTING_UPDATE_BRIDGES, "1");
00097       foreach (QString bridge, bridges) {
00098         conf.insert(SETTING_BRIDGE_LIST, bridge);
00099       }
00100     } else {
00101       conf.insert(SETTING_USE_BRIDGES, "0");
00102       conf.insert(SETTING_BRIDGE_LIST, "");
00103       conf.insert(SETTING_UPDATE_BRIDGES, "0");
00104     }
00105   }
00106   return torControl()->setConf(conf, errmsg);
00107 }
00108 
00109 /** Returns true if we need to set ReachableAddresses because we're behind a
00110  * restrictive firewall that limits the ports Tor can connect to. */
00111 bool
00112 NetworkSettings::getFascistFirewall()
00113 {
00114   return localValue(SETTING_FASCIST_FIREWALL).toBool();
00115 }
00116 
00117 /** Sets to <b>fascistFirewall</b> whether Tor should only create outgoing
00118  * connections to the list of ports specified in setReachablePorts().
00119  * \sa setReachablePorts() */
00120 void
00121 NetworkSettings::setFascistFirewall(bool fascistFirewall)
00122 {
00123   setValue(SETTING_FASCIST_FIREWALL, fascistFirewall);
00124 }
00125 
00126 /** Returns a list of ports to be specified in ReachableAddresses. */
00127 QList<quint16>
00128 NetworkSettings::getReachablePorts()
00129 {
00130   QList<quint16> reachablePorts;
00131   QStringList lineList;
00132   bool ok;
00133 
00134   lineList = value(SETTING_REACHABLE_ADDRESSES).toStringList();
00135   foreach (QString line, lineList) {
00136     foreach (QString address, line.split(",", QString::SkipEmptyParts)) {
00137       QStringList parts = address.split(":");
00138       if (parts.size() >= 2) {
00139         quint16 port = parts.at(1).toUInt(&ok);
00140         if (ok)
00141           reachablePorts << port;
00142       }
00143     }
00144   }
00145   return reachablePorts;
00146 }
00147 
00148 /** Sets the list of ports that will be specified in ReachableAddresses to
00149  * <b>reachablePorts</b>. */
00150 void
00151 NetworkSettings::setReachablePorts(const QList<quint16> &reachablePorts)
00152 {
00153   if (!reachablePorts.isEmpty()) {
00154     QStringList portList;
00155     foreach (quint16 port, reachablePorts) {
00156       portList << "*:" + QString::number(port);
00157     }
00158     setValue(SETTING_REACHABLE_ADDRESSES, portList);
00159   }
00160 }
00161 
00162 /** Returns true if Tor should make all its directory requests through a
00163  * proxy. */
00164 bool
00165 NetworkSettings::getUseHttpProxy()
00166 {
00167   return localValue(SETTING_USE_HTTP_PROXY).toBool();
00168 }
00169 
00170 /** Sets to <b>useHttpProxy</b> whether Tor should make all its directory
00171  * requests through the proxy specified to setHttpProxy().
00172  * \sa setHttpProxy() */
00173 void
00174 NetworkSettings::setUseHttpProxy(bool useHttpProxy)
00175 {
00176   setValue(SETTING_USE_HTTP_PROXY, useHttpProxy);
00177 }
00178 
00179 /** Returns the proxy used for making Tor's directory requests, in the form
00180  * of <i>host[:port]</i>. */
00181 QString
00182 NetworkSettings::getHttpProxy()
00183 {
00184   return value(SETTING_HTTP_PROXY).toString();
00185 }
00186 
00187 /** Sets the proxy used for making Tor's directory requests. <b>proxy</b>
00188  * should be in the form <i>host[:port]</i>. If <i>:port</i> is not
00189  * specified, then Tor will use its default of port 80. */
00190 void
00191 NetworkSettings::setHttpProxy(const QString &proxy)
00192 {
00193   setValue(SETTING_HTTP_PROXY, proxy);
00194 }
00195 
00196 /** Returns the authentication information Tor should use to authenticate to
00197  * an Http proxy. The returned value is in the form 
00198  * <i>username:password</i>. */
00199 QString
00200 NetworkSettings::getHttpProxyAuthenticator()
00201 {
00202   return value(SETTING_HTTP_PROXY_AUTH).toString();
00203 }
00204 
00205 /** Sets the authentication information required by an Http proxy.
00206  * <b>authenticator</b> should be in the form <i>username:password</i>. */
00207 void
00208 NetworkSettings::setHttpProxyAuthenticator(const QString &auth)
00209 {
00210   setValue(SETTING_HTTP_PROXY_AUTH, auth);
00211 }
00212 
00213 /** Returns true if Tor should make all its OR connections through a
00214  * proxy. */
00215 bool
00216 NetworkSettings::getUseHttpsProxy()
00217 {
00218   return localValue(SETTING_USE_HTTPS_PROXY).toBool();
00219 }
00220 
00221 /** Sets to <b>useHttpsProxy</b> whether Tor should make all its OR
00222  * connections through the proxy specified to setHttpsProxy().
00223  * \sa setHttpsProxy() */
00224 void
00225 NetworkSettings::setUseHttpsProxy(bool useHttpsProxy)
00226 {
00227   setValue(SETTING_USE_HTTPS_PROXY, useHttpsProxy);
00228 }
00229 
00230 /** Returns the proxy used for making Tor's OR connections, in the form
00231  * of <i>host[:port]</i>. */
00232 QString
00233 NetworkSettings::getHttpsProxy()
00234 {
00235   return value(SETTING_HTTPS_PROXY).toString();
00236 }
00237 
00238 /** Sets the proxy used for making Tor's OR connections. <b>proxy</b>
00239  * should be in the form <i>host[:port]</i>. If <i>:port</i> is not
00240  * specified, then Tor will use its default of port 443. */
00241 void
00242 NetworkSettings::setHttpsProxy(const QString &proxy)
00243 {
00244   setValue(SETTING_HTTPS_PROXY, proxy);
00245 }
00246 
00247 /** Returns the authentication information Tor should use to authenticate to
00248  * an Https proxy. The returned value is in the form 
00249  * <i>username:password</i>. */
00250 QString
00251 NetworkSettings::getHttpsProxyAuthenticator()
00252 {
00253   return value(SETTING_HTTPS_PROXY_AUTH).toString();
00254 }
00255 
00256 /** Sets the authentication information required by an Https proxy.
00257  * <b>authenticator</b> should be in the form <i>username:password</i>. */
00258 void
00259 NetworkSettings::setHttpsProxyAuthenticator(const QString &auth)
00260 {
00261   setValue(SETTING_HTTPS_PROXY_AUTH, auth);
00262 }
00263 
00264 /** Returns true if Tor should try to use bridge nodes to access the Tor
00265  * network. */
00266 bool
00267 NetworkSettings::getUseBridges()
00268 {
00269   return value(SETTING_USE_BRIDGES).toBool();
00270 }
00271 
00272 /** Sets to <b>useBridges</b> whether Tor should try to use bridge nodes
00273  * to access the Tor network. */
00274 void
00275 NetworkSettings::setUseBridges(bool useBridges)
00276 {
00277   setValue(SETTING_USE_BRIDGES, useBridges);
00278 }
00279 
00280 /** Returns a list of bridge nodes Tor should use. */
00281 QStringList
00282 NetworkSettings::getBridgeList()
00283 {
00284   return value(SETTING_BRIDGE_LIST).toStringList();
00285 }
00286 
00287 /** Sets to <b>bridgeList</b> the list of bridge nodes Tor should use. */
00288 void
00289 NetworkSettings::setBridgeList(const QStringList &bridgeList)
00290 {
00291   setValue(SETTING_BRIDGE_LIST, bridgeList);
00292 }
00293 
00294 /** Returns true if Tor is configured to try to tunnel its directory
00295  * connections through a one-hop circuit. */
00296 bool
00297 NetworkSettings::getTunnelDirConns()
00298 {
00299   return value(SETTING_TUNNEL_DIR_CONNS).toBool();
00300 }
00301 

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1