improved blacklisting

This commit is contained in:
Kevin Froman 2018-08-12 22:48:33 -05:00
parent 5f21d15cdd
commit aab7d4296f
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
5 changed files with 32 additions and 14 deletions

View File

@ -24,7 +24,7 @@ from gevent.wsgi import WSGIServer
import sys, random, threading, hmac, hashlib, base64, time, math, os, json import sys, random, threading, hmac, hashlib, base64, time, math, os, json
from core import Core from core import Core
from onionrblockapi import Block from onionrblockapi import Block
import onionrutils, onionrcrypto, blockimporter, onionrevents as events, logger, config import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config
class API: class API:
''' '''
@ -119,9 +119,7 @@ class API:
''' '''
Simply define the request as not having yet failed, before every request. Simply define the request as not having yet failed, before every request.
''' '''
self.requestFailed = False self.requestFailed = False
return return
@app.after_request @app.after_request
@ -397,10 +395,14 @@ class API:
pass pass
else: else:
if sys.getsizeof(data) < 100000000: if sys.getsizeof(data) < 100000000:
if blockimporter.importBlockFromData(data, self._core): try:
resp = 'success' if blockimporter.importBlockFromData(data, self._core):
else: resp = 'success'
logger.warn('Error encountered importing uploaded block') else:
logger.warn('Error encountered importing uploaded block')
except onionrexceptions.BlacklistedBlock:
logger.debug('uploaded block is blacklisted')
pass
resp = Response(resp) resp = Response(resp)
return resp return resp

View File

@ -20,6 +20,12 @@
import core, onionrexceptions, logger import core, onionrexceptions, logger
def importBlockFromData(content, coreInst): def importBlockFromData(content, coreInst):
retData = False retData = False
dataHash = coreInst._getSha3Hash(content)
if coreInst._blacklist.inBlacklist(dataHash):
raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,))
if not isinstance(coreInst, core.Core): if not isinstance(coreInst, core.Core):
raise Exception("coreInst must be an Onionr core instance") raise Exception("coreInst must be an Onionr core instance")

View File

@ -133,7 +133,6 @@ class Core:
for i in c.execute("SELECT * FROM adders where address = '" + address + "';"): for i in c.execute("SELECT * FROM adders where address = '" + address + "';"):
try: try:
if i[0] == address: if i[0] == address:
logger.warn('Not adding existing address')
conn.close() conn.close()
return False return False
except ValueError: except ValueError:
@ -311,16 +310,24 @@ class Core:
return data return data
def setData(self, data): def _getSha3Hash(self, data):
'''
Set the data assciated with a hash
'''
data = data
hasher = hashlib.sha3_256() hasher = hashlib.sha3_256()
if not type(data) is bytes: if not type(data) is bytes:
data = data.encode() data = data.encode()
hasher.update(data) hasher.update(data)
dataHash = hasher.hexdigest() dataHash = hasher.hexdigest()
return dataHash
def setData(self, data):
'''
Set the data assciated with a hash
'''
data = data
if not type(data) is bytes:
data = data.encode()
dataHash = self._getSha3Hash(data)
if type(dataHash) is bytes: if type(dataHash) is bytes:
dataHash = dataHash.decode() dataHash = dataHash.decode()
blockFileName = self.blockDataLocation + dataHash + '.dat' blockFileName = self.blockDataLocation + dataHash + '.dat'

View File

@ -38,6 +38,9 @@ class InvalidPubkey(Exception):
class InvalidMetadata(Exception): class InvalidMetadata(Exception):
pass pass
class BlacklistedBlock(Exception):
pass
class InvalidHexHash(Exception): class InvalidHexHash(Exception):
'''When a string is not a valid hex string of appropriate length for a hash value''' '''When a string is not a valid hex string of appropriate length for a hash value'''
pass pass

View File

@ -72,7 +72,7 @@ def getScoreSortedPeerList(coreInst):
return peerList return peerList
def peerCleanup(coreInst): def peerCleanup(coreInst):
'''Removes peers who have been offline too long''' '''Removes peers who have been offline too long or score too low'''
if not type(coreInst is core.Core): if not type(coreInst is core.Core):
raise TypeError('coreInst must be instance of core.Core') raise TypeError('coreInst must be instance of core.Core')