Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Commit

Permalink
Merge PR 45: Automatically reconnect to the Tor control port
Browse files Browse the repository at this point in the history
  • Loading branch information
DonnchaC committed Jan 17, 2017
2 parents a2aeac9 + 1193356 commit 070b245
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
29 changes: 25 additions & 4 deletions onionbalance/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from onionbalance import log
from onionbalance import config
from onionbalance import util

logger = log.get_logger()

Expand All @@ -16,14 +17,31 @@ def fetch_instance_descriptors(controller):
"""
logger.info("Initiating fetch of descriptors for all service instances.")

# Clear Tor descriptor cache before making fetches by sending NEWNYM
# pylint: disable=no-member
controller.signal(stem.control.Signal.NEWNYM)
time.sleep(5)

while True:
try:
# Clear Tor descriptor cache before making fetches by sending
# the NEWNYM singal
controller.signal(stem.control.Signal.NEWNYM)
time.sleep(5) # Sleep to allow Tor time to build new circuits
except stem.SocketClosed:
logger.error("Failed to send NEWNYM signal, socket is closed.")
util.reauthenticate(controller, logger)
else:
break

for service in config.services:
for instance in service.instances:
instance.fetch_descriptor()
while True:
try:
instance.fetch_descriptor()
except stem.SocketClosed:
logger.error("Failed to fecth descriptor, socket "
"is closed")
util.reauthenticate(controller, logger)
else:
break


class Instance(object):
Expand Down Expand Up @@ -66,6 +84,9 @@ def fetch_descriptor(self):
try:
self.controller.get_hidden_service_descriptor(self.onion_address,
await_result=False)
except stem.SocketClosed:
# Tor maybe restarting.
raise
except stem.DescriptorUnavailable:
# Could not find the descriptor on the HSDir
self.received = None
Expand Down
22 changes: 15 additions & 7 deletions onionbalance/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,26 @@ def _publish_descriptor(self, deviation=0):
def _upload_descriptor(self, signed_descriptor, replica, hsdirs=None):
"""
Convenience method to upload a descriptor
Handle some error checking and logging inside the Service class
"""
if hsdirs and not isinstance(hsdirs, list):
hsdirs = [hsdirs]

try:
descriptor.upload_descriptor(self.controller, signed_descriptor,
hsdirs=hsdirs)
except stem.ControllerError:
logger.exception("Error uploading descriptor for service "
"%s.onion.", self.onion_address)
while True:
try:
descriptor.upload_descriptor(self.controller,
signed_descriptor,
hsdirs=hsdirs)
break
except stem.SocketClosed:
logger.error("Error uploading descriptor for service "
"%s.onion, Socket is closed.",
self.onion_address)
util.reauthenticate(self.controller, logger)
except stem.ControllerError:
logger.exception("Error uploading descriptor for service "
"%s.onion.", self.onion_address)
break

def descriptor_publish(self, force_publish=False):
"""
Expand Down
15 changes: 15 additions & 0 deletions onionbalance/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import base64
import binascii
import os
import stem
import time

# import Crypto.Util
import Crypto.PublicKey

from onionbalance import config


def add_pkcs1_padding(message):
"""Add PKCS#1 padding to **message**."""
Expand Down Expand Up @@ -160,3 +164,14 @@ def is_directory_empty(path):
return False
else:
return True


def reauthenticate(controller, logger):
"""
Tries to authenticate to the controller
"""
time.sleep(10)
try:
controller.authenticate(password=config.TOR_CONTROL_PASSWORD)
except stem.connection.AuthenticationFailure:
logger.error("Failed to re-authenticate controller.")

0 comments on commit 070b245

Please sign in to comment.