2018-06-12 07:34:33 +00:00
|
|
|
'''
|
2019-06-16 06:06:32 +00:00
|
|
|
Onionr - Private P2P Communication
|
2018-06-12 07:34:33 +00:00
|
|
|
|
2018-07-27 03:07:50 +00:00
|
|
|
This file contains both the PeerProfiles class for network profiling of Onionr nodes
|
2018-06-12 07:34:33 +00:00
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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/>.
|
2018-07-27 03:07:50 +00:00
|
|
|
'''
|
2019-06-12 00:05:15 +00:00
|
|
|
import sqlite3
|
|
|
|
import core, config, logger
|
2019-06-25 23:07:35 +00:00
|
|
|
from onionrutils import epoch
|
2019-06-16 06:06:32 +00:00
|
|
|
config.reload()
|
2018-07-27 03:07:50 +00:00
|
|
|
class PeerProfiles:
|
|
|
|
'''
|
|
|
|
PeerProfiles
|
|
|
|
'''
|
|
|
|
def __init__(self, address, coreInst):
|
|
|
|
self.address = address # node address
|
|
|
|
self.score = None
|
|
|
|
self.friendSigCount = 0
|
|
|
|
self.success = 0
|
|
|
|
self.failure = 0
|
2019-02-02 03:49:11 +00:00
|
|
|
self.connectTime = None
|
2018-07-27 03:07:50 +00:00
|
|
|
|
|
|
|
if not isinstance(coreInst, core.Core):
|
|
|
|
raise TypeError("coreInst must be a type of core.Core")
|
|
|
|
self.coreInst = coreInst
|
|
|
|
|
|
|
|
self.loadScore()
|
2019-02-02 03:49:11 +00:00
|
|
|
self.getConnectTime()
|
2018-07-27 03:07:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def loadScore(self):
|
|
|
|
'''Load the node's score from the database'''
|
|
|
|
try:
|
2018-07-31 05:34:16 +00:00
|
|
|
self.success = int(self.coreInst.getAddressInfo(self.address, 'success'))
|
2018-08-02 20:18:01 +00:00
|
|
|
except (TypeError, ValueError) as e:
|
2018-07-27 03:07:50 +00:00
|
|
|
self.success = 0
|
2018-07-30 22:48:29 +00:00
|
|
|
self.score = self.success
|
2019-02-02 03:49:11 +00:00
|
|
|
|
|
|
|
def getConnectTime(self):
|
|
|
|
try:
|
|
|
|
self.connectTime = int(self.coreInst.getAddressInfo(self.address, 'lastConnect'))
|
|
|
|
except (KeyError, ValueError, TypeError) as e:
|
|
|
|
pass
|
|
|
|
|
2018-07-27 03:07:50 +00:00
|
|
|
def saveScore(self):
|
|
|
|
'''Save the node's score to the database'''
|
2018-07-30 22:48:29 +00:00
|
|
|
self.coreInst.setAddressInfo(self.address, 'success', self.score)
|
2018-07-27 03:07:50 +00:00
|
|
|
return
|
|
|
|
|
2018-07-30 22:48:29 +00:00
|
|
|
def addScore(self, toAdd):
|
|
|
|
'''Add to the peer's score (can add negative)'''
|
|
|
|
self.score += toAdd
|
|
|
|
self.saveScore()
|
|
|
|
|
2018-07-27 03:07:50 +00:00
|
|
|
def getScoreSortedPeerList(coreInst):
|
|
|
|
if not type(coreInst is core.Core):
|
|
|
|
raise TypeError('coreInst must be instance of core.Core')
|
|
|
|
|
|
|
|
peerList = coreInst.listAdders()
|
|
|
|
peerScores = {}
|
2019-02-02 03:49:11 +00:00
|
|
|
peerTimes = {}
|
2018-07-27 03:07:50 +00:00
|
|
|
|
|
|
|
for address in peerList:
|
|
|
|
# Load peer's profiles into a list
|
|
|
|
profile = PeerProfiles(address, coreInst)
|
|
|
|
peerScores[address] = profile.score
|
2019-02-02 03:49:11 +00:00
|
|
|
if not isinstance(profile.connectTime, type(None)):
|
|
|
|
peerTimes[address] = profile.connectTime
|
|
|
|
else:
|
|
|
|
peerTimes[address] = 9000
|
2018-07-27 03:07:50 +00:00
|
|
|
|
2019-02-02 03:49:11 +00:00
|
|
|
# Sort peers by their score, greatest to least, and then last connected time
|
2018-07-27 03:07:50 +00:00
|
|
|
peerList = sorted(peerScores, key=peerScores.get, reverse=True)
|
2019-02-02 03:49:11 +00:00
|
|
|
peerList = sorted(peerTimes, key=peerTimes.get, reverse=True)
|
2018-08-01 07:22:22 +00:00
|
|
|
return peerList
|
|
|
|
|
2018-08-02 07:28:26 +00:00
|
|
|
def peerCleanup(coreInst):
|
2018-08-13 03:48:33 +00:00
|
|
|
'''Removes peers who have been offline too long or score too low'''
|
2018-08-02 07:28:26 +00:00
|
|
|
if not type(coreInst is core.Core):
|
|
|
|
raise TypeError('coreInst must be instance of core.Core')
|
2018-08-03 06:28:26 +00:00
|
|
|
|
|
|
|
logger.info('Cleaning peers...')
|
|
|
|
|
|
|
|
adders = getScoreSortedPeerList(coreInst)
|
|
|
|
adders.reverse()
|
2019-01-08 05:51:39 +00:00
|
|
|
|
|
|
|
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:
|
2019-06-25 23:07:35 +00:00
|
|
|
if (int(epoch.get_epoch()) - int(coreInst.getPeerInfo(address, 'dateSeen'))) >= 600:
|
2019-01-08 05:51:39 +00:00
|
|
|
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 + '.')
|
2018-08-15 04:20:08 +00:00
|
|
|
|
|
|
|
# Unban probably not malicious peers TODO improve
|
2018-09-24 23:48:00 +00:00
|
|
|
coreInst._blacklist.deleteExpired(dataType=1)
|