[tor-commits] [ooni-probe/master] Refactor code and write minimal client implementation

art at torproject.org art at torproject.org
Fri Jul 20 14:05:39 UTC 2012


commit 6d588153f83b43fd1ca4b592816f37abc5f8bea8
Author: Arturo Filastò <hellais at torproject.org>
Date:   Thu Jul 12 18:30:18 2012 +0200

    Refactor code and write minimal client implementation
---
 ooni/protocols/b0wser.py |   84 ++++++++++++++++++++++++++++++++++++++++++++++
 oonib/b0wser.py          |   81 +-------------------------------------------
 2 files changed, 85 insertions(+), 80 deletions(-)

diff --git a/ooni/protocols/b0wser.py b/ooni/protocols/b0wser.py
index ae6b002..e0a3a56 100644
--- a/ooni/protocols/b0wser.py
+++ b/ooni/protocols/b0wser.py
@@ -102,3 +102,87 @@ class Mutator:
         print "Mutating %s with idx %s" % (data, self.idx)
         return self._mutate(data, self.idx)
 
+class B0wserProtocol(protocol.Protocol):
+    """
+    This implements the B0wser protocol for the server side.
+    It gets instanced once for every client that connects to the oonib.
+    For every instance of protocol there is only 1 mutation.
+    Once the last step is reached the connection is closed on the serverside.
+    """
+    steps = [{'data': "STEP1", 'wait': 4},
+             {'data': "STEP2", 'wait': 4},
+             {'data': "STEP3", 'wait': 4}]
+    mutator = None
+
+    state = 0
+    total_states = len(steps) - 1
+    received_data = 0
+    report = reports.Report('b0wser', 'b0wser.yamlooni')
+
+    def next_state(self):
+        """
+        This is called once I have completed one step of the protocol and need
+        to proceed to the next step.
+        """
+        data = self.mutator.get_mutation(self.state)
+        self.transport.write(data)
+        self.state += 1
+        self.received_data = 0
+
+    def dataReceived(self, data):
+        """
+        This is called every time some data is received. I transition to the
+        next step once the amount of data that I expect to receive is received.
+
+        @param data: the data that has been sent by the client.
+        """
+        if len(self.steps) <= self.state:
+            print "I have reached the end of the state machine"
+            print "Censorship fingerprint bruteforced!"
+            report = {'mutator_state': self.mutator.state()}
+            self.report(report)
+            self.transport.loseConnection()
+            return
+        self.received_data += len(data)
+        if self.received_data >= self.steps[self.state]['wait']:
+            print "Moving to next state %s" % self.state
+            self.next_state()
+
+    def censorship_detected(self, report):
+        """
+        I have detected the possible presence of censorship we need to write a
+        report on it.
+
+        @param report: a dict containing the report to be written. Must contain
+                       the keys 'reason', 'proto_state' and 'mutator_state'.
+                       The reason is the reason for which the connection was
+                       closed. The proto_state is the current state of the
+                       protocol instance and mutator_state is what was being
+                       mutated.
+        """
+        print "The connection was closed because of %s" % report['reason']
+        print "State %s, Mutator %s" % (report['proto_state'],
+                                        report['mutator_state'])
+        self.mutator.next_mutation()
+
+
+
+    def connectionLost(self, reason):
+        """
+        The connection was closed. This may be because of a legittimate reason
+        or it may be because of a censorship event.
+        """
+        report = {'reason': reason, 'proto_state': self.state,
+                'mutator_state': self.mutator.state(), 'trigger': None}
+
+        if self.state < self.total_states:
+            report['trigger'] = 'did not finish state walk'
+            self.censorship_detected(report)
+
+        if reason.check(ConnectionDone):
+            print "Connection closed cleanly"
+        else:
+            report['trigger'] = 'unclean connection closure'
+            self.censorship_detected(report)
+
+
diff --git a/oonib/b0wser.py b/oonib/b0wser.py
index 4b01cc7..33501de 100644
--- a/oonib/b0wser.py
+++ b/oonib/b0wser.py
@@ -2,86 +2,7 @@ from twisted.internet import protocol
 from twisted.internet.error import ConnectionDone
 
 from ooni.plugoo import reports
-from ooni.protocols.b0wser import Mutator
-
-class B0wserProtocol(protocol.Protocol):
-    """
-    This implements the B0wser protocol for the server side.
-    It gets instanced once for every client that connects to the oonib.
-    For every instance of protocol there is only 1 mutation.
-    Once the last step is reached the connection is closed on the serverside.
-    """
-    steps = [{'data': "STEP1", 'wait': 4},
-             {'data': "STEP2", 'wait': 4},
-             {'data': "STEP3", 'wait': 4}]
-    mutator = None
-
-    state = 0
-    total_states = len(steps) - 1
-    received_data = 0
-    report = reports.Report('b0wser', 'b0wser.yamlooni')
-
-    def next_state(self):
-        """
-        This is called once I have completed one step of the protocol and need
-        to proceed to the next step.
-        """
-        data = self.mutator.get_mutation(self.state)
-        self.transport.write(data)
-        self.state += 1
-        self.received_data = 0
-
-    def dataReceived(self, data):
-        """
-        This is called every time some data is received. I transition to the
-        next step once the amount of data that I expect to receive is received.
-
-        @param data: the data that has been sent by the client.
-        """
-        if len(self.steps) <= self.state:
-            self.transport.loseConnection()
-            return
-        self.received_data += len(data)
-        if self.received_data >= self.steps[self.state]['wait']:
-            print "Moving to next state %s" % self.state
-            self.next_state()
-
-    def censorship_detected(self, report):
-        """
-        I have detected the possible presence of censorship we need to write a
-        report on it.
-
-        @param report: a dict containing the report to be written. Must contain
-                       the keys 'reason', 'proto_state' and 'mutator_state'.
-                       The reason is the reason for which the connection was
-                       closed. The proto_state is the current state of the
-                       protocol instance and mutator_state is what was being
-                       mutated.
-        """
-        print "The connection was closed because of %s" % report['reason']
-        print "I may have matched the censorship fingerprint"
-        print "State %s, Mutator %s" % (report['proto_state'],
-                                        report['mutator_state'])
-        self.report(report)
-
-
-    def connectionLost(self, reason):
-        """
-        The connection was closed. This may be because of a legittimate reason
-        or it may be because of a censorship event.
-        """
-        report = {'reason': reason, 'proto_state': self.state,
-                'mutator_state': self.mutator.state(), 'trigger': None}
-
-        if self.state < self.total_states:
-            report['trigger'] = 'did not finish state walk'
-            self.censorship_detected(report)
-
-        if reason.check(ConnectionDone):
-            print "Connection closed cleanly"
-        else:
-            report['trigger'] = 'unclean connection closure'
-            self.censorship_detected(report)
+from ooni.protocols.b0wser import Mutator, B0wserProtocol
 
 class B0wserServer(protocol.ServerFactory):
     """





More information about the tor-commits mailing list