lock databases with timeout from onionrvalues

master
Kevin Froman 2019-10-29 00:36:56 -05:00
parent df57e2300e
commit 304df0fbb4
11 changed files with 37 additions and 19 deletions

View File

@ -18,8 +18,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sqlite3 import sqlite3
from etc import onionrvalues
from . import expiredblocks, updateblockinfo, add from . import expiredblocks, updateblockinfo, add
from .. import dbfiles from .. import dbfiles
update_block_info = updateblockinfo.update_block_info update_block_info = updateblockinfo.update_block_info
add_to_block_DB = add.add_to_block_DB add_to_block_DB = add.add_to_block_DB
def get_block_list(dateRec = None, unsaved = False): def get_block_list(dateRec = None, unsaved = False):
@ -29,7 +32,7 @@ def get_block_list(dateRec = None, unsaved = False):
if dateRec == None: if dateRec == None:
dateRec = 0 dateRec = 0
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
execute = 'SELECT hash FROM hashes WHERE dateReceived >= ? ORDER BY dateReceived ASC;' execute = 'SELECT hash FROM hashes WHERE dateReceived >= ? ORDER BY dateReceived ASC;'
@ -46,7 +49,7 @@ def get_block_date(blockHash):
Returns the date a block was received Returns the date a block was received
''' '''
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
execute = 'SELECT dateReceived FROM hashes WHERE hash=?;' execute = 'SELECT dateReceived FROM hashes WHERE hash=?;'
@ -62,7 +65,7 @@ def get_blocks_by_type(blockType, orderDate=True):
Returns a list of blocks by the type Returns a list of blocks by the type
''' '''
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
if orderDate: if orderDate:

View File

@ -19,6 +19,7 @@
''' '''
import os, sqlite3, secrets import os, sqlite3, secrets
from onionrutils import epoch, blockmetadata from onionrutils import epoch, blockmetadata
from etc import onionrvalues
from .. import dbfiles from .. import dbfiles
def add_to_block_DB(newHash, selfInsert=False, dataSaved=False): def add_to_block_DB(newHash, selfInsert=False, dataSaved=False):
''' '''
@ -29,7 +30,7 @@ def add_to_block_DB(newHash, selfInsert=False, dataSaved=False):
if blockmetadata.has_block(newHash): if blockmetadata.has_block(newHash):
return return
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
currentTime = epoch.get_epoch() + secrets.randbelow(301) currentTime = epoch.get_epoch() + secrets.randbelow(301)
if selfInsert or dataSaved: if selfInsert or dataSaved:

View File

@ -20,9 +20,11 @@
import sqlite3 import sqlite3
from onionrutils import epoch from onionrutils import epoch
from .. import dbfiles from .. import dbfiles
from etc import onionrvalues
def get_expired_blocks(): def get_expired_blocks():
'''Returns a list of expired blocks''' '''Returns a list of expired blocks'''
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
date = int(epoch.get_epoch()) date = int(epoch.get_epoch())

View File

@ -19,6 +19,7 @@
''' '''
import sqlite3 import sqlite3
from .. import dbfiles from .. import dbfiles
from etc import onionrvalues
def update_block_info(hash, key, data): def update_block_info(hash, key, data):
''' '''
sets info associated with a block sets info associated with a block
@ -38,7 +39,7 @@ def update_block_info(hash, key, data):
'dataSaved', 'sig', 'author', 'dateClaimed', 'expire'): 'dataSaved', 'sig', 'author', 'dateClaimed', 'expire'):
raise ValueError('Key must be in the allowed list') raise ValueError('Key must be in the allowed list')
conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
args = (data, hash) args = (data, hash)
# Unfortunately, not really possible # Unfortunately, not really possible

View File

