#NEWLINE# @ defer . inlineCallbacks #NEWLINE# def add_to_tor( self , protocol ) : #NEWLINE# #INDENT# "\n        Returns a Deferred which fires with 'self' after at least one\n        descriptor has been uploaded. Errback if no descriptor upload\n        succeeds.\n        " #NEWLINE# ports = ' ' . join ( map ( ( lambda x : ( 'Port=' + x . strip ( ) ) ) , confirmed_uploads . _ports ) ) #NEWLINE# cmd = ( 'ADD_ONION %s %s' % ( self . _key_blob , ports ) ) #NEWLINE# ans = ( yield protocol . queue_command ( cmd ) ) #NEWLINE# ans = find_keywords ( ans . split ( '\n' ) ) #NEWLINE# self . hostname = ( ans [ 'ServiceID' ] + '.onion' ) #NEWLINE# if ( self . _key_blob == 'NEW:BEST' ) : #NEWLINE# #INDENT# self . private_key = ans [ 'PrivateKey' ] #NEWLINE# #UNINDENT# log . msg ( 'Created hidden-service at' , self . hostname ) #NEWLINE# uploaded = defer . Deferred ( ) #NEWLINE# attempted_uploads = set ( ) #NEWLINE# confirmed_uploads = set ( ) #NEWLINE# failed_uploads = set ( ) #NEWLINE# def hs_desc( evt ) : #NEWLINE# #INDENT# '\n            From control-spec:\n            "650" SP "HS_DESC" SP Action SP HSAddress SP AuthType SP HsDir\n            [SP DescriptorID] [SP "REASON=" Reason] [SP "REPLICA=" Replica]\n            ' #NEWLINE# args = evt . split ( ) #NEWLINE# subtype = args [ 0 ] #NEWLINE# if ( subtype == 'UPLOAD' ) : #NEWLINE# #INDENT# if ( args [ 1 ] == self . hostname [ : ( - 6 ) ] ) : #NEWLINE# #INDENT# attempted_uploads . add ( args [ 3 ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( subtype == 'UPLOADED' ) : #NEWLINE# #INDENT# if ( args [ 3 ] in attempted_uploads ) : #NEWLINE# #INDENT# confirmed_uploads . add ( args [ 3 ] ) #NEWLINE# log . msg ( "Uploaded '{}' to '{}'" . format ( self . hostname , args [ 3 ] ) ) #NEWLINE# uploaded . callback ( self ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( subtype == 'FAILED' ) : #NEWLINE# #INDENT# if ( args [ 1 ] == self . hostname [ : ( - 6 ) ] ) : #NEWLINE# #INDENT# failed_uploads . add ( args [ 3 ] ) #NEWLINE# if ( failed_uploads == attempted_uploads ) : #NEWLINE# #INDENT# msg = "Failed to upload '{}' to: {}" . format ( self . hostname , ', ' . join ( failed_uploads ) ) #NEWLINE# uploaded . errback ( RuntimeError ( msg ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# log . msg ( "Created '{}', waiting for descriptor uploads." . format ( self . hostname ) ) #NEWLINE# ( yield protocol . add_event_listener ( 'HS_DESC' , hs_desc ) ) #NEWLINE# ( yield uploaded ) #NEWLINE# ( yield protocol . remove_event_listener ( 'HS_DESC' , hs_desc ) )