[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1429: Implement the new exit policy interface stuff. (in trunk/src: config gui/config)



Author: edmanm
Date: 2006-11-05 02:48:27 -0500 (Sun, 05 Nov 2006)
New Revision: 1429

Modified:
   trunk/src/config/exitpolicy.cpp
   trunk/src/config/exitpolicy.h
   trunk/src/gui/config/serverpage.cpp
   trunk/src/gui/config/serverpage.h
Log:
Implement the new exit policy interface stuff.


Modified: trunk/src/config/exitpolicy.cpp
===================================================================
--- trunk/src/config/exitpolicy.cpp	2006-11-05 07:47:11 UTC (rev 1428)
+++ trunk/src/config/exitpolicy.cpp	2006-11-05 07:48:27 UTC (rev 1429)
@@ -39,20 +39,20 @@
 ExitPolicy::ExitPolicy(SpecialExitPolicy exitPolicy)
 {
   if (exitPolicy == Middleman) {
-    addPolicy(Policy(Policy::RejectAll));
+    _exitPolicy << Policy(Policy::RejectAll);
   } else if (exitPolicy == Default) {
-    addPolicy(Policy("reject *:25"));
-    addPolicy(Policy("reject *:119"));
-    addPolicy(Policy("reject *:135-139"));
-    addPolicy(Policy("reject *:445"));
-    addPolicy(Policy("reject *:465"));
-    addPolicy(Policy("reject *:587"));
-    addPolicy(Policy("reject *:1214"));
-    addPolicy(Policy("reject *:4661-4666"));
-    addPolicy(Policy("reject *:6346-6429"));
-    addPolicy(Policy("reject *:6699"));
-    addPolicy(Policy("reject *:6881-6999"));
-    addPolicy(Policy("accept *:*"));
+    _exitPolicy << Policy("reject *:25")
+                << Policy("reject *:119")
+                << Policy("reject *:135-139")
+                << Policy("reject *:445")
+                << Policy("reject *:465")
+                << Policy("reject *:587")
+                << Policy("reject *:1214")
+                << Policy("reject *:4661-4666")
+                << Policy("reject *:6346-6429")
+                << Policy("reject *:6699")
+                << Policy("reject *:6881-6999")
+                << Policy("accept *:*");
   }
 }
 
@@ -89,15 +89,77 @@
   }
 }
 