@ -23,6 +23,7 @@ from onionrplugins import onionrevents as events
from onionrutils import localcommand, epoch from onionrutils import localcommand, epoch
from .. import dbfiles from .. import dbfiles
from onionrsetup import dbcreator from onionrsetup import dbcreator
from etc import onionrvalues
def daemon_queue()->str: def daemon_queue()->str:
''' '''
@ -35,7 +36,7 @@ def daemon_queue()->str:
if not os.path.exists(dbfiles.daemon_queue_db): if not os.path.exists(dbfiles.daemon_queue_db):
dbcreator.createDaemonDB() dbcreator.createDaemonDB()
else: else:
conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=30) conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
try: try:
for row in c.execute('SELECT command, data, date, min(ID), responseID FROM commands group by id'): for row in c.execute('SELECT command, data, date, min(ID), responseID FROM commands group by id'):
@ -59,7 +60,7 @@ def daemon_queue_add(command: str, data='', responseID: str =''):
retData = True retData = True
date = epoch.get_epoch() date = epoch.get_epoch()
conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=30) conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
t = (command, data, date, responseID) t = (command, data, date, responseID)
try: try:
@ -83,7 +84,7 @@ def clear_daemon_queue():
''' '''
Clear the daemon queue (somewhat dangerous) Clear the daemon queue (somewhat dangerous)
''' '''
conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=30) conn = sqlite3.connect(dbfiles.daemon_queue_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
c.execute('DELETE FROM commands;') c.execute('DELETE FROM commands;')

View File

@ -24,6 +24,8 @@ from . import listkeys
from utils import gettransports from utils import gettransports
from .. import dbfiles from .. import dbfiles
import onionrcrypto import onionrcrypto
from etc import onionrvalues
def add_peer(peerID, name=''): def add_peer(peerID, name=''):
''' '''
Adds a public key to the key database (misleading function name) Adds a public key to the key database (misleading function name)
@ -37,7 +39,7 @@ def add_peer(peerID, name=''):
#events.event('pubkey_add', data = {'key': peerID}, onionr = core_inst.onionrInst) #events.event('pubkey_add', data = {'key': peerID}, onionr = core_inst.onionrInst)
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
hashID = "" hashID = ""
c = conn.cursor() c = conn.cursor()
t = (peerID, name, 'unknown', hashID, 0) t = (peerID, name, 'unknown', hashID, 0)
@ -67,7 +69,7 @@ def add_address(address):
if stringvalidators.validate_transport(address): if stringvalidators.validate_transport(address):
if address in gettransports.get(): if address in gettransports.get():
return False return False
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) conn = sqlite3.connect(dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
# check if address is in database # check if address is in database
# this is safe to do because the address is validated above, but we strip some chars here too just in case # this is safe to do because the address is validated above, but we strip some chars here too just in case

View File

@ -20,6 +20,7 @@
import sqlite3 import sqlite3
import logger import logger
from onionrutils import epoch from onionrutils import epoch
from etc import onionrvalues
from .. import dbfiles from .. import dbfiles
from . import userinfo, transportinfo from . import userinfo, transportinfo
def list_peers(randomOrder=True, getPow=False, trust=0): def list_peers(randomOrder=True, getPow=False, trust=0):
@ -29,7 +30,7 @@ def list_peers(randomOrder=True, getPow=False, trust=0):
randomOrder determines if the list should be in a random order randomOrder determines if the list should be in a random order
trust sets the minimum trust to list trust sets the minimum trust to list
''' '''
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
payload = '' payload = ''
@ -63,7 +64,7 @@ def list_adders(randomOrder=True, i2p=True, recent=0):
''' '''
Return a list of transport addresses Return a list of transport addresses
''' '''
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) conn = sqlite3.connect(dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
if randomOrder: if randomOrder:
addresses = c.execute('SELECT * FROM adders ORDER BY RANDOM();') addresses = c.execute('SELECT * FROM adders ORDER BY RANDOM();')

View File

@ -22,6 +22,7 @@ from onionrplugins import onionrevents as events
from onionrutils import stringvalidators from onionrutils import stringvalidators
from onionrutils import mnemonickeys from onionrutils import mnemonickeys
from .. import dbfiles from .. import dbfiles
from etc import onionrvalues
def remove_address(address): def remove_address(address):
''' '''
@ -29,7 +30,7 @@ def remove_address(address):
''' '''
if stringvalidators.validate_transport(address): if stringvalidators.validate_transport(address):
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) conn = sqlite3.connect(dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
t = (address,) t = (address,)
c.execute('Delete from adders where address=?;', t) c.execute('Delete from adders where address=?;', t)
@ -47,7 +48,7 @@ def remove_user(pubkey: str)->bool:
''' '''
pubkey = mnemonickeys.get_base32(pubkey) pubkey = mnemonickeys.get_base32(pubkey)
if stringvalidators.validate_pub_key(pubkey): if stringvalidators.validate_pub_key(pubkey):
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
t = (pubkey,) t = (pubkey,)
c.execute('Delete from peers where id=?;', t) c.execute('Delete from peers where id=?;', t)

View File

@ -19,6 +19,8 @@
''' '''
import sqlite3 import sqlite3
from .. import dbfiles from .. import dbfiles
from etc import onionrvalues
def get_address_info(address, info): def get_address_info(address, info):
''' '''
Get info about an address from its database entry Get info about an address from its database entry
@ -35,7 +37,7 @@ def get_address_info(address, info):
introduced 9 introduced 9
''' '''
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) conn = sqlite3.connect(dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (address,) command = (address,)
@ -60,7 +62,7 @@ def set_address_info(address, key, data):
Update an address for a key Update an address for a key
''' '''
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30) conn = sqlite3.connect(dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (data, address) command = (data, address)

View File

@ -19,6 +19,8 @@
''' '''
import sqlite3 import sqlite3
from .. import dbfiles from .. import dbfiles
from etc import onionrvalues
def get_user_info(peer, info): def get_user_info(peer, info):
''' '''
Get info about a peer from their database entry Get info about a peer from their database entry
@ -30,7 +32,7 @@ def get_user_info(peer, info):
trust int 4 trust int 4
hashID text 5 hashID text 5
''' '''
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (peer,) command = (peer,)
@ -56,7 +58,7 @@ def set_peer_info(peer, key, data):
Update a peer for a key Update a peer for a key
''' '''
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
c = conn.cursor() c = conn.cursor()
command = (data, peer) command = (data, peer)

View File

@ -39,6 +39,8 @@ MAX_BLOCK_CLOCK_SKEW = 120
MAIN_PUBLIC_KEY_SIZE = 32 MAIN_PUBLIC_KEY_SIZE = 32
ORIG_RUN_DIR_ENV_VAR = 'ORIG_ONIONR_RUN_DIR' ORIG_RUN_DIR_ENV_VAR = 'ORIG_ONIONR_RUN_DIR'
DATABASE_LOCK_TIMEOUT = 60
# Block creation anonymization requirements # Block creation anonymization requirements
MIN_BLOCK_UPLOAD_PEER_PERCENT = 0.1 MIN_BLOCK_UPLOAD_PEER_PERCENT = 0.1