From 021fa87f3020a88371c30d0012d246feb9029bef Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 12 Jul 2019 12:21:08 -0500 Subject: [PATCH] refactored onionrpeers into a module --- onionr/communicator/__init__.py | 2 +- onionr/communicatorutils/connectnewpeers.py | 2 +- .../__init__.py} | 60 ++----------------- onionr/onionrpeers/peercleanup.py | 52 ++++++++++++++++ onionr/onionrpeers/scoresortedpeerlist.py | 37 ++++++++++++ 5 files changed, 95 insertions(+), 58 deletions(-) rename onionr/{onionrpeers.py => onionrpeers/__init__.py} (50%) create mode 100644 onionr/onionrpeers/peercleanup.py create mode 100644 onionr/onionrpeers/scoresortedpeerlist.py diff --git a/onionr/communicator/__init__.py b/onionr/communicator/__init__.py index 55daf1c8..ea471bf1 100755 --- a/onionr/communicator/__init__.py +++ b/onionr/communicator/__init__.py @@ -210,7 +210,7 @@ class OnionrCommunicatorDaemon: def peerCleanup(self): '''This just calls onionrpeers.cleanupPeers, which removes dead or bad peers (offline too long, too slow)''' - onionrpeers.peerCleanup(self._core) + onionrpeers.peer_cleanup(self._core) self.decrementThreadCount('peerCleanup') def getPeerProfileInstance(self, peer): diff --git a/onionr/communicatorutils/connectnewpeers.py b/onionr/communicatorutils/connectnewpeers.py index fd333d7a..35cd3000 100755 --- a/onionr/communicatorutils/connectnewpeers.py +++ b/onionr/communicatorutils/connectnewpeers.py @@ -36,7 +36,7 @@ def connect_new_peer_to_communicator(comm_inst, peer='', useBootstrap=False): peerList = comm_inst._core.listAdders() mainPeerList = comm_inst._core.listAdders() - peerList = onionrpeers.getScoreSortedPeerList(comm_inst._core) + peerList = onionrpeers.get_score_sorted_peer_list(comm_inst._core) # If we don't have enough peers connected or random chance, select new peers to try if len(peerList) < 8 or secrets.randbelow(4) == 3: diff --git a/onionr/onionrpeers.py b/onionr/onionrpeers/__init__.py similarity index 50% rename from onionr/onionrpeers.py rename to onionr/onionrpeers/__init__.py index d0c0c5cb..3d3f3f38 100755 --- a/onionr/onionrpeers.py +++ b/onionr/onionrpeers/__init__.py @@ -20,7 +20,11 @@ import sqlite3 import core, config, logger from onionrutils import epoch +from . import scoresortedpeerlist, peercleanup +get_score_sorted_peer_list = scoresortedpeerlist.get_score_sorted_peer_list +peer_cleanup = peercleanup.peer_cleanup config.reload() + class PeerProfiles: ''' PeerProfiles @@ -64,59 +68,3 @@ class PeerProfiles: '''Add to the peer's score (can add negative)''' self.score += toAdd self.saveScore() - -def getScoreSortedPeerList(coreInst): - if not type(coreInst is core.Core): - raise TypeError('coreInst must be instance of core.Core') - - peerList = coreInst.listAdders() - peerScores = {} - peerTimes = {} - - for address in peerList: - # Load peer's profiles into a list - profile = PeerProfiles(address, coreInst) - peerScores[address] = profile.score - if not isinstance(profile.connectTime, type(None)): - peerTimes[address] = profile.connectTime - else: - peerTimes[address] = 9000 - - # Sort peers by their score, greatest to least, and then last connected time - peerList = sorted(peerScores, key=peerScores.get, reverse=True) - peerList = sorted(peerTimes, key=peerTimes.get, reverse=True) - return peerList - -def peerCleanup(coreInst): - '''Removes peers who have been offline too long or score too low''' - if not type(coreInst is core.Core): - raise TypeError('coreInst must be instance of core.Core') - - logger.info('Cleaning peers...') - - adders = getScoreSortedPeerList(coreInst) - adders.reverse() - - if len(adders) > 1: - - minScore = int(config.get('peers.minimum_score', -100)) - maxPeers = int(config.get('peers.max_stored', 5000)) - - for address in adders: - # Remove peers that go below the negative score - if PeerProfiles(address, coreInst).score < minScore: - coreInst.removeAddress(address) - try: - if (int(epoch.get_epoch()) - int(coreInst.getPeerInfo(address, 'dateSeen'))) >= 600: - expireTime = 600 - else: - expireTime = 86400 - coreInst._blacklist.addToDB(address, dataType=1, expire=expireTime) - except sqlite3.IntegrityError: #TODO just make sure its not a unique constraint issue - pass - except ValueError: - pass - logger.warn('Removed address ' + address + '.') - - # Unban probably not malicious peers TODO improve - coreInst._blacklist.deleteExpired(dataType=1) diff --git a/onionr/onionrpeers/peercleanup.py b/onionr/onionrpeers/peercleanup.py new file mode 100644 index 00000000..0ac9e033 --- /dev/null +++ b/onionr/onionrpeers/peercleanup.py @@ -0,0 +1,52 @@ +''' + Onionr - Private P2P Communication + + Cleanup the peer database +''' +''' + 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 sqlite3 +import logger +def peer_cleanup(core_inst): + '''Removes peers who have been offline too long or score too low''' + config = core_inst.config + logger.info('Cleaning peers...') + + adders = get_score_sorted_peer_list(core_inst) + adders.reverse() + + if len(adders) > 1: + + min_score = int(config.get('peers.minimum_score', -100)) + max_peers = int(config.get('peers.max_stored', 5000)) + + for address in adders: + # Remove peers that go below the negative score + if PeerProfiles(address, core_inst).score < min_score: + core_inst.removeAddress(address) + try: + if (int(epoch.get_epoch()) - int(core_inst.getPeerInfo(address, 'dateSeen'))) >= 600: + expireTime = 600 + else: + expireTime = 86400 + core_inst._blacklist.addToDB(address, dataType=1, expire=expireTime) + except sqlite3.IntegrityError: #TODO just make sure its not a unique constraint issue + pass + except ValueError: + pass + logger.warn('Removed address ' + address + '.') + + # Unban probably not malicious peers TODO improve + core_inst._blacklist.deleteExpired(dataType=1) \ No newline at end of file diff --git a/onionr/onionrpeers/scoresortedpeerlist.py b/onionr/onionrpeers/scoresortedpeerlist.py new file mode 100644 index 00000000..22696766 --- /dev/null +++ b/onionr/onionrpeers/scoresortedpeerlist.py @@ -0,0 +1,37 @@ +''' + Onionr - Private P2P Communication + + Return a reliability score sorted list of peers +''' +''' + 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 . +''' +def get_score_sorted_peer_list(coreInst): + peer_list = coreInst.listAdders() + peer_scores = {} + peer_times = {} + + for address in peer_list: + # Load peer's profiles into a list + profile = PeerProfiles(address, coreInst) + peer_scores[address] = profile.score + if not isinstance(profile.connectTime, type(None)): + peer_times[address] = profile.connectTime + else: + peer_times[address] = 9000 + + # Sort peers by their score, greatest to least, and then last connected time + peer_list = sorted(peer_scores, key=peer_scores.get, reverse=True) + peer_list = sorted(peer_times, key=peer_times.get, reverse=True) + return peer_list \ No newline at end of file