From 4f39c5792ae28fccb5d48c85a3d04d8434262be2 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 21 Feb 2019 14:25:45 -0600 Subject: [PATCH] work on UI friends manager --- onionr/api.py | 19 ++++++-- onionr/communicator.py | 5 +- onionr/core.py | 5 -- onionr/httpapi/friendsapi/__init__.py | 48 +++++++++++++++++++ .../default-plugins/contactmanager/info.json | 5 ++ .../default-plugins/contactmanager/main.py | 39 +++++++++++++++ onionr/static-data/www/friends/friends.js | 21 ++++++++ onionr/static-data/www/friends/index.html | 32 +++++++++++++ onionr/static-data/www/friends/style.css | 15 ++++++ onionr/static-data/www/private/index.html | 7 +-- onionr/tests/test_blocks.py | 1 - 11 files changed, 183 insertions(+), 14 deletions(-) create mode 100644 onionr/httpapi/friendsapi/__init__.py create mode 100644 onionr/static-data/default-plugins/contactmanager/info.json create mode 100644 onionr/static-data/default-plugins/contactmanager/main.py create mode 100644 onionr/static-data/www/friends/friends.js create mode 100644 onionr/static-data/www/friends/index.html create mode 100644 onionr/static-data/www/friends/style.css diff --git a/onionr/api.py b/onionr/api.py index e98142ad..5db29a9b 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -21,10 +21,12 @@ from gevent.pywsgi import WSGIServer, WSGIHandler from gevent import Timeout import flask, cgi, uuid from flask import request, Response, abort, send_from_directory -import sys, random, threading, hmac, hashlib, base64, time, math, os, json, socket +import sys, random, threading, hmac, base64, time, os, json, socket import core from onionrblockapi import Block -import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config, onionr +import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config +from httpapi import friendsapi +import onionr class FDSafeHandler(WSGIHandler): '''Our WSGI handler. Doesn't do much non-default except timeouts''' @@ -250,7 +252,7 @@ class API: ''' # assert isinstance(onionrInst, onionr.Onionr) # configure logger and stuff - onionr.Onionr.setupConfig('data/', self = self) + #onionr.Onionr.setupConfig('data/', self = self) self.debug = debug self._core = onionrInst.onionrCore @@ -262,7 +264,7 @@ class API: self.bindPort = bindPort # Be extremely mindful of this. These are endpoints available without a password - self.whitelistEndpoints = ('site', 'www', 'onionrhome', 'board', 'boardContent', 'sharedContent', 'mail', 'mailindex') + self.whitelistEndpoints = ('site', 'www', 'onionrhome', 'board', 'boardContent', 'sharedContent', 'mail', 'mailindex', 'friends', 'friendsindex') self.clientToken = config.get('client.webpassword') self.timeBypassToken = base64.b16encode(os.urandom(32)).decode() @@ -276,6 +278,7 @@ class API: self.pluginResponses = {} # Responses for plugin endpoints self.queueResponse = {} onionrInst.setClientAPIInst(self) + app.register_blueprint(friendsapi.friends) @app.before_request def validateRequest(): @@ -315,6 +318,14 @@ class API: @app.route('/mail/', endpoint='mailindex') def loadMailIndex(): return send_from_directory('static-data/www/mail/', 'index.html') + + @app.route('/friends/', endpoint='friends') + def loadContacts(path): + return send_from_directory('static-data/www/friends/', path) + + @app.route('/friends/', endpoint='friendsindex') + def loadContacts(): + return send_from_directory('static-data/www/friends/', 'index.html') @app.route('/board/', endpoint='boardContent') def boardContent(path): diff --git a/onionr/communicator.py b/onionr/communicator.py index b6847422..6af66fac 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -171,7 +171,8 @@ class OnionrCommunicatorDaemon: # Validate new peers are good format and not already in queue invalid = [] for x in newPeers: - if not self._core._utils.validateID(x) or x in self.newPeers: + x = x.strip() + if not self._core._utils.validateID(x) or x in self.newPeers or x == self._core.hsAddress: invalid.append(x) for x in invalid: newPeers.remove(x) @@ -431,6 +432,8 @@ class OnionrCommunicatorDaemon: for address in peerList: if not config.get('tor.v3onions') and len(address) == 62: continue + if address == self._core.hsAddress: + continue if len(address) == 0 or address in tried or address in self.onlinePeers or address in self.cooldownPeer: continue if self.shutdown: diff --git a/onionr/core.py b/onionr/core.py index c1b743e4..b135c4f2 100755 --- a/onionr/core.py +++ b/onionr/core.py @@ -121,7 +121,6 @@ class Core: ''' Hack to refresh some vars which may not be set on first start ''' - if os.path.exists(self.dataDir + '/hs/hostname'): with open(self.dataDir + '/hs/hostname', 'r') as hs: self.hsAddress = hs.read().strip() @@ -597,10 +596,6 @@ class Core: conn = sqlite3.connect(self.blockDB, timeout=30) c = conn.cursor() - # if unsaved: - # execute = 'SELECT hash FROM hashes WHERE dataSaved != 1 ORDER BY RANDOM();' - # else: - # execute = 'SELECT hash FROM hashes ORDER BY dateReceived ASC;' execute = 'SELECT hash FROM hashes WHERE dateReceived >= ? ORDER BY dateReceived ASC;' args = (dateRec,) rows = list() diff --git a/onionr/httpapi/friendsapi/__init__.py b/onionr/httpapi/friendsapi/__init__.py new file mode 100644 index 00000000..bca0ad92 --- /dev/null +++ b/onionr/httpapi/friendsapi/__init__.py @@ -0,0 +1,48 @@ +''' + Onionr - P2P Anonymous Storage Network + + This file creates http endpoints for friend management +''' +''' + 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 . +''' +import core +from onionrusers import contactmanager +from flask import Blueprint, Response, request, abort + +friends = Blueprint('friends', __name__) + +@friends.route('/friends/add/', methods=['POST']) +def add_friend(pubkey): + contactmanager.ContactManager(core.Core(), pubkey, saveUser=True).setTrust(1) + return 'success' + +@friends.route('/friends/remove/', methods=['POST']) +def remove_friend(pubkey): + contactmanager.ContactManager(core.Core(), pubkey).setTrust(0) + return 'success' + +@friends.route('/friends/setinfo//', methods=['POST']) +def set_info(pubkey, key): + data = request.form['data'] + contactmanager.ContactManager(core.Core(), pubkey).set_info(key, data) + return 'success' + +@friends.route('/friends/getinfo//') +def get_info(pubkey, key): + retData = contactmanager.ContactManager(core.Core(), pubkey).get_info(key) + if retData is None: + abort(404) + else: + return retData \ No newline at end of file diff --git a/onionr/static-data/default-plugins/contactmanager/info.json b/onionr/static-data/default-plugins/contactmanager/info.json new file mode 100644 index 00000000..60967d52 --- /dev/null +++ b/onionr/static-data/default-plugins/contactmanager/info.json @@ -0,0 +1,5 @@ +{ + "name" : "contactmanager", + "version" : "1.0", + "author" : "onionr" +} diff --git a/onionr/static-data/default-plugins/contactmanager/main.py b/onionr/static-data/default-plugins/contactmanager/main.py new file mode 100644 index 00000000..2aaa423a --- /dev/null +++ b/onionr/static-data/default-plugins/contactmanager/main.py @@ -0,0 +1,39 @@ +''' + Onionr - P2P Anonymous Storage Network + + This is an interactive menu-driven CLI interface for Onionr +''' +''' + 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 . +''' + +plugin_name = 'contactmanager' + +class OnionrContactManager: + def __init__(self, api): + return + +def on_init(api, data = None): + ''' + This event is called after Onionr is initialized, but before the command + inputted is executed. Could be called when daemon is starting or when + just the client is running. + ''' + + # Doing this makes it so that the other functions can access the api object + # by simply referencing the variable `pluginapi`. + pluginapi = api + ui = OnionrCLIUI(api) + #api.commands.register('interactive', ui.start) + return diff --git a/onionr/static-data/www/friends/friends.js b/onionr/static-data/www/friends/friends.js new file mode 100644 index 00000000..b8c673f3 --- /dev/null +++ b/onionr/static-data/www/friends/friends.js @@ -0,0 +1,21 @@ +/* + Onionr - P2P Anonymous Storage Network + + This file handles the UI for managing friends/contacts + + 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 . +*/ + +friendListDisplay = document.getElementById('friendList') + diff --git a/onionr/static-data/www/friends/index.html b/onionr/static-data/www/friends/index.html new file mode 100644 index 00000000..265eb9ed --- /dev/null +++ b/onionr/static-data/www/friends/index.html @@ -0,0 +1,32 @@ + + + + + + Onionr + + + + + + +
+
+

Your node will shutdown. Thank you for using Onionr.

+
+
+
+ + Onionr Web Control Panel +

Friend Manager

+
+ + + +
+
+
+ + + + \ No newline at end of file diff --git a/onionr/static-data/www/friends/style.css b/onionr/static-data/www/friends/style.css new file mode 100644 index 00000000..37e4ede0 --- /dev/null +++ b/onionr/static-data/www/friends/style.css @@ -0,0 +1,15 @@ +h2, h3{ + font-family: Arial, Helvetica, sans-serif; +} + +form{ + border: 1px solid black; + border-radius: 5px; + padding: 1em; + margin-right: 10%; +} +form label{ + display: block; + margin-top: 0.5em; + margin-bottom: 0.5em; +} \ No newline at end of file diff --git a/onionr/static-data/www/private/index.html b/onionr/static-data/www/private/index.html index 80da5cdc..0b61cd45 100755 --- a/onionr/static-data/www/private/index.html +++ b/onionr/static-data/www/private/index.html @@ -14,11 +14,12 @@

Your node will shutdown. Thank you for using Onionr.

- - Onionr Web Control Panel
+ + Onionr Web Control Panel +

-

Mail +

Mail - Friend Manager

Stats

Uptime:

Last Received Connection: Unknown

diff --git a/onionr/tests/test_blocks.py b/onionr/tests/test_blocks.py index e0c699e9..257238b1 100644 --- a/onionr/tests/test_blocks.py +++ b/onionr/tests/test_blocks.py @@ -13,7 +13,6 @@ c = core.Core() class OnionrBlockTests(unittest.TestCase): def test_plaintext_insert(self): - return message = 'hello world' c.insertBlock(message)