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.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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