+/** Adds the ports specified in <b>portList</b> to a list of ports accepted
+ * by this exit policy. Ports may be given either individually or as ranges. */
+void
+ExitPolicy::addAcceptedPorts(QStringList portList)
+{
+  foreach (QString port, portList) {
+    addPolicy(Policy("accept *:" + port));
+  }
+}
+
+/** Returns true if this exit policy accepts all ports specified in
+ * <b>portList</b>. Ports in <b>portList</b> may be given either individually
+ * or in ranges (e.g., "6660-6669"). */
+bool
+ExitPolicy::acceptsPorts(QStringList portList)
+{
+  foreach (QString port, portList) {
+    if (!contains(Policy("accept *:" + port))) {
+      return false;
+    }
+  }
+  return true;
+}
+
+/** Adds the ports specified in <b>portList</b> to a list of ports rejected
+ * by this exit policy. Ports may be given either individually or as ranges. */
+void
+ExitPolicy::addRejectedPorts(QStringList portList)
+{
+  foreach (QString port, portList) {
+    addPolicy(Policy("reject *:" + port));
+  }
+}
+
+/** Returns true if this exit policy rejects all ports specified in
+ * <b>portList</b>. Ports in <b>portList</b> may be given either individually
+ * or in ranges (e.g., "6660-6669"). */
+bool
+ExitPolicy::rejectsPorts(QStringList portList)
+{
+  foreach (QString port, portList) {
+    if (!contains(Policy("reject *:" + port))) {
+      return false;
+    }
+  }
+  return true;
+}
+
 /** Returns true if this exit policy contains the given policy. */ 
 bool
 ExitPolicy::contains(Policy policy)
 {
+  Policy acceptAll(Policy::AcceptAll);
+  Policy rejectAll(Policy::RejectAll);
+  
+  /* Check for this policy item in the explicitly defined policies */
   foreach (Policy p, _exitPolicy) {
-    if (p == policy) {
+    if (p.matches(policy)) {
       return true;
     }
+    if ((p == acceptAll) || (p == rejectAll)) {
+      /* This exit policy replaces the default policy, so stop checking */
+      return false;
+    }
   }
+  /* Now check the default exit policy */
+  foreach (Policy p, ExitPolicy(Default).policyList()) {
+    if (p.matches(policy)) {
+      return true;
+    }
+  }
   return false;
 }
 

Modified: trunk/src/config/exitpolicy.h
===================================================================
--- trunk/src/config/exitpolicy.h	2006-11-05 07:47:11 UTC (rev 1428)
+++ trunk/src/config/exitpolicy.h	2006-11-05 07:48:27 UTC (rev 1429)
@@ -28,8 +28,9 @@
 #ifndef _EXITPOLICY_H
 #define _EXITPOLICY_H
 
+#include <QList>
 #include <QString>
-#include <QList>
+#include <QStringList>
 
 #include "policy.h"
 
@@ -49,7 +50,22 @@
   ExitPolicy(SpecialExitPolicy exitPolicy);
   /** Creates an exit policy from the given comma-delimited list of policies. */
   ExitPolicy(QString exitPolicy);
-
+  
+  /** Adds the ports specified in <b>portList</b> to a list of ports accepted
+   * by this exit policy. Ports may be given either individually or as ranges. */
+  void addAcceptedPorts(QStringList portList);
+  /** Returns true if this exit policy accepts all ports specified in
+   * <b>portList</b>. Ports in <b>portList</b> may be given either individually
+   * or as ranges. */
+  bool acceptsPorts(QStringList portList);
+  /** Adds the ports specified in <b>portList</b> to a list of ports rejected
+   * by this exit policy. Ports may be given either individually or as ranges. */
+  void addRejectedPorts(QStringList portList);
+  /** Returns true if this exit policy rejects all ports specified in
+   * <b>portList</b>. Ports in <b>portList</b> may be given either individually
+   * or as ranges. */
+  bool rejectsPorts(QStringList portList);
+  
   /** Adds a rule to the exit policy. */
   void addPolicy(Policy policy);
   /** Removes a rule from the exit policy. */

Modified: trunk/src/gui/config/serverpage.cpp
===================================================================
--- trunk/src/gui/config/serverpage.cpp	2006-11-05 07:47:11 UTC (rev 1428)
+++ trunk/src/gui/config/serverpage.cpp	2006-11-05 07:48:27 UTC (rev 1429)
@@ -31,6 +31,8 @@
 #include <util/html.h>
 #include <gui/common/vmessagebox.h>
 
+#include <QtDebug>
+
 #include "serverpage.h"
 #include "ipvalidator.h"
 #include "portvalidator.h"
@@ -55,14 +57,27 @@
 #define T1_MAX_RATE             (384*1024)
 #define HIGHBW_AVG_RATE         (3072*1024)
 #define HIGHBW_MAX_RATE         (6144*1024)
-
 /** Minimum allowed bandwidth rate (20KB) */
 #define MIN_BANDWIDTH_RATE      20
 /** Maximum bandwidth rate. This is limited to 2147483646 bytes, 
  * or 2097151 kilobytes. (2147483646/1024) */ 
 #define MAX_BANDWIDTH_RATE      2097151
 
+/** Ports represented by the "Websites" checkbox. (80) */
+#define PORTS_HTTP   (QStringList() << "80")
+/** Ports represented by the "Secure Websites" checkbox. (443) */
+#define PORTS_HTTPS  (QStringList() << "443")
+/** Ports represented by the "Retrieve Mail" checkbox. (110,143,993,995) */
+#define PORTS_MAIL   (QStringList() << "110" << "143" << "993" << "995")
+/** Ports represented by the "Instant Messaging" checkbox.
+ * (703,1863,5050,5190,5222,8300,8888) */
+#define PORTS_IM     (QStringList() << "706" << "1863" << "5050" << "5190" \
+                                    << "5222" << "8300" << "8888")
+/** Ports represented by the "Internet Relay Chat" checkbox. 
+ * (6660-6669,6697) */
+#define PORTS_IRC    (QStringList() << "6660-6669" << "6697")
 
+
 /** Constructor */
 ServerPage::ServerPage(QWidget *parent)
 : ConfigPage(parent)
@@ -152,15 +167,9 @@
   _settings->setAddress(ui.lineServerAddress->text());
   _settings->setContactInfo(ui.lineServerContact->text());
   saveBandwidthLimits();
+  saveExitPolicies();
   setAutoUpdateTimer(ui.chkAutoUpdate->isChecked());
 
-  /* Save exit polices */
-//   ExitPolicy exitPolicy;
-//   for (int i = 0; i < ui.lstExitPolicies->topLevelItemCount(); ++i) {
-//     savePolicy(ui.lstExitPolicies->topLevelItem(i), exitPolicy);
-//   }
-//   _settings->setExitPolicy(exitPolicy);
-  
   /* If we're connected to Tor and we've changed the server settings, attempt
    * to apply the new settings now. */
   if (_torControl->isConnected() && _settings->changedSinceLastApply()) {
@@ -187,84 +196,11 @@
   ui.lineServerAddress->setText(_settings->getAddress());
   ui.lineServerContact->setText(_settings->getContactInfo());
   loadBandwidthLimits();
+  loadExitPolicies();
 
-//   foreach (Policy policy, _settings->getExitPolicy().policyList()) {
-//     addPolicyItem(policy);
-//   }
-  
   ui.frmServer->setVisible(ui.chkEnableServer->isChecked());
 }
 
-/** Adds the exit policy contained in item to the exitPolicy */
-// void
-// ServerPage::savePolicy(QTreeWidgetItem *item, ExitPolicy &exitPolicy)
-// {
-//   /* Add policy to ServerSettings */
-//   exitPolicy.addPolicy(Policy(item->text(COL_ACTION),
-//                               item->text(COL_ADDRESS),
-//                               item->text(COL_PORT)));
-// }
-
-// /** Adds a new exit policy to the user's configuration */
-// void
-// ServerPage::addPolicy()
-// {
-//   /* They must enter a valid address */
-//   QString address = ui.lineExitAddress->text();
-//   if (!address.isEmpty()) {
-//     if (((IPValidator *)ui.lineExitAddress->validator())->
-//         validate(address) != QValidator::Acceptable) {
-//       return;
-//     }
-//   }
-  
-//   /* If port range specified, must be valid */
-//   QString fromPort = ui.lineExitFromPort->text();
-//   QString toPort = ui.lineExitToPort->text();
-  
-//   if (!fromPort.isEmpty() && !toPort.isEmpty() && toPort != fromPort) {
-//     if (toPort == "*") toPort = "65535";
-//     if (fromPort == "*") fromPort = "1";
-//     if (fromPort.toUShort() > toPort.toUShort()) {
-//       return;
-//     }
-//   }
-  
-//   /* Add the policy to the list */
-//   addPolicyItem(Policy(Policy::toAction(ui.cmboExitAction->currentText()), 
-//                        QHostAddress(ui.lineExitAddress->text()),
-//                        ui.lineExitMask->text().toUShort(), 
-//                        fromPort.toUShort(),
-//                        toPort.toUShort()), false);
-
-//   /* Clear input text boxes */
-//   ui.lineExitAddress->clear();
-//   ui.lineExitMask->clear();
-//   ui.lineExitFromPort->clear();
-//   ui.lineExitToPort->clear();
-// }
-
-// /** Adds a new QTreeWidget item to the exit policy list */
-// void
-// ServerPage::addPolicyItem(Policy policy, bool append)
-// {
-//   QTreeWidgetItem *newPolicy = new QTreeWidgetItem();
-
-//   newPolicy->setText(COL_ACTION,  policy.action());
-//   newPolicy->setText(COL_ADDRESS, policy.address());
-//   newPolicy->setText(COL_PORT,    policy.ports());
- 
-//   for (int i = 0; i < newPolicy->columnCount(); i++) {
-//     newPolicy->setTextAlignment(i, Qt::AlignHCenter);
-//   }
-
-//   if (append) {
-//     ui.lstExitPolicies->addTopLevelItem(newPolicy);
-//   } else {
-//     ui.lstExitPolicies->insertTopLevelItem(0, newPolicy);
-//   }
-// }
-
 /** Shows exit policy related help information */
 void
 ServerPage::exitPolicyHelp()
@@ -427,6 +363,74 @@
   _settings->setBandwidthBurstRate(maxRate);
 }
 
