2020-08-17 00:52:50 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-06-26 06:34:34 +00:00
|
|
|
|
2020-08-17 00:52:50 +00:00
|
|
|
get or set transport address meta information
|
|
|
|
"""
|
|
|
|
import sqlite3
|
|
|
|
from .. import dbfiles
|
|
|
|
from etc import onionrvalues
|
|
|
|
"""
|
2019-06-26 06:34:34 +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/>.
|
2020-08-17 00:52:50 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-29 05:36:56 +00:00
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
def get_address_info(address, info):
|
2020-08-17 00:52:50 +00:00
|
|
|
"""
|
|
|
|
Get info about an address from its database entry
|
2019-06-22 21:16:12 +00:00
|
|
|
|
2020-08-17 00:52:50 +00:00
|
|
|
address text, 0
|
|
|
|
type int, 1
|
|
|
|
knownPeer text, 2
|
|
|
|
speed int, 3
|
|
|
|
success int, 4
|
|
|
|
powValue 5
|
|
|
|
failure int 6
|
|
|
|
lastConnect 7
|
|
|
|
trust 8
|
|
|
|
introduced 9
|
|
|
|
"""
|
2019-06-22 21:16:12 +00:00
|
|
|
|
2020-08-17 00:52:50 +00:00
|
|
|
conn = sqlite3.connect(
|
|
|
|
dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
|
2019-06-22 21:16:12 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
|
|
|
|
command = (address,)
|
|
|
|
infoNumbers = {'address': 0, 'type': 1, 'knownPeer': 2, 'speed': 3, 'success': 4, 'powValue': 5, 'failure': 6, 'lastConnect': 7, 'trust': 8, 'introduced': 9}
|
|
|
|
info = infoNumbers[info]
|
|
|
|
iterCount = 0
|
|
|
|
retVal = ''
|
|
|
|
|
|
|
|
for row in c.execute('SELECT * FROM adders WHERE address=?;', command):
|
|
|
|
for i in row:
|
|
|
|
if iterCount == info:
|
|
|
|
retVal = i
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
iterCount += 1
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2020-08-17 00:52:50 +00:00
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
def set_address_info(address, key, data):
|
2020-08-17 00:52:50 +00:00
|
|
|
"""Update an address for a key."""
|
2019-06-22 21:16:12 +00:00
|
|
|
|
2020-08-17 00:52:50 +00:00
|
|
|
conn = sqlite3.connect(
|
|
|
|
dbfiles.address_info_db, timeout=onionrvalues.DATABASE_LOCK_TIMEOUT)
|
2019-06-22 21:16:12 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
|
|
|
|
command = (data, address)
|
|
|
|
|
|
|
|
if key not in ('address', 'type', 'knownPeer', 'speed', 'success', 'failure', 'powValue', 'lastConnect', 'lastConnectAttempt', 'trust', 'introduced'):
|
2019-09-09 00:21:36 +00:00
|
|
|
raise ValueError("Got invalid database key when setting address info, must be in whitelist")
|
2019-06-22 21:16:12 +00:00
|
|
|
else:
|
|
|
|
c.execute('UPDATE adders SET ' + key + ' = ? WHERE address=?', command)
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|