Changeset 549

Show
Ignore:
Timestamp:
04/01/06 07:46:27 (3 years ago)
Author:
hipplej
Message:

First crack at getting the exit policy stuff to work. I'm tired so this is probably fundamentally flawed in some way but it appears to work.

Location:
trunk/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/config/serversettings.cpp

    r449 r549  
    5151#define SETTING_SERVER_CONTACT    "Server/"SERVER_CONTACTINFO 
    5252#define SETTING_SERVER_EXITPOLICY "Server/"SERVER_EXITPOLICY 
     53#define SETTING_SERVER_OVERRIDE   "Server/OverrideDefault" 
    5354 
    5455/* Default server configuration */ 
     
    6364#define DEFAULT_SERVER_ADDRESS    net_local_address().toString()  
    6465#define DEFAULT_SERVER_EXITPOLICY (ExitPolicy().toString())  
    65  
     66#define DEFAULT_SERVER_OVERRIDE   false 
    6667 
    6768/** Constructor. 
     
    387388} 
    388389 
     390/** Sets whether we override the default exit policy */ 
     391void 
     392ServerSettings::setOverridePolicy(bool override) 
     393{ 
     394  setValue(SETTING_SERVER_OVERRIDE, override); 
     395} 
     396 
     397/** Returns whether we override the default exit policy */ 
     398bool 
     399ServerSettings::getOverridePolicy() 
     400{ 
     401  return value(SETTING_SERVER_OVERRIDE, DEFAULT_SERVER_OVERRIDE).toBool(); 
     402} 
     403 
  • trunk/src/config/serversettings.h

    r449 r549  
    9393  ExitPolicy getExitPolicy(); 
    9494 
     95  /** Sets whether we override the default exit policy */ 
     96  void setOverridePolicy(bool override); 
     97  /** Gets whether we override the default exit policy */ 
     98  bool getOverridePolicy(); 
     99 
    95100private: 
    96101  /** Sets a value indicating that the server settings have changed since 
  • trunk/src/gui/config/serverpage.cpp

    r535 r549  
    3333#include "portvalidator.h" 
    3434 
     35/* Default Exit Policy */ 
     36#define DEFAULT_POLICY    Policy(AcceptAll) 
    3537/* Columns of the Exit Policy list */ 
    3638#define COL_ACTION    0 
     
    9597  _settings->setAddress(ui.lineServerAddress->text()); 
    9698  _settings->setContactInfo(ui.lineServerContact->text()); 
     99  _settings->setOverridePolicy(ui.chkExitOverride->isChecked()); 
     100 
     101  /* Remove/Add necessary default exit rule */ 
     102  setDefaultRule(); 
     103   
     104  /* Save exit polices */ 
     105  ExitPolicy exitPolicy; 
     106  for (int i = 0; i < ui.lstExitPolicies->topLevelItemCount(); ++i) { 
     107    savePolicy(ui.lstExitPolicies->topLevelItem(i), exitPolicy); 
     108  } 
     109  _settings->setExitPolicy(exitPolicy); 
     110   
    97111  bool success = (_torControl->isConnected() ? _settings->apply(&errmsg) : true); 
    98112  if (!success) { 
     
    109123  ui.chkMirrorDirectory->setChecked(_settings->isDirectoryMirror()); 
    110124  ui.chkMiddleMan->setChecked(_settings->isMiddleman()); 
     125   
    111126  ui.lineServerNickname->setText(_settings->getNickname()); 
    112127  ui.lineServerPort->setText(QString::number(_settings->getORPort())); 
     
    114129  ui.lineServerAddress->setText(_settings->getAddress()); 
    115130  ui.lineServerContact->setText(_settings->getContactInfo()); 
    116  
     131  ui.chkExitOverride->setChecked(_settings->getOverridePolicy()); 
     132   
     133  /* Load the exit policies into the list */ 
     134  ui.lstExitPolicies->clear(); 
     135   
     136  foreach (Policy policy, _settings->getExitPolicy().policyList()) { 
     137    addPolicyItem(policy); 
     138  } 
     139   
    117140  ui.frmServer->setVisible(ui.chkEnableServer->isChecked()); 
     141} 
     142 
     143/** Adds the exit policy contained in item to the exitPolicy */ 
     144void 
     145ServerPage::savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy) 
     146{ 
     147  /* Build policy string */ 
     148  QString policyString = item->text(COL_ACTION) + " "; 
     149  policyString += item->text(COL_ADDRESS) + ":" + item->text(COL_PORT); 
     150   
     151  /* Add policy to ServerSettings */ 
     152  exitPolicy.addPolicy(Policy(policyString)); 
     153} 
     154 
     155/** Moves or appends the correct default exit rule to the policy list */ 
     156void 
     157ServerPage::setDefaultRule() 
     158{ 
     159  bool override = ui.chkExitOverride->isChecked(); 
     160  Policy::SpecialPolicy action; 
     161  QString actionString; 
     162  QList<QTreeWidgetItem *> list; 
     163  bool found = false; 
     164   
     165  if (override) { 
     166    action = Policy::RejectAll; 
     167    actionString = "reject"; 
     168  } else { 
     169    action = Policy::AcceptAll; 
     170    actionString = "accept"; 
     171  } 
     172   
     173  /* Search for the policy, if exists: move to bottom else: append it */ 
     174  /* Remove any of the opposite default exit policy */ 
     175  list = ui.lstExitPolicies->findItems("0.0.0.0", Qt::MatchExactly, COL_ADDRESS); 
     176   
     177  foreach (QTreeWidgetItem *item, list) { 
     178    if (item->text(COL_PORT) == "*") { 
     179      int index = ui.lstExitPolicies->indexOfTopLevelItem(item); 
     180       
     181      /* Found target so move to bottom of list */ 
     182      if (item->text(COL_ACTION) == actionString) { 
     183        ui.lstExitPolicies->addTopLevelItem(ui.lstExitPolicies-> 
     184                                                      takeTopLevelItem(index)); 
     185        found = true; 
     186         
     187      /* Found the opposite so remove */ 
     188      } else { 
     189        ui.lstExitPolicies->takeTopLevelItem(index); 
     190      } 
     191    } 
     192  } 
     193 
     194  /* Search failed so just append the necessary policy */ 
     195  if (!found) addPolicyItem(Policy(action)); 
    118196} 
    119197 
     
    127205  } 
    128206   
    129   QTreeWidgetItem *newPolicy = new QTreeWidgetItem(); 
    130    
    131   /* Add new policy's action */ 
    132   newPolicy->setText(COL_ACTION, ui.cmboExitAction->currentText()); 
    133   newPolicy->setTextAlignment(COL_ACTION, Qt::AlignCenter); 
    134  
    135   /* Add new policy's ip address */ 
    136   QString address = ui.lineExitAddress->text(); 
    137   if (!ui.lineExitMask->text().isEmpty()) { 
    138     address += ":" + ui.lineExitMask->text(); 
    139   }   
    140   newPolicy->setText(COL_ADDRESS, address); 
    141   newPolicy->setTextAlignment(COL_ADDRESS, Qt::AlignCenter); 
    142    
    143   /* Add new policy's port or port range */ 
    144   QString portRange = ui.lineExitFromPort->text(); 
    145   if (!portRange.isEmpty()) { 
    146     QString toPort = ui.lineExitToPort->text(); 
    147     if (!toPort.isEmpty() && portRange != "*") { 
    148       portRange += "-" + ui.lineExitToPort->text(); 
    149     } 
    150   } 
    151   newPolicy->setText(COL_PORT, portRange); 
    152   newPolicy->setTextAlignment(COL_PORT, Qt::AlignCenter); 
    153    
    154   /* Add new policy to list */ 
    155   ui.lstExitPolicies->addTopLevelItem(newPolicy); 
     207  /* Add the policy to the list */ 
     208  addPolicyItem(ui.cmboExitAction->currentText(), ui.lineExitAddress->text(), 
     209                ui.lineExitMask->text(), ui.lineExitFromPort->text(), 
     210                ui.lineExitToPort->text()); 
    156211 
    157212  /* Clear input text boxes */ 
     
    162217} 
    163218 
     219/* Adds a new QTreeWidget item to the exit policy list */ 
     220void 
     221ServerPage::addPolicyItem(Policy policy) 
     222{ 
     223  /* Convert to strings */ 
     224  QString action = (policy.action() == Policy::Accept ? "accept" : "reject"); 
     225  QHostAddress address = policy.address(); 
     226  QString addr = (address.isNull() ? "*" : address.toString()); 
     227  QString mask = (policy.mask() ? QString::number(policy.mask()) : ""); 
     228  int port = policy.fromPort(); 
     229  QString fromPort = (port ? QString::number(port) : ""); 
     230  port = policy.toPort(); 
     231  QString toPort = (port ? QString::number(port) : ""); 
     232 
     233  /* Add to list */ 
     234  addPolicyItem(action, addr, mask, fromPort, toPort); 
     235} 
     236 
     237/* Adds a new QTreeWidget item to the exit policy list */ 
     238void 
     239ServerPage::addPolicyItem(QString action, QString address, QString mask, 
     240                          QString fromPort, QString toPort) 
     241{ 
     242  QTreeWidgetItem *newPolicy = new QTreeWidgetItem(); 
     243   
     244  newPolicy->setText(COL_ACTION, action); 
     245  newPolicy->setTextAlignment(COL_ACTION, Qt::AlignCenter); 
     246   
     247  if (!mask.isEmpty()) { 
     248    address += "/" + mask; 
     249  } 
     250  newPolicy->setText(COL_ADDRESS, address); 
     251  newPolicy->setTextAlignment(COL_ADDRESS, Qt::AlignCenter); 
     252 
     253  if (!fromPort.isEmpty()) { 
     254    if (!toPort.isEmpty() && fromPort != "*") { 
     255      fromPort += "-" + toPort; 
     256    } 
     257  } else { 
     258    fromPort = "*"; 
     259  } 
     260 
     261  newPolicy->setText(COL_PORT, fromPort); 
     262  newPolicy->setTextAlignment(COL_PORT, Qt::AlignCenter); 
     263 
     264  ui.lstExitPolicies->addTopLevelItem(newPolicy); 
     265} 
     266 
    164267/** Removes selected exit policy from the user's configuration */ 
    165268void 
     
    202305ServerPage::selectedIndex() 
    203306{ 
     307  if (ui.lstExitPolicies->selectedItems().isEmpty()) return -1; 
     308   
    204309  /* This list only contains one element so take it */ 
    205310  QTreeWidgetItem *selectedItem = ui.lstExitPolicies->selectedItems()[0]; 
    206  
    207   if (selectedItem) { 
    208     return ui.lstExitPolicies->indexOfTopLevelItem(selectedItem); 
    209   } 
    210   return -1; 
     311  return ui.lstExitPolicies->indexOfTopLevelItem(selectedItem); 
    211312} 
    212313 
  • trunk/src/gui/config/serverpage.h

    r535 r549  
    3131#include <control/torcontrol.h> 
    3232#include <config/serversettings.h> 
     33#include <config/exitpolicy.h> 
    3334#include "../help/browser/helpbrowser.h" 
    3435 
     
    6970  /** Returns the index of the selected item in lstExitPolicies */ 
    7071  int selectedIndex(); 
    71   
     72  /** Adds a new exit policy to the exit policy list */ 
     73  void addPolicyItem(QString action, QString address, QString mask, 
     74                     QString fromPort, QString toPort); 
     75  void addPolicyItem(Policy policy); 
     76  /** Saves the policy specified in item to the exitPolicy */ 
     77  void savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy); 
     78  /** Moves or appends the necessary default rule as specified by the user */ 
     79  void setDefaultRule(); 
     80 
    7281  /** A TorControl object used to talk to Tor */ 
    7382  TorControl* _torControl; 
  • trunk/src/gui/config/serverpage.ui

    r526 r549  
    10621062             <item> 
    10631063              <property name="text" > 
    1064                <string>Accept</string> 
     1064               <string>accept</string> 
    10651065              </property> 
    10661066             </item> 
    10671067             <item> 
    10681068              <property name="text" > 
    1069                <string>Reject</string> 
     1069               <string>reject</string> 
    10701070              </property> 
    10711071             </item> 
     
    10881088         </item> 
    10891089         <item row="2" column="0" colspan="3" > 
    1090           <widget class="QCheckBox" name="checkBox" > 
     1090          <widget class="QCheckBox" name="chkExitOverride" > 
     1091           <property name="contextMenuPolicy" > 
     1092            <enum>Qt::NoContextMenu</enum> 
     1093           </property> 
    10911094           <property name="toolTip" > 
    10921095            <string>If enabled, appends 'reject *.*' to the end of the policy</string>