From 45940b2dba45e80fd9de2ac9d4a528dca65c629b Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 3 Oct 2020 22:43:18 +0000 Subject: [PATCH] + added address adding input to web ui in sidebar fixes #10 --- src/httpapi/miscclientapi/addpeer.py | 14 ++++++++++ src/httpapi/miscclientapi/endpoints.py | 9 ++++++ src/onionrblocks/insert/main.py | 2 +- static-data/www/shared/sidebar/sidebar.html | 12 ++++++++ static-data/www/shared/sidebar/sidebar.js | 31 ++++++++++++++++++++- 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 src/httpapi/miscclientapi/addpeer.py diff --git a/src/httpapi/miscclientapi/addpeer.py b/src/httpapi/miscclientapi/addpeer.py new file mode 100644 index 00000000..82c2cc64 --- /dev/null +++ b/src/httpapi/miscclientapi/addpeer.py @@ -0,0 +1,14 @@ +from onionrutils.stringvalidators import validate_transport +from coredb.keydb.addkeys import add_address +from coredb.keydb.listkeys import list_adders + + +def add_peer(peer): + # this is ok for security since add_address does this manually + assert validate_transport(peer) + if peer in list_adders(): + return "already added" + if add_address(peer): + return "success" + else: + return "failure, invalid address" diff --git a/src/httpapi/miscclientapi/endpoints.py b/src/httpapi/miscclientapi/endpoints.py index aa64b462..01408114 100644 --- a/src/httpapi/miscclientapi/endpoints.py +++ b/src/httpapi/miscclientapi/endpoints.py @@ -20,6 +20,7 @@ from onionrutils import bytesconverter from etc import onionrvalues from utils import reconstructhash from utils.gettransports import get as get_tor +from .addpeer import add_peer """ 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 @@ -46,6 +47,14 @@ class PrivateEndpoints: private_endpoints_bp = Blueprint('privateendpoints', __name__) self.private_endpoints_bp = private_endpoints_bp + @private_endpoints_bp.route('/addpeer/', methods=['post']) + def add_peer_endpoint(name): + result = add_peer(name) + if result == "success": + return Response("success") + else: + return Response(result, 409) + @private_endpoints_bp.route('/www/', endpoint='www') def wwwPublic(path): if not config.get("www.private.run", True): diff --git a/src/onionrblocks/insert/main.py b/src/onionrblocks/insert/main.py index 97c3a98c..ebae296e 100644 --- a/src/onionrblocks/insert/main.py +++ b/src/onionrblocks/insert/main.py @@ -205,7 +205,7 @@ def insert_block(data: Union[str, bytes], header: str = 'txt', # send block data (and metadata) to POW module to get tokenized block data payload = subprocesspow.SubprocessPOW(data, metadata).start() - print(payload) + if payload != False: # noqa try: retData = onionrstorage.set_data(payload) diff --git a/static-data/www/shared/sidebar/sidebar.html b/static-data/www/shared/sidebar/sidebar.html index dea7078e..4538e35b 100644 --- a/static-data/www/shared/sidebar/sidebar.html +++ b/static-data/www/shared/sidebar/sidebar.html @@ -10,6 +10,18 @@
Blocks to upload: unknown
+
+
+
+ +
+ +
+
diff --git a/static-data/www/shared/sidebar/sidebar.js b/static-data/www/shared/sidebar/sidebar.js index 8c5d1253..861892db 100644 --- a/static-data/www/shared/sidebar/sidebar.js +++ b/static-data/www/shared/sidebar/sidebar.js @@ -7,8 +7,38 @@ fetch('/shared/sidebar/sidebar.html', { .then(function(resp) { document.getElementById('sidebarContainer').innerHTML = resp var quickviews = bulmaQuickview.attach() + sidebarAddPeerRegister() }) +function sidebarAddPeerRegister(){ + document.getElementById('addPeerBtn').onclick = function(){ + let newPeer = document.getElementById('addPeerInput').value + fetch('/addpeer/' + newPeer, { + method: 'POST', + headers: { + "token": webpass + }}) + .then((resp) => resp.text()) + .then(function(data) { + alert(data) + if (data == "success"){ + PNotify.success({ + text: 'Peer added' + }) + return + } + else if(data == "already added"){ + PNotify.notice({ + text: 'Peer already added' + }) + return + } + PNotify.error({text: data}) + + }) + } +} + window.addEventListener("keydown", function(event) { if (event.key === "s"){ if (document.activeElement.nodeName == "TEXTAREA" || document.activeElement.nodeName == "INPUT"){ @@ -44,7 +74,6 @@ window.addEventListener("keydown", function(event) { }}) .then((resp) => resp.text()) .then(function(resp) { - console.debug(resp.length, existingUploadValue) if (resp.length <= 2 && existingUploadValue !== "0"){ document.getElementById("uploadBlocks").innerText = "0" return