Onionr/src/onionrblocks/onionrblacklist.py

127 lines
4.1 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
2018-08-10 07:03:49 +00:00
Handle maintenance of a blacklist database, for blocks and peers
"""
import sqlite3
import os
from onionrplugins.onionrevents import event
import onionrcrypto
from onionrutils import epoch, bytesconverter
from coredb import dbfiles
from etc.onionrvalues import DATABASE_LOCK_TIMEOUT
"""
2018-08-10 07:03:49 +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/>.
"""
2019-07-20 15:52:03 +00:00
2018-08-10 07:03:49 +00:00
class OnionrBlackList:
2019-07-19 19:49:56 +00:00
def __init__(self):
self.blacklistDB = dbfiles.blacklist_db
2019-07-19 19:49:56 +00:00
if not os.path.exists(dbfiles.blacklist_db):
2018-08-10 07:03:49 +00:00
self.generateDB()
return
2018-08-10 07:03:49 +00:00
def inBlacklist(self, data):
2019-07-20 15:52:03 +00:00
hashed = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(data))
retData = False
2018-11-17 07:23:10 +00:00
if not hashed.isalnum():
raise Exception("Hashed data is not alpha numeric")
if len(hashed) > 64:
raise Exception("Hashed data is too large")
2018-11-17 07:23:10 +00:00
for i in self._dbExecute("SELECT * FROM blacklist WHERE hash = ?", (hashed,)):
retData = True # this only executes if an entry is present by that hash
break
2019-01-18 01:14:26 +00:00
return retData
2018-08-10 07:03:49 +00:00
def _dbExecute(self, toExec, params=()):
conn = sqlite3.connect(self.blacklistDB, timeout=DATABASE_LOCK_TIMEOUT)
2018-08-10 07:03:49 +00:00
c = conn.cursor()
retData = c.execute(toExec, params)
2018-08-10 07:03:49 +00:00
conn.commit()
return retData
2018-08-10 07:03:49 +00:00
def deleteBeforeDate(self, date):
# TODO, delete blacklist entries before date
return
2018-08-15 04:20:08 +00:00
def deleteExpired(self, dataType=0):
"""Delete expired entries"""
2018-08-15 04:20:08 +00:00
deleteList = []
curTime = epoch.get_epoch()
2018-08-15 04:20:08 +00:00
try:
int(dataType)
except AttributeError:
raise TypeError("dataType must be int")
for i in self._dbExecute('SELECT * FROM blacklist WHERE dataType = ?', (dataType,)):
2018-08-15 04:20:08 +00:00
if i[1] == dataType:
if (curTime - i[2]) >= i[3]:
deleteList.append(i[0])
2018-08-15 04:20:08 +00:00
for thing in deleteList:
self._dbExecute("DELETE FROM blacklist WHERE hash = ?", (thing,))
2018-08-10 07:03:49 +00:00
def generateDB(self):
return
2018-08-10 07:03:49 +00:00
def clearDB(self):
self._dbExecute("""DELETE FROM blacklist;""")
2018-08-10 07:03:49 +00:00
def getList(self):
data = self._dbExecute('SELECT * FROM blacklist')
myList = []
for i in data:
myList.append(i[0])
return myList
def addToDB(self, data, dataType=0, expire=0):
"""Add to the blacklist. Intended to be block hash, block data, peers, or transport addresses
2018-08-15 04:20:08 +00:00
0=block
1=peer
2=pubkey
"""
2019-07-20 15:52:03 +00:00
# we hash the data so we can remove data entirely from our node's disk
2019-07-28 05:33:26 +00:00
hashed = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(data))
event('blacklist_add', data={'data': data, 'hash': hashed})
if len(hashed) > 64:
raise Exception("Hashed data is too large")
2018-08-17 03:30:36 +00:00
2018-08-10 07:03:49 +00:00
if not hashed.isalnum():
raise Exception("Hashed data is not alpha numeric")
try:
int(dataType)
except ValueError:
raise Exception("dataType is not int")
try:
int(expire)
except ValueError:
raise Exception("expire is not int")
if self.inBlacklist(hashed):
return
2018-08-10 07:03:49 +00:00
insert = (hashed,)
blacklistDate = epoch.get_epoch()
2019-02-12 05:30:56 +00:00
try:
self._dbExecute("INSERT INTO blacklist (hash, dataType, blacklistDate, expire) VALUES(?, ?, ?, ?);", (str(hashed), dataType, blacklistDate, expire))
except sqlite3.IntegrityError:
pass