refactored onionrpeers into a module

master
Kevin Froman 2019-07-12 12:21:08 -05:00
parent ec2f24b257
commit 021fa87f30
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
5 changed files with 95 additions and 58 deletions

View File

@ -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):

View File

@ -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:

View File

@ -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)

View File

@ -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 <https://www.gnu.org/licenses/>.
'''
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)

View File

@ -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 <https://www.gnu.org/licenses/>.
'''
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