work on chat

This commit is contained in:
Kevin Froman 2019-07-28 00:33:26 -05:00
parent 7514fd5228
commit da21999287
13 changed files with 58 additions and 31 deletions

View File

@ -22,20 +22,24 @@ import logger
from onionrusers import onionrusers from onionrusers import onionrusers
from onionrutils import epoch from onionrutils import epoch
from coredb import blockmetadb, dbfiles from coredb import blockmetadb, dbfiles
import onionrstorage
from onionrstorage import removeblock from onionrstorage import removeblock
import onionrblacklist
def clean_old_blocks(comm_inst): def clean_old_blocks(comm_inst):
'''Delete old blocks if our disk allocation is full/near full, and also expired blocks''' '''Delete old blocks if our disk allocation is full/near full, and also expired blocks'''
blacklist = onionrblacklist.OnionrBlackList()
# Delete expired blocks # Delete expired blocks
for bHash in blockmetadb.expiredblocks.get_expired_blocks(): for bHash in blockmetadb.expiredblocks.get_expired_blocks():
comm_inst.blacklist.addToDB(bHash) blacklist.addToDB(bHash)
removeblock.remove_block(bHash) removeblock.remove_block(bHash)
onionrstorage.deleteBlock(bHash)
logger.info('Deleted block: %s' % (bHash,)) logger.info('Deleted block: %s' % (bHash,))
while comm_inst.storage_counter.isFull(): while comm_inst.storage_counter.isFull():
oldest = blockmetadb.get_block_list()[0] oldest = blockmetadb.get_block_list()[0]
comm_inst.blacklist.addToDB(oldest) blacklist.addToDB(oldest)
removeblock.remove_block(oldest) removeblock.remove_block(oldest)
onionrstorage.deleteBlock(bHash)
logger.info('Deleted block: %s' % (oldest,)) logger.info('Deleted block: %s' % (oldest,))
comm_inst.decrementThreadCount('clean_old_blocks') comm_inst.decrementThreadCount('clean_old_blocks')

View File

@ -34,6 +34,6 @@ def service_creator(daemon):
signer = bytesconverter.bytes_to_str(bl.signer) signer = bytesconverter.bytes_to_str(bl.signer)
daemon.active_services.append(b) daemon.active_services.append(b)
daemon.active_services.append(signer) daemon.active_services.append(signer)
daemon.services.create_server(signer, bs) daemon.services.create_server(signer, bs, daemon)
daemon.decrementThreadCount('service_creator') daemon.decrementThreadCount('service_creator')

View File

@ -163,5 +163,21 @@ def createDaemonDB():
conn.commit() conn.commit()
conn.close() conn.close()
def create_blacklist_db():
if os.path.exists(dbfiles.blacklist_db):
raise FileExistsError("Blacklist db already exists")
conn = sqlite3.connect(dbfiles.blacklist_db, timeout=10)
c = conn.cursor()
# Create table
c.execute('''CREATE TABLE blacklist(
hash text primary key not null,
dataType int,
blacklistDate int,
expire int
);
''')
conn.commit()
conn.close()
create_funcs = [createAddressDB, createPeerDB, createBlockDB, createBlockDataDB, createForwardKeyDB, createDaemonDB]
create_funcs = [createAddressDB, createPeerDB, createBlockDB, createBlockDataDB, createForwardKeyDB, createDaemonDB, create_blacklist_db]

View File

@ -93,7 +93,7 @@ class Onionr:
try: try:
os.mkdir(plugins.get_plugin_data_folder(name)) os.mkdir(plugins.get_plugin_data_folder(name))
except Exception as e: except Exception as e:
logger.warn('Error enabling plugin: ' + str(e)) #logger.warn('Error enabling plugin: ' + str(e), terminal=True)
plugins.disable(name, onionr = self, stop_event = False) plugins.disable(name, onionr = self, stop_event = False)
self.communicatorInst = None self.communicatorInst = None

View File

