Add debug logging to Onion, OnionShare, and Settings objects

This commit is contained in:
Micah Lee 2017-05-16 11:23:18 -07:00
parent 7003349873
commit 9c166a07d2
No known key found for this signature in database
GPG key ID: 403C2657CD994F73
5 changed files with 39 additions and 3 deletions

View file

@ -82,7 +82,7 @@ def main(cwd=None):
# Start the onionshare app
try:
app = OnionShare(onion, debug, local_only, stay_open)
app = OnionShare(onion, local_only, stay_open)
app.set_stealth(stealth)
app.start_onion_service()
except KeyboardInterrupt:

View file

@ -21,13 +21,18 @@ import sys, os, inspect, hashlib, base64, platform, zipfile, tempfile, math, tim
from random import SystemRandom
debug = False
def log(module, func, msg):
def log(module, func, msg=None):
"""
If debug mode is on, log error messages to stdout
"""
global debug
if debug:
print("[{}.{}] {}".format(module, func, msg))
timestamp = time.strftime("%b %d %Y %X")
final_msg = "[{}] {}.{}".format(timestamp, module, func)
if msg:
final_msg = '{}: {}'.format(final_msg, msg)
print(final_msg)
def set_debug(new_debug):
global debug

View file

@ -120,6 +120,8 @@ class Onion(object):
is necessary for status updates to reach the GUI.
"""
def __init__(self):
common.log('Onion', '__init__')
self.stealth = False
self.service_id = None
@ -138,6 +140,8 @@ class Onion(object):
self.tor_proc = None
def connect(self, settings=False, tor_status_update_func=None):
common.log('Onion', 'connect')
# Either use settings that are passed in, or load them from disk
if settings:
self.settings = settings
@ -349,6 +353,8 @@ class Onion(object):
Start a onion service on port 80, pointing to the given port, and
return the onion hostname.
"""
common.log('Onion', 'start_onion_service')
self.auth_string = None
if not self.supports_ephemeral:
raise TorTooOld(strings._('error_ephemeral_not_supported'))
@ -386,6 +392,8 @@ class Onion(object):
"""
Stop onion services that were created earlier. If there's a tor subprocess running, kill it.
"""
common.log('Onion', 'cleanup')
# Cleanup the ephemeral onion service
if self.service_id:
try:
@ -406,6 +414,8 @@ class Onion(object):
"""
Returns a (address, port) tuple for the Tor SOCKS port
"""
common.log('Onion', 'get_tor_socks_port')
if self.settings.get('connection_type') == 'bundled':
return ('127.0.0.1', self.tor_socks_port)
elif self.settings.get('connection_type') == 'automatic':

View file

@ -28,6 +28,8 @@ class OnionShare(object):
start_onion_service and it will do the magic.
"""
def __init__(self, onion, local_only=False, stay_open=False):
common.log('OnionShare', '__init__')
# The Onion object
self.onion = onion
@ -45,6 +47,8 @@ class OnionShare(object):
self.stay_open = stay_open
def set_stealth(self, stealth):
common.log('OnionShare', 'set_stealth', 'stealth={}'.format(stealth))
self.stealth = stealth
self.onion.stealth = stealth
@ -52,6 +56,8 @@ class OnionShare(object):
"""
Start the onionshare onion service.
"""
common.log('OnionShare', 'start_onion_service')
# Choose a random port
self.port = common.get_available_port(17600, 17650)
@ -68,6 +74,8 @@ class OnionShare(object):
"""
Shut everything down and clean up temporary files, etc.
"""
common.log('OnionShare', 'cleanup')
# cleanup files
for filename in self.cleanup_filenames:
if os.path.isfile(filename):

View file

@ -30,6 +30,8 @@ class Settings(object):
settings.
"""
def __init__(self):
common.log('Settings', '__init__')
self.filename = self.build_filename()
# These are the default settings. They will get overwritten when loading from disk
@ -56,6 +58,8 @@ class Settings(object):
If there are any missing settings from self._settings, replace them with
their default values.
"""
common.log('Settings', 'fill_in_defaults')
for key in self.default_settings:
if key not in self._settings:
self._settings[key] = self.default_settings[key]
@ -64,6 +68,8 @@ class Settings(object):
"""
Returns the path of the settings file.
"""
common.log('Settings', 'build_filename')
p = platform.system()
if p == 'Windows':
appdata = os.environ['APPDATA']
@ -77,6 +83,8 @@ class Settings(object):
"""
Load the settings from file.
"""
common.log('Settings', 'load')
# If the settings file exists, load it
if os.path.exists(self.filename):
try:
@ -89,6 +97,8 @@ class Settings(object):
"""
Save settings to file.
"""
common.log('Settings', 'save')
try:
os.makedirs(os.path.dirname(self.filename))
except:
@ -97,9 +107,12 @@ class Settings(object):
print(strings._('settings_saved').format(self.filename))
def get(self, key):
common.log('Settings', 'get', 'key={} (val={})'.format(key, self._settings[key]))
return self._settings[key]
def set(self, key, val):
common.log('Settings', 'set', 'key={}, val={}'.format(key, val))
# If typecasting int values fails, fallback to default values
if key == 'control_port_port' or key == 'socks_port':
try: