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

[tor-commits] [stem/master] Optionally wait for publication when creating HS



commit 53058f868667e4c885ec21f69fb187f4993d6637
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun May 10 17:45:09 2015 -0700

    Optionally wait for publication when creating HS
    
    Option for ephemeral hidden service creation to wait for the UPLOADED event.
    This has a few warts to it, but should work reasonably well in practice until
    we have something better. Based on a change by Yawning to do the same...
    
      https://github.com/Yawning/onionwrap/commit/8edda8c76b2a39d22c8fa197c0d4df7879f632a3
---
 stem/control.py |   34 +++++++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/stem/control.py b/stem/control.py
index 3e426ea..39fc3c9 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -2582,7 +2582,7 @@ class Controller(BaseController):
 
     return result
 
-  def create_ephemeral_hidden_service(self, ports, key_type = 'NEW', key_content = 'BEST', discard_key = False, detached = False):
+  def create_ephemeral_hidden_service(self, ports, key_type = 'NEW', key_content = 'BEST', discard_key = False, detached = False, await_publication = False):
     """
     Creates a new hidden service. Unlike
     :func:`~stem.control.Controller.create_hidden_service` this style of
@@ -2619,6 +2619,8 @@ class Controller(BaseController):
     :param bool discard_key: avoid providing the key back in our response
     :param bool detached: continue this hidden service even after this control
       connection is closed if **True**
+    :param bool await_publication: blocks until our descriptor is successfully
+      published if **True**
 
     :returns: :class:`~stem.response.AddOnionResponse` with the response
 
@@ -2629,6 +2631,15 @@ class Controller(BaseController):
     # if self.get_version() < stem.version.Requirement.ADD_ONION:
     #   raise stem.UnsatisfiableRequest(message = 'Ephemeral hidden services were added in tor version %s' % stem.version.Requirement.ADD_ONION)
 
+    hs_desc_queue, hs_desc_listener = queue.Queue(), None
+
+    if await_publication:
+      def hs_desc_listener(event):
+        print event
+        hs_desc_queue.put(event)
+
+      self.add_event_listener(hs_desc_listener, EventType.HS_DESC)
+
     request = 'ADD_ONION %s:%s' % (key_type, key_content)
 
     flags = []
@@ -2655,6 +2666,27 @@ class Controller(BaseController):
 
     response = self.msg(request)
     stem.response.convert('ADD_ONION', response)
+
+    if await_publication:
+      # We should receive five UPLOAD events, followed by up to another five
+      # UPLOADED to indicate they've finished. Presently tor seems to have an
+      # issue where the address is provided for UPLOAD but not UPLOADED so need
+      # to just guess that if it's for the same hidden service authority then
+      # it's what we're looking for.
+
+      authorities_uploaded_to = []
+
+      try:
+        while True:
+          event = hs_desc_queue.get()
+
+          if event.action == stem.HSDescAction.UPLOAD and event.address == response.service_id:
+            authorities_uploaded_to.append(event.directory_fingerprint)
+          elif event.action == stem.HSDescAction.UPLOADED and event.directory_fingerprint in authorities_uploaded_to:
+            break  # successfully uploaded to a HS authority... maybe
+      finally:
+        self.remove_event_listener(hs_desc_listener)
+
     return response
 
   def remove_ephemeral_hidden_service(self, service_id):

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits