From 3199f93d4b4016137514cb77764a2f2c76898c80 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 19 Sep 2020 08:25:10 +0000 Subject: [PATCH] work on making mail more efficient, added --private-ke argument imp to run script --- run-onionr-node.py | 16 ++++++++++++---- static-data/default-plugins/pms/mailapi.py | 13 +++++++++++++ static-data/default-plugins/pms/web/index.html | 1 + static-data/default-plugins/pms/web/mail.js | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/run-onionr-node.py b/run-onionr-node.py index b32a4a8c..d4ef3ad5 100755 --- a/run-onionr-node.py +++ b/run-onionr-node.py @@ -75,7 +75,7 @@ parser.add_argument( type=int, default=1) parser.add_argument( '--private-key', help='Use existing private key', - type=int, default=1) + type=str, default=0) parser.add_argument( '--animated-background', help='Animated background on webui index. Just for looks.', type=int, default=0) @@ -86,14 +86,22 @@ args = parser.parse_args() p = Popen([sub_script, 'version'], stdout=DEVNULL) p.wait() -from filepaths import config_file +from filepaths import config_file, keys_file from coredb import blockmetadb - +import onionrcrypto with open(config_file, 'r') as cf: config = ujson.loads(cf.read()) + +if args.private_key: + priv = args.private_key + pub = onionrcrypto.cryptoutils.get_pub_key_from_priv(priv) + with open(keys_file, "a") as f: + f.write(',' + pub.decode() + ',' + priv) + config['general']['public_key'] = pub + if not args.onboarding: config['onboarding']['done'] = True if not args.random_localhost_ip: @@ -110,7 +118,7 @@ config['general']['display_header'] = False config['general']['security_level'] = args.security_level with open(config_file, 'w') as cf: - cf.write(ujson.dumps(config)) + cf.write(ujson.dumps(config, reject_bytes=False)) if args.open_ui: p = Popen([sub_script, 'start'], stdout=DEVNULL) diff --git a/static-data/default-plugins/pms/mailapi.py b/static-data/default-plugins/pms/mailapi.py index fde35956..53783a09 100755 --- a/static-data/default-plugins/pms/mailapi.py +++ b/static-data/default-plugins/pms/mailapi.py @@ -10,9 +10,11 @@ from flask import Response, request, redirect, Blueprint, abort from flask import send_from_directory import deadsimplekv as simplekv +from httpapi.sse.wrapper import SSEWrapper from onionrusers import contactmanager from onionrutils import stringvalidators from utils import reconstructhash, identifyhome +from utils.bettersleep import better_sleep as sleep sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) import loadinbox @@ -35,6 +37,7 @@ flask_blueprint = Blueprint('mail', __name__) kv = simplekv.DeadSimpleKV(identifyhome.identify_home() + '/mailcache.dat') root = os.path.dirname(os.path.realpath(__file__)) +sse_wrapper = SSEWrapper() @flask_blueprint.route('/mail/', endpoint='mailstatic') def load_mail(path): @@ -68,6 +71,16 @@ def mail_delete(block): def list_inbox(): return ','.join(loadinbox.load_inbox()) + +@flask_blueprint.route('/mail/streaminbox') +def stream_inbox(): + def _stream(): + while True: + yield "data: " + ','.join(loadinbox.load_inbox()) + "\n\n" + sleep(1) + return sse_wrapper.handle_sse_request(_stream) + + @flask_blueprint.route('/mail/getsentbox') def list_sentbox(): kv.refresh() diff --git a/static-data/default-plugins/pms/web/index.html b/static-data/default-plugins/pms/web/index.html index a67be33f..4b6edc96 100755 --- a/static-data/default-plugins/pms/web/index.html +++ b/static-data/default-plugins/pms/web/index.html @@ -15,6 +15,7 @@ + diff --git a/static-data/default-plugins/pms/web/mail.js b/static-data/default-plugins/pms/web/mail.js index f693e808..8153c817 100755 --- a/static-data/default-plugins/pms/web/mail.js +++ b/static-data/default-plugins/pms/web/mail.js @@ -382,6 +382,9 @@ function refreshPms(callNext){ if (! window.inboxActive){ return } + if (document.hidden){ + return + } fetch('/mail/getinbox', { headers: { "token": webpass @@ -448,3 +451,17 @@ document.addEventListener("visibilitychange", function() { refreshPms() } }) + +/* +let mailStream = function(){ + var streamSource = new EventSourcePolyfill('/mail/streaminbox', { + headers: { + "token": webpass + } + }) + streamSource.onmessage = function(e){ + console.debug(e.data) + } +} +mailStream() +*/ \ No newline at end of file