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 2403 2008-03-13 04:34:19Z 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   conf.insert(SETTING_HTTP_PROXY,
00066     (getUseHttpProxy() ? localValue(SETTING_HTTP_PROXY).toString() : ""));
00067   conf.insert(SETTING_HTTP_PROXY_AUTH,
00068               localValue(SETTING_HTTP_PROXY_AUTH).toString());
00069   
00070   conf.insert(SETTING_HTTPS_PROXY,
00071     (getUseHttpsProxy() ? localValue(SETTING_HTTPS_PROXY).toString() : ""));
00072   conf.insert(SETTING_HTTPS_PROXY_AUTH,
00073               localValue(SETTING_HTTPS_PROXY_AUTH).toString());
00074   
00075   if (getUseBridges()) {
00076     /* We want to always enable TunnelDirConns and friends when using
00077      * bridge relays. */
00078     conf.insert(SETTING_TUNNEL_DIR_CONNS, "1");
00079     conf.insert(SETTING_PREFER_TUNNELED_DIR_CONNS, "1");
00080   } else if (torVersion <= 0x020021) {
00081     /* TunnelDirConns is enabled by default on Tor >= 0.2.0.22-rc, so don't
00082      * disable it if our Tor is recent enough. */
00083     conf.insert(SETTING_TUNNEL_DIR_CONNS, "0");
00084     conf.insert(SETTING_PREFER_TUNNELED_DIR_CONNS, "0");
00085   }
00086   
00087   if (torVersion >= 0x020003) {
00088     /* Do the bridge stuff only on Tor >= 0.2.0.3-alpha */
00089     QStringList bridges = localValue(SETTING_BRIDGE_LIST).toStringList();
00090     if (getUseBridges() && !bridges.isEmpty()) {
00091       conf.insert(SETTING_USE_BRIDGES, "1");
00092       conf.insert(SETTING_UPDATE_BRIDGES, "1");
00093       foreach (QString bridge, bridges) {
00094         conf.insert(SETTING_BRIDGE_LIST, bridge);
00095       }
00096     } else {
00097       conf.insert(SETTING_USE_BRIDGES, "0");
00098       conf.insert(SETTING_BRIDGE_LIST, "");
00099       conf.insert(SETTING_UPDATE_BRIDGES, "0");
00100     }
00101   }
00102   return torControl()->setConf(conf, errmsg);
00103 }
00104 
00105 /** Returns true if we need to set ReachableAddresses because we're behind a
00106  * restrictive firewall that limits the ports Tor can connect to. */
00107 bool
00108 NetworkSettings::getFascistFirewall()
00109 {
00110   return localValue(SETTING_FASCIST_FIREWALL).toBool();
00111 }
00112 
00113 /** Sets to <b>fascistFirewall</b> whether Tor should only create outgoing
00114  * connections to the list of ports specified in setReachablePorts().
00115  * \sa setReachablePorts() */
00116 void
00117 NetworkSettings::setFascistFirewall(bool fascistFirewall)
00118 {
00119   setValue(SETTING_FASCIST_FIREWALL, fascistFirewall);
00120 }
00121 
00122 /** Returns a list of ports to be specified in ReachableAddresses. */
00123 QList<quint16>
00124 NetworkSettings::getReachablePorts()
00125 {
00126   QList<quint16> reachablePorts;
00127   QStringList lineList;
00128   bool ok;
00129 
00130   lineList = value(SETTING_REACHABLE_ADDRESSES).toStringList();
00131   foreach (QString line, lineList) {
00132     foreach (QString address, line.split(",", QString::SkipEmptyParts)) {
00133       QStringList parts = address.split(":");
00134       if (parts.size() >= 2) {
00135         quint16 port = parts.at(1).toUInt(&ok);
00136         if (ok)
00137           reachablePorts << port;
00138       }
00139     }
00140   }
00141   return reachablePorts;
00142 }
00143 
00144 /** Sets the list of ports that will be specified in ReachableAddresses to
00145  * <b>reachablePorts</b>. */
00146 void
00147 NetworkSettings::setReachablePorts(const QList<quint16> &reachablePorts)
00148 {
00149   if (!reachablePorts.isEmpty()) {
00150     QStringList portList;
00151     foreach (quint16 port, reachablePorts) {
00152       portList << "*:" + QString::number(port);
00153     }
00154     setValue(SETTING_REACHABLE_ADDRESSES, portList);
00155   }
00156 }
00157 
00158 /** Returns true if Tor should make all its directory requests through a
00159  * proxy. */
00160 bool
00161 NetworkSettings::getUseHttpProxy()
00162 {
00163   return localValue(SETTING_USE_HTTP_PROXY).toBool();
00164 }
00165 
00166 /** Sets to <b>useHttpProxy</b> whether Tor should make all its directory
00167  * requests through the proxy specified to setHttpProxy().
00168  * \sa setHttpProxy() */
00169 void
00170 NetworkSettings::setUseHttpProxy(bool useHttpProxy)
00171 {
00172   setValue(SETTING_USE_HTTP_PROXY, useHttpProxy);
00173 }
00174 
00175 /** Returns the proxy used for making Tor's directory requests, in the form
00176  * of <i>host[:port]</i>. */
00177 QString
00178 NetworkSettings::getHttpProxy()
00179 {
00180   return value(SETTING_HTTP_PROXY).toString();
00181 }
00182 
00183 /** Sets the proxy used for making Tor's directory requests. <b>proxy</b>
00184  * should be in the form <i>host[:port]</i>. If <i>:port</i> is not
00185  * specified, then Tor will use its default of port 80. */
00186 void
00187 NetworkSettings::setHttpProxy(const QString &proxy)
00188 {
00189   setValue(SETTING_HTTP_PROXY, proxy);
00190 }
00191 
00192 /** Returns the authentication information Tor should use to authenticate to
00193  * an Http proxy. The returned value is in the form 
00194  * <i>username:password</i>. */
00195 QString
00196 NetworkSettings::getHttpProxyAuthenticator()
00197 {
00198   return value(SETTING_HTTP_PROXY_AUTH).toString();
00199 }
00200 
00201 /** Sets the authentication information required by an Http proxy.
00202  * <b>authenticator</b> should be in the form <i>username:password</i>. */
00203 void
00204 NetworkSettings::setHttpProxyAuthenticator(const QString &auth)
00205 {
00206   setValue(SETTING_HTTP_PROXY_AUTH, auth);
00207 }
00208 
00209 /** Returns true if Tor should make all its OR connections through a
00210  * proxy. */
00211 bool
00212 NetworkSettings::getUseHttpsProxy()
00213 {
00214   return localValue(SETTING_USE_HTTPS_PROXY).toBool();
00215 }
00216 
00217 /** Sets to <b>useHttpsProxy</b> whether Tor should make all its OR
00218  * connections through the proxy specified to setHttpsProxy().
00219  * \sa setHttpsProxy() */
00220 void
00221 NetworkSettings::setUseHttpsProxy(bool useHttpsProxy)
00222 {
00223   setValue(SETTING_USE_HTTPS_PROXY, useHttpsProxy);
00224 }
00225 
00226 /** Returns the proxy used for making Tor's OR connections, in the form
00227  * of <i>host[:port]</i>. */
00228 QString
00229 NetworkSettings::getHttpsProxy()
00230 {
00231   return value(SETTING_HTTPS_PROXY).toString();
00232 }
00233 
00234 /** Sets the proxy used for making Tor's OR connections. <b>proxy</b>
00235  * should be in the form <i>host[:port]</i>. If <i>:port</i> is not
00236  * specified, then Tor will use its default of port 443. */
00237 void
00238 NetworkSettings::setHttpsProxy(const QString &proxy)
00239 {
00240   setValue(SETTING_HTTPS_PROXY, proxy);
00241 }
00242 
00243 /** Returns the authentication information Tor should use to authenticate to
00244  * an Https proxy. The returned value is in the form 
00245  * <i>username:password</i>. */
00246 QString
00247 NetworkSettings::getHttpsProxyAuthenticator()
00248 {
00249   return value(SETTING_HTTPS_PROXY_AUTH).toString();
00250 }
00251 
00252 /** Sets the authentication information required by an Https proxy.
00253  * <b>authenticator</b> should be in the form <i>username:password</i>. */
00254 void
00255 NetworkSettings::setHttpsProxyAuthenticator(const QString &auth)
00256 {
00257   setValue(SETTING_HTTPS_PROXY_AUTH, auth);
00258 }
00259 
00260 /** Returns true if Tor should try to use bridge nodes to access the Tor
00261  * network. */
00262 bool
00263 NetworkSettings::getUseBridges()
00264 {
00265   return value(SETTING_USE_BRIDGES).toBool();
00266 }
00267 
00268 /** Sets to <b>useBridges</b> whether Tor should try to use bridge nodes
00269  * to access the Tor network. */
00270 void
00271 NetworkSettings::setUseBridges(bool useBridges)
00272 {
00273   setValue(SETTING_USE_BRIDGES, useBridges);
00274 }
00275 
00276 /** Returns a list of bridge nodes Tor should use. */
00277 QStringList
00278 NetworkSettings::getBridgeList()
00279 {
00280   return value(SETTING_BRIDGE_LIST).toStringList();
00281 }
00282 
00283 /** Sets to <b>bridgeList</b> the list of bridge nodes Tor should use. */
00284 void
00285 NetworkSettings::setBridgeList(const QStringList &bridgeList)
00286 {
00287   setValue(SETTING_BRIDGE_LIST, bridgeList);
00288 }
00289 
00290 /** Returns true if Tor is configured to try to tunnel its directory
00291  * connections through a one-hop circuit. */
00292 bool
00293 NetworkSettings::getTunnelDirConns()
00294 {
00295   return value(SETTING_TUNNEL_DIR_CONNS).toBool();
00296 }
00297 

Generated on Sat Aug 16 17:38:35 2008 for Vidalia by  doxygen 1.5.6