commit 64a20eadb3d10980fe86ed154c065f3e2132c61b
Author: Tomas Touceda <chiiph(a)torproject.org>
Date: Wed Jul 27 11:01:02 2011 -0300
Add auto configuration of the control host:port to stable
This is just a hand-rebase of the bug3077_auto branch.
---
src/vidalia/MainWindow.cpp | 54 +++++++++++++++++++++++++++--------
src/vidalia/config/AdvancedPage.cpp | 12 ++++++++
src/vidalia/config/AdvancedPage.h | 3 ++
src/vidalia/config/AdvancedPage.ui | 17 ++++++++---
src/vidalia/config/TorSettings.cpp | 14 +++++++++
src/vidalia/config/TorSettings.h | 7 ++++
6 files changed, 90 insertions(+), 17 deletions(-)
diff --git a/src/vidalia/MainWindow.cpp b/src/vidalia/MainWindow.cpp
index 052d5ac..7ef08c8 100644
--- a/src/vidalia/MainWindow.cpp
+++ b/src/vidalia/MainWindow.cpp
@@ -1002,8 +1002,8 @@ MainWindow::start()
/* Check if Tor is already running separately */
if(settings.getControlMethod() == ControlMethod::Port) {
- if (net_test_connect(settings.getControlAddress(),
- settings.getControlPort())) {
+ if(!settings.autoControlPort() && net_test_connect(settings.getControlAddress(),
+ settings.getControlPort())) {
started();
return;
}
@@ -1036,14 +1036,21 @@ MainWindow::start()
/* Specify Tor's data directory, if different from the default */
QString dataDirectory = settings.getDataDirectory();
+ QString expDataDirectory = expand_filename(dataDirectory);
if (!dataDirectory.isEmpty())
- args << "DataDirectory" << expand_filename(dataDirectory);
+ args << "DataDirectory" << expDataDirectory;
if(settings.getControlMethod() == ControlMethod::Port) {
- /* Add the intended control port value */
- quint16 controlPort = settings.getControlPort();
- if (controlPort)
- args << "ControlPort" << QString::number(controlPort);
+ if(settings.autoControlPort()) {
+ args << "ControlPort" << "auto";
+ args << "SocksPort" << "auto";
+ args << "ControlPortWriteToFile" << QString("%1/port.conf").arg(expDataDirectory);
+ } else {
+ /* Add the intended control port value */
+ quint16 controlPort = settings.getControlPort();
+ if (controlPort)
+ args << "ControlPort" << QString::number(controlPort);
+ }
} else {
QString path = settings.getSocketPath();
args << "ControlSocket" << path;
@@ -1137,11 +1144,34 @@ MainWindow::started()
/* Remember whether we started Tor or not */
_isVidaliaRunningTor = _torControl->isVidaliaRunningTor();
/* Try to connect to Tor's control port */
- if(settings.getControlMethod() == ControlMethod::Port)
- _torControl->connect(settings.getControlAddress(),
- settings.getControlPort());
- else
- _torControl->connect(settings.getSocketPath());
+ if(settings.autoControlPort()) {
+ QString dataDirectory = settings.getDataDirectory();
+ QFile file(QString("%1/port.conf").arg(expand_filename(dataDirectory)));
+ if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
+ return;
+
+ QTextStream in(&file);
+ if(!in.atEnd()) {
+ QString line = in.readLine();
+ QStringList parts = line.split("=");
+ if(parts.size() != 2) return;
+ if(parts[0].trimmed() != "PORT") return;
+
+ QStringList addrPort = parts[1].split(":");
+ if(addrPort.size() != 2) return;
+
+ QHostAddress addr(addrPort.at(0));
+ quint16 port = addrPort.at(1).toInt();
+ _torControl->connect(addr, port);
+ }
+ } else {
+ /* Try to connect to Tor's control port */
+ if(settings.getControlMethod() == ControlMethod::Port)
+ _torControl->connect(settings.getControlAddress(),
+ settings.getControlPort());
+ else
+ _torControl->connect(settings.getSocketPath());
+ }
setStartupProgress(STARTUP_PROGRESS_CONNECTING, tr("Connecting to Tor"));
}
diff --git a/src/vidalia/config/AdvancedPage.cpp b/src/vidalia/config/AdvancedPage.cpp
index a879ecc..4e7c3f6 100644
--- a/src/vidalia/config/AdvancedPage.cpp
+++ b/src/vidalia/config/AdvancedPage.cpp
@@ -64,6 +64,7 @@ AdvancedPage::AdvancedPage(QWidget *parent)
this, SLOT(displayTorrcDialog()));
connect(ui.rdoControlPort, SIGNAL(toggled(bool)), this, SLOT(toggleControl(bool)));
connect(ui.btnBrowseSocketPath, SIGNAL(clicked()), this, SLOT(browseSocketPath()));
+ connect(ui.chkAuto, SIGNAL(toggled(bool)), this, SLOT(toggleAuto(bool)));
/* Hide platform specific features */
#if defined(Q_WS_WIN)
@@ -187,6 +188,7 @@ AdvancedPage::save(QString &errmsg)
_settings->setControlAddress(controlAddress);
_settings->setControlPort(ui.lineControlPort->text().toUShort());
_settings->setSocketPath(ui.lineSocketPath->text());
+ _settings->setAutoControlPort(ui.chkAuto->isChecked());
_settings->setAuthenticationMethod(authMethod);
_settings->setUseRandomPassword(ui.chkRandomPassword->isChecked());
@@ -212,6 +214,7 @@ AdvancedPage::load()
ui.lineControlPort->setText(QString::number(_settings->getControlPort()));
ui.lineTorConfig->setText(_settings->getTorrc());
ui.lineTorDataDirectory->setText(_settings->getDataDirectory());
+ ui.chkAuto->setChecked(_settings->autoControlPort());
ui.cmbAuthMethod->setCurrentIndex(
authMethodToIndex(_settings->getAuthenticationMethod()));
@@ -402,3 +405,12 @@ AdvancedPage::toggleControl(bool)
#endif
}
}
+
+void
+AdvancedPage::toggleAuto(bool)
+{
+ ui.lblAddress->setEnabled(!ui.chkAuto->isChecked());
+ ui.lineControlAddress->setEnabled(!ui.chkAuto->isChecked());
+ ui.label->setEnabled(!ui.chkAuto->isChecked());
+ ui.lineControlPort->setEnabled(!ui.chkAuto->isChecked());
+}
diff --git a/src/vidalia/config/AdvancedPage.h b/src/vidalia/config/AdvancedPage.h
index 4ba26cc..4510310 100644
--- a/src/vidalia/config/AdvancedPage.h
+++ b/src/vidalia/config/AdvancedPage.h
@@ -75,6 +75,9 @@ private slots:
* a more advanced way */
void displayTorrcDialog();
+ /** Called when the user checks "Configure ControlPort automatically" */
+ void toggleAuto(bool);
+
private:
/** Returns the authentication method for the given <b>index</b>. */
TorSettings::AuthenticationMethod indexToAuthMethod(int index);
diff --git a/src/vidalia/config/AdvancedPage.ui b/src/vidalia/config/AdvancedPage.ui
index add13c0..9473a81 100644
--- a/src/vidalia/config/AdvancedPage.ui
+++ b/src/vidalia/config/AdvancedPage.ui
@@ -31,7 +31,7 @@
<property name="verticalSpacing">
<number>6</number>
</property>
- <item row="4" column="0">
+ <item row="5" column="0">
<widget class="QLabel" name="lblAuth">
<property name="text">
<string>Authentication:</string>
@@ -51,7 +51,7 @@
</property>
</widget>
</item>
- <item row="4" column="2" colspan="3">
+ <item row="5" column="2" colspan="3">
<layout class="QHBoxLayout">
<property name="spacing">
<number>2</number>
@@ -185,7 +185,7 @@
</property>
</widget>
</item>
- <item row="3" column="0">
+ <item row="4" column="0">
<widget class="QLabel" name="lblPath">
<property name="enabled">
<bool>false</bool>
@@ -198,7 +198,7 @@
</property>
</widget>
</item>
- <item row="3" column="1" colspan="2">
+ <item row="4" column="1" colspan="2">
<widget class="QLineEdit" name="lineSocketPath">
<property name="enabled">
<bool>false</bool>
@@ -212,7 +212,7 @@
</property>
</widget>
</item>
- <item row="3" column="3">
+ <item row="4" column="3">
<widget class="QPushButton" name="btnBrowseSocketPath">
<property name="enabled">
<bool>false</bool>
@@ -222,6 +222,13 @@
</property>
</widget>
</item>
+ <item row="3" column="2">
+ <widget class="QCheckBox" name="chkAuto">
+ <property name="text">
+ <string>Configure ControlPort automatically</string>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
</layout>
diff --git a/src/vidalia/config/TorSettings.cpp b/src/vidalia/config/TorSettings.cpp
index b05b428..42e036f 100644
--- a/src/vidalia/config/TorSettings.cpp
+++ b/src/vidalia/config/TorSettings.cpp
@@ -44,6 +44,7 @@
#define SETTING_REJECT_PLAINTEXT_PORTS "RejectPlaintextPorts"
#define SETTING_BOOTSTRAP "Bootstrap"
#define SETTING_BOOTSTRAP_FROM "BootstrapFrom"
+#define SETTING_AUTOCONTROL "AutoControl"
/** Default to using hashed password authentication */
#define DEFAULT_AUTH_METHOD PasswordAuth
@@ -92,6 +93,7 @@ TorSettings::TorSettings(TorControl *torControl)
setDefault(SETTING_REJECT_PLAINTEXT_PORTS, QList<QVariant>());
setDefault(SETTING_BOOTSTRAP, false);
setDefault(SETTING_BOOTSTRAP_FROM, "");
+ setDefault(SETTING_AUTOCONTROL, true);
}
/** Applies any changes to Tor's control port or authentication settings. */
@@ -450,3 +452,15 @@ TorSettings::bootstrapFrom() const
{
return QDir::convertSeparators(value(SETTING_BOOTSTRAP_FROM).toString());
}
+
+bool
+TorSettings::autoControlPort() const
+{
+ return value(SETTING_AUTOCONTROL).toBool();
+}
+
+void
+TorSettings::setAutoControlPort(const bool autoControl)
+{
+ setValue(SETTING_AUTOCONTROL, autoControl);
+}
diff --git a/src/vidalia/config/TorSettings.h b/src/vidalia/config/TorSettings.h
index a4f8ec9..5038c3d 100644
--- a/src/vidalia/config/TorSettings.h
+++ b/src/vidalia/config/TorSettings.h
@@ -72,6 +72,13 @@ public:
/** Get the current control method */
ControlMethod::Method getControlMethod() const;
+ /** Returns true if Vidalia will get the ControlPort automatically from
+ * Tor */
+ bool autoControlPort() const;
+
+ /** Sets whether Vidalia should get the ControlPort from Tor */
+ void setAutoControlPort(const bool auto);
+
/** Set the control method */
void setControlMethod(ControlMethod::Method method);