From ce2423e6d9ac70e04d99e9902ccd45fde933fd2d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Mon, 10 Sep 2018 00:02:28 -0500 Subject: [PATCH] * moved metadata processor to its own module * improved some comments * removed defunct utils functions --- onionr/blockprocessor.py | 0 onionr/onionrsockets.py | 19 +++++++ onionr/onionrutils.py | 34 +++++------- .../metadataprocessor/info.json | 5 ++ .../default-plugins/metadataprocessor/main.py | 52 +++++++++++++++++++ 5 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 onionr/blockprocessor.py create mode 100644 onionr/onionrsockets.py create mode 100644 onionr/static-data/default-plugins/metadataprocessor/info.json create mode 100644 onionr/static-data/default-plugins/metadataprocessor/main.py diff --git a/onionr/blockprocessor.py b/onionr/blockprocessor.py new file mode 100644 index 00000000..e69de29b diff --git a/onionr/onionrsockets.py b/onionr/onionrsockets.py new file mode 100644 index 00000000..df4a1ad7 --- /dev/null +++ b/onionr/onionrsockets.py @@ -0,0 +1,19 @@ +''' + Onionr - P2P Anonymous Storage Network + + Onionr Socket interface +''' +''' + 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 . +''' diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 470dc125..6449e152 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -37,17 +37,20 @@ class OnionrUtils: Various useful functions for validating things, etc functions, connectivity ''' def __init__(self, coreInstance): - self.fingerprintFile = 'data/own-fingerprint.txt' - self._core = coreInstance + #self.fingerprintFile = 'data/own-fingerprint.txt' #TODO Remove since probably not needed + self._core = coreInstance # onionr core instance - self.timingToken = '' + self.timingToken = '' # for when we make local connections to our http api, to bypass timing attack defense mechanism self.avoidDupe = [] # list used to prevent duplicate requests per peer for certain actions self.peerProcessing = {} # dict of current peer actions: peer, actionList - self.storageCounter = storagecounter.StorageCounter(self._core) - config.reload() + self.storageCounter = storagecounter.StorageCounter(self._core) # used to keep track of how much data onionr is using on disk + config.reload() # onionr config return def getTimeBypassToken(self): + ''' + Load our timingToken from disk for faster local HTTP API + ''' try: if os.path.exists('data/time-bypass.txt'): with open('data/time-bypass.txt', 'r') as bypass: @@ -64,22 +67,6 @@ class OnionrUtils: epoch = self.getEpoch() return epoch - (epoch % roundS) - def incrementAddressSuccess(self, address): - ''' - Increase the recorded sucesses for an address - ''' - increment = self._core.getAddressInfo(address, 'success') + 1 - self._core.setAddressInfo(address, 'success', increment) - return - - def decrementAddressSuccess(self, address): - ''' - Decrease the recorded sucesses for an address - ''' - increment = self._core.getAddressInfo(address, 'success') - 1 - self._core.setAddressInfo(address, 'success', increment) - return - def mergeKeys(self, newKeyList): ''' Merge ed25519 key list to our database, comma seperated string @@ -89,6 +76,7 @@ class OnionrUtils: if newKeyList != False: for key in newKeyList.split(','): key = key.split('-') + # Test if key is valid try: if len(key[0]) > 60 or len(key[1]) > 1000: logger.warn('%s or its pow value is too large.' % key[0]) @@ -100,15 +88,19 @@ class OnionrUtils: value = base64.b64decode(key[1]) except binascii.Error: continue + # Load the pow token hashedKey = self._core._crypto.blake2bHash(key[0]) powHash = self._core._crypto.blake2bHash(value + hashedKey) try: powHash = powHash.encode() except AttributeError: pass + # if POW meets required difficulty, TODO make configurable/dynamic if powHash.startswith(b'0000'): + # if we don't already have the key and its not our key, add it. if not key[0] in self._core.listPeers(randomOrder=False) and type(key) != None and key[0] != self._core._crypto.pubKey: if self._core.addPeer(key[0], key[1]): + # Check if the peer has a set username already onionrusers.OnionrUser(self._core, key[0]).findAndSetID() retVal = True else: diff --git a/onionr/static-data/default-plugins/metadataprocessor/info.json b/onionr/static-data/default-plugins/metadataprocessor/info.json new file mode 100644 index 00000000..355d98f1 --- /dev/null +++ b/onionr/static-data/default-plugins/metadataprocessor/info.json @@ -0,0 +1,5 @@ +{ + "name" : "metadataprocessor", + "version" : "1.0", + "author" : "onionr" +} diff --git a/onionr/static-data/default-plugins/metadataprocessor/main.py b/onionr/static-data/default-plugins/metadataprocessor/main.py new file mode 100644 index 00000000..842eaf88 --- /dev/null +++ b/onionr/static-data/default-plugins/metadataprocessor/main.py @@ -0,0 +1,52 @@ +''' + Onionr - P2P Anonymous Storage Network + + This processes metadata for Onionr blocks +''' +''' + 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 . +''' + +# useful libraries +import logger, config +import os, sys, json, time, random, shutil, base64, getpass, datetime, re +from onionrblockapi import Block + +plugin_name = 'metadataprocessor' + +# event listeners + +def on_processBlocks(api): + myBlock = api.data['block'] + blockType = api.data['type'] + print('blockType is ' + blockType) + if blockType == 'userInfo': + if myBlock.verifySig(): + peerName = myBlock.getMetadata('name') + try: + if len(peerName) > 20: + raise onionrexceptions.InvalidMetdata('Peer name specified is too large') + except TypeError: + pass + except onionrexceptions.InvalidMetadata: + pass + else: + api.get_core().setPeerInfo(signer, 'name', peerName) + logger.info('%s is now using the name %s.' % (signer, api.get_utils().escapeAnsi(peerName))) + +def on_init(api, data = None): + + pluginapi = api + + return