[Pkg-privacy-commits] [onionbalance] 20/117: Refactor config loading, modify log messages
Donncha O'Cearbahill
donncha-guest at moszumanska.debian.org
Wed Dec 16 23:18:42 UTC 2015
This is an automated email from the git hooks/post-receive script.
donncha-guest pushed a commit to branch debian/sid
in repository onionbalance.
commit dad33cfdc296fee8b3681182e6074d27ebaa8a09
Author: Donncha O'Cearbhaill <donncha at donncha.is>
Date: Mon Jun 15 17:31:08 2015 +0100
Refactor config loading, modify log messages
---
config.yaml.example | 2 +-
onionbalance/config.py | 28 +++++++++++-----------------
onionbalance/eventhandler.py | 6 +++---
onionbalance/hiddenservice.py | 42 ++++++++++++++++++++----------------------
onionbalance/manager.py | 36 +++++++++++++++++++++---------------
5 files changed, 56 insertions(+), 58 deletions(-)
diff --git a/config.yaml.example b/config.yaml.example
index b2ce5ec..fc2905b 100644
--- a/config.yaml.example
+++ b/config.yaml.example
@@ -4,7 +4,7 @@
# or more instances which contain the onion address of the load balancing
# HS and any authentication data needed to access that HS.
-refresh: 600 # How often to poll for updated descriptors
+REFRESH_INTERVAL: 600 # How often to poll for updated descriptors
services:
- key: /path/to/private_key # 7s4hxwwifcslrus2.onion
instances:
diff --git a/onionbalance/config.py b/onionbalance/config.py
index 6460129..79880d2 100644
--- a/onionbalance/config.py
+++ b/onionbalance/config.py
@@ -1,19 +1,13 @@
# -*- coding: utf-8 -*-
-class Config(object):
- """
- Config object to store the global state
- """
- def __init__(self):
- # Configure some defaults
- self.config = {
- 'replicas': 2,
- 'max_intro_points': 10,
- 'descriptor_validity_period': 24 * 60 * 60,
- 'descriptor_overlap_period': 60 * 60,
- 'descriptor_upload_period': 60 * 60, # Re-upload every hour
- 'refresh': 10 * 60
- }
- # In memory list of hidden services and balancing instances
- self.hidden_services = []
-cfg = Config()
+# Set default configuration options for the management server
+
+REPLICAS = 2
+MAX_INTRO_POINTS = 10
+DESCRIPTOR_VALIDITY_PERIOD = 24 * 60 * 60
+DESCRIPTOR_OVERLAP_PERIOD = 60 * 60
+DESCRIPTOR_UPLOAD_PERIOD = 60 * 60 # Re-upload descriptor every hour
+REFRESH_INTERVAL = 10 * 60
+
+# Store global data about onion services and their instance nodes.
+services = []
diff --git a/onionbalance/eventhandler.py b/onionbalance/eventhandler.py
index 6abdd10..07b2c9b 100644
--- a/onionbalance/eventhandler.py
+++ b/onionbalance/eventhandler.py
@@ -30,18 +30,18 @@ class EventHandler(object):
Update the HS instance object with the data from the new descriptor.
"""
- logger.debug("Received new HS_DESC_CONTENT event for %s" %
+ logger.debug("Received new HS_DESC_CONTENT event for %s.onion" %
desc_content_event.address)
# Make sure the descriptor is not empty
descriptor_text = str(desc_content_event.descriptor).encode('utf-8')
if len(descriptor_text) < 5:
- logger.debug("Empty descriptor received for %s" %
+ logger.debug("Empty descriptor received for %s.onion" %
desc_content_event.address)
return
# Find the HS instance for this descriptor
- for service in config.cfg.hidden_services:
+ for service in config.services:
for instance in service.instances:
if instance.onion_address == desc_content_event.address:
instance.update_descriptor(descriptor_text)
diff --git a/onionbalance/hiddenservice.py b/onionbalance/hiddenservice.py
index c11dff1..7658b5a 100644
--- a/onionbalance/hiddenservice.py
+++ b/onionbalance/hiddenservice.py
@@ -25,7 +25,7 @@ def fetch_all_descriptors(controller):
controller.signal(stem.control.Signal.NEWNYM)
time.sleep(5)
- for service in config.cfg.hidden_services:
+ for service in config.services:
for instance in service.instances:
instance.fetch_descriptor()
@@ -35,7 +35,7 @@ def publish_all_descriptors():
Called periodically to upload new super-descriptors if needed
"""
logger.info("Running periodic descriptor publish")
- for service in config.cfg.hidden_services:
+ for service in config.services:
service.publish()
@@ -87,8 +87,7 @@ class HiddenService(object):
return True
descriptor_age = (datetime.datetime.utcnow() - self.last_uploaded)
- if (descriptor_age.total_seconds() >
- config.cfg.config.get("descriptor_upload_period")):
+ if (descriptor_age.total_seconds() > config.DESCRIPTOR_UPLOAD_PERIOD):
return True
return False
@@ -101,7 +100,7 @@ class HiddenService(object):
time.time(), base64.b32decode(self.onion_address, 1))
# Check if descriptor ID will be changing within the overlap period.
- if seconds_valid < config.cfg.config.get('descriptor_overlap_period'):
+ if seconds_valid < config.DESCRIPTOR_OVERLAP_PERIOD:
return True
return False
@@ -123,14 +122,14 @@ class HiddenService(object):
for instance in self.instances:
if not instance.last_fetched:
- logger.debug("No descriptor fetched for instance '%s' yet. "
- "Skipping!" % instance.onion_address)
+ logger.debug("No descriptor fetched for instance %s.onion "
+ "yet. Skipping!" % instance.onion_address)
continue
# Check if the intro points are too old
intro_age = datetime.datetime.utcnow() - instance.last_fetched
if intro_age.total_seconds() > 60 * 60:
- logger.info("Our introduction points for instance '%s' "
+ logger.info("Our introduction points for instance %s.onion "
"are too old. Skipping!" % instance.onion_address)
continue
@@ -138,11 +137,9 @@ class HiddenService(object):
instance.changed_since_published = False
combined_intro_points.extend(instance.introduction_points)
- # Choose up to `max_intro_points` IPs from the combined set
+ # Choose up to `MAX_INTRO_POINTS` IPs from the combined set
max_introduction_points = min(
- len(combined_intro_points),
- config.cfg.config.get("max_intro_points")
- )
+ len(combined_intro_points), config.MAX_INTRO_POINTS)
choosen_intro_points = random.sample(combined_intro_points,
max_introduction_points)
@@ -150,7 +147,7 @@ class HiddenService(object):
# instances are online and have introduction points included.
random.shuffle(choosen_intro_points)
- logger.debug("Selected %d IPs (of %d) for service '%s'" %
+ logger.debug("Selected %d IPs (of %d) for service %s.onion" %
(len(choosen_intro_points), len(combined_intro_points),
self.onion_address))
@@ -177,7 +174,7 @@ class HiddenService(object):
"""
Create, sign and upload a super-descriptors for this HS
"""
- for replica in range(0, config.cfg.config.get("replicas")):
+ for replica in range(0, config.REPLICAS):
signed_descriptor = self._get_signed_descriptor(
replica=replica, timestamp=timestamp)
@@ -196,7 +193,7 @@ class HiddenService(object):
if (self._intro_points_modified() or
self._descriptor_expiring() or
force):
- logger.info("Publishing new descriptor for '%s'" %
+ logger.info("Publishing new descriptor for service %s.onion" %
self.onion_address)
self._upload_descriptor()
@@ -204,13 +201,13 @@ class HiddenService(object):
# If the descriptor ID will change soon, need to upload under
# the new ID too.
if self._descriptor_id_changing_soon():
- logger.info("Publishing new descriptor for '%s' under "
- "next descriptor ID" % self.onion_address)
+ logger.info("Publishing new descriptor for service %s.onion "
+ "under next descriptor ID" % self.onion_address)
next_time = datetime.datetime.utcnow() + datetime.timedelta(1)
self._upload_descriptor(timestamp=next_time)
else:
- logger.debug("Not publishing a descriptor for '%s'" %
+ logger.debug("Not publishing a descriptor for %s.onion" %
self.onion_address)
@@ -236,7 +233,7 @@ class Instance(object):
"""
Try fetch a fresh descriptor for this back-end HS instance.
"""
- logger.debug("Trying to fetch a descriptor of instance '%s'" %
+ logger.debug("Trying to fetch a descriptor for instance %s.onion" %
self.onion_address)
return descriptor.fetch_descriptor(self.controller, self.onion_address)
@@ -248,7 +245,8 @@ class Instance(object):
points for this HS instance.
"""
- logger.info("Received a descriptor for %s" % self.onion_address)
+ logger.info("Received a descriptor for instance %s.onion" %
+ self.onion_address)
self.last_fetched = datetime.datetime.utcnow()
@@ -262,8 +260,8 @@ class Instance(object):
descriptor_onion_address = util.calc_onion_address(permanent_key)
if self.onion_address != descriptor_onion_address:
- logger.error("Received descriptor for service (%s) did not match "
- "the expected onion address %s" %
+ logger.error("Received a descriptor with address %s.onion that "
+ " did not match the expected onion address %s.onion" %
(descriptor_onion_address, self.onion_address))
return None
diff --git a/onionbalance/manager.py b/onionbalance/manager.py
index 5243946..b62a8b4 100644
--- a/onionbalance/manager.py
+++ b/onionbalance/manager.py
@@ -72,7 +72,12 @@ def main():
Entry point when invoked over the command line.
"""
args = parse_cmd_args()
- config.cfg.config.update(parse_config_file(args.config))
+ config_file_options = parse_config_file(args.config)
+
+ # Update global configuration with options specified in the config file
+ for setting in dir(config):
+ if setting.isupper() and config_file_options.get(setting):
+ setattr(config, setting, config_file_options.get(setting))
logger.setLevel(logging.__dict__[args.verbosity.upper()])
@@ -95,7 +100,7 @@ def main():
else:
logger.debug("Successfully authenticated to the Tor control port")
- # Check that the Tor client has support for HSPOST contorl command
+ # Check that the Tor client supports the HSPOST control port command
if not controller.get_version() >= stem.version.Requirement.HSPOST:
logger.error("A Tor version >= {} is required. You may need to "
"compile Tor from source or install a package from "
@@ -103,24 +108,24 @@ def main():
stem.version.Requirement.HSPOST))
sys.exit(1)
- # Load the keys and configuration for each hidden service
- for service in config.cfg.config.get("services"):
+ # Load the keys and config for each onion service
+ for service in config_file_options.get('services'):
service_key = util.key_decrypt_prompt(service.get("key"))
if not service_key:
- logger.error("Private key could not be imported (%s)" %
+ logger.error("Private key %s could not be loaded" %
args.key)
sys.exit(0)
else:
- # Successfully import the private key
+ # Successfully imported the private key
onion_address = util.calc_onion_address(service_key)
- logger.debug("Loaded private key for service '%s'" %
+ logger.debug("Loaded private key for service %s.onion" %
onion_address)
- # Parse the load balancing instances for this hidden service
+ # Load all instances for the current onion service
instance_config = service.get("instances", [])
if not instance_config:
- logger.error("No load balancing hidden service "
- "specified for %s" % onion_address)
+ logger.error("Could not load and instances for service "
+ "%s.onion" % onion_address)
sys.exit(1)
else:
instances = []
@@ -131,10 +136,10 @@ def main():
authentication_cookie=instance.get("auth")
))
- logger.info("Loaded {} balancing services for {}".format(
+ logger.info("Loaded {} instances for service {}.onion".format(
len(instances), onion_address))
- config.cfg.hidden_services.append(hiddenservice.HiddenService(
+ config.services.append(hiddenservice.HiddenService(
controller=controller,
service_key=service_key,
instances=instances
@@ -147,9 +152,9 @@ def main():
EventType.HS_DESC_CONTENT)
# Schedule descriptor fetch and upload events
- schedule.every(config.cfg.config.get('refresh')).seconds.do(
+ schedule.every(config.REFRESH_INTERVAL).seconds.do(
hiddenservice.fetch_all_descriptors, controller)
- schedule.every(config.cfg.config.get('refresh')).seconds.do(
+ schedule.every(config.REFRESH_INTERVAL).seconds.do(
hiddenservice.publish_all_descriptors)
# Run initial fetch of HS instance descriptors
@@ -161,6 +166,7 @@ def main():
schedule.run_pending()
time.sleep(1)
except KeyboardInterrupt:
- logger.info("Stopping descriptor uploads")
+ logger.info("Keyboard interrupt received. Stopping the "
+ "management server")
return 0
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionbalance.git
More information about the Pkg-privacy-commits
mailing list