make config use ujson

made httpapi use ujson

made onionrblocks mod use ujson

made setup config use ujson

made devreporting use ujson

made transports/tor use ujson

made contactmanager use ujson
master
Kevin Froman 2020-04-03 04:02:36 -05:00
parent 5be4df1276
commit dee5d8ceed
11 changed files with 149 additions and 111 deletions

View File

@ -2,7 +2,10 @@
This file deals with configuration management. This file deals with configuration management.
""" """
import os, json, logger import os
import ujson as json
import logger
import filepaths import filepaths
from . import onboarding from . import onboarding

View File

@ -1,4 +1,5 @@
import json import ujson as json
from onionrblocks import onionrblockapi from onionrblocks import onionrblockapi
from onionrutils import bytesconverter, stringvalidators from onionrutils import bytesconverter, stringvalidators
import onionrexceptions import onionrexceptions

View File

@ -17,8 +17,9 @@
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 json import ujson as json
from flask import Blueprint, request, Response, abort from flask import Blueprint, request, Response, abort
import config, onionrutils import config, onionrutils
from onionrutils.bytesconverter import bytes_to_str from onionrutils.bytesconverter import bytes_to_str

View File

@ -1,9 +1,13 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
This file creates http endpoints for friend management This file creates http endpoints for friend management
''' """
''' import ujson as json
from onionrusers import contactmanager
from flask import Blueprint, Response, request, abort, redirect
from coredb import keydb
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,12 +20,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 json
from onionrusers import contactmanager
from flask import Blueprint, Response, request, abort, redirect
from coredb import keydb
friends = Blueprint('friends', __name__) friends = Blueprint('friends', __name__)
@friends.route('/friends/list') @friends.route('/friends/list')

View File

@ -2,7 +2,7 @@
Create blocks with the client api server Create blocks with the client api server
""" """
import json import ujson as json
import threading import threading
from flask import Blueprint, Response, request, g from flask import Blueprint, Response, request, g

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
from typing import Union from typing import Union
import json import ujson as json
from gevent import spawn from gevent import spawn

View File

@ -1,9 +1,26 @@
''' """Onionr - P2P Anonymous Storage Network.
Onionr - P2P Anonymous Storage Network
This file contains the OnionrBlocks class which is a class for working with Onionr blocks OnionrBlocks class for abstraction of blocks
''' """
''' import binascii
import os
import datetime
import onionrstorage
import unpaddedbase32
import ujson as json
import nacl.exceptions
import logger
import onionrexceptions
from onionrusers import onionrusers
from onionrutils import stringvalidators, epoch
from coredb import blockmetadb
from onionrutils import bytesconverter
from onionrstorage import removeblock
import onionrblocks
from onionrcrypto import encryption, cryptoutils as cryptoutils, signing
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,18 +33,9 @@
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 unpaddedbase32
import binascii
import logger, config, onionrexceptions, nacl.exceptions
import json, os, sys, datetime, base64, onionrstorage
from onionrusers import onionrusers
from onionrutils import stringvalidators, epoch
from coredb import blockmetadb
from onionrutils import bytesconverter
from onionrstorage import removeblock
import onionrblocks
from onionrcrypto import encryption, cryptoutils as cryptoutils, signing
class Block: class Block:
blockCacheOrder = list() # NEVER write your own code that writes to this! blockCacheOrder = list() # NEVER write your own code that writes to this!
blockCache = dict() # should never be accessed directly, look at Block.getCache() blockCache = dict() # should never be accessed directly, look at Block.getCache()
@ -62,9 +70,9 @@ class Block:
self.update() self.update()
def decrypt(self, encodedData = True): def decrypt(self, encodedData = True):
''' """
Decrypt a block, loading decrypted data into their vars Decrypt a block, loading decrypted data into their vars
''' """
if self.decrypted: if self.decrypted:
return True return True
@ -126,9 +134,9 @@ class Block:
return retData return retData
def verifySig(self): def verifySig(self):
''' """
Verify if a block's signature is signed by its claimed signer Verify if a block's signature is signed by its claimed signer
''' """
if self.signer is None: if self.signer is None:
return False return False
if signing.ed_verify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True): if signing.ed_verify(data=self.signedData, key=self.signer, sig=self.signature, encodedData=True):
@ -138,7 +146,7 @@ class Block:
return self.validSig return self.validSig
def update(self, data = None, file = None): def update(self, data = None, file = None):
''' """
Loads data from a block in to the current object. Loads data from a block in to the current object.
Inputs: Inputs:
@ -151,7 +159,7 @@ class Block:
Outputs: Outputs:
- (bool): indicates whether or not the operation was successful - (bool): indicates whether or not the operation was successful
''' """
try: try:
# import from string # import from string
blockdata = data blockdata = data
@ -205,12 +213,12 @@ class Block:
return False return False
def delete(self): def delete(self):
''' """
Deletes the block's file and records, if they exist Deletes the block's file and records, if they exist
Outputs: Outputs:
- (bool): whether or not the operation was successful - (bool): whether or not the operation was successful
''' """
if self.exists(): if self.exists():
try: try:
@ -224,7 +232,7 @@ class Block:
return False return False
def save(self, sign = False, recreate = True): def save(self, sign = False, recreate = True):
''' """
Saves a block to file and imports it into Onionr Saves a block to file and imports it into Onionr
Inputs: Inputs:
@ -233,7 +241,7 @@ class Block:
Outputs: Outputs:
- (bool): whether or not the operation was successful - (bool): whether or not the operation was successful
''' """
try: try:
if self.isValid() is True: if self.isValid() is True:
@ -253,45 +261,45 @@ class Block:
# getters # getters
def getExpire(self): def getExpire(self):
''' """
Returns the expire time for a block Returns the expire time for a block
Outputs: Outputs:
- (int): the expire time for a block, or None - (int): the expire time for a block, or None
''' """
return self.expire return self.expire
def getHash(self): def getHash(self):
''' """
Returns the hash of the block if saved to file Returns the hash of the block if saved to file
Outputs: Outputs:
- (str): the hash of the block, or None - (str): the hash of the block, or None
''' """
return self.hash return self.hash
def getType(self): def getType(self):
''' """
Returns the type of the block Returns the type of the block
Outputs: Outputs:
- (str): the type of the block - (str): the type of the block
''' """
return self.btype return self.btype
def getRaw(self): def getRaw(self):
''' """
Returns the raw contents of the block, if saved to file Returns the raw contents of the block, if saved to file
Outputs: Outputs:
- (bytes): the raw contents of the block, or None - (bytes): the raw contents of the block, or None
''' """
return self.raw return self.raw
def getHeader(self, key = None, default = None): def getHeader(self, key = None, default = None):
''' """
Returns the header information Returns the header information
Inputs: Inputs:
@ -299,7 +307,7 @@ class Block:
Outputs: Outputs:
- (dict/str): either the whole header as a dict, or one value - (dict/str): either the whole header as a dict, or one value
''' """
if not key is None: if not key is None:
if key in self.getHeader(): if key in self.getHeader():
@ -308,7 +316,7 @@ class Block:
return self.bheader return self.bheader
def getMetadata(self, key = None, default = None): def getMetadata(self, key = None, default = None):
''' """
Returns the metadata information Returns the metadata information
Inputs: Inputs:
@ -316,7 +324,7 @@ class Block:
Outputs: Outputs:
- (dict/str): either the whole metadata as a dict, or one value - (dict/str): either the whole metadata as a dict, or one value
''' """
if not key is None: if not key is None:
if key in self.getMetadata(): if key in self.getMetadata():
@ -325,77 +333,77 @@ class Block:
return self.bmetadata return self.bmetadata
def getContent(self): def getContent(self):
''' """
Returns the contents of the block Returns the contents of the block
Outputs: Outputs:
- (str): the contents of the block - (str): the contents of the block
''' """
return self.bcontent return self.bcontent
def getDate(self): def getDate(self):
''' """
Returns the date that the block was received, if loaded from file Returns the date that the block was received, if loaded from file
Outputs: Outputs:
- (datetime): the date that the block was received - (datetime): the date that the block was received
''' """
return self.date return self.date
def getBlockFile(self): def getBlockFile(self):
''' """
Returns the location of the block file if it is saved Returns the location of the block file if it is saved
Outputs: Outputs:
- (str): the location of the block file, or None - (str): the location of the block file, or None
''' """
return self.blockFile return self.blockFile
def isValid(self): def isValid(self):
''' """
Checks if the block is valid Checks if the block is valid
Outputs: Outputs:
- (bool): whether or not the block is valid - (bool): whether or not the block is valid
''' """
return self.valid return self.valid
def isSigned(self): def isSigned(self):
''' """
Checks if the block was signed Checks if the block was signed
Outputs: Outputs:
- (bool): whether or not the block is signed - (bool): whether or not the block is signed
''' """
return self.signed return self.signed
def getSignature(self): def getSignature(self):
''' """
Returns the base64-encoded signature Returns the base64-encoded signature
Outputs: Outputs:
- (str): the signature, or None - (str): the signature, or None
''' """
return self.signature return self.signature
def getSignedData(self): def getSignedData(self):
''' """
Returns the data that was signed Returns the data that was signed
Outputs: Outputs:
- (str): the data that was signed, or None - (str): the data that was signed, or None
''' """
return self.signedData return self.signedData
def isSigner(self, signer, encodedData = True): def isSigner(self, signer, encodedData = True):
''' """
Checks if the block was signed by the signer inputted Checks if the block was signed by the signer inputted
Inputs: Inputs:
@ -404,7 +412,7 @@ class Block:
Outputs: Outputs:
- (bool): whether or not the signer of the block is the signer inputted - (bool): whether or not the signer of the block is the signer inputted
''' """
signer = unpaddedbase32.repad(bytesconverter.str_to_bytes(signer)) signer = unpaddedbase32.repad(bytesconverter.str_to_bytes(signer))
try: try:
if (not self.isSigned()) or (not stringvalidators.validate_pub_key(signer)): if (not self.isSigned()) or (not stringvalidators.validate_pub_key(signer)):
@ -417,7 +425,7 @@ class Block:
# setters # setters
def setType(self, btype): def setType(self, btype):
''' """
Sets the type of the block Sets the type of the block
Inputs: Inputs:
@ -425,13 +433,13 @@ class Block:
Outputs: Outputs:
- (Block): the Block instance - (Block): the Block instance
''' """
self.btype = btype self.btype = btype
return self return self
def setMetadata(self, key, val): def setMetadata(self, key, val):
''' """
Sets a custom metadata value Sets a custom metadata value
Metadata should not store block-specific data structures. Metadata should not store block-specific data structures.
@ -442,13 +450,13 @@ class Block:
Outputs: Outputs:
- (Block): the Block instance - (Block): the Block instance
''' """
self.bmetadata[key] = val self.bmetadata[key] = val
return self return self
def setContent(self, bcontent): def setContent(self, bcontent):
''' """
Sets the contents of the block Sets the contents of the block
Inputs: Inputs:
@ -456,14 +464,14 @@ class Block:
Outputs: Outputs:
- (Block): the Block instance - (Block): the Block instance
''' """
self.bcontent = str(bcontent) self.bcontent = str(bcontent)
return self return self
# static functions # static functions
def exists(bHash): def exists(bHash):
''' """
Checks if a block is saved to file or not Checks if a block is saved to file or not
Inputs: Inputs:
@ -473,7 +481,7 @@ class Block:
Outputs: Outputs:
- (bool): whether or not the block file exists - (bool): whether or not the block file exists
''' """
# no input data? scrap it. # no input data? scrap it.
if bHash is None: if bHash is None:

View File

@ -1,9 +1,19 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Initialize Onionr configuration Initialize Onionr configuration
''' """
''' import os
import base64
import ujson as json
import config
import logger
import netcontroller
from etc import onionrvalues
from logger.settings import *
from utils import readstatic
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,15 +26,10 @@
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 os, json, base64
import config, logger, netcontroller
from etc import onionrvalues
from logger.settings import *
from utils import readstatic
def setup_config(): def setup_config():
if not os.path.exists(config._configfile): if not os.path.exists(config._configfile):
# this is the default config, it will be overwritten if a config file already exists. Else, it saves it # this is the default config, it will be overwritten if a config file already exists. Else, it saves it
conf_data = readstatic.read_static('default_config.json', ret_bin=False) conf_data = readstatic.read_static('default_config.json', ret_bin=False)

View File

@ -1,10 +1,29 @@
"""Onionr - Private P2P Communication.
Dev utility to profile an Onionr subnetwork.
"""
import ujson as json
import config import config
from utils.bettersleep import better_sleep from utils.bettersleep import better_sleep
from utils.gettransports import get as get_transports from utils.gettransports import get as get_transports
from onionrutils import basicrequests from onionrutils import basicrequests
from onionrutils import epoch from onionrutils import epoch
"""
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 <https://www.gnu.org/licenses/>.
"""
import json
def statistics_reporter(shared_state): def statistics_reporter(shared_state):
server = config.get('statistics.server', '') server = config.get('statistics.server', '')

View File

@ -2,8 +2,7 @@
""" """
import json import ujson as json
from stem import CircStatus from stem import CircStatus
from netcontroller.torcontrol.torcontroller import get_controller from netcontroller.torcontrol.torcontroller import get_controller

View File

@ -1,9 +1,19 @@
''' """Onionr - Private P2P Communication.
Onionr - Private P2P Communication
Sets more abstract information related to a peer. Can be thought of as traditional 'contact' system Set more abstract information related to a peer.
''' Can be thought of as traditional 'contact' system
''' """
import os
import ujson as json
import unpaddedbase32
import onionrexceptions
from onionrusers import onionrusers
from onionrutils import bytesconverter, epoch
from utils import identifyhome
from onionrutils import mnemonickeys
"""
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
@ -16,16 +26,9 @@
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 os, json
import unpaddedbase32
import niceware
import onionrexceptions
from onionrusers import onionrusers
from onionrutils import bytesconverter, epoch
from utils import identifyhome
from onionrutils import mnemonickeys
class ContactManager(onionrusers.OnionrUser): class ContactManager(onionrusers.OnionrUser):
def __init__(self, publicKey, saveUser=False, recordExpireSeconds=5): def __init__(self, publicKey, saveUser=False, recordExpireSeconds=5):
try: try:
@ -43,10 +46,10 @@ class ContactManager(onionrusers.OnionrUser):
self.recordExpire = recordExpireSeconds self.recordExpire = recordExpireSeconds
self.data = self._loadData() self.data = self._loadData()
self.deleted = False self.deleted = False
if not os.path.exists(self.dataDir): if not os.path.exists(self.dataDir):
os.mkdir(self.dataDir) os.mkdir(self.dataDir)
def _writeData(self): def _writeData(self):
data = json.dumps(self.data) data = json.dumps(self.data)
with open(self.dataFile, 'w') as dataFile: with open(self.dataFile, 'w') as dataFile:
@ -68,7 +71,7 @@ class ContactManager(onionrusers.OnionrUser):
if autoWrite: if autoWrite:
self._writeData() self._writeData()
return return
def get_info(self, key, forceReload=False): def get_info(self, key, forceReload=False):
if self.deleted: if self.deleted:
raise onionrexceptions.ContactDeleted raise onionrexceptions.ContactDeleted