[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [vidalia/alpha] Migrate TorSettings to the new structure



commit fa3f968392247de08fbc99e3cc20107c38e25d9a
Author: Tomás Touceda <chiiph@xxxxxxxxxxxxxx>
Date:   Fri Feb 3 22:16:26 2012 -0300

    Migrate TorSettings to the new structure
---
 src/vidalia/config/TorSettings.cpp |  131 ++++++++++++++++++++++--------------
 src/vidalia/config/TorSettings.h   |    2 +-
 2 files changed, 81 insertions(+), 52 deletions(-)

diff --git a/src/vidalia/config/TorSettings.cpp b/src/vidalia/config/TorSettings.cpp
index 9fa5369..85164d0 100644
--- a/src/vidalia/config/TorSettings.cpp
+++ b/src/vidalia/config/TorSettings.cpp
@@ -29,8 +29,8 @@
 /* Tor Settings */
 #define SETTING_TOR_EXECUTABLE      "TorExecutable"
 #define SETTING_TORRC               "Torrc"
-#define SETTING_CONTROL_ADDR        "ControlAddr"
 #define SETTING_DEFAULTS_TORRC      "DefaultsTorrc"
+#define SETTING_CONTROL_ADDR        "ControlListenAddress"
 #define SETTING_CONTROL_PORT        "ControlPort"
 #define SETTING_SOCKET_PATH         "ControlSocket"
 #define SETTING_CONTROL_METHOD      "ControlMethod"
@@ -61,6 +61,7 @@
 #define TOR_ARG_HASHED_PASSWORD "HashedControlPassword"
 #define TOR_ARG_COOKIE_AUTH     "CookieAuthentication"
 #define TOR_ARG_SOCKSPORT       "SocksPort"
+#define TOR_ARG_CONTROLFILE     "ControlPortWriteToFile"
 
 /** Generate random control passwords of 16 characters */
 #define PASSWORD_LEN    16
@@ -91,35 +92,51 @@ TorSettings::TorSettings(TorControl *torControl)
   setDefault(SETTING_DATA_DIRECTORY, "");
   setDefault(SETTING_CONTROL_PASSWORD, "");
   setDefault(SETTING_USE_RANDOM_PASSWORD, true);
-  setDefault(SETTING_WARN_PLAINTEXT_PORTS, QList<QVariant>() << 23 << 109 
-                                                             << 110 << 143);
-  setDefault(SETTING_REJECT_PLAINTEXT_PORTS, QList<QVariant>());
   setDefault(SETTING_BOOTSTRAP, false);
   setDefault(SETTING_BOOTSTRAP_FROM, "");
-  setDefault(SETTING_AUTOCONTROL, true);
+  setDefault(SETTING_AUTOCONTROL, false);
 }
 
 /** Applies any changes to Tor's control port or authentication settings. */
 bool
 TorSettings::apply(QString *errmsg)
 {
-  QHash<QString, QString> conf;
-  QString hashedPassword;
-
-  conf.insert(SETTING_CONTROL_PORT,
-              localValue(SETTING_CONTROL_PORT).toString());
-
-  if(localValue(SETTING_AUTOCONTROL).toBool())
-    conf.insert(TOR_ARG_SOCKSPORT, "auto");
-  else
-    conf.insert(TOR_ARG_SOCKSPORT, "9050");
+  Torrc *torrc = Vidalia::torrc();
   
+  torrc->setValue(SETTING_CONTROL_PORT, 
+                  QString::number(volatileValue(SETTING_CONTROL_PORT).toInt()));
+
+  torrc->clear(QStringList()
+               << TOR_ARG_SOCKSPORT
+               << SETTING_CONTROL_PORT);
+
+  torrc->setValue(SETTING_DATA_DIRECTORY, volatileValue(SETTING_DATA_DIRECTORY).toString());
+
+  if(volatileValue(SETTING_AUTOCONTROL).toBool()) {
+    torrc->setValue(SETTING_CONTROL_PORT, "auto");
+    torrc->setValue(TOR_ARG_SOCKSPORT, "auto");
+    torrc->setValue(TOR_ARG_CONTROLFILE, QString("%1/port.conf").arg(getDataDirectory()));
+  } else {
+    QString socks = "9050", control = "9051";
+    {
+      with_torrc_value(TOR_ARG_SOCKSPORT)
+        socks = ret.at(0);
+    }
+    {
+      with_torrc_value(TOR_ARG_SOCKSPORT)
+        control = ret.at(0);
+    }
+    torrc->setValue(TOR_ARG_SOCKSPORT, socks);
+    torrc->setValue(SETTING_CONTROL_PORT, control);
+  }
+
+  QString hashedPassword;
   AuthenticationMethod authMethod = 
     toAuthenticationMethod(localValue(SETTING_AUTH_METHOD).toString());
   switch (authMethod) {
     case CookieAuth:
-      conf.insert(TOR_ARG_COOKIE_AUTH,    "1");
-      conf.insert(TOR_ARG_HASHED_PASSWORD, "");
+      torrc->setValue(TOR_ARG_COOKIE_AUTH,    "1");
+      torrc->setValue(TOR_ARG_HASHED_PASSWORD, "");
       break;
     case PasswordAuth:
       hashedPassword = useRandomPassword() 
@@ -130,34 +147,33 @@ TorSettings::apply(QString *errmsg)
           *errmsg =  tr("Failed to hash the control password.");
         return false;
       }
-      conf.insert(TOR_ARG_COOKIE_AUTH,    "0");
-      conf.insert(TOR_ARG_HASHED_PASSWORD, hashedPassword);
+      torrc->setValue(TOR_ARG_COOKIE_AUTH,    "0");
+      if(not useRandomPassword())
+        torrc->setValue(TOR_ARG_HASHED_PASSWORD, hashedPassword);
       break;
     default:
-      conf.insert(TOR_ARG_COOKIE_AUTH,    "0");
-      conf.insert(TOR_ARG_HASHED_PASSWORD, "");
+      torrc->setValue(TOR_ARG_COOKIE_AUTH,    "0");
+      torrc->setValue(TOR_ARG_HASHED_PASSWORD, "");
   }
 
