[tor-commits] [ooni-probe/master] Implement a working b0wser serveside component

art at torproject.org art at torproject.org
Wed Jul 11 13:56:42 UTC 2012


commit 32a0027388f4ce5a9404ab02e3e9a21ed9314465
Author: Arturo Filastò <hellais at torproject.org>
Date:   Tue Jul 10 20:37:38 2012 +0200

    Implement a working b0wser serveside component
---
 oonib/b0wser.py |  113 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/oonib/b0wser.py b/oonib/b0wser.py
index 072b4b9..186c9a4 100644
--- a/oonib/b0wser.py
+++ b/oonib/b0wser.py
@@ -3,13 +3,27 @@ from twisted.internet import protocol
 class Mutator:
     idx = 0
     step = 0
-    state = 0
+
     waiting = False
     waiting_step = 0
 
     def __init__(self, steps):
         self.steps = steps
 
+    def _mutate(self, data, idx):
+        """
+        Mutate the idx bytes by increasing it's value by one
+
+        @param data: the data to be mutated.
+
+        @param idx: what byte should be mutated.
+        """
+        print "idx: %s, data: %s" % (idx, data)
+        ret = data[:idx]
+        ret += chr(ord(data[idx]) + 1)
+        ret += data[idx+1:]
+        return ret
+
     def next_mutation(self):
         """
         Increases by one the mutation state.
@@ -18,81 +32,74 @@ class Mutator:
         before [___*] [____]
                step1   step2
         after  [____] [*___]
+
+        Should be called every time you need to proceed onto the next mutation.
+        It changes the internal state of the mutator to that of the next
+        mutatation.
+
+        returns True if another mutation is available.
+        returns False if all the possible mutations have been done.
         """
-        current_idx = self.idx + 1
+        if self.step > len(self.steps):
+            self.waiting = True
+            return False
+
+        self.idx += 1
+        current_idx = self.idx
         current_step = self.step
-        current_data = self.steps[c_step]['data']
-        data_to_receive = self.steps[c_step]]['recv']
+        current_data = self.steps[current_step]['data']
+        data_to_receive = self.steps[current_step]['wait']
 
         if self.waiting and self.waiting_step == data_to_receive:
+            print "I am no longer waiting..."
             self.waiting = False
             self.waiting_step = 0
+            self.idx = 0
 
         elif self.waiting:
+            print "Waiting some more..."
             self.waiting_step += 1
 
-        elif current_idx > len(current_data):
+        elif current_idx >= len(current_data):
+            print "Entering waiting mode..."
             self.step += 1
             self.idx = 0
             self.waiting = True
+        print "current index %s current data %s" % (current_idx, len(current_data))
+        return True
 
-    @classmethod
-    def mutate(data, idx):
-        """
-        Mutate the idx bytes by increasing it's value by one
-
-        @param data: the data to be mutated.
-
-        @param idx: what byte should be mutated.
+    def get_mutation(self, state):
         """
-        ret = data[:idx-1]
-        ret += chr(ord(data[idx]) + 1)
-        ret += data[idx:]
-        return ret
-
-    def get_mutation(self):
-        """
-        returns the current packet to be sent to the wire.
+        Returns the current packet to be sent to the wire.
         If no mutation is necessary it will return the plain data.
-        """
-        self.next_mutation()
-        if self.state != self.step or self.waiting:
-            return self.steps[self.state]
+        Should be called when you are interested in obtaining the data to be
+        sent for the selected state.
 
-        data = self.steps[self.state]
-        return self.mutate(data, self.idx)
+        @param step: the current step you want the mutation for
 
-    def get_data(self, i):
+        returns the mutated packet for the specified step.
         """
-        XXX remove this shit.
-        """
-        j = 0
-        pkt_size = len(self.steps[j]['data'])
-        while i > pkt_size:
-            j += 1
-            pkt_size += len(self.steps[j])
-        # I am not in a state to send mutations
-        if j != self.state:
-            return self.steps[j]['data']
-
-        rel_idx = i % (pkt_size - len(self.steps[j-1]))
-        data = self.steps[j]
-        data[rel_idx] = chr(ord(data[rel_idx]) + 1)
-        return data
+        if step != self.step or self.waiting:
+            print "I am not going to do anything :)"
+            return self.steps[step]['data']
+
+        data = self.steps[step]['data']
+        print "Mutating %s with idx %s" % (data, self.idx)
+        return self._mutate(data, self.idx)
 
 class B0wserProtocol(protocol.Protocol):
-    steps = [{'data': "STEP1", 'recv': 20},
-             {'data': "STEP2", 'recv': 20},
-             {'data': "STEP3", 'recv': 20}]
+    steps = [{'data': "STEP1", 'wait': 4},
+             {'data': "STEP2", 'wait': 4},
+             {'data': "STEP3", 'wait': 4}]
 
     mutator = None
     state = 0
     received_data = 0
 
     def next_state(self):
-        data = self.mutator.get_mutation()
+        data = self.mutator.get_mutation(self.state)
         self.transport.write(data)
-        self.mutator.state += 1
+        self.state += 1
         self.received_data = 0
 
     def dataReceived(self, data):
@@ -100,8 +107,8 @@ class B0wserProtocol(protocol.Protocol):
             self.transport.loseConnection()
             return
         self.received_data += len(data)
-        if self.received_data >= self.steps[self.state]['recv']:
-            print self.received_data
+        if self.received_data >= self.steps[self.state]['wait']:
+            print "Moving to next state %s" % self.state
             self.next_state()
 
 class B0wserServer(protocol.ServerFactory):
@@ -112,6 +119,10 @@ class B0wserServer(protocol.ServerFactory):
         p.factory = self
 
         if addr.host not in self.mutations:
-            self.mutations[addr.host] = Mutation(p.steps)
+            self.mutations[addr.host] = Mutator(p.steps)
+        else:
+            print "Moving on to next mutation"
+            self.mutations[addr.host].next_mutation()
         p.mutator = self.mutations[addr.host]
         return p
+





More information about the tor-commits mailing list