diff --git a/onionr/config.py b/onionr/config.py index 47373f8b..98085828 100755 --- a/onionr/config.py +++ b/onionr/config.py @@ -19,11 +19,9 @@ ''' import os, json, logger - +from utils import identifyhome # set data dir -dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) -if not dataDir.endswith('/'): - dataDir += '/' +dataDir = identifyhome.identify_home() _configfile = os.path.abspath(dataDir + 'config.json') _config = {} diff --git a/onionr/core.py b/onionr/core.py index 195a8d04..b523c5ff 100755 --- a/onionr/core.py +++ b/onionr/core.py @@ -30,6 +30,7 @@ import dbcreator, onionrstorage, serializeddata, subprocesspow from etc import onionrvalues, powchoice from onionrutils import localcommand, stringvalidators, bytesconverter, epoch from onionrutils import blockmetadata +from utils import identifyhome import storagecounter class Core: @@ -38,9 +39,7 @@ class Core: Initialize Core Onionr library ''' # set data dir - self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) - if not self.dataDir.endswith('/'): - self.dataDir += '/' + self.dataDir = identifyhome.identify_home() try: self.usageFile = self.dataDir + 'disk-usage.txt' diff --git a/onionr/logger/readline.py b/onionr/logger/readline.py index 9929a30e..73ed4f60 100644 --- a/onionr/logger/readline.py +++ b/onionr/logger/readline.py @@ -29,7 +29,7 @@ def readline(message = ''): color = colors.fg.green + colors.bold output = colors.reset + str(color) + '... ' + colors.reset + str(message) + colors.reset - if not settings.get_settings() & USE_ANSI: + if not settings.get_settings() & settings.USE_ANSI: output = colors.filter(output) sys.stdout.write(output) diff --git a/onionr/logger/settings.py b/onionr/logger/settings.py index 62bad00b..98f11357 100644 --- a/onionr/logger/settings.py +++ b/onionr/logger/settings.py @@ -18,7 +18,9 @@ along with this program. If not, see . ''' import os -data_home = os.environ.get('DATA_DIR', os.environ.get('DATA_DIR', 'data')) +from utils import identifyhome + +data_home = os.environ.get('ONIONR_LOG_DIR', identifyhome.identify_home()) # Use the bitwise operators to merge these settings USE_ANSI = 0b100 if os.name == 'nt': diff --git a/onionr/netcontroller/netcontrol.py b/onionr/netcontroller/netcontrol.py index 28532ed5..e0b54804 100644 --- a/onionr/netcontroller/netcontrol.py +++ b/onionr/netcontroller/netcontrol.py @@ -20,6 +20,7 @@ import os, sys, base64, subprocess, signal import config, logger from . import getopenport +from utils import identifyhome config.reload() class NetController: ''' @@ -28,9 +29,7 @@ class NetController: def __init__(self, hsPort, apiServerIP='127.0.0.1'): # set data dir - self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) - if not self.dataDir.endswith('/'): - self.dataDir += '/' + self.dataDir = identifyhome.identify_home() self.torConfigLocation = self.dataDir + 'torrc' self.readyState = False diff --git a/onionr/onionr.py b/onionr/onionr.py index d67943cf..f9b361ab 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -43,11 +43,12 @@ from netcontroller import NetController from onionrblockapi import Block import onionrproofs, onionrexceptions, communicator, setupconfig import onionrcommands as commands # Many command definitions are here +from utils import identifyhome try: from urllib3.contrib.socks import SOCKSProxyManager except ImportError: - raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)") + raise ImportError("You need the PySocks module (for use with socks5 proxy to use Tor)") class Onionr: def __init__(self): @@ -67,7 +68,7 @@ class Onionr: pass # set data dir - self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) + self.dataDir = identifyhome.identify_home() if not self.dataDir.endswith('/'): self.dataDir += '/' @@ -97,7 +98,8 @@ class Onionr: if not os.path.exists(plugins.get_plugin_data_folder(name)): try: os.mkdir(plugins.get_plugin_data_folder(name)) - except: + except Exception as e: + logger.warn('Error enabling plugin: ' + str(e)) plugins.disable(name, onionr = self, stop_event = False) self.communicatorInst = None @@ -139,7 +141,8 @@ class Onionr: command = '' finally: self.execute(command) - + + os.chdir(self.userRunDir) return def exitSigterm(self, signum, frame): @@ -175,7 +178,7 @@ class Onionr: def get_hostname(self): try: - with open('./' + self.dataDir + 'hs/hostname', 'r') as hostname: + with open(self.dataDir + 'hs/hostname', 'r') as hostname: return hostname.read().strip() except FileNotFoundError: return "Not Generated" diff --git a/onionr/onionrplugins.py b/onionr/onionrplugins.py index 9698f5d1..a76fc92b 100755 --- a/onionr/onionrplugins.py +++ b/onionr/onionrplugins.py @@ -19,11 +19,9 @@ ''' import os, re, importlib import onionrevents as events, config, logger - +from utils import identifyhome # set data dir -dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) -if not dataDir.endswith('/'): - dataDir += '/' +dataDir = identifyhome.identify_home() _pluginsfolder = dataDir + 'plugins/' _instances = dict() @@ -75,15 +73,15 @@ def enable(name, onionr = None, start_event = True): return False else: enabled_plugins.append(name) - config.set('plugins.enabled', enabled_plugins, True) - + config.set('plugins.enabled', enabled_plugins, savefile=True) + if start_event is True: start(name) return True else: return False else: - logger.error('Failed to enable plugin \"%s\", disabling plugin.' % name) + logger.error('Failed to enable plugin \"%s\", disabling plugin.' % name, terminal=True) disable(name) return False @@ -245,7 +243,7 @@ def get_plugin_data_folder(name, absolute = True): Returns the location of a plugin's data folder ''' - return get_plugins_folder(name, absolute) + dataDir + return get_plugins_folder(name, absolute) def check(): ''' diff --git a/onionr/utils/identifyhome.py b/onionr/utils/identifyhome.py new file mode 100644 index 00000000..b62fdf22 --- /dev/null +++ b/onionr/utils/identifyhome.py @@ -0,0 +1,39 @@ +''' + Onionr - Private P2P Communication + + Identify a data directory for Onionr +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +import os, platform + +def identify_home(): + + path = os.environ.get('ONIONR_HOME', None) + if path is None: + system = platform.system() + if system == 'Linux': + path = os.path.expanduser('~') + '/.local/share/onionr/' + elif system == 'Windows': + path = os.path.expanduser('~') + '\\AppData\\Local\\onionr\\' + elif system == 'Darwin': + path = os.path.expanduser('~' + '/Library/Application Support/onionr/') + else: + path = 'data/' + else: + path = os.path.abspath(path) + if not path.endswith('/'): + path += '/' + return path \ No newline at end of file