-  conf.insert(SETTING_WARN_PLAINTEXT_PORTS,
-              localValue(SETTING_WARN_PLAINTEXT_PORTS).toStringList().join(","));
-  conf.insert(SETTING_REJECT_PLAINTEXT_PORTS,
-              localValue(SETTING_REJECT_PLAINTEXT_PORTS).toStringList().join(","));
-
-  return torControl()->setConf(conf, errmsg);
+  return torrc->apply(Vidalia::torControl(), errmsg);
 }
 
 /** Gets the location of Tor's data directory. */
 QString
-TorSettings::getDataDirectory() const
+TorSettings::getDataDirectory()
 {
-  return QDir::convertSeparators(value(SETTING_DATA_DIRECTORY).toString());
+  with_torrc_value(SETTING_DATA_DIRECTORY) {
+    return ret.at(0);
+  }
+  return QDir::convertSeparators(volatileValue(SETTING_DATA_DIRECTORY).toString());
 }
 
 /** Sets the location to use as Tor's data directory. */
 void
 TorSettings::setDataDirectory(const QString &dataDirectory)
 {
-  setValue(SETTING_DATA_DIRECTORY, dataDirectory);
+  setVolatileValue(SETTING_DATA_DIRECTORY, dataDirectory);
 }
 
 /** Returns a fully-qualified path to Tor's executable, including the
@@ -222,7 +238,10 @@ TorSettings::setDefaultsTorrc(const QString &torrc)
 QHostAddress
 TorSettings::getControlAddress() const
 {
-  QString addr = localValue(SETTING_CONTROL_ADDR).toString();
+  QString addr = defaultValue(SETTING_CONTROL_ADDR).toString();
+  with_torrc_value(SETTING_CONTROL_ADDR) {
+    addr = ret.at(0);
+  } 
   return QHostAddress(addr);
 }
 
@@ -230,35 +249,42 @@ TorSettings::getControlAddress() const
 void
 TorSettings::setControlAddress(const QHostAddress &addr)
 {
-  setValue(SETTING_CONTROL_ADDR, addr.toString());
+  setVolatileValue(SETTING_CONTROL_ADDR, addr.toString());
 }
 
 /** Get the control port used to connect to Tor */
 quint16
 TorSettings::getControlPort() const
 {
-  return (quint16)value(SETTING_CONTROL_PORT).toInt();
+  quint16 port = defaultValue(SETTING_CONTROL_PORT).toInt();
+  with_torrc_value(SETTING_CONTROL_PORT) {
+    port = (quint16)ret.at(0).toUInt();
+  }
+  return port;
 }
 
 /** Set the control port used to connect to Tor */
 void
 TorSettings::setControlPort(quint16 port)
 {
-  setValue(SETTING_CONTROL_PORT, port);
+  setVolatileValue(SETTING_CONTROL_PORT, port);
 }
 
 /** Get the path for ControlSocket */
 QString 
 TorSettings::getSocketPath() const
 {
-  return value(SETTING_SOCKET_PATH).toString();
+  with_torrc_value(SETTING_SOCKET_PATH) {
+    return ret.at(0);
+  }
+  return defaultValue(SETTING_SOCKET_PATH).toString();
 }
 
 /** Set the path for ControlSocket */
 void 
 TorSettings::setSocketPath(const QString &path)
 {
-  setValue(SETTING_SOCKET_PATH, path);
+  setVolatileValue(SETTING_SOCKET_PATH, path);
 }
 
 /** Get the current control method */
@@ -343,10 +369,12 @@ QList<quint16>
 TorSettings::getWarnPlaintextPorts() const
 {
   QList<quint16> out;
-  QList<QVariant> ports;
+  QStringList ports;
 
-  ports = value(SETTING_WARN_PLAINTEXT_PORTS).toList();
-  foreach (QVariant port, ports) {
+  ports = Vidalia::torrc()->value(SETTING_WARN_PLAINTEXT_PORTS);
+  if(ports.size() > 0)
+    ports = ports.at(0).split(",");
+  foreach (QString port, ports) {
     out << port.toUInt();
   }
   return out;
@@ -357,11 +385,11 @@ TorSettings::getWarnPlaintextPorts() const
 void
 TorSettings::setWarnPlaintextPorts(const QList<quint16> &ports)
 {
-  QList<QVariant> warnList;
+  QStringList warnList;
   foreach (quint16 port, ports) {
-    warnList << QVariant(port);
+    warnList << QString::number(port);
   }
-  setValue(SETTING_WARN_PLAINTEXT_PORTS, warnList);
+  setValue(SETTING_WARN_PLAINTEXT_PORTS, warnList.join(","));
 }
 
 /** Returns the current list of ports that will cause Tor to reject the
@@ -370,10 +398,11 @@ QList<quint16>
 TorSettings::getRejectPlaintextPorts() const
 {
   QList<quint16> out;
-  QList<QVariant> ports;
-
-  ports = value(SETTING_REJECT_PLAINTEXT_PORTS).toList();
-  foreach (QVariant port, ports) {
+  QStringList ports;
+  ports = Vidalia::torrc()->value(SETTING_REJECT_PLAINTEXT_PORTS);
+  if(ports.size() > 0)
+    ports = ports.at(0).split(",");
+  foreach (QString port, ports) {
     out << port.toUInt();
   }
   return out;
@@ -384,11 +413,11 @@ TorSettings::getRejectPlaintextPorts() const
 void
 TorSettings::setRejectPlaintextPorts(const QList<quint16> &ports)
 {
-  QList<QVariant> rejectList;
+  QStringList rejectList;
   foreach (quint16 port, ports) {
-    rejectList << QVariant(port);
+    rejectList << QString::number(port);
   }
-  setValue(SETTING_REJECT_PLAINTEXT_PORTS, rejectList);
+  setValue(SETTING_REJECT_PLAINTEXT_PORTS, rejectList.join(","));
 }
 
 /** Returns the string description of the authentication method specified by
@@ -484,11 +513,11 @@ TorSettings::bootstrapFrom() const
 bool 
 TorSettings::autoControlPort() const
 {
-  return value(SETTING_AUTOCONTROL).toBool();
+  return getControlPort() == 0;
 }
 
 void 
 TorSettings::setAutoControlPort(const bool autoControl)
 {
-  setValue(SETTING_AUTOCONTROL, autoControl);
+  setVolatileValue(SETTING_AUTOCONTROL, autoControl);
 }
diff --git a/src/vidalia/config/TorSettings.h b/src/vidalia/config/TorSettings.h
index 9e6622e..2fdf1e5 100644
--- a/src/vidalia/config/TorSettings.h
+++ b/src/vidalia/config/TorSettings.h
@@ -47,7 +47,7 @@ public:
   void setExecutable(const QString &torExecutable);
  
   /** Gets the location of Tor's data directory. */
-  QString getDataDirectory() const;
+  QString getDataDirectory();
   /** Sets the location to use for Tor's data directory. */
   void setDataDirectory(const QString &dataDir);
   



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits