From c89bf5b430cc6054c969a3e42096995a4bb799b6 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 2 Mar 2019 00:22:59 -0600 Subject: [PATCH] work on plugins doing http endpoints --- .gitlab-ci.yml | 0 Makefile | 3 ++ onionr/__init__.py | 0 onionr/api.py | 7 +++-- .../onionrcommunicatortimers.py | 0 onionr/core.py | 4 +-- onionr/httpapi/__init__.py | 10 ++++++ onionr/httpapi/friendsapi/__init__.py | 0 onionr/httpapi/simplecache/__init__.py | 31 +++++++++++++++++++ onionr/onionrfragment.py | 0 onionr/onionrplugins.py | 1 - onionr/onionrstorage.py | 8 ----- onionr/onionrusers/contactmanager.py | 0 .../default-plugins/contactmanager/info.json | 0 .../default-plugins/contactmanager/main.py | 0 .../static-data/default-plugins/pms/main.py | 27 ++++++++++++++-- onionr/static-data/www/friends/friends.js | 0 onionr/static-data/www/friends/index.html | 0 onionr/static-data/www/friends/style.css | 0 onionr/static-data/www/mail/mail.js | 1 - onionr/static-data/www/mail/sendmail.js | 0 onionr/tests/test_blocks.py | 0 onionr/tests/test_database_actions.py | 0 onionr/tests/test_database_creation.py | 0 onionr/tests/test_forward_secrecy.py | 0 onionr/tests/test_highlevelcrypto.py | 0 onionr/tests/test_onionrusers.py | 0 onionr/tests/test_stringvalidations.py | 0 onionr/utils/netutils.py | 0 onionr/utils/networkmerger.py | 0 requirements.txt | 1 + 31 files changed, 76 insertions(+), 17 deletions(-) mode change 100644 => 100755 .gitlab-ci.yml mode change 100644 => 100755 onionr/__init__.py mode change 100644 => 100755 onionr/communicatorutils/onionrcommunicatortimers.py create mode 100755 onionr/httpapi/__init__.py mode change 100644 => 100755 onionr/httpapi/friendsapi/__init__.py create mode 100755 onionr/httpapi/simplecache/__init__.py mode change 100644 => 100755 onionr/onionrfragment.py mode change 100644 => 100755 onionr/onionrusers/contactmanager.py mode change 100644 => 100755 onionr/static-data/default-plugins/contactmanager/info.json mode change 100644 => 100755 onionr/static-data/default-plugins/contactmanager/main.py mode change 100644 => 100755 onionr/static-data/www/friends/friends.js mode change 100644 => 100755 onionr/static-data/www/friends/index.html mode change 100644 => 100755 onionr/static-data/www/friends/style.css mode change 100644 => 100755 onionr/static-data/www/mail/sendmail.js mode change 100644 => 100755 onionr/tests/test_blocks.py mode change 100644 => 100755 onionr/tests/test_database_actions.py mode change 100644 => 100755 onionr/tests/test_database_creation.py mode change 100644 => 100755 onionr/tests/test_forward_secrecy.py mode change 100644 => 100755 onionr/tests/test_highlevelcrypto.py mode change 100644 => 100755 onionr/tests/test_onionrusers.py mode change 100644 => 100755 onionr/tests/test_stringvalidations.py mode change 100644 => 100755 onionr/utils/netutils.py mode change 100644 => 100755 onionr/utils/networkmerger.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml old mode 100644 new mode 100755 diff --git a/Makefile b/Makefile index 4721e97c..4fdae5b8 100755 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ PREFIX = /usr/local .DEFAULT_GOAL := setup +SHELL := env ONIONR_HOME=$(ONIONR_HOME) $(SHELL) +ONIONR_HOME ?= "data" + setup: sudo pip3 install -r requirements.txt -@cd onionr/static-data/ui/; ./compile.py diff --git a/onionr/__init__.py b/onionr/__init__.py old mode 100644 new mode 100755 diff --git a/onionr/api.py b/onionr/api.py index 647d7635..baf60023 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -25,7 +25,8 @@ 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 -from httpapi import friendsapi +import httpapi +from httpapi import friendsapi, simplecache import onionr class FDSafeHandler(WSGIHandler): @@ -279,6 +280,8 @@ class API: self.queueResponse = {} onionrInst.setClientAPIInst(self) app.register_blueprint(friendsapi.friends) + app.register_blueprint(simplecache.simplecache) + httpapi.load_plugin_blueprints(app) @app.before_request def validateRequest(): @@ -521,13 +524,13 @@ class API: responseTimeout = 20 startTime = self._core._utils.getEpoch() postData = {} + print('spth', subpath) if request.method == 'POST': postData = request.form['postData'] if len(subpath) > 1: data = subpath.split('/') if len(data) > 1: plName = data[0] - events.event('pluginRequest', {'name': plName, 'path': subpath, 'pluginResponse': pluginResponseCode, 'postData': postData}, onionr=onionrInst) while True: try: diff --git a/onionr/communicatorutils/onionrcommunicatortimers.py b/onionr/communicatorutils/onionrcommunicatortimers.py old mode 100644 new mode 100755 diff --git a/onionr/core.py b/onionr/core.py index 6b2e1505..73e237ea 100755 --- a/onionr/core.py +++ b/onionr/core.py @@ -19,7 +19,7 @@ ''' import sqlite3, os, sys, time, math, base64, tarfile, nacl, logger, json, netcontroller, math, config, uuid from onionrblockapi import Block - +import deadsimplekv as simplekv import onionrutils, onionrcrypto, onionrproofs, onionrevents as events, onionrexceptions import onionrblacklist from onionrusers import onionrusers @@ -65,6 +65,7 @@ class Core: self.dataNonceFile = self.dataDir + 'block-nonces.dat' self.dbCreate = dbcreator.DBCreator(self) self.forwardKeysFile = self.dataDir + 'forward-keys.db' + #self.keyStore = simplekv.DeadSimpleKV(self.dataDir + 'cachedstorage.dat', refresh_seconds=5) # Socket data, defined here because of multithreading constraints with gevent self.killSockets = False @@ -105,7 +106,6 @@ class Core: logger.warn('Warning: address bootstrap file not found ' + self.bootstrapFileLocation) self._utils = onionrutils.OnionrUtils(self) - self.blockCache = onionrstorage.BlockCache() # Initialize the crypto object self._crypto = onionrcrypto.OnionrCrypto(self) self._blacklist = onionrblacklist.OnionrBlackList(self) diff --git a/onionr/httpapi/__init__.py b/onionr/httpapi/__init__.py new file mode 100755 index 00000000..137fe137 --- /dev/null +++ b/onionr/httpapi/__init__.py @@ -0,0 +1,10 @@ +from flask import request +import onionrplugins + +def load_plugin_blueprints(flaskapp): + for plugin in onionrplugins.get_enabled_plugins(): + plugin = onionrplugins.get_plugin(plugin) + try: + flaskapp.register_blueprint(getattr(plugin, 'flask_blueprint')) + except AttributeError: + pass \ No newline at end of file diff --git a/onionr/httpapi/friendsapi/__init__.py b/onionr/httpapi/friendsapi/__init__.py old mode 100644 new mode 100755 diff --git a/onionr/httpapi/simplecache/__init__.py b/onionr/httpapi/simplecache/__init__.py new file mode 100755 index 00000000..75a645a0 --- /dev/null +++ b/onionr/httpapi/simplecache/__init__.py @@ -0,0 +1,31 @@ +''' + 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 flask import Blueprint, Response, request, abort + +simplecache = Blueprint('simplecache', __name__) + +@simplecache.route('/get/') +def get_key(key): + return + +@simplecache.route('/set/', methods=['POST']) +def set_key(key): + return \ No newline at end of file diff --git a/onionr/onionrfragment.py b/onionr/onionrfragment.py old mode 100644 new mode 100755 diff --git a/onionr/onionrplugins.py b/onionr/onionrplugins.py index 7d81e08c..198e58da 100755 --- a/onionr/onionrplugins.py +++ b/onionr/onionrplugins.py @@ -59,7 +59,6 @@ def reload(onionr = None, stop_event = True): return False - def enable(name, onionr = None, start_event = True): ''' Enables a plugin diff --git a/onionr/onionrstorage.py b/onionr/onionrstorage.py index 63aa150d..65fe6757 100755 --- a/onionr/onionrstorage.py +++ b/onionr/onionrstorage.py @@ -21,13 +21,6 @@ import core, sys, sqlite3, os, dbcreator DB_ENTRY_SIZE_LIMIT = 10000 # Will be a config option -class BlockCache: - def __init__(self): - self.blocks = {} - def cleanCache(self): - while sys.getsizeof(self.blocks) > 100000000: - self.blocks.pop(list(self.blocks.keys())[0]) - def dbCreate(coreInst): try: dbcreator.DBCreator(coreInst).createBlockDataDB() @@ -84,7 +77,6 @@ def store(coreInst, data, blockHash=''): else: with open('%s/%s.dat' % (coreInst.blockDataLocation, blockHash), 'wb') as blockFile: blockFile.write(data) - coreInst.blockCache.cleanCache() def getData(coreInst, bHash): assert isinstance(coreInst, core.Core) diff --git a/onionr/onionrusers/contactmanager.py b/onionr/onionrusers/contactmanager.py old mode 100644 new mode 100755 diff --git a/onionr/static-data/default-plugins/contactmanager/info.json b/onionr/static-data/default-plugins/contactmanager/info.json old mode 100644 new mode 100755 diff --git a/onionr/static-data/default-plugins/contactmanager/main.py b/onionr/static-data/default-plugins/contactmanager/main.py old mode 100644 new mode 100755 diff --git a/onionr/static-data/default-plugins/pms/main.py b/onionr/static-data/default-plugins/pms/main.py index d0552a94..8afd099e 100755 --- a/onionr/static-data/default-plugins/pms/main.py +++ b/onionr/static-data/default-plugins/pms/main.py @@ -23,15 +23,21 @@ import logger, config, threading, time, readline, datetime from onionrblockapi import Block import onionrexceptions from onionrusers import onionrusers +from flask import Response, request, redirect, Blueprint import locale, sys, os, json locale.setlocale(locale.LC_ALL, '') +plugin_name = 'pms' +PLUGIN_VERSION = '0.0.1' +flask_blueprint = Blueprint('mail', __name__) + sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) import sentboxdb # import after path insert -plugin_name = 'pms' -PLUGIN_VERSION = '0.0.1' +@flask_blueprint.route('/mailhello') +def mailhello(): + return "hello world from mail" def draw_border(text): #https://stackoverflow.com/a/20757491 @@ -190,7 +196,6 @@ class OnionrMail: finally: if choice == '-q': entering = False - return def get_sent_list(self, display=True): @@ -293,6 +298,15 @@ class OnionrMail: logger.warn('Invalid choice.') return +def add_deleted(keyStore, bHash): + existing = keyStore.get('deleted_mail') + if existing is None: + existing = [] + else: + if bHash in existing: + return + keyStore.put('deleted_mail', existing.append(bHash)) + def on_insertblock(api, data={}): sentboxTools = sentboxdb.SentBox(api.get_core()) meta = json.loads(data['meta']) @@ -306,11 +320,17 @@ def on_pluginrequest(api, data=None): postData = {} blockID = '' sentboxTools = sentboxdb.SentBox(api.get_core()) + keyStore = api.get_core().keyStore if data['name'] == 'mail': path = data['path'] + print(cmd) cmd = path.split('/')[1] if cmd == 'sentbox': resp = OnionrMail(api).get_sent_list(display=False) + elif cmd == 'deletemsg': + print('path', data['path']) + #add_deleted(keyStore, data['path'].split('/')[2]) + resp = 'success' if resp != '': api.get_onionr().clientAPIInst.pluginResponses[data['pluginResponse']] = resp @@ -325,4 +345,5 @@ def on_init(api, data = None): mail = OnionrMail(pluginapi) api.commands.register(['mail'], mail.menu) api.commands.register_help('mail', 'Interact with OnionrMail') + print("YEET2") return diff --git a/onionr/static-data/www/friends/friends.js b/onionr/static-data/www/friends/friends.js old mode 100644 new mode 100755 diff --git a/onionr/static-data/www/friends/index.html b/onionr/static-data/www/friends/index.html old mode 100644 new mode 100755 diff --git a/onionr/static-data/www/friends/style.css b/onionr/static-data/www/friends/style.css old mode 100644 new mode 100755 diff --git a/onionr/static-data/www/mail/mail.js b/onionr/static-data/www/mail/mail.js index 71b75c6c..d057099b 100755 --- a/onionr/static-data/www/mail/mail.js +++ b/onionr/static-data/www/mail/mail.js @@ -233,7 +233,6 @@ for (var i = 0; i < idStrings.length; i++){ } } - for (var i = 0; i < document.getElementsByClassName('refresh').length; i++){ document.getElementsByClassName('refresh')[i].style.float = 'right' } diff --git a/onionr/static-data/www/mail/sendmail.js b/onionr/static-data/www/mail/sendmail.js old mode 100644 new mode 100755 diff --git a/onionr/tests/test_blocks.py b/onionr/tests/test_blocks.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_database_actions.py b/onionr/tests/test_database_actions.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_database_creation.py b/onionr/tests/test_database_creation.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_forward_secrecy.py b/onionr/tests/test_forward_secrecy.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_highlevelcrypto.py b/onionr/tests/test_highlevelcrypto.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_onionrusers.py b/onionr/tests/test_onionrusers.py old mode 100644 new mode 100755 diff --git a/onionr/tests/test_stringvalidations.py b/onionr/tests/test_stringvalidations.py old mode 100644 new mode 100755 diff --git a/onionr/utils/netutils.py b/onionr/utils/netutils.py old mode 100644 new mode 100755 diff --git a/onionr/utils/networkmerger.py b/onionr/utils/networkmerger.py old mode 100644 new mode 100755 diff --git a/requirements.txt b/requirements.txt index 6bceb49a..6b1cb663 100755 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ defusedxml==0.5.0 Flask==1.0.2 PySocks==1.6.8 stem==1.6.0 +deadsimplekv==0.0.1 \ No newline at end of file