@ -75,13 +75,6 @@ class OnionrBlackList:
self._dbExecute("DELETE FROM blacklist WHERE hash = ?", (thing,)) self._dbExecute("DELETE FROM blacklist WHERE hash = ?", (thing,))
def generateDB(self): def generateDB(self):
self._dbExecute('''CREATE TABLE blacklist(
hash text primary key not null,
dataType int,
blacklistDate int,
expire int
);
''')
return return
def clearDB(self): def clearDB(self):
@ -102,7 +95,7 @@ class OnionrBlackList:
''' '''
# we hash the data so we can remove data entirely from our node's disk # we hash the data so we can remove data entirely from our node's disk
hashed = bytesconverter.bytes_to_str(crypto.sha3Hash(data)) hashed = bytesconverter.bytes_to_str(onionrcrypto.hashers.sha3_hash(data))
if len(hashed) > 64: if len(hashed) > 64:
raise Exception("Hashed data is too large") raise Exception("Hashed data is too large")

View File

@ -32,7 +32,7 @@ class OnionrServices:
self.shutdown = False self.shutdown = False
return return
def create_server(self, peer, address): def create_server(self, peer, address, comm_inst):
''' '''
When a client wants to connect, contact their bootstrap address and tell them our When a client wants to connect, contact their bootstrap address and tell them our
ephemeral address for our service by creating a new ConnectionServer instance ephemeral address for our service by creating a new ConnectionServer instance
@ -46,7 +46,7 @@ class OnionrServices:
for x in range(BOOTSTRAP_TRIES): for x in range(BOOTSTRAP_TRIES):
if basicrequests.do_get_request(base_url + 'ping', port=socks, ignoreAPI=True) == 'pong!': if basicrequests.do_get_request(base_url + 'ping', port=socks, ignoreAPI=True) == 'pong!':
# if bootstrap sever is online, tell them our service address # if bootstrap sever is online, tell them our service address
connectionserver.ConnectionServer(peer, address) connectionserver.ConnectionServer(peer, address, comm_inst=comm_inst)
else: else:
time.sleep(TRY_WAIT) time.sleep(TRY_WAIT)
else: else:

View File

@ -24,7 +24,7 @@ from flask import Flask, Response
from netcontroller import get_open_port from netcontroller import get_open_port
from . import httpheaders from . import httpheaders
from onionrutils import stringvalidators, epoch from onionrutils import stringvalidators, epoch
import config, onionrblocks
def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300): def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300):
''' '''
Bootstrap client services Bootstrap client services
@ -68,12 +68,12 @@ def bootstrap_client_service(peer, onionr_inst=None, bootstrap_timeout=300):
else: else:
return Response("") return Response("")
with Controller.from_port(port=onionr_inst.config.get('tor.controlPort')) as controller: with Controller.from_port(port=config.get('tor.controlPort')) as controller:
# Connect to the Tor process for Onionr # Connect to the Tor process for Onionr
controller.authenticate(onionr_inst.config.get('tor.controlpassword')) controller.authenticate(config.get('tor.controlpassword'))
# Create the v3 onion service # Create the v3 onion service
response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', key_content = 'ED25519-V3', await_publication = True) response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', key_content = 'ED25519-V3', await_publication = True)
core_inst.insertBlock(response.service_id, header='con', sign=True, encryptType='asym', onionrblocks.insert(response.service_id, header='con', sign=True, encryptType='asym',
asymPeer=peer, disableForward=True, expire=(epoch.get_epoch() + bootstrap_timeout)) asymPeer=peer, disableForward=True, expire=(epoch.get_epoch() + bootstrap_timeout))
# Run the bootstrap server # Run the bootstrap server
try: try:

View File

@ -21,24 +21,24 @@ from gevent.pywsgi import WSGIServer
from stem.control import Controller from stem.control import Controller
from flask import Flask from flask import Flask
import logger, httpapi import logger, httpapi
import onionrexceptions import onionrexceptions, config
from netcontroller import get_open_port from netcontroller import get_open_port
from httpapi import apiutils from httpapi import apiutils
from onionrutils import stringvalidators, basicrequests, bytesconverter from onionrutils import stringvalidators, basicrequests, bytesconverter
from . import httpheaders from . import httpheaders
class ConnectionServer: class ConnectionServer:
def __init__(self, peer, address, onionr_inst=None): def __init__(self, peer, address, comm_inst=None):
if not stringvalidators.validate_pub_key(peer): if not stringvalidators.validate_pub_key(peer):
raise ValueError('Peer must be valid base32 ed25519 public key') raise ValueError('Peer must be valid base32 ed25519 public key')
socks = onionr_inst.config.get('tor.socksport') # Load config for Tor socks port for proxy socks = config.get('tor.socksport') # Load config for Tor socks port for proxy
service_app = Flask(__name__) # Setup Flask app for server. service_app = Flask(__name__) # Setup Flask app for server.
service_port = get_open_port() service_port = get_open_port()
service_ip = apiutils.setbindip.set_bind_IP() service_ip = apiutils.setbindip.set_bind_IP()
http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None) http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None)
onionr_inst.communicatorInst.service_greenlets.append(http_server) comm_inst.service_greenlets.append(http_server)
# TODO define basic endpoints useful for direct connections like stats # TODO define basic endpoints useful for direct connections like stats
@ -50,7 +50,7 @@ class ConnectionServer:
@service_app.route('/close') @service_app.route('/close')
def shutdown_server(): def shutdown_server():
onionr_inst.communicatorInst.service_greenlets.remove(http_server) comm_inst.service_greenlets.remove(http_server)
http_server.stop() http_server.stop()
return Response('goodbye') return Response('goodbye')
@ -60,9 +60,9 @@ class ConnectionServer:
resp = httpheaders.set_default_onionr_http_headers(resp) resp = httpheaders.set_default_onionr_http_headers(resp)
return resp return resp
with Controller.from_port(port=onionr_inst.config.get('tor.controlPort')) as controller: with Controller.from_port(port=config.get('tor.controlPort')) as controller:
# Connect to the Tor process for Onionr # Connect to the Tor process for Onionr
controller.authenticate(onionr_inst.config.get('tor.controlpassword')) controller.authenticate(config.get('tor.controlpassword'))
# Create the v3 onion service for the peer to connect to # Create the v3 onion service for the peer to connect to
response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW', key_content = 'ED25519-V3') response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW', key_content = 'ED25519-V3')

View File

@ -11,7 +11,7 @@ def remove_block(block):
''' '''
if stringvalidators.validate_hash(block): if stringvalidators.validate_hash(block):
conn = sqlite3.connect(dbfiles.block_data_db, timeout=30) conn = sqlite3.connect(dbfiles.block_meta_db, timeout=30)
c = conn.cursor() c = conn.cursor()
t = (block,) t = (block,)
c.execute('Delete from hashes where hash=?;', t) c.execute('Delete from hashes where hash=?;', t)

View File

@ -87,8 +87,7 @@ def on_init(api, data = None):
inputted is executed. Could be called when daemon is starting or when inputted is executed. Could be called when daemon is starting or when
just the client is running. just the client is running.
''' '''
pluginapi = api pluginapi = api
chat = chat(pluginapi) chat = Chat(pluginapi)
api.commands.register(['chat'], chat.create) api.commands.register(['chat'], chat.create)
return return

View File

@ -50,6 +50,7 @@ def sendto():
else: else:
msg = json.dumps(msg) msg = json.dumps(msg)
localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, postData=msg) localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, postData=msg)
print(msg)
return Response('success') return Response('success')
@direct_blueprint.route('/chat/poll') @direct_blueprint.route('/chat/poll')

View File

@ -6,7 +6,7 @@
"minimum_block_pow" : 4, "minimum_block_pow" : 4,
"minimum_send_pow" : 4, "minimum_send_pow" : 4,
"use_subprocess_pow_if_possible" : true, "use_subprocess_pow_if_possible" : true,
"socket_servers" : false, "socket_servers" : true,
"security_level" : 0, "security_level" : 0,
"hide_created_blocks" : true, "hide_created_blocks" : true,
"insert_deniable_blocks" : true, "insert_deniable_blocks" : true,

14
test.html Executable file

File diff suppressed because one or more lines are too long