Commit 3ae5eb99 authored by Tomas Touceda's avatar Tomas Touceda
Browse files

Remember ports used when using automatic port selection

Tries to use the remembered ports if restarting in auto configuration and
it didn't failed last time.
parent 9ba8c114
Loading
Loading
Loading
Loading
+48 −2
Original line number Diff line number Diff line
@@ -105,6 +105,8 @@ void qt_mac_set_dock_menu(QMenu *menu);
MainWindow::MainWindow()
: VidaliaWindow("MainWindow")
{
  _startedWithPrevious = false;

  /* Create a new TorControl object, used to communicate with Tor */
  _torControl = Vidalia::torControl(); 

@@ -592,8 +594,20 @@ MainWindow::start()
  
  if(settings.getControlMethod() == ControlMethod::Port) {
    if(settings.autoControlPort()) {
      args << "ControlPort" << "auto";
      args << "SocksPort" << "auto";
      QString control_str = "auto";
      QString socks_str = "auto";

      if(!_previousControlPort.isEmpty()) {
        control_str = _previousControlPort;
        _startedWithPrevious = true;
      }
      if(!_previousSocksPort.isEmpty()) {
        socks_str = _previousSocksPort;
        _startedWithPrevious = true;
      }

      args << "ControlPort" << control_str;
      args << "SocksPort" << socks_str;
      args << "ControlPortWriteToFile" << QString("%1/port.conf").arg(expDataDirectory);
    } else {
      /* Add the intended control port value */
@@ -807,6 +821,15 @@ MainWindow::startFailed(QString errmsg)
   * will make users sad. */
  Q_UNUSED(errmsg);

  if(_startedWithPrevious) {
    _startedWithPrevious = false;
    _previousControlPort = "";
    _previousSocksPort = "";
    vWarn("Retrying with new ports");
    start();
    return;
  }
 
  updateTorStatus(Stopped);

  /* Display an error message and see if the user wants some help */
@@ -833,6 +856,29 @@ void
MainWindow::connected()
{
  authenticate();

  TorSettings settings;
  if(settings.autoControlPort()) {
    // We want to remember the ports if it's on auto
    QString control_str = "", socks_str = "";
    if(_torControl->getInfo("net/listeners/control", control_str)) {
      QStringList control_parts = control_str.split(":");
      if(control_parts.size() > 1)
        control_str = control_parts[1];
    }
    if(_torControl->getInfo("net/listeners/socks", socks_str)) {
      QStringList socks_parts = socks_str.split(":");
      if(socks_parts.size() > 1)
        socks_str = socks_parts[1];
    }
    
    _previousControlPort = control_str;
    _previousSocksPort = socks_str;
  } else {
    // Otherwise we want to clear the remembered ports
    _previousControlPort = "";
    _previousSocksPort = "";
  }
}

/** Called when the connection to the control socket fails. The reason will be
+4 −0
Original line number Diff line number Diff line
@@ -302,6 +302,10 @@ private:
  PluginEngine *_engine;
  QStringList _tabMap; /**< Map to handle opened tabs */
  QStringList _detachedTabMap; /**< Map to handle detached tabs */
  
  bool _startedWithPrevious; /**< True if Vidalia tried to start Tor with the previous ports */
  QString _previousControlPort; /**< Holds the previous controlport used */
  QString _previousSocksPort; /**< Holds the previous socksport used */
};

#endif