From 53577a4c10e8ab2b3002aa324ce66f8da4b45b09 Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 21 Aug 2018 15:01:50 -0500 Subject: [PATCH] work on netcheck and configuration --- onionr/communicator2.py | 3 +++ onionr/onionrdaemontools.py | 11 ++++++++++- onionr/onionrutils.py | 27 +++++++++++++++++++++++--- onionr/static-data/default_config.json | 2 +- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/onionr/communicator2.py b/onionr/communicator2.py index 38ba2692..a575a350 100755 --- a/onionr/communicator2.py +++ b/onionr/communicator2.py @@ -27,6 +27,8 @@ from defusedxml import minidom class OnionrCommunicatorDaemon: def __init__(self, debug, developmentMode): + self.isOnline = True # Assume we're connected to the internet + # list of timer instances self.timers = [] @@ -93,6 +95,7 @@ class OnionrCommunicatorDaemon: OnionrCommunicatorTimers(self, self.clearOfflinePeer, 58) OnionrCommunicatorTimers(self, self.lookupKeys, 60, requiresPeer=True) OnionrCommunicatorTimers(self, self.lookupAdders, 60, requiresPeer=True) + netCheckTimer = OnionrCommunicatorTimers(self, self.daemonTools.netCheck, 600) announceTimer = OnionrCommunicatorTimers(self, self.daemonTools.announceNode, 305, requiresPeer=True, maxThreads=1) cleanupTimer = OnionrCommunicatorTimers(self, self.peerCleanup, 300, requiresPeer=True) diff --git a/onionr/onionrdaemontools.py b/onionr/onionrdaemontools.py index 8410cb80..36264600 100644 --- a/onionr/onionrdaemontools.py +++ b/onionr/onionrdaemontools.py @@ -53,4 +53,13 @@ class DaemonTools: if self.daemon._core._utils.doPostRequest(url, data) == 'Success': retData = True self.daemon.decrementThreadCount('announceNode') - return retData \ No newline at end of file + return retData + + def netCheck(self): + '''Check if we are connected to the internet or not when we can't connect to any peers''' + if len(self.daemon.onlinePeers) != 0: + if not self.daemon._core._utils.checkNetwork(): + logger.warn('Network check failed, are you connected to the internet?') + self.daemon.isOnline = False + + self.daemon.decrementThreadCount('netCheck') \ No newline at end of file diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 6d22992c..6fffbee8 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -131,8 +131,12 @@ class OnionrUtils: if not config.get('tor.v3onions') and len(adder) == 62: continue if self._core.addAddress(adder): - logger.info('Added %s to db.' % adder, timestamp = True) - retVal = True + # Check if we have the maxmium amount of allowed stored peers + if config.get('peers.maxStoredPeers') > len(self._core.listAdders): + logger.info('Added %s to db.' % adder, timestamp = True) + retVal = True + else: + logger.warn('Reached the maximum amount of peers in the net database as allowed by your config.') else: pass #logger.debug('%s is either our address or already in our DB' % adder) @@ -630,6 +634,23 @@ class OnionrUtils: except AttributeError: pass return data + + def checkNetwork(self): + '''Check if we are connected to the internet (through Tor)''' + retData = False + connectURLs = [] + try: + with open('static-data/connect-check.txt', 'r') as connectTest: + connectURLs = connectTest.read().split(',') + + for url in connectURLs: + if self.doGetRequest(url) != False: + retData = True + break + + except FileNotFoundError: + pass + return retData def size(path='.'): ''' @@ -655,4 +676,4 @@ def humanSize(num, suffix='B'): if abs(num) < 1024.0: return "%.1f %s%s" % (num, unit, suffix) num /= 1024.0 - return "%.1f %s%s" % (num, 'Yi', suffix) + return "%.1f %s%s" % (num, 'Yi', suffix) \ No newline at end of file diff --git a/onionr/static-data/default_config.json b/onionr/static-data/default_config.json index 5458db4a..86d44499 100644 --- a/onionr/static-data/default_config.json +++ b/onionr/static-data/default_config.json @@ -58,7 +58,7 @@ }, "peers":{ "minimumScore": -100, - "maxStoredPeers": 500, + "maxStoredPeers": 5000, "maxConnect": 5 } }