From c61c8336587d8c8166b93f831fb5db4a3b11279d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 22 Feb 2019 15:04:03 -0600 Subject: [PATCH] work on UI friends manager --- onionr/api.py | 12 ++++--- onionr/httpapi/friendsapi/__init__.py | 4 +-- onionr/onionrblockapi.py | 32 +++--------------- onionr/onionrdaemontools.py | 3 +- onionr/static-data/www/friends/friends.js | 34 ++++++++++++++++++-- onionr/static-data/www/friends/index.html | 4 +-- onionr/static-data/www/friends/style.css | 11 +++++++ onionr/static-data/www/shared/main/style.css | 20 +++++++++++- onionr/static-data/www/shared/misc.js | 20 ++++++++++++ 9 files changed, 100 insertions(+), 40 deletions(-) diff --git a/onionr/api.py b/onionr/api.py index 5db29a9b..402622d9 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -290,9 +290,11 @@ class API: return try: if not hmac.compare_digest(request.headers['token'], self.clientToken): - abort(403) + if not hmac.compare_digest(request.form['token'], self.clientToken): + abort(403) except KeyError: - abort(403) + if not hmac.compare_digest(request.form['token'], self.clientToken): + abort(403) @app.after_request def afterReq(resp): @@ -417,14 +419,16 @@ class API: if self._core._utils.validateHash(bHash): try: resp = Block(bHash).bcontent + except onionrexceptions.NoDataAvailable: + abort(404) except TypeError: pass try: resp = base64.b64decode(resp) except: pass - if resp == 'Not Found': - abourt(404) + if resp == 'Not Found' or not resp: + abort(404) return Response(resp) @app.route('/waitforshare/', methods=['post']) diff --git a/onionr/httpapi/friendsapi/__init__.py b/onionr/httpapi/friendsapi/__init__.py index 80ade1f4..d6a3c2f5 100644 --- a/onionr/httpapi/friendsapi/__init__.py +++ b/onionr/httpapi/friendsapi/__init__.py @@ -19,7 +19,7 @@ ''' import core, json from onionrusers import contactmanager -from flask import Blueprint, Response, request, abort +from flask import Blueprint, Response, request, abort, redirect friends = Blueprint('friends', __name__) @@ -45,7 +45,7 @@ def remove_friend(pubkey): def set_info(pubkey, key): data = request.form['data'] contactmanager.ContactManager(core.Core(), pubkey).set_info(key, data) - return 'success' + return redirect(request.referrer + '#' + request.form['token']) @friends.route('/friends/getinfo//') def get_info(pubkey, key): diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index e9724b03..6921ab90 100755 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -139,32 +139,10 @@ class Block: # import from file if blockdata is None: - blockdata = onionrstorage.getData(self.core, self.getHash()).decode() - ''' - - filelocation = file - - readfile = True - - if filelocation is None: - if self.getHash() is None: - return False - elif self.getHash() in Block.getCache(): - # get the block from cache, if it's in it - blockdata = Block.getCache(self.getHash()) - readfile = False - - # read from file if it's still None - if blockdata is None: - filelocation = self.core.dataDir + 'blocks/%s.dat' % self.getHash() - - if readfile: + try: blockdata = onionrstorage.getData(self.core, self.getHash()).decode() - #with open(filelocation, 'rb') as f: - #blockdata = f.read().decode() - - self.blockFile = filelocation - ''' + except AttributeError: + raise onionrexceptions.NoDataAvailable('Block does not exist') else: self.blockFile = None # parse block @@ -200,11 +178,11 @@ class Block: return True except Exception as e: - logger.error('Failed to parse block %s.' % self.getHash(), error = e, timestamp = False) + logger.warn('Failed to parse block %s.' % self.getHash(), error = e, timestamp = False) # if block can't be parsed, it's a waste of precious space. Throw it away. if not self.delete(): - logger.error('Failed to delete invalid block %s.' % self.getHash(), error = e) + logger.warn('Failed to delete invalid block %s.' % self.getHash(), error = e) else: logger.debug('Deleted invalid block %s.' % self.getHash(), timestamp = False) diff --git a/onionr/onionrdaemontools.py b/onionr/onionrdaemontools.py index dace5b06..bae9031e 100755 --- a/onionr/onionrdaemontools.py +++ b/onionr/onionrdaemontools.py @@ -89,7 +89,8 @@ class DaemonTools: '''Check if we are connected to the internet or not when we can't connect to any peers''' if len(self.daemon.onlinePeers) == 0: if not netutils.checkNetwork(self.daemon._core._utils, torPort=self.daemon.proxyPort): - logger.warn('Network check failed, are you connected to the internet?') + if not self.daemon.shutdown: + logger.warn('Network check failed, are you connected to the internet?') self.daemon.isOnline = False else: self.daemon.isOnline = True diff --git a/onionr/static-data/www/friends/friends.js b/onionr/static-data/www/friends/friends.js index 313e1bb8..51e5fc21 100644 --- a/onionr/static-data/www/friends/friends.js +++ b/onionr/static-data/www/friends/friends.js @@ -18,6 +18,24 @@ */ friendListDisplay = document.getElementById('friendList') +addForm = document.getElementById('addFriend') + +addForm.onsubmit = function(){ + var friend = document.getElementsByName('addKey')[0] + var alias = document.getElementsByName('data')[0] + + fetch('/friends/add/' + friend.value, { + method: 'POST', + headers: { + "token": webpass + }}).then(function(data) { + if (alias.value.trim().length > 0){ + post_to_url('/friends/setinfo/' + friend.value + '/name', {'data': alias.value, 'token': webpass}) + } + }) + + return false +} fetch('/friends/list', { headers: { @@ -28,16 +46,26 @@ fetch('/friends/list', { var keys = []; for(var k in resp) keys.push(k); console.log(keys) + friendListDisplay.innerHTML = 'Click name to view info

