renamed onionr dir and bugfixes/linting progress
This commit is contained in:
parent
2b996da17f
commit
720efe4fca
226 changed files with 179 additions and 142 deletions
4
src/onionrpeers/__init__.py
Executable file
4
src/onionrpeers/__init__.py
Executable file
|
@ -0,0 +1,4 @@
|
|||
from . import scoresortedpeerlist, peercleanup, peerprofiles
|
||||
get_score_sorted_peer_list = scoresortedpeerlist.get_score_sorted_peer_list
|
||||
peer_cleanup = peercleanup.peer_cleanup
|
||||
PeerProfiles = peerprofiles.PeerProfiles
|
56
src/onionrpeers/peercleanup.py
Normal file
56
src/onionrpeers/peercleanup.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
'''
|
||||
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
|
||||
from onionrutils import epoch
|
||||
from . import scoresortedpeerlist, peerprofiles
|
||||
from onionrblocks import onionrblacklist
|
||||
import config
|
||||
from coredb import keydb
|
||||
def peer_cleanup():
|
||||
'''Removes peers who have been offline too long or score too low'''
|
||||
logger.info('Cleaning peers...')
|
||||
blacklist = onionrblacklist.OnionrBlackList()
|
||||
adders = scoresortedpeerlist.get_score_sorted_peer_list()
|
||||
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.PeerProfiles(address).score < min_score:
|
||||
keydb.removekeys.remove_address(address)
|
||||
try:
|
||||
if (int(epoch.get_epoch()) - int(keydb.transportinfo.get_address_info(address, 'lastConnect'))) >= 600:
|
||||
expireTime = 600
|
||||
else:
|
||||
expireTime = 86400
|
||||
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
|
||||
blacklist.deleteExpired(dataType=1)
|
81
src/onionrpeers/peerprofiles.py
Normal file
81
src/onionrpeers/peerprofiles.py
Normal file
|
@ -0,0 +1,81 @@
|
|||
'''
|
||||
Onionr - Private P2P Communication
|
||||
|
||||
This file contains both the PeerProfiles class for network profiling of Onionr nodes
|
||||
'''
|
||||
'''
|
||||
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/>.
|
||||
'''
|
||||
from coredb import keydb
|
||||
from onionrutils import epoch
|
||||
from onionrutils import stringvalidators
|
||||
from onionrblocks import onionrblacklist
|
||||
import onionrexceptions
|
||||
|
||||
UPDATE_DELAY = 300
|
||||
|
||||
class PeerProfiles:
|
||||
'''
|
||||
PeerProfiles
|
||||
'''
|
||||
def __init__(self, address):
|
||||
if not stringvalidators.validate_transport(address): raise onionrexceptions.InvalidAddress
|
||||
self.address = address # node address
|
||||
self.score = None
|
||||
self.friendSigCount = 0
|
||||
self.success = 0
|
||||
self.failure = 0
|
||||
self.connectTime = None
|
||||
|
||||
self.loadScore()
|
||||
self.getConnectTime()
|
||||
|
||||
self.last_updated = {'connect_time': UPDATE_DELAY, 'score': UPDATE_DELAY} # Last time a given value was updated
|
||||
|
||||
if not address in keydb.listkeys.list_adders() and not onionrblacklist.OnionrBlackList().inBlacklist(address):
|
||||
keydb.addkeys.add_address(address)
|
||||
|
||||
def loadScore(self):
|
||||
'''Load the node's score from the database'''
|
||||
try:
|
||||
self.success = int(keydb.transportinfo.get_address_info(self.address, 'success'))
|
||||
except (TypeError, ValueError) as e:
|
||||
self.success = 0
|
||||
self.score = self.success
|
||||
|
||||
def getConnectTime(self):
|
||||
"""set the connectTime variable for when we last connected to them, using the db value"""
|
||||
try:
|
||||
self.connectTime = int(keydb.transportinfo.get_address_info(self.address, 'lastConnect'))
|
||||
except (KeyError, ValueError, TypeError) as e:
|
||||
pass
|
||||
else:
|
||||
return self.connectTime
|
||||
|
||||
def update_connect_time(self):
|
||||
if epoch.get_epoch() - self.last_updated['connect_time'] >= UPDATE_DELAY:
|
||||
self.last_updated['connect_time'] = epoch.get_epoch()
|
||||
keydb.transportinfo.set_address_info(self.address, 'lastConnect', epoch.get_epoch())
|
||||
|
||||
def saveScore(self):
|
||||
'''Save the node's score to the database'''
|
||||
if epoch.get_epoch() - self.last_updated['score'] >= UPDATE_DELAY:
|
||||
self.last_updated['score'] = epoch.get_epoch()
|
||||
keydb.transportinfo.set_address_info(self.address, 'success', self.score)
|
||||
return
|
||||
|
||||
def addScore(self, toAdd):
|
||||
'''Add to the peer's score (can add negative)'''
|
||||
self.score += toAdd
|
||||
self.saveScore()
|
39
src/onionrpeers/scoresortedpeerlist.py
Normal file
39
src/onionrpeers/scoresortedpeerlist.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
'''
|
||||
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/>.
|
||||
'''
|
||||
from . import peerprofiles
|
||||
from coredb import keydb
|
||||
def get_score_sorted_peer_list():
|
||||
peer_list = keydb.listkeys.list_adders()
|
||||
peer_scores = {}
|
||||
peer_times = {}
|
||||
|
||||
for address in peer_list:
|
||||
# Load peer's profiles into a list
|
||||
profile = peerprofiles.PeerProfiles(address)
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue