block data portion now pure bytes when encrypted (barely tested), and improved config testing

This commit is contained in:
Kevin Froman 2019-09-10 15:25:50 -05:00
parent d24d0fc315
commit 63b32f98df
6 changed files with 91 additions and 64 deletions

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import binascii
import logger, config, onionrexceptions, nacl.exceptions
import json, os, sys, datetime, base64, onionrstorage
from onionrusers import onionrusers
@ -69,8 +69,11 @@ class Block:
retData = False
# decrypt data
if self.getHeader('encryptType') == 'asym':
try:
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):
'''

View File

@ -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:

View File

@ -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)

View File

@ -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
}
}

View File

@ -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()

View File

@ -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: