2019-06-26 06:34:34 +00:00
|
|
|
'''
|
|
|
|
Onionr - Private P2P Communication
|
|
|
|
|
|
|
|
add user keys or transport addresses
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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-06-22 21:16:12 +00:00
|
|
|
import sqlite3
|
2019-09-21 22:45:46 +00:00
|
|
|
from onionrplugins import onionrevents as events
|
2019-06-23 17:41:07 +00:00
|
|
|
from onionrutils import stringvalidators
|
2019-07-17 22:41:33 +00:00
|
|
|
from . import listkeys
|
2019-07-18 23:07:18 +00:00
|
|
|
from utils import gettransports
|
|
|
|
from .. import dbfiles
|
2019-07-21 16:15:20 +00:00
|
|
|
import onionrcrypto
|
2019-07-18 23:07:18 +00:00
|
|
|
def add_peer(peerID, name=''):
|
2019-06-22 21:16:12 +00:00
|
|
|
'''
|
|
|
|
Adds a public key to the key database (misleading function name)
|
|
|
|
'''
|
2019-07-21 16:15:20 +00:00
|
|
|
if peerID in listkeys.list_peers() or peerID == onionrcrypto.pub_key:
|
2019-06-22 21:16:12 +00:00
|
|
|
raise ValueError("specified id is already known")
|
|
|
|
|
|
|
|
# This function simply adds a peer to the DB
|
2019-06-25 08:21:36 +00:00
|
|
|
if not stringvalidators.validate_pub_key(peerID):
|
2019-06-22 21:16:12 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
#events.event('pubkey_add', data = {'key': peerID}, onionr = core_inst.onionrInst)
|
2019-06-22 21:16:12 +00:00
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30)
|
2019-07-21 16:15:20 +00:00
|
|
|
hashID = ""
|
2019-06-22 21:16:12 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
t = (peerID, name, 'unknown', hashID, 0)
|
|
|
|
|
|
|
|
for i in c.execute("SELECT * FROM peers WHERE id = ?;", (peerID,)):
|
|
|
|
try:
|
|
|
|
if i[0] == peerID:
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
c.execute('INSERT INTO peers (id, name, dateSeen, hashID, trust) VALUES(?, ?, ?, ?, ?);', t)
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
def add_address(address):
|
2019-06-22 21:16:12 +00:00
|
|
|
'''
|
|
|
|
Add an address to the address database (only tor currently)
|
|
|
|
'''
|
|
|
|
|
|
|
|
if type(address) is None or len(address) == 0:
|
|
|
|
return False
|
2019-06-23 17:41:07 +00:00
|
|
|
if stringvalidators.validate_transport(address):
|
2019-07-24 16:32:23 +00:00
|
|
|
if address in gettransports.get():
|
2019-06-22 21:16:12 +00:00
|
|
|
return False
|
2019-07-18 23:07:18 +00:00
|
|
|
conn = sqlite3.connect(dbfiles.address_info_db, timeout=30)
|
2019-06-22 21:16:12 +00:00
|
|
|
c = conn.cursor()
|
|
|
|
# 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
|
|
|
|
address = address.replace('\'', '').replace(';', '').replace('"', '').replace('\\', '')
|
|
|
|
for i in c.execute("SELECT * FROM adders WHERE address = ?;", (address,)):
|
|
|
|
try:
|
|
|
|
if i[0] == address:
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
t = (address, 1)
|
|
|
|
c.execute('INSERT INTO adders (address, type) VALUES(?, ?);', t)
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
2019-07-18 23:07:18 +00:00
|
|
|
#events.event('address_add', data = {'address': address}, onionr = core_inst.onionrInst)
|
2019-06-22 21:16:12 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|