+/** */
+void
+ServerPage::loadExitPolicies()
+{
+  ExitPolicy exitPolicy = _settings->getExitPolicy();
+  
+  if (exitPolicy.contains(Policy(Policy::RejectAll))) {
+    /* If the policy ends with reject *:*, check if the policy explicitly
+     * accepts these ports */
+    ui.chkWebsites->setChecked(exitPolicy.acceptsPorts(PORTS_HTTP));
+    ui.chkSecWebsites->setChecked(exitPolicy.acceptsPorts(PORTS_HTTPS));
+    ui.chkMail->setChecked(exitPolicy.acceptsPorts(PORTS_MAIL));
+    ui.chkIRC->setChecked(exitPolicy.acceptsPorts(PORTS_IRC));
+    ui.chkIM->setChecked(exitPolicy.acceptsPorts(PORTS_IM));
+    ui.chkMisc->setChecked(false);
+  } else {
+    /* If the exit policy ends with accept *:*, check if the policy explicitly
+     * rejects these ports */
+    ui.chkWebsites->setChecked(!exitPolicy.rejectsPorts(PORTS_HTTP));
+    ui.chkSecWebsites->setChecked(!exitPolicy.rejectsPorts(PORTS_HTTPS));
+    ui.chkMail->setChecked(!exitPolicy.rejectsPorts(PORTS_MAIL));
+    ui.chkIRC->setChecked(!exitPolicy.rejectsPorts(PORTS_IRC));
+    ui.chkIM->setChecked(!exitPolicy.rejectsPorts(PORTS_IM));
+    ui.chkMisc->setChecked(true);
+  }
+}
+
+/** */
+void
+ServerPage::saveExitPolicies()
+{
+  ExitPolicy exitPolicy;
+  bool rejectUnchecked = ui.chkMisc->isChecked();
+  
+  /* If misc is checked, then reject unchecked items and leave the default exit
+   * policy alone. Else, accept only checked items and end with reject *:*,
+   * replacing the default exit policy. */
+  if (ui.chkWebsites->isChecked() && !rejectUnchecked) {
+    exitPolicy.addAcceptedPorts(PORTS_HTTP);
+  } else if (!ui.chkWebsites->isChecked() && rejectUnchecked) {
+    exitPolicy.addRejectedPorts(PORTS_HTTP);
+  }
+  if (ui.chkSecWebsites->isChecked() && !rejectUnchecked) {
+    exitPolicy.addAcceptedPorts(PORTS_HTTPS);
+  } else if (!ui.chkSecWebsites->isChecked() && rejectUnchecked) {
+    exitPolicy.addRejectedPorts(PORTS_HTTPS);
+  }
+  if (ui.chkMail->isChecked() && !rejectUnchecked) {
+    exitPolicy.addAcceptedPorts(PORTS_MAIL);
+  } else if (!ui.chkMail->isChecked() && rejectUnchecked) {
+    exitPolicy.addRejectedPorts(PORTS_MAIL);
+  }
+  if (ui.chkIRC->isChecked() && !rejectUnchecked) {
+    exitPolicy.addAcceptedPorts(PORTS_IRC);
+  } else if (!ui.chkIRC->isChecked() && rejectUnchecked) {
+    exitPolicy.addRejectedPorts(PORTS_IRC);
+  }
+  if (ui.chkIM->isChecked() && !rejectUnchecked) {
+    exitPolicy.addAcceptedPorts(PORTS_IM);
+  } else if (!ui.chkIM->isChecked() && rejectUnchecked) {
+    exitPolicy.addRejectedPorts(PORTS_IM);
+  }
+  if (!ui.chkMisc->isChecked()) {
+    exitPolicy.addPolicy(Policy(Policy::RejectAll));
+  }
+  _settings->setExitPolicy(exitPolicy);
+}
+
 /** Called when the user selects a new value from the rate combo box. */
 void
 ServerPage::rateChanged(int index)

Modified: trunk/src/gui/config/serverpage.h
===================================================================
--- trunk/src/gui/config/serverpage.h	2006-11-05 07:47:11 UTC (rev 1428)
+++ trunk/src/gui/config/serverpage.h	2006-11-05 07:48:27 UTC (rev 1429)
@@ -89,6 +89,10 @@
   void saveBandwidthLimits();
   /** Loads the server's bandwidth average and burst limits. */
   void loadBandwidthLimits();
+  /** Saves the server's exit policies. */
+  void saveExitPolicies();
+  /** Loads the server's exit policies. */
+  void loadExitPolicies();
 
   /** A TorControl object used to talk to Tor */
   TorControl* _torControl;