From 274505a51f26d133e5d55f10cbae2ac6e5570a8f Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 24 Jul 2019 11:32:23 -0500 Subject: [PATCH] progress in removing core --- onionr/apiservers/public/__init__.py | 6 +++++- onionr/communicator/__init__.py | 3 +-- onionr/communicator/bootstrappeers.py | 2 +- onionr/communicatorutils/connectnewpeers.py | 3 ++- onionr/communicatorutils/lookupadders.py | 2 ++ onionr/coredb/keydb/addkeys.py | 2 +- onionr/httpapi/security/public.py | 2 ++ onionr/onionr.py | 6 +++--- onionr/setupconfig.py | 13 ++----------- onionr/utils/gettransports.py | 19 +++++++++---------- 10 files changed, 28 insertions(+), 30 deletions(-) diff --git a/onionr/apiservers/public/__init__.py b/onionr/apiservers/public/__init__.py index 00306022..4b73fcd3 100644 --- a/onionr/apiservers/public/__init__.py +++ b/onionr/apiservers/public/__init__.py @@ -34,7 +34,11 @@ class PublicAPI: self.i2pEnabled = config.get('i2p.host', False) self.hideBlocks = [] # Blocks to be denied sharing self.host = apiutils.setbindip.set_bind_IP(filepaths.public_API_host_file) - self.torAdder = gettransports.get()[0] + transports = [] + while len(transports) == 0: + transports = gettransports.get() + time.sleep(0.3) + self.torAdder = transports[0] self.bindPort = config.get('client.public.port') self.lastRequest = 0 self.hitCount = 0 # total rec requests to public api since server started diff --git a/onionr/communicator/__init__.py b/onionr/communicator/__init__.py index a4158ed4..1a0e83ee 100755 --- a/onionr/communicator/__init__.py +++ b/onionr/communicator/__init__.py @@ -39,12 +39,11 @@ class OnionrCommunicatorDaemon: def __init__(self, onionrInst, proxyPort, developmentMode=config.get('general.dev_mode', False)): onionrInst.communicatorInst = self # configure logger and stuff - onionr.Onionr.setupConfig('data/', self = self) + onionr.Onionr.setupConfig(onionrInst) self.onionrInst = onionrInst self.config = config self.storage_counter = storagecounter.StorageCounter() self.proxyPort = proxyPort - self.hsAddress = gettransports.get()[0] self.isOnline = True # Assume we're connected to the internet # list of timer instances diff --git a/onionr/communicator/bootstrappeers.py b/onionr/communicator/bootstrappeers.py index 243cec0e..12bd0090 100644 --- a/onionr/communicator/bootstrappeers.py +++ b/onionr/communicator/bootstrappeers.py @@ -25,6 +25,6 @@ def add_bootstrap_list_to_peer_list(comm_inst, peerList): Add the bootstrap list to the peer list (no duplicates) ''' for i in bootstrap_peers: - if i not in peerList and i not in comm_inst.offlinePeers and i != gettransports.get()[0] and len(str(i).strip()) > 0: + if i not in peerList and i not in comm_inst.offlinePeers and not i in gettransports.get() and len(str(i).strip()) > 0: peerList.append(i) keydb.addkeys.add_address(i) \ No newline at end of file diff --git a/onionr/communicatorutils/connectnewpeers.py b/onionr/communicatorutils/connectnewpeers.py index fa7fe17c..ccef9afc 100755 --- a/onionr/communicatorutils/connectnewpeers.py +++ b/onionr/communicatorutils/connectnewpeers.py @@ -19,7 +19,7 @@ ''' import time, sys, secrets import onionrexceptions, logger, onionrpeers -from utils import networkmerger +from utils import networkmerger, gettransports from onionrutils import stringvalidators, epoch from communicator import peeraction, bootstrappeers from coredb import keydb @@ -27,6 +27,7 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False): config = comm_inst.config retData = False tried = comm_inst.offlinePeers + transports = gettransports.get() if peer != '': if stringvalidators.validate_transport(peer): peerList = [peer] diff --git a/onionr/communicatorutils/lookupadders.py b/onionr/communicatorutils/lookupadders.py index d9a7a345..4f0ac9ed 100755 --- a/onionr/communicatorutils/lookupadders.py +++ b/onionr/communicatorutils/lookupadders.py @@ -26,6 +26,8 @@ def lookup_new_peer_transports_with_communicator(comm_inst): logger.info('Looking up new addresses...') tryAmount = 1 newPeers = [] + if len(transports) == 0: + transports = list(gettransports.get()) for i in range(tryAmount): # Download new peer address list from random online peers if len(newPeers) > 10000: diff --git a/onionr/coredb/keydb/addkeys.py b/onionr/coredb/keydb/addkeys.py index cee465c6..f62b10cf 100644 --- a/onionr/coredb/keydb/addkeys.py +++ b/onionr/coredb/keydb/addkeys.py @@ -65,7 +65,7 @@ def add_address(address): if type(address) is None or len(address) == 0: return False if stringvalidators.validate_transport(address): - if address == gettransports.get()[0]: + if address in gettransports.get(): return False conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) c = conn.cursor() diff --git a/onionr/httpapi/security/public.py b/onionr/httpapi/security/public.py index 6a883854..4a42dd6e 100644 --- a/onionr/httpapi/security/public.py +++ b/onionr/httpapi/security/public.py @@ -31,6 +31,8 @@ class PublicAPISecurity: def validate_request(): '''Validate request has the correct hostname''' # If high security level, deny requests to public (HS should be disabled anyway for Tor, but might not be for I2P) + if len(transports) == 0: + transports = list(gettransports.get()) if public_api.config.get('general.security_level', default=1) > 0: abort(403) if request.host not in transports: diff --git a/onionr/onionr.py b/onionr/onionr.py index d2d04514..4d2d5886 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -76,7 +76,7 @@ class Onionr: self.dataDir += '/' # Load global configuration data - data_exists = Onionr.setupConfig(self.dataDir, self) + data_exists = Onionr.setupConfig(self) if netcontroller.tor_binary() is None: logger.error('Tor is not installed', terminal=True) @@ -149,8 +149,8 @@ class Onionr: def exitSigterm(self, signum, frame): self.killed = True - def setupConfig(dataDir, self = None): - return setupconfig.setup_config(dataDir, self) + def setupConfig(self): + return setupconfig.setup_config(self) def cmdHeader(self): if len(sys.argv) >= 3: diff --git a/onionr/setupconfig.py b/onionr/setupconfig.py index 3919ecff..09242815 100755 --- a/onionr/setupconfig.py +++ b/onionr/setupconfig.py @@ -21,10 +21,7 @@ import os, json import config, logger from logger.settings import * -def setup_config(dataDir, o_inst = None): - data_exists = os.path.exists(dataDir) - if not data_exists: - os.mkdir(dataDir) +def setup_config(o_inst = None): config.reload() if not os.path.exists(config._configfile): @@ -32,10 +29,6 @@ def setup_config(dataDir, o_inst = None): # this is the default config, it will be overwritten if a config file already exists. Else, it saves it with open('static-data/default_config.json', 'r') as configReadIn: config.set_config(json.loads(configReadIn.read())) - else: - # the default config file doesn't exist, try hardcoded config - logger.warn('Default configuration file does not exist, switching to hardcoded fallback configuration!') - config.set_config({'dev_mode': True, 'log': {'file': {'output': True, 'path': dataDir + 'output.log'}, 'console': {'output': True, 'color': True}}}) config.save() @@ -84,6 +77,4 @@ def setup_config(dataDir, o_inst = None): if verbosity in map: set_level(map[verbosity]) else: - logger.warn('Verbosity level %s is not valid, using default verbosity.' % verbosity) - - return data_exists \ No newline at end of file + logger.warn('Verbosity level %s is not valid, using default verbosity.' % verbosity) \ No newline at end of file diff --git a/onionr/utils/gettransports.py b/onionr/utils/gettransports.py index f0ca2493..f5fd823e 100644 --- a/onionr/utils/gettransports.py +++ b/onionr/utils/gettransports.py @@ -4,15 +4,14 @@ files = [filepaths.tor_hs_address_file] def get(): transports = [] - while len(transports) == 0: - for file in files: - try: - with open(file, 'r') as transport_file: - transports.append(transport_file.read().strip()) - except FileNotFoundError: - transports.append('') - else: - break + for file in files: + try: + with open(file, 'r') as transport_file: + transports.append(transport_file.read().strip()) + except FileNotFoundError: + pass else: - time.sleep(1) + break + else: + time.sleep(1) return list(transports) \ No newline at end of file