From 5e73f83c572aadb9f2d4604d951fa398a06cbcf0 Mon Sep 17 00:00:00 2001 From: KF Date: Fri, 14 Jun 2019 20:31:01 -0500 Subject: [PATCH] + added powchoice.py to use old pow on windows, temporary fix for subprocess method not working there * various bug fixes --- onionr/api.py | 3 ++- onionr/core.py | 10 ++++++--- onionr/{proofofmemory.py => etc/powchoice.py} | 22 +++++++++---------- onionr/netcontroller.py | 6 ++++- onionr/onionrcommands/daemonlaunch.py | 14 +++++++++--- onionr/onionrcommands/openwebinterface.py | 2 +- onionr/onionrproofs.py | 15 +++++++------ onionr/static-data/default_config.json | 1 + run-windows-dev.bat | 6 +++++ 9 files changed, 51 insertions(+), 28 deletions(-) rename onionr/{proofofmemory.py => etc/powchoice.py} (65%) create mode 100644 run-windows-dev.bat diff --git a/onionr/api.py b/onionr/api.py index daefd6ef..3a5f9f87 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -29,7 +29,7 @@ import httpapi from httpapi import friendsapi, profilesapi, configapi, miscpublicapi from onionrservices import httpheaders import onionr - +config.reload() class FDSafeHandler(WSGIHandler): '''Our WSGI handler. Doesn't do much non-default except timeouts''' def handle(self): @@ -57,6 +57,7 @@ def setBindIP(filePath=''): else: data = '127.0.0.1' if filePath != '': + print(filePath) with open(filePath, 'w') as bindFile: bindFile.write(data) return data diff --git a/onionr/core.py b/onionr/core.py index 53d834f6..e7cf8a3a 100755 --- a/onionr/core.py +++ b/onionr/core.py @@ -25,7 +25,7 @@ import onionrutils, onionrcrypto, onionrproofs, onionrevents as events, onionrex import onionrblacklist from onionrusers import onionrusers import dbcreator, onionrstorage, serializeddata, subprocesspow -from etc import onionrvalues +from etc import onionrvalues, powchoice if sys.version_info < (3, 6): try: @@ -65,7 +65,7 @@ class Core: self.dbCreate = dbcreator.DBCreator(self) self.forwardKeysFile = self.dataDir + 'forward-keys.db' self.keyStore = simplekv.DeadSimpleKV(self.dataDir + 'cachedstorage.dat', refresh_seconds=5) - + # Socket data, defined here because of multithreading constraints with gevent self.killSockets = False self.startSocket = {} @@ -104,6 +104,7 @@ class Core: else: logger.warn('Warning: address bootstrap file not found ' + self.bootstrapFileLocation) + self.use_subprocess = powchoice.use_subprocess(self) self._utils = onionrutils.OnionrUtils(self) # Initialize the crypto object self._crypto = onionrcrypto.OnionrCrypto(self) @@ -792,7 +793,10 @@ class Core: metadata['expire'] = expire # send block data (and metadata) to POW module to get tokenized block data - payload = subprocesspow.SubprocessPOW(data, metadata, self).start() + if self.use_subprocess: + payload = subprocesspow.SubprocessPOW(data, metadata, self).start() + else: + payload = onionrproofs.POW(metadata, data).waitForResult() if payload != False: try: retData = self.setData(payload) diff --git a/onionr/proofofmemory.py b/onionr/etc/powchoice.py similarity index 65% rename from onionr/proofofmemory.py rename to onionr/etc/powchoice.py index 4b0b0fa7..cf1c7990 100644 --- a/onionr/proofofmemory.py +++ b/onionr/etc/powchoice.py @@ -1,7 +1,7 @@ ''' - Onionr - P2P Anonymous Storage Network + Onionr - Private P2P Communication - This file handles proof of memory functionality + This file does determinations for what proof of work module should be used ''' ''' This program is free software: you can redistribute it and/or modify @@ -17,13 +17,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' - -class ProofOfMemory: - def __init__(self, commInst): - self.communicator = commInst - return - - def checkRandomPeer(self): - return - def checkPeer(self, peer): - return \ No newline at end of file +import platform +def use_subprocess(core_inst): + use = True + if not core_inst.config.get('general.use_subprocess_pow_if_possible', True): + use = False + if 'Windows' == platform.system(): + use = False + return use \ No newline at end of file diff --git a/onionr/netcontroller.py b/onionr/netcontroller.py index 69a5f21e..213c4ce7 100755 --- a/onionr/netcontroller.py +++ b/onionr/netcontroller.py @@ -189,7 +189,11 @@ HiddenServicePort 80 ''' + self.apiServerIP + ''':''' + str(self.hsPort) return try: - os.kill(int(pidN), signal.SIGTERM) + try: + os.kill(int(pidN), signal.SIGTERM) + except PermissionError: + # seems to happen on win 10 + pass os.remove(self.dataDir + 'torPid.txt') except ProcessLookupError: pass diff --git a/onionr/onionrcommands/daemonlaunch.py b/onionr/onionrcommands/daemonlaunch.py index 4b343ede..88e43fae 100644 --- a/onionr/onionrcommands/daemonlaunch.py +++ b/onionr/onionrcommands/daemonlaunch.py @@ -23,6 +23,11 @@ from threading import Thread import onionr, api, logger, communicator import onionrevents as events from netcontroller import NetController + +def _proper_shutdown(o_inst): + o_inst.onionrUtils.localCommand('shutdown') + sys.exit(1) + def daemon(o_inst): ''' Starts the Onionr communication daemon @@ -39,8 +44,7 @@ def daemon(o_inst): time.sleep(0) except KeyboardInterrupt: logger.debug('Got keyboard interrupt, shutting down...') - time.sleep(1) - o_inst.onionrUtils.localCommand('shutdown') + _proper_shutdown(o_inst) apiHost = '' while apiHost == '': @@ -64,7 +68,11 @@ def daemon(o_inst): else: logger.debug('.onion service disabled') logger.debug('Using public key: %s' % (logger.colors.underline + o_inst.onionrCore._crypto.pubKey)) - time.sleep(1) + + try: + time.sleep(1) + except KeyboardInterrupt: + _proper_shutdown(o_inst) o_inst.onionrCore.torPort = net.socksPort communicatorThread = Thread(target=communicator.startCommunicator, args=(o_inst, str(net.socksPort))) diff --git a/onionr/onionrcommands/openwebinterface.py b/onionr/onionrcommands/openwebinterface.py index 823703a0..c28f3217 100644 --- a/onionr/onionrcommands/openwebinterface.py +++ b/onionr/onionrcommands/openwebinterface.py @@ -26,5 +26,5 @@ def open_home(o_inst): logger.error('Onionr seems to not be running (could not get api host)') else: url = 'http://%s/#%s' % (url, o_inst.onionrCore.config.get('client.webpassword')) - print('If Onionr does not open automatically, use this URL:', url) + logger.info('If Onionr does not open automatically, use this URL:', url) webbrowser.open_new_tab(url) \ No newline at end of file diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index ac64575f..efd114f1 100755 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -205,18 +205,19 @@ class POW: else: myCore = coreInst - dataLen = len(data) + len(json.dumps(metadata)) + json_metadata = json.dumps(metadata).encode() - if forceDifficulty > 0: - self.difficulty = forceDifficulty - else: - # Calculate difficulty. Dumb for now, may use good algorithm in the future. - self.difficulty = getDifficultyForNewBlock(dataLen) - try: self.data = self.data.encode() except AttributeError: pass + + if forceDifficulty > 0: + self.difficulty = forceDifficulty + else: + # Calculate difficulty. Dumb for now, may use good algorithm in the future. + self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data)) + logger.info('Computing POW (difficulty: %s)...' % self.difficulty) diff --git a/onionr/static-data/default_config.json b/onionr/static-data/default_config.json index 2f1954f8..65cad2f5 100755 --- a/onionr/static-data/default_config.json +++ b/onionr/static-data/default_config.json @@ -4,6 +4,7 @@ "display_header" : false, "minimum_block_pow": 4, "minimum_send_pow": 4, + "use_subprocess_pow_if_possible": true, "socket_servers": false, "security_level": 0, "hide_created_blocks": true, diff --git a/run-windows-dev.bat b/run-windows-dev.bat new file mode 100644 index 00000000..31ba8c88 --- /dev/null +++ b/run-windows-dev.bat @@ -0,0 +1,6 @@ +@echo off +set ONIONR_HOME=data%random% +echo Using %ONIONR_HOME% +setlocal +chdir onionr +python onionr.py %*