improved blacklisting

master
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
from core import Core
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:
'''
@ -119,9 +119,7 @@ class API:
'''
Simply define the request as not having yet failed, before every request.
'''
self.requestFailed = False
return
@app.after_request
@ -397,10 +395,14 @@ class API:
pass
else:
if sys.getsizeof(data) < 100000000:
try:
if blockimporter.importBlockFromData(data, self._core):
resp = 'success'
else:
logger.warn('Error encountered importing uploaded block')
except onionrexceptions.BlacklistedBlock:
logger.debug('uploaded block is blacklisted')
pass
resp = Response(resp)
return resp

View File

@ -20,6 +20,12 @@
import core, onionrexceptions, logger
def importBlockFromData(content, coreInst):
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):
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 + "';"):
try:
if i[0] == address:
logger.warn('Not adding existing address')
conn.close()
return False
except ValueError:
@ -311,16 +310,24 @@ class Core:
return data
def setData(self, data):
'''
Set the data assciated with a hash
'''
data = data
def _getSha3Hash(self, data):
hasher = hashlib.sha3_256()
if not type(data) is bytes:
data = data.encode()
hasher.update(data)
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:
dataHash = dataHash.decode()
blockFileName = self.blockDataLocation + dataHash + '.dat'

View File

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

View File

@ -72,7 +72,7 @@ def getScoreSortedPeerList(coreInst):
return peerList
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):
raise TypeError('coreInst must be instance of core.Core')