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 You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import binascii
import logger, config, onionrexceptions, nacl.exceptions import logger, config, onionrexceptions, nacl.exceptions
import json, os, sys, datetime, base64, onionrstorage import json, os, sys, datetime, base64, onionrstorage
from onionrusers import onionrusers from onionrusers import onionrusers
@ -70,7 +70,10 @@ class Block:
# decrypt data # decrypt data
if self.getHeader('encryptType') == 'asym': if self.getHeader('encryptType') == 'asym':
try: 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) bmeta = encryption.pub_key_decrypt(self.bmetadata, encodedData=encodedData)
try: try:
bmeta = bmeta.decode() bmeta = bmeta.decode()
@ -81,7 +84,7 @@ class Block:
self.signature = encryption.pub_key_decrypt(self.signature, encodedData=encodedData) self.signature = encryption.pub_key_decrypt(self.signature, encodedData=encodedData)
self.signer = encryption.pub_key_decrypt(self.signer, encodedData=encodedData) self.signer = encryption.pub_key_decrypt(self.signer, encodedData=encodedData)
self.bheader['signer'] = self.signer.decode() 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.signer is None:
if not self.verifySig(): if not self.verifySig():
@ -152,15 +155,15 @@ class Block:
# import from file # import from file
if blockdata is None: if blockdata is None:
try: try:
blockdata = onionrstorage.getData(self.getHash()).decode() blockdata = onionrstorage.getData(self.getHash())#.decode()
except AttributeError: except AttributeError:
raise onionrexceptions.NoDataAvailable('Block does not exist') raise onionrexceptions.NoDataAvailable('Block does not exist')
else: else:
self.blockFile = None self.blockFile = None
# parse block # parse block
self.raw = str(blockdata) self.raw = blockdata
self.bheader = json.loads(self.getRaw()[:self.getRaw().index('\n')]) self.bheader = json.loads(self.getRaw()[:self.getRaw().index(b'\n')])
self.bcontent = self.getRaw()[self.getRaw().index('\n') + 1:] self.bcontent = self.getRaw()[self.getRaw().index(b'\n') + 1:]
if ('encryptType' in self.bheader) and (self.bheader['encryptType'] in ('asym', 'sym')): if ('encryptType' in self.bheader) and (self.bheader['encryptType'] in ('asym', 'sym')):
self.bmetadata = self.getHeader('meta', None) self.bmetadata = self.getHeader('meta', None)
self.isEncrypted = True self.isEncrypted = True
@ -278,10 +281,10 @@ class Block:
Returns the raw contents of the block, if saved to file Returns the raw contents of the block, if saved to file
Outputs: 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): 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 # Encrypt block data with forward secrecy key first, but not meta
jsonMeta = json.dumps(meta) jsonMeta = json.dumps(meta)
jsonMeta = crypto.encryption.pub_key_encrypt(jsonMeta, asymPeer, encodedData=True).decode() 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() signature = crypto.encryption.pub_key_encrypt(signature, asymPeer, encodedData=True).decode()
signer = crypto.encryption.pub_key_encrypt(signer, asymPeer, encodedData=True).decode() signer = crypto.encryption.pub_key_encrypt(signer, asymPeer, encodedData=True).decode()
try: try:

View File

@ -19,6 +19,8 @@
''' '''
import json import json
from onionrutils import bytesconverter
def get_block_metadata_from_data(block_data): def get_block_metadata_from_data(block_data):
''' '''
accepts block contents as string, returns a tuple of accepts block contents as string, returns a tuple of
@ -34,11 +36,11 @@ def get_block_metadata_from_data(block_data):
pass pass
try: 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: except json.decoder.JSONDecodeError:
pass pass
else: else:
data = block_data[block_data.find(b'\n'):].decode() data = block_data[block_data.find(b'\n'):]
meta = metadata['meta'] meta = metadata['meta']
return (metadata, meta, data) return (metadata, meta, data)

View File

@ -1,93 +1,94 @@
{ {
"general" : { "general": {
"dev_mode" : true, "dev_mode": true,
"announce_node" : true, "announce_node": true,
"display_header" : false, "display_header": false,
"minimum_block_pow" : 4, "minimum_block_pow": 4,
"minimum_send_pow" : 4, "minimum_send_pow": 4,
"use_subprocess_pow_if_possible" : true, "use_subprocess_pow_if_possible": true,
"socket_servers" : true, "socket_servers": true,
"security_level" : 0, "security_level": 0,
"hide_created_blocks" : true, "hide_created_blocks": true,
"insert_deniable_blocks" : true, "insert_deniable_blocks": true,
"max_block_age" : 2678400, "max_block_age": 2678400,
"public_key" : "", "public_key": "",
"random_bind_ip" : false "random_bind_ip": false,
"use_bootstrap_list": false
}, },
"www" : { "www": {
"public" : { "public": {
"run" : true, "run": true,
"path" : "static-data/www/public/" "path": "static-data/www/public/"
}, },
"private" : { "private": {
"run" : true, "run": true,
"path" : "static-data/www/private/" "path": "static-data/www/private/"
}, },
"ui" : { "ui": {
"run" : true, "run": true,
"private" : true "private": true
} }
}, },
"client" : { "client": {
}, },
"plugins" : { "plugins": {
"enabled" : { "enabled": {
}, },
"disabled" : { "disabled": {
} }
}, },
"log" : { "log": {
"verbosity" : "default", "verbosity": "default",
"file": { "file": {
"output": true, "output": true,
"path": "output.log" "path": "output.log"
}, },
"console" : { "console": {
"output" : true, "output": true,
"color" : true "color": true
} }
}, },
"tor" : { "tor": {
"v3onions" : true, "v3onions": true,
"use_bridge": false, "use_bridge": false,
"bridge_ip": "", "bridge_ip": "",
"bridge_fingerprint": "" "bridge_fingerprint": ""
}, },
"i2p" : { "i2p": {
"host" : false, "host": false,
"connect" : true, "connect": true,
"own_addr" : "" "own_addr": ""
}, },
"allocations" : { "allocations": {
"disk" : 100000000, "disk": 100000000,
"net_total" : 1000000000, "net_total": 1000000000,
"blockCache" : 5000000, "blockCache": 5000000,
"blockCacheTotal" : 50000000 "blockCacheTotal": 50000000
}, },
"peers" : { "peers": {
"minimum_score" : -100, "minimum_score": -100,
"max_stored_peers" : 5000, "max_stored_peers": 5000,
"max_connect" : 1000 "max_connect": 1000
}, },
"timers" : { "timers": {
"lookupBlocks" : 25, "lookupBlocks": 25,
"getBlocks" : 10 "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 import os
def get_static_dir(): def get_static_dir():
return os.path.dirname(os.path.realpath(__file__)) + '/../static-data/' 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 static_file = get_static_dir() + file
if ret_bin: if ret_bin: