From 63b32f98dfb4e389592efcb06049548c3ce97bc5 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Tue, 10 Sep 2019 15:25:50 -0500 Subject: [PATCH] block data portion now pure bytes when encrypted (barely tested), and improved config testing --- onionr/onionrblockapi.py | 21 ++-- onionr/onionrblocks/insert.py | 2 +- onionr/onionrutils/blockmetadata/fromdata.py | 6 +- onionr/static-data/default_config.json | 103 ++++++++++--------- onionr/tests/test_default_config_json.py | 21 ++++ onionr/utils/readstatic.py | 2 +- 6 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 onionr/tests/test_default_config_json.py diff --git a/onionr/onionrblockapi.py b/onionr/onionrblockapi.py index 2d50d686..14ebbd52 100755 --- a/onionr/onionrblockapi.py +++ b/onionr/onionrblockapi.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' - +import binascii import logger, config, onionrexceptions, nacl.exceptions import json, os, sys, datetime, base64, onionrstorage from onionrusers import onionrusers @@ -70,7 +70,10 @@ class Block: # decrypt data if self.getHeader('encryptType') == 'asym': try: - self.bcontent = encryption.pub_key_decrypt(self.bcontent, encodedData=encodedData) + try: + self.bcontent = encryption.pub_key_decrypt(self.bcontent, encodedData=encodedData) + except binascii.Error: + self.bcontent = encryption.pub_key_decrypt(self.bcontent, encodedData=not encodedData) bmeta = encryption.pub_key_decrypt(self.bmetadata, encodedData=encodedData) try: bmeta = bmeta.decode() @@ -81,7 +84,7 @@ class Block: self.signature = encryption.pub_key_decrypt(self.signature, encodedData=encodedData) self.signer = encryption.pub_key_decrypt(self.signer, encodedData=encodedData) self.bheader['signer'] = self.signer.decode() - self.signedData = json.dumps(self.bmetadata) + self.bcontent.decode() + self.signedData = json.dumps(self.bmetadata).encode() + self.bcontent if not self.signer is None: if not self.verifySig(): @@ -152,15 +155,15 @@ class Block: # import from file if blockdata is None: try: - blockdata = onionrstorage.getData(self.getHash()).decode() + blockdata = onionrstorage.getData(self.getHash())#.decode() except AttributeError: raise onionrexceptions.NoDataAvailable('Block does not exist') else: self.blockFile = None # parse block - self.raw = str(blockdata) - self.bheader = json.loads(self.getRaw()[:self.getRaw().index('\n')]) - self.bcontent = self.getRaw()[self.getRaw().index('\n') + 1:] + self.raw = blockdata + self.bheader = json.loads(self.getRaw()[:self.getRaw().index(b'\n')]) + self.bcontent = self.getRaw()[self.getRaw().index(b'\n') + 1:] if ('encryptType' in self.bheader) and (self.bheader['encryptType'] in ('asym', 'sym')): self.bmetadata = self.getHeader('meta', None) self.isEncrypted = True @@ -278,10 +281,10 @@ class Block: Returns the raw contents of the block, if saved to file Outputs: - - (str): the raw contents of the block, or None + - (bytes): the raw contents of the block, or None ''' - return str(self.raw) + return self.raw def getHeader(self, key = None, default = None): ''' diff --git a/onionr/onionrblocks/insert.py b/onionr/onionrblocks/insert.py index 6297d317..b99333ab 100644 --- a/onionr/onionrblocks/insert.py +++ b/onionr/onionrblocks/insert.py @@ -101,7 +101,7 @@ def insert_block(data: Union[str, bytes], header: str ='txt', # Encrypt block data with forward secrecy key first, but not meta jsonMeta = json.dumps(meta) jsonMeta = crypto.encryption.pub_key_encrypt(jsonMeta, asymPeer, encodedData=True).decode() - data = crypto.encryption.pub_key_encrypt(data, asymPeer, encodedData=True).decode() + data = crypto.encryption.pub_key_encrypt(data, asymPeer, encodedData=False)#.decode() signature = crypto.encryption.pub_key_encrypt(signature, asymPeer, encodedData=True).decode() signer = crypto.encryption.pub_key_encrypt(signer, asymPeer, encodedData=True).decode() try: diff --git a/onionr/onionrutils/blockmetadata/fromdata.py b/onionr/onionrutils/blockmetadata/fromdata.py index 3ab5b6ab..a39f54c3 100644 --- a/onionr/onionrutils/blockmetadata/fromdata.py +++ b/onionr/onionrutils/blockmetadata/fromdata.py @@ -19,6 +19,8 @@ ''' import json + +from onionrutils import bytesconverter def get_block_metadata_from_data(block_data): ''' accepts block contents as string, returns a tuple of @@ -34,11 +36,11 @@ def get_block_metadata_from_data(block_data): pass try: - metadata = json.loads(block_data[:block_data.find(b'\n')].decode()) + metadata = json.loads(bytesconverter.bytes_to_str(block_data[:block_data.find(b'\n')])) except json.decoder.JSONDecodeError: pass else: - data = block_data[block_data.find(b'\n'):].decode() + data = block_data[block_data.find(b'\n'):] meta = metadata['meta'] return (metadata, meta, data) diff --git a/onionr/static-data/default_config.json b/onionr/static-data/default_config.json index c378050a..435f35c9 100755 --- a/onionr/static-data/default_config.json +++ b/onionr/static-data/default_config.json @@ -1,93 +1,94 @@ { - "general" : { - "dev_mode" : true, - "announce_node" : true, - "display_header" : false, - "minimum_block_pow" : 4, - "minimum_send_pow" : 4, - "use_subprocess_pow_if_possible" : true, - "socket_servers" : true, - "security_level" : 0, - "hide_created_blocks" : true, - "insert_deniable_blocks" : true, - "max_block_age" : 2678400, - "public_key" : "", - "random_bind_ip" : false + "general": { + "dev_mode": true, + "announce_node": true, + "display_header": false, + "minimum_block_pow": 4, + "minimum_send_pow": 4, + "use_subprocess_pow_if_possible": true, + "socket_servers": true, + "security_level": 0, + "hide_created_blocks": true, + "insert_deniable_blocks": true, + "max_block_age": 2678400, + "public_key": "", + "random_bind_ip": false, + "use_bootstrap_list": false }, - "www" : { - "public" : { - "run" : true, - "path" : "static-data/www/public/" + "www": { + "public": { + "run": true, + "path": "static-data/www/public/" }, - "private" : { - "run" : true, - "path" : "static-data/www/private/" + "private": { + "run": true, + "path": "static-data/www/private/" }, - "ui" : { - "run" : true, - "private" : true + "ui": { + "run": true, + "private": true } }, - "client" : { + "client": { }, - "plugins" : { - "enabled" : { + "plugins": { + "enabled": { }, - "disabled" : { + "disabled": { } }, - "log" : { - "verbosity" : "default", + "log": { + "verbosity": "default", "file": { "output": true, "path": "output.log" }, - "console" : { - "output" : true, - "color" : true + "console": { + "output": true, + "color": true } }, - "tor" : { - "v3onions" : true, + "tor": { + "v3onions": true, "use_bridge": false, "bridge_ip": "", "bridge_fingerprint": "" }, - "i2p" : { - "host" : false, - "connect" : true, - "own_addr" : "" + "i2p": { + "host": false, + "connect": true, + "own_addr": "" }, - "allocations" : { - "disk" : 100000000, - "net_total" : 1000000000, - "blockCache" : 5000000, - "blockCacheTotal" : 50000000 + "allocations": { + "disk": 100000000, + "net_total": 1000000000, + "blockCache": 5000000, + "blockCacheTotal": 50000000 }, - "peers" : { - "minimum_score" : -100, - "max_stored_peers" : 5000, - "max_connect" : 1000 + "peers": { + "minimum_score": -100, + "max_stored_peers": 5000, + "max_connect": 1000 }, - "timers" : { - "lookupBlocks" : 25, - "getBlocks" : 10 + "timers": { + "lookupBlocks": 25, + "getBlocks": 10 } } diff --git a/onionr/tests/test_default_config_json.py b/onionr/tests/test_default_config_json.py new file mode 100644 index 00000000..7cd69687 --- /dev/null +++ b/onionr/tests/test_default_config_json.py @@ -0,0 +1,21 @@ +import sys, os +sys.path.append(".") +import unittest, uuid, json +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR +import onionrblocks +from utils import createdirs +from utils import readstatic +createdirs.create_dirs() +class OnionrConfig(unittest.TestCase): + def test_default_file(self): + json.loads(readstatic.read_static('default_config.json')) + + def test_installed_config(self): + import onionrsetup + onionrsetup.setup_config() + with open(TEST_DIR + 'config.json') as conf: + json.loads(conf.read()) + +unittest.main() \ No newline at end of file diff --git a/onionr/utils/readstatic.py b/onionr/utils/readstatic.py index cab49ca1..4abd67c8 100644 --- a/onionr/utils/readstatic.py +++ b/onionr/utils/readstatic.py @@ -1,7 +1,7 @@ import os def get_static_dir(): return os.path.dirname(os.path.realpath(__file__)) + '/../static-data/' -def read_static(file, ret_bin=False): +def read_static(file:str, ret_bin:bool=False)->str: static_file = get_static_dir() + file if ret_bin: