Commit d583bda6 authored by Matt Edman's avatar Matt Edman
Browse files

Implement ticket #15 - add an option for checking to see if the server's IP

address has changed within the last hour. If it has, let Tor know about it.


svn:r710
parent b21092c3
Loading
Loading
Loading
Loading
+55 −0
Changes for src/gui/config/serverpage.cpp: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@
#define COL_ADDRESS   1
#define COL_PORT      2

/** Delay between updating our server IP address (in ms). */
#define AUTO_UPDATE_ADDR_INTERVAL  1000*60*60

/* Help topics */
#define EXIT_HELP     "server.exitpolicy"

@@ -56,6 +59,10 @@ ServerPage::ServerPage(QWidget *parent)
  /* Create ServerSettings object */
  _settings = new ServerSettings(_torControl);

  /* Create a timer that we can use to remind ourselves to check if our IP
   * changed since last time we looked. */
  _autoUpdateTimer = new QTimer(this);
    
  /* Bind events to actions */
  connect(ui.btnAddPolicy, SIGNAL(clicked()), this, SLOT(addPolicy()));
  connect(ui.btnRemovePolicy, SIGNAL(clicked()), this, SLOT(removePolicy()));
@@ -79,6 +86,19 @@ ServerPage::~ServerPage()
  delete _settings;
}

/** Enables or disables the automatic IP address update timer. */
void
ServerPage::setAutoUpdateTimer(bool enabled)
{
  if (enabled) {
    connect(_autoUpdateTimer, SIGNAL(timeout()), 
            this, SLOT(updateServerIP()));
    _autoUpdateTimer->start(AUTO_UPDATE_ADDR_INTERVAL);
  } else {
    _autoUpdateTimer->stop();
  }
}

/** Saves changes made to settings on the Server settings page. */
bool
ServerPage::save(QString &errmsg)
@@ -92,11 +112,13 @@ ServerPage::save(QString &errmsg)
  _settings->setServerEnabled(ui.chkEnableServer->isChecked());
  _settings->setDirectoryMirror(ui.chkMirrorDirectory->isChecked());
  _settings->setMiddleman(ui.chkMiddleMan->isChecked());
  _settings->setAutoUpdateAddress(ui.chkAutoUpdate->isChecked()); 
  _settings->setNickname(ui.lineServerNickname->text());
  _settings->setORPort(ui.lineServerPort->text().toUInt());
  _settings->setDirPort(ui.lineDirPort->text().toUInt());
  _settings->setAddress(ui.lineServerAddress->text());
  _settings->setContactInfo(ui.lineServerContact->text());
  setAutoUpdateTimer(ui.chkAutoUpdate->isChecked());

  /* Save exit polices */
  ExitPolicy exitPolicy;
@@ -119,6 +141,8 @@ ServerPage::load()
  ui.chkEnableServer->setChecked(_settings->isServerEnabled());
  ui.chkMirrorDirectory->setChecked(_settings->isDirectoryMirror());
  ui.chkMiddleMan->setChecked(_settings->isMiddleman());
  ui.chkAutoUpdate->setChecked(_settings->getAutoUpdateAddress());
  setAutoUpdateTimer(_settings->getAutoUpdateAddress());

  ui.lineServerNickname->setText(_settings->getNickname());
  ui.lineServerPort->setText(QString::number(_settings->getORPort()));
@@ -301,3 +325,34 @@ ServerPage::getServerAddress()
  }
}

/** Checks to see if this server's public IP had changed. If it has, then
 * update the UI, and Tor (if it's running). */
void
ServerPage::updateServerIP()
{
  bool changed = false;
  QString ip;
  QHostAddress addr = net_local_address();
  
  if (net_is_private_address(addr)) {
    /* Try to get our public IP and see if it changed recently. */
    if (net_get_public_ip(ip) && ip != _settings->getAddress()) {
      changed = true;
    }
  } else if (addr.toString() != _settings->getAddress()) {
    ip = addr.toString();
    changed = true;
  }
  
  if (changed) {
    /* It changed so update our settings and the UI. */
    _settings->setAddress(ip);
    ui.lineServerAddress->setText(ip);

    /* If Tor is running, let it know about the change */
    if (_torControl->isConnected()) {
      _settings->apply();
    }
  }
}
+7 −0
Changes for src/gui/config/serverpage.h: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#define _SERVERPAGE_H

#include <QMessageBox>
#include <QTimer>
#include <control/torcontrol.h>
#include <config/serversettings.h>
#include <config/exitpolicy.h>
@@ -63,6 +64,8 @@ private slots:
  void lowerPriority();
  /** Called when the user clicks the exit policy help button */
  void exitHelp();
  /** Called when the user's public IP address needs to be updated. */
  void updateServerIP();
  
private:
  /** Attempts to find the server's public IP address */
@@ -73,12 +76,16 @@ private:
  void addPolicyItem(Policy policy);
  /** Saves the policy specified in item to the exitPolicy */
  void savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy);
  /** Enables or disables the automatic update timer. */
  void setAutoUpdateTimer(bool enabled);

  /** A TorControl object used to talk to Tor */
  TorControl* _torControl;
  /** A ServerSettings object used to get and set information about how a
   * local Tor server is configured. */
  ServerSettings*  _settings;
  /** A timer that tells us when it's time to check if our server IP changed.*/
  QTimer* _autoUpdateTimer;

  /** Qt Designer generated object */
  Ui::ServerPage ui;