work on onboarding and pep8 compliance. re-worded about
This commit is contained in:
parent
a4a0d240ac
commit
ee5f4409af
16 changed files with 267 additions and 60 deletions
|
@ -1,3 +1,10 @@
|
|||
"""Flask WSGI apps for the public and private API servers
|
||||
|
||||
Public is net-facing server meant for other nodes
|
||||
Private is meant for controlling and accessing this node
|
||||
"""
|
||||
|
||||
from . import public, private
|
||||
|
||||
PublicAPI = public.PublicAPI
|
||||
ClientAPI = private.PrivateAPI
|
||||
ClientAPI = private.PrivateAPI
|
||||
|
|
|
@ -3,6 +3,21 @@
|
|||
|
||||
This file handles all incoming http requests to the client, using Flask
|
||||
'''
|
||||
import base64
|
||||
import os
|
||||
|
||||
import flask
|
||||
from gevent.pywsgi import WSGIServer
|
||||
|
||||
from onionrutils import epoch
|
||||
import httpapi
|
||||
from filepaths import private_API_host_file
|
||||
import logger
|
||||
|
||||
from etc import waitforsetvar
|
||||
from . import register_private_blueprints
|
||||
import config
|
||||
from .. import public
|
||||
'''
|
||||
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
|
||||
|
@ -17,39 +32,33 @@
|
|||
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 base64, os, time
|
||||
import flask
|
||||
from gevent.pywsgi import WSGIServer
|
||||
from onionrutils import epoch
|
||||
import httpapi, filepaths, logger
|
||||
from . import register_private_blueprints
|
||||
from etc import waitforsetvar
|
||||
import serializeddata, config
|
||||
from .. import public
|
||||
|
||||
|
||||
class PrivateAPI:
|
||||
'''
|
||||
Client HTTP api
|
||||
'''
|
||||
|
||||
callbacks = {'public' : {}, 'private' : {}}
|
||||
callbacks = {'public': {}, 'private': {}}
|
||||
|
||||
def __init__(self):
|
||||
'''
|
||||
Initialize the api server, preping variables for later use
|
||||
|
||||
This initialization defines all of the API entry points and handlers for the endpoints and errors
|
||||
This initialization defines all of the API entry points
|
||||
and handlers for the endpoints and errors
|
||||
This also saves the used host (random localhost IP address) to the data folder in host.txt
|
||||
'''
|
||||
self.config = config
|
||||
|
||||
self.startTime = epoch.get_epoch()
|
||||
app = flask.Flask(__name__)
|
||||
bindPort = int(config.get('client.client.port', 59496))
|
||||
self.bindPort = bindPort
|
||||
bind_port = int(config.get('client.client.port', 59496))
|
||||
self.bindPort = bind_port
|
||||
|
||||
self.clientToken = config.get('client.webpassword')
|
||||
self.timeBypassToken = base64.b16encode(os.urandom(32)).decode()
|
||||
|
||||
self.host = httpapi.apiutils.setbindip.set_bind_IP(filepaths.private_API_host_file)
|
||||
self.host = httpapi.apiutils.setbindip.set_bind_IP(private_API_host_file)
|
||||
logger.info('Running api on %s:%s' % (self.host, self.bindPort))
|
||||
self.httpServer = ''
|
||||
|
||||
|
@ -58,7 +67,7 @@ class PrivateAPI:
|
|||
register_private_blueprints.register_private_blueprints(self, app)
|
||||
httpapi.load_plugin_blueprints(app)
|
||||
self.app = app
|
||||
|
||||
|
||||
def start(self):
|
||||
waitforsetvar.wait_for_set_var(self, "_too_many")
|
||||
self.publicAPI = self._too_many.get(public.PublicAPI)
|
||||
|
|
|
@ -3,6 +3,20 @@
|
|||
|
||||
Download blocks using the communicator instance
|
||||
'''
|
||||
import onionrexceptions
|
||||
import logger
|
||||
import onionrpeers
|
||||
import communicator
|
||||
from communicator import peeraction
|
||||
from communicator import onlinepeers
|
||||
from onionrutils import blockmetadata
|
||||
from onionrutils import validatemetadata
|
||||
from coredb import blockmetadb
|
||||
import onionrcrypto
|
||||
import onionrstorage
|
||||
from onionrblocks import onionrblacklist
|
||||
from onionrblocks import storagecounter
|
||||
from . import shoulddownload
|
||||
'''
|
||||
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
|
||||
|
@ -17,17 +31,10 @@
|
|||
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 communicator, onionrexceptions
|
||||
import logger, onionrpeers
|
||||
from onionrutils import blockmetadata, stringvalidators, validatemetadata
|
||||
from coredb import blockmetadb
|
||||
from . import shoulddownload
|
||||
from communicator import peeraction, onlinepeers
|
||||
import onionrcrypto, onionrstorage
|
||||
from onionrblocks import onionrblacklist, storagecounter
|
||||
def download_blocks_from_communicator(comm_inst):
|
||||
'''Use Onionr communicator instance to download blocks in the communicator's queue'''
|
||||
assert isinstance(comm_inst, communicator.OnionrCommunicatorDaemon)
|
||||
|
||||
|
||||
def download_blocks_from_communicator(comm_inst: "OnionrCommunicatorDaemon"):
|
||||
'''Use communicator instance to download blocks in the comms's queue'''
|
||||
blacklist = onionrblacklist.OnionrBlackList()
|
||||
storage_counter = storagecounter.StorageCounter()
|
||||
LOG_SKIP_COUNT = 50 # for how many iterations we skip logging the counter
|
||||
|
@ -53,7 +60,6 @@ def download_blocks_from_communicator(comm_inst):
|
|||
break
|
||||
# Do not download blocks being downloaded
|
||||
if blockHash in comm_inst.currentDownloading:
|
||||
#logger.debug('Already downloading block %s...' % blockHash)
|
||||
continue
|
||||
|
||||
comm_inst.currentDownloading.append(blockHash) # So we can avoid concurrent downloading in other threads of same block
|
||||
|
@ -130,4 +136,4 @@ def download_blocks_from_communicator(comm_inst):
|
|||
except KeyError:
|
||||
pass
|
||||
comm_inst.currentDownloading.remove(blockHash)
|
||||
comm_inst.decrementThreadCount('getBlocks')
|
||||
comm_inst.decrementThreadCount('getBlocks')
|
||||
|
|
34
src/config/onboarding.py
Normal file
34
src/config/onboarding.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
"""
|
||||
Onionr - Private P2P Communication
|
||||
|
||||
Setup config from onboarding choices
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
from filepaths import onboarding_mark_file
|
||||
import onionrtypes
|
||||
"""
|
||||
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/>.
|
||||
"""
|
||||
|
||||
|
||||
def set_config_from_onboarding(config_settings: onionrtypes.OnboardingConfig):
|
||||
return
|
||||
|
||||
def set_onboarding_finished():
|
||||
"""Create the onboarding completed setting file"""
|
||||
Path(onboarding_mark_file).touch()
|
||||
|
||||
def is_onboarding_finished() -> bool:
|
||||
return True
|
|
@ -28,4 +28,6 @@ run_check_file = home + '.runcheck'
|
|||
|
||||
data_nonce_file = home + 'block-nonces.dat'
|
||||
|
||||
keys_file = home + 'keys.txt'
|
||||
keys_file = home + 'keys.txt'
|
||||
|
||||
onboarding_mark_file = home + 'onboarding-completed'
|
||||
|
|
|
@ -3,6 +3,14 @@
|
|||
|
||||
Accept block uploads to the public API server
|
||||
'''
|
||||
import sys
|
||||
from flask import Response
|
||||
from flask import abort
|
||||
|
||||
from onionrblocks import blockimporter
|
||||
import onionrexceptions
|
||||
import logger
|
||||
|
||||
'''
|
||||
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
|
||||
|
@ -17,18 +25,15 @@
|
|||
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 sys
|
||||
from flask import Response, abort
|
||||
|
||||
from onionrblocks import blockimporter
|
||||
import onionrexceptions, logger
|
||||
|
||||
def accept_upload(request):
|
||||
"""Accept uploaded blocks to our public Onionr protocol API server"""
|
||||
resp = 'failure'
|
||||
data = request.get_data()
|
||||
if sys.getsizeof(data) < 100000000:
|
||||
try:
|
||||
if blockimporter.importBlockFromData(data):
|
||||
if blockimporter.import_block_from_data(data):
|
||||
resp = 'success'
|
||||
else:
|
||||
resp = 'failure'
|
||||
|
|
|
@ -3,6 +3,17 @@
|
|||
|
||||
Import block data and save it
|
||||
'''
|
||||
from onionrexceptions import BlacklistedBlock
|
||||
from onionrexceptions import DiskAllocationReached
|
||||
from onionrexceptions import InvalidProof
|
||||
import logger
|
||||
from onionrutils import validatemetadata
|
||||
from onionrutils import blockmetadata
|
||||
from coredb import blockmetadb
|
||||
import onionrstorage
|
||||
import onionrcrypto as crypto
|
||||
from . import onionrblacklist
|
||||
|
||||
'''
|
||||
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
|
||||
|
@ -17,39 +28,41 @@
|
|||
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 onionrexceptions, logger
|
||||
from onionrutils import validatemetadata, blockmetadata
|
||||
from coredb import blockmetadb
|
||||
from . import onionrblacklist
|
||||
import onionrstorage
|
||||
import onionrcrypto as crypto
|
||||
def importBlockFromData(content):
|
||||
|
||||
|
||||
def import_block_from_data(content):
|
||||
blacklist = onionrblacklist.OnionrBlackList()
|
||||
retData = False
|
||||
ret_data = False
|
||||
|
||||
try:
|
||||
content = content.encode()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
dataHash = crypto.hashers.sha3_hash(content)
|
||||
data_hash = crypto.hashers.sha3_hash(content)
|
||||
|
||||
if blacklist.inBlacklist(dataHash):
|
||||
raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,))
|
||||
if blacklist.inBlacklist(data_hash):
|
||||
raise BlacklistedBlock(f'%s is a blacklisted block {data_hash}')
|
||||
|
||||
metas = blockmetadata.get_block_metadata_from_data(content) # returns tuple(metadata, meta), meta is also in metadata
|
||||
# returns tuple(metadata, meta), meta is also in metadata
|
||||
metas = blockmetadata.get_block_metadata_from_data(content)
|
||||
metadata = metas[0]
|
||||
if validatemetadata.validate_metadata(metadata, metas[2]): # check if metadata is valid
|
||||
if crypto.cryptoutils.verify_POW(content): # check if POW is enough/correct
|
||||
logger.info('Imported block passed proof, saving.', terminal=True)
|
||||
|
||||
# check if metadata is valid
|
||||
if validatemetadata.validate_metadata(metadata, metas[2]):
|
||||
# check if POW is enough/correct
|
||||
if crypto.cryptoutils.verify_POW(content):
|
||||
logger.info(f'Imported block passed proof, saving: {data_hash}.',
|
||||
terminal=True)
|
||||
try:
|
||||
blockHash = onionrstorage.set_data(content)
|
||||
except onionrexceptions.DiskAllocationReached:
|
||||
except DiskAllocationReached:
|
||||
logger.warn('Failed to save block due to full disk allocation')
|
||||
else:
|
||||
blockmetadb.add_to_block_DB(blockHash, dataSaved=True)
|
||||
blockmetadata.process_block_metadata(blockHash) # caches block metadata values to block database
|
||||
retData = True
|
||||
# caches block metadata values to block database
|
||||
blockmetadata.process_block_metadata(blockHash)
|
||||
ret_data = True
|
||||
else:
|
||||
raise onionrexceptions.InvalidProof
|
||||
return retData
|
||||
raise InvalidProof
|
||||
return ret_data
|
||||
|
|
|
@ -6,3 +6,5 @@ UserIDSecretKey = NewType('UserIDSecretKey', str)
|
|||
DeterministicKeyPassphrase = NewType('DeterministicKeyPassphrase', str)
|
||||
|
||||
BlockHash = NewType('BlockHash', str)
|
||||
|
||||
OnboardingConfig = NewType('OnboardingConfig', str)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue