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

Commit

Permalink
Automatically reconnect to the Tor control port
Browse files Browse the repository at this point in the history
  • Loading branch information
csucu committed Jan 16, 2017
1 parent a2aeac9 commit 9a68cda
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
23 changes: 20 additions & 3 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 @@ -18,12 +19,25 @@ def fetch_instance_descriptors(controller):

# 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:
controller.signal(stem.control.Signal.NEWNYM)
time.sleep(5)
break
except stem.SocketClosed:
logger.error("Failed to send NEWNYM signal, socket is closed.")
util.reauthenticate(controller, logger)

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


class Instance(object):
Expand Down Expand Up @@ -66,6 +80,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
20 changes: 13 additions & 7 deletions onionbalance/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,24 @@ 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
16 changes: 16 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,15 @@ 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 9a68cda

Please sign in to comment.