' for (var i = 0; i < keys.length; i++){ - friendListDisplay.innerText = '' var peer = keys[i] var name = resp[keys[i]]['name'] if (name === null || name === ''){ - name = 'Anonymous' + name = peer } var entry = document.createElement('div') + var nameText = document.createElement('input') + removeButton = document.createElement('button') + removeButton.classList.add('friendRemove') + removeButton.classList.add('dangerBtn') + entry.setAttribute('data-pubkey', peer) + removeButton.innerText = 'X' + nameText.value = name + nameText.readOnly = true + nameText.style.fontStyle = "italic" entry.style.paddingTop = '8px' - entry.innerText = name + ' - ' + peer + entry.appendChild(removeButton) + entry.appendChild(nameText) friendListDisplay.appendChild(entry) } }) \ No newline at end of file diff --git a/onionr/static-data/www/friends/index.html b/onionr/static-data/www/friends/index.html index e45bf6ce..6110a29d 100644 --- a/onionr/static-data/www/friends/index.html +++ b/onionr/static-data/www/friends/index.html @@ -16,8 +16,8 @@

Friend Manager

- - + +

Friend List:

None Yet :(
diff --git a/onionr/static-data/www/friends/style.css b/onionr/static-data/www/friends/style.css index 37e4ede0..663c4b4e 100644 --- a/onionr/static-data/www/friends/style.css +++ b/onionr/static-data/www/friends/style.css @@ -12,4 +12,15 @@ form label{ display: block; margin-top: 0.5em; margin-bottom: 0.5em; +} + +#friendList{ + display: inline; +} +#friendList span{ + text-align: center; +} +#friendList button{ + display: inline; + margin-right: 10px; } \ No newline at end of file diff --git a/onionr/static-data/www/shared/main/style.css b/onionr/static-data/www/shared/main/style.css index 96cb5e88..65c378eb 100755 --- a/onionr/static-data/www/shared/main/style.css +++ b/onionr/static-data/www/shared/main/style.css @@ -150,4 +150,22 @@ body{ .closeOverlay:after{ content: '❌'; padding: 5px; - } \ No newline at end of file + } + + .btn, .warnBtn, .dangerBtn, .successBtn{ + padding: 5px; + border-radius: 5px; + border: 2px solid black; + } +.warnBtn{ + background-color: orange; + color: black; +} +.dangerBtn{ + background-color: #f44336; + color: black; +} +.successBtn{ + background-color: #4CAF50; + color: black; +} \ No newline at end of file diff --git a/onionr/static-data/www/shared/misc.js b/onionr/static-data/www/shared/misc.js index d4322887..bdde2f84 100755 --- a/onionr/static-data/www/shared/misc.js +++ b/onionr/static-data/www/shared/misc.js @@ -20,6 +20,25 @@ webpass = document.location.hash.replace('#', '') nowebpass = false +function post_to_url(path, params) { + + var form = document.createElement("form") + + form.setAttribute("method", "POST") + form.setAttribute("action", path) + + for(var key in params) { + var hiddenField = document.createElement("input") + hiddenField.setAttribute("type", "hidden") + hiddenField.setAttribute("name", key) + hiddenField.setAttribute("value", params[key]) + form.appendChild(hiddenField) + } + + document.body.appendChild(form) + form.submit() +} + if (typeof webpass == "undefined"){ webpass = localStorage['webpass'] } @@ -67,3 +86,4 @@ for(var i = 0; i < refreshLinks.length; i++) { location.reload() } } +