2018-01-09 22:58:12 +00:00
|
|
|
'''
|
2019-06-12 00:05:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2018-01-14 08:48:23 +00:00
|
|
|
|
|
|
|
OnionrUtils offers various useful functions to Onionr. Relatively misc.
|
|
|
|
'''
|
|
|
|
'''
|
2018-01-09 22:58:12 +00:00
|
|
|
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/>.
|
|
|
|
'''
|
|
|
|
# Misc functions that do not fit in the main api, but are useful
|
2019-06-11 06:08:44 +00:00
|
|
|
import sys, os, sqlite3, binascii, time, base64, json, glob, shutil, math, re, urllib.parse, string
|
|
|
|
import requests
|
2018-02-21 09:32:31 +00:00
|
|
|
import nacl.signing, nacl.encoding
|
2019-06-19 06:57:13 +00:00
|
|
|
import unpaddedbase32
|
2018-05-19 22:11:51 +00:00
|
|
|
from onionrblockapi import Block
|
2019-06-11 06:08:44 +00:00
|
|
|
import onionrexceptions, config, logger
|
2018-10-27 03:29:25 +00:00
|
|
|
from onionr import API_VERSION
|
2018-09-09 05:12:41 +00:00
|
|
|
import onionrevents
|
2019-02-17 20:21:03 +00:00
|
|
|
import storagecounter
|
2019-03-27 18:55:43 +00:00
|
|
|
from etc import pgpwords, onionrvalues
|
2019-02-17 20:21:03 +00:00
|
|
|
from onionrusers import onionrusers
|
2019-06-23 07:00:27 +00:00
|
|
|
from . import localcommand, blockmetadata, validatemetadata
|
|
|
|
|
2019-06-16 06:06:32 +00:00
|
|
|
config.reload()
|
2018-01-26 06:28:11 +00:00
|
|
|
class OnionrUtils:
|
2018-02-23 01:58:36 +00:00
|
|
|
'''
|
2018-07-03 21:24:14 +00:00
|
|
|
Various useful functions for validating things, etc functions, connectivity
|
2018-02-23 01:58:36 +00:00
|
|
|
'''
|
2018-01-26 06:28:11 +00:00
|
|
|
def __init__(self, coreInstance):
|
2018-09-10 05:02:28 +00:00
|
|
|
#self.fingerprintFile = 'data/own-fingerprint.txt' #TODO Remove since probably not needed
|
|
|
|
self._core = coreInstance # onionr core instance
|
2018-04-16 02:22:19 +00:00
|
|
|
|
2018-07-03 21:24:14 +00:00
|
|
|
self.avoidDupe = [] # list used to prevent duplicate requests per peer for certain actions
|
|
|
|
self.peerProcessing = {} # dict of current peer actions: peer, actionList
|
2018-09-10 05:02:28 +00:00
|
|
|
self.storageCounter = storagecounter.StorageCounter(self._core) # used to keep track of how much data onionr is using on disk
|
2018-01-09 22:58:12 +00:00
|
|
|
return
|
2018-04-19 01:47:35 +00:00
|
|
|
|
2018-07-23 07:43:10 +00:00
|
|
|
def getRoundedEpoch(self, roundS=60):
|
2018-04-19 01:47:35 +00:00
|
|
|
'''
|
2018-07-23 07:43:10 +00:00
|
|
|
Returns the epoch, rounded down to given seconds (Default 60)
|
2018-06-11 07:40:45 +00:00
|
|
|
'''
|
|
|
|
epoch = self.getEpoch()
|
2018-07-23 07:43:10 +00:00
|
|
|
return epoch - (epoch % roundS)
|
2019-01-13 22:20:10 +00:00
|
|
|
|
|
|
|
def getClientAPIServer(self):
|
|
|
|
retData = ''
|
|
|
|
try:
|
|
|
|
with open(self._core.privateApiHostFile, 'r') as host:
|
|
|
|
hostname = host.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise FileNotFoundError
|
|
|
|
else:
|
|
|
|
retData += '%s:%s' % (hostname, config.get('client.client.port'))
|
|
|
|
return retData
|
2018-04-03 21:47:48 +00:00
|
|
|
|
2019-02-10 18:43:45 +00:00
|
|
|
def localCommand(self, command, data='', silent = True, post=False, postData = {}, maxWait=20):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
|
|
|
Send a command to the local http API server, securely. Intended for local clients, DO NOT USE for remote peers.
|
|
|
|
'''
|
2019-06-23 07:00:27 +00:00
|
|
|
return localcommand.local_command(self, command, data, silent, post, postData, maxWait)
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-07-03 08:18:07 +00:00
|
|
|
def getHumanReadableID(self, pub=''):
|
|
|
|
'''gets a human readable ID from a public key'''
|
|
|
|
if pub == '':
|
|
|
|
pub = self._core._crypto.pubKey
|
|
|
|
pub = base64.b16encode(base64.b32decode(pub)).decode()
|
2019-02-05 18:47:11 +00:00
|
|
|
return ' '.join(pgpwords.wordify(pub))
|
2019-02-10 02:21:36 +00:00
|
|
|
|
|
|
|
def convertHumanReadableID(self, pub):
|
|
|
|
'''Convert a human readable pubkey id to base32'''
|
2019-02-11 23:44:39 +00:00
|
|
|
pub = pub.lower()
|
2019-02-10 18:43:45 +00:00
|
|
|
return self.bytesToStr(base64.b32encode(binascii.unhexlify(pgpwords.hexify(pub.strip()))))
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-06-16 07:33:54 +00:00
|
|
|
def getBlockMetadataFromData(self, blockData):
|
|
|
|
'''
|
2019-02-11 23:44:39 +00:00
|
|
|
accepts block contents as string, returns a tuple of
|
|
|
|
metadata, meta (meta being internal metadata, which will be
|
|
|
|
returned as an encrypted base64 string if it is encrypted, dict if not).
|
2018-06-16 07:33:54 +00:00
|
|
|
'''
|
2019-06-23 07:00:27 +00:00
|
|
|
return blockmetadata.get_block_metadata_from_data(self, blockData)
|
2018-06-16 07:33:54 +00:00
|
|
|
|
2018-07-11 07:35:22 +00:00
|
|
|
def processBlockMetadata(self, blockHash):
|
|
|
|
'''
|
|
|
|
Read metadata from a block and cache it to the block database
|
|
|
|
'''
|
2019-06-23 07:00:27 +00:00
|
|
|
return blockmetadata.process_block_metadata(self, blockHash)
|
2018-07-11 07:35:22 +00:00
|
|
|
|
2018-07-12 07:37:10 +00:00
|
|
|
def escapeAnsi(self, line):
|
|
|
|
'''
|
|
|
|
Remove ANSI escape codes from a string with regex
|
2018-07-31 04:41:32 +00:00
|
|
|
|
2019-05-09 05:27:15 +00:00
|
|
|
taken or adapted from: https://stackoverflow.com/a/38662876 by user https://stackoverflow.com/users/802365/%c3%89douard-lopez
|
|
|
|
cc-by-sa-3 license https://creativecommons.org/licenses/by-sa/3.0/
|
2018-07-12 07:37:10 +00:00
|
|
|
'''
|
|
|
|
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
|
|
|
|
return ansi_escape.sub('', line)
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-29 02:52:48 +00:00
|
|
|
def hasBlock(self, hash):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
|
|
|
Check for new block in the list
|
|
|
|
'''
|
2018-01-29 02:52:48 +00:00
|
|
|
conn = sqlite3.connect(self._core.blockDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
if not self.validateHash(hash):
|
|
|
|
raise Exception("Invalid hash")
|
2018-11-10 06:29:32 +00:00
|
|
|
for result in c.execute("SELECT COUNT() FROM hashes WHERE hash = ?", (hash,)):
|
2018-01-29 02:52:48 +00:00
|
|
|
if result[0] >= 1:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return False
|
|
|
|
|
2018-04-23 03:42:37 +00:00
|
|
|
def hasKey(self, key):
|
|
|
|
'''
|
|
|
|
Check for key in list of public keys
|
|
|
|
'''
|
|
|
|
return key in self._core.listPeers()
|
|
|
|
|
2018-01-26 06:28:11 +00:00
|
|
|
def validateHash(self, data, length=64):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
2019-02-11 23:44:39 +00:00
|
|
|
Validate if a string is a valid hash hex digest (does not compare, just checks length and charset)
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
2018-01-26 06:28:11 +00:00
|
|
|
retVal = True
|
2018-01-28 22:38:10 +00:00
|
|
|
if data == False or data == True:
|
2018-01-28 21:59:13 +00:00
|
|
|
return False
|
2018-01-28 22:21:51 +00:00
|
|
|
data = data.strip()
|
2018-01-26 06:28:11 +00:00
|
|
|
if len(data) != length:
|
|
|
|
retVal = False
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
int(data, 16)
|
|
|
|
except ValueError:
|
|
|
|
retVal = False
|
2018-02-04 03:44:29 +00:00
|
|
|
|
2018-01-26 07:22:48 +00:00
|
|
|
return retVal
|
2018-07-31 04:41:32 +00:00
|
|
|
|
2018-08-16 05:01:40 +00:00
|
|
|
def validateMetadata(self, metadata, blockData):
|
2018-06-25 07:34:43 +00:00
|
|
|
'''Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string'''
|
2019-06-23 07:00:27 +00:00
|
|
|
return validatemetadata.validate_metadata(self, metadata, blockData)
|
2018-02-23 01:58:36 +00:00
|
|
|
|
2018-02-21 09:32:31 +00:00
|
|
|
def validatePubKey(self, key):
|
2018-04-19 02:16:10 +00:00
|
|
|
'''
|
|
|
|
Validate if a string is a valid base32 encoded Ed25519 key
|
|
|
|
'''
|
2019-02-14 23:48:41 +00:00
|
|
|
if type(key) is type(None):
|
|
|
|
return False
|
2019-06-19 06:57:13 +00:00
|
|
|
# Accept keys that have no = padding
|
|
|
|
key = unpaddedbase32.repad(self.strToBytes(key))
|
|
|
|
|
|
|
|
retVal = False
|
2018-02-21 09:32:31 +00:00
|
|
|
try:
|
2018-02-22 06:08:04 +00:00
|
|
|
nacl.signing.SigningKey(seed=key, encoder=nacl.encoding.Base32Encoder)
|
2018-02-21 09:32:31 +00:00
|
|
|
except nacl.exceptions.ValueError:
|
|
|
|
pass
|
2018-04-18 03:43:33 +00:00
|
|
|
except base64.binascii.Error as err:
|
2018-04-02 07:21:58 +00:00
|
|
|
pass
|
2018-02-22 06:08:04 +00:00
|
|
|
else:
|
|
|
|
retVal = True
|
2018-02-21 09:32:31 +00:00
|
|
|
return retVal
|
2018-07-31 04:41:32 +00:00
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def validateID(id):
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
2018-02-21 09:32:31 +00:00
|
|
|
Validate if an address is a valid tor or i2p hidden service
|
2018-02-04 03:44:29 +00:00
|
|
|
'''
|
2018-04-19 02:16:10 +00:00
|
|
|
try:
|
|
|
|
idLength = len(id)
|
|
|
|
retVal = True
|
|
|
|
idNoDomain = ''
|
|
|
|
peerType = ''
|
|
|
|
# i2p b32 addresses are 60 characters long (including .b32.i2p)
|
|
|
|
if idLength == 60:
|
|
|
|
peerType = 'i2p'
|
|
|
|
if not id.endswith('.b32.i2p'):
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
2018-01-27 00:10:38 +00:00
|
|
|
else:
|
2018-04-19 02:16:10 +00:00
|
|
|
idNoDomain = id.split('.b32.i2p')[0]
|
|
|
|
# Onion v2's are 22 (including .onion), v3's are 62 with .onion
|
|
|
|
elif idLength == 22 or idLength == 62:
|
|
|
|
peerType = 'onion'
|
|
|
|
if not id.endswith('.onion'):
|
2018-01-26 09:46:21 +00:00
|
|
|
retVal = False
|
2018-04-19 02:16:10 +00:00
|
|
|
else:
|
|
|
|
idNoDomain = id.split('.onion')[0]
|
|
|
|
else:
|
2018-01-27 00:10:38 +00:00
|
|
|
retVal = False
|
2018-04-19 02:16:10 +00:00
|
|
|
if retVal:
|
|
|
|
if peerType == 'i2p':
|
|
|
|
try:
|
|
|
|
id.split('.b32.i2p')[2]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
retVal = False
|
|
|
|
elif peerType == 'onion':
|
|
|
|
try:
|
|
|
|
id.split('.onion')[2]
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
retVal = False
|
|
|
|
if not idNoDomain.isalnum():
|
|
|
|
retVal = False
|
2018-11-10 06:29:32 +00:00
|
|
|
|
2018-11-04 23:01:58 +00:00
|
|
|
# Validate address is valid base32 (when capitalized and minus extension); v2/v3 onions and .b32.i2p use base32
|
2019-02-14 23:48:41 +00:00
|
|
|
for x in idNoDomain.upper():
|
|
|
|
if x not in string.ascii_uppercase and x not in '234567':
|
|
|
|
retVal = False
|
2018-02-23 01:58:36 +00:00
|
|
|
|
2018-04-19 02:16:10 +00:00
|
|
|
return retVal
|
|
|
|
except:
|
|
|
|
return False
|
2018-04-18 03:43:33 +00:00
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
@staticmethod
|
|
|
|
def isIntegerString(data):
|
2019-02-12 05:30:56 +00:00
|
|
|
'''Check if a string is a valid base10 integer (also returns true if already an int)'''
|
|
|
|
try:
|
|
|
|
int(data)
|
2019-02-14 23:48:41 +00:00
|
|
|
except (ValueError, TypeError) as e:
|
2019-02-12 05:30:56 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2018-05-02 06:50:29 +00:00
|
|
|
def isCommunicatorRunning(self, timeout = 5, interval = 0.1):
|
|
|
|
try:
|
2018-09-26 04:58:11 +00:00
|
|
|
runcheck_file = self._core.dataDir + '.runcheck'
|
2018-05-02 06:22:40 +00:00
|
|
|
|
2018-11-10 07:17:19 +00:00
|
|
|
if not os.path.isfile(runcheck_file):
|
|
|
|
open(runcheck_file, 'w+').close()
|
2018-05-02 06:22:40 +00:00
|
|
|
|
2018-11-10 07:17:19 +00:00
|
|
|
# self._core.daemonQueueAdd('runCheck') # deprecated
|
2018-05-02 06:50:29 +00:00
|
|
|
starttime = time.time()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
time.sleep(interval)
|
2018-11-11 03:25:40 +00:00
|
|
|
|
2018-11-10 07:17:19 +00:00
|
|
|
if not os.path.isfile(runcheck_file):
|
2018-05-02 06:50:29 +00:00
|
|
|
return True
|
|
|
|
elif time.time() - starttime >= timeout:
|
|
|
|
return False
|
|
|
|
except:
|
|
|
|
return False
|
2018-05-03 03:22:07 +00:00
|
|
|
|
2018-05-10 07:42:24 +00:00
|
|
|
def importNewBlocks(self, scanDir=''):
|
2018-05-11 02:05:56 +00:00
|
|
|
'''
|
|
|
|
This function is intended to scan for new blocks ON THE DISK and import them
|
|
|
|
'''
|
2018-05-10 07:42:24 +00:00
|
|
|
blockList = self._core.getBlockList()
|
2019-03-09 06:37:57 +00:00
|
|
|
exist = False
|
2018-05-10 07:42:24 +00:00
|
|
|
if scanDir == '':
|
|
|
|
scanDir = self._core.blockDataLocation
|
|
|
|
if not scanDir.endswith('/'):
|
|
|
|
scanDir += '/'
|
|
|
|
for block in glob.glob(scanDir + "*.dat"):
|
|
|
|
if block.replace(scanDir, '').replace('.dat', '') not in blockList:
|
2019-03-09 06:37:57 +00:00
|
|
|
exist = True
|
2018-05-13 03:45:32 +00:00
|
|
|
logger.info('Found new block on dist %s' % block)
|
2018-05-10 07:42:24 +00:00
|
|
|
with open(block, 'rb') as newBlock:
|
|
|
|
block = block.replace(scanDir, '').replace('.dat', '')
|
|
|
|
if self._core._crypto.sha3Hash(newBlock.read()) == block.replace('.dat', ''):
|
|
|
|
self._core.addToBlockDB(block.replace('.dat', ''), dataSaved=True)
|
2018-05-13 03:45:32 +00:00
|
|
|
logger.info('Imported block %s.' % block)
|
2018-09-30 16:53:39 +00:00
|
|
|
self._core._utils.processBlockMetadata(block)
|
2018-05-10 07:42:24 +00:00
|
|
|
else:
|
2018-05-13 03:45:32 +00:00
|
|
|
logger.warn('Failed to verify hash for %s' % block)
|
2019-03-09 06:37:57 +00:00
|
|
|
if not exist:
|
2019-06-16 17:55:48 +00:00
|
|
|
logger.info('No blocks found to import')
|
2018-05-11 02:05:56 +00:00
|
|
|
|
2018-05-11 05:18:39 +00:00
|
|
|
def progressBar(self, value = 0, endvalue = 100, width = None):
|
|
|
|
'''
|
|
|
|
Outputs a progress bar with a percentage. Write \n after use.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if width is None or height is None:
|
|
|
|
width, height = shutil.get_terminal_size((80, 24))
|
|
|
|
|
|
|
|
bar_length = width - 6
|
|
|
|
|
|
|
|
percent = float(value) / endvalue
|
|
|
|
arrow = '─' * int(round(percent * bar_length)-1) + '>'
|
|
|
|
spaces = ' ' * (bar_length - len(arrow))
|
|
|
|
|
|
|
|
sys.stdout.write("\r┣{0}┫ {1}%".format(arrow + spaces, int(round(percent * 100))))
|
|
|
|
sys.stdout.flush()
|
2018-05-19 22:11:51 +00:00
|
|
|
|
2018-05-18 21:49:05 +00:00
|
|
|
def getEpoch(self):
|
2018-05-18 06:22:16 +00:00
|
|
|
'''returns epoch'''
|
|
|
|
return math.floor(time.time())
|
2018-05-11 05:18:39 +00:00
|
|
|
|
2018-07-23 07:43:10 +00:00
|
|
|
def doPostRequest(self, url, data={}, port=0, proxyType='tor'):
|
|
|
|
'''
|
|
|
|
Do a POST request through a local tor or i2p instance
|
|
|
|
'''
|
|
|
|
if proxyType == 'tor':
|
|
|
|
if port == 0:
|
|
|
|
port = self._core.torPort
|
2018-08-04 05:50:07 +00:00
|
|
|
proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)}
|
2018-07-23 07:43:10 +00:00
|
|
|
elif proxyType == 'i2p':
|
|
|
|
proxies = {'http': 'http://127.0.0.1:4444'}
|
|
|
|
else:
|
|
|
|
return
|
2019-01-18 05:34:13 +00:00
|
|
|
headers = {'user-agent': 'PyOnionr', 'Connection':'close'}
|
2018-07-23 07:43:10 +00:00
|
|
|
try:
|
2018-08-04 05:50:07 +00:00
|
|
|
proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)}
|
2018-07-23 07:43:10 +00:00
|
|
|
r = requests.post(url, data=data, headers=headers, proxies=proxies, allow_redirects=False, timeout=(15, 30))
|
|
|
|
retData = r.text
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
logger.debug('Error: %s' % str(e))
|
|
|
|
retData = False
|
|
|
|
return retData
|
|
|
|
|
2019-03-11 05:10:37 +00:00
|
|
|
def doGetRequest(self, url, port=0, proxyType='tor', ignoreAPI=False, returnHeaders=False):
|
2018-06-12 07:34:33 +00:00
|
|
|
'''
|
|
|
|
Do a get request through a local tor or i2p instance
|
|
|
|
'''
|
2018-08-06 07:50:08 +00:00
|
|
|
retData = False
|
2018-06-12 07:34:33 +00:00
|
|
|
if proxyType == 'tor':
|
|
|
|
if port == 0:
|
|
|
|
raise onionrexceptions.MissingPort('Socks port required for Tor HTTP get request')
|
2018-08-04 05:50:07 +00:00
|
|
|
proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)}
|
2018-06-12 07:34:33 +00:00
|
|
|
elif proxyType == 'i2p':
|
|
|
|
proxies = {'http': 'http://127.0.0.1:4444'}
|
|
|
|
else:
|
|
|
|
return
|
2019-01-18 05:34:13 +00:00
|
|
|
headers = {'user-agent': 'PyOnionr', 'Connection':'close'}
|
2018-11-11 03:25:40 +00:00
|
|
|
response_headers = dict()
|
2018-06-13 22:22:48 +00:00
|
|
|
try:
|
2018-08-04 05:50:07 +00:00
|
|
|
proxies = {'http': 'socks4a://127.0.0.1:' + str(port), 'https': 'socks4a://127.0.0.1:' + str(port)}
|
2019-01-18 05:34:13 +00:00
|
|
|
r = requests.get(url, headers=headers, proxies=proxies, allow_redirects=False, timeout=(15, 30), )
|
2018-10-27 03:29:25 +00:00
|
|
|
# Check server is using same API version as us
|
2018-12-09 17:29:39 +00:00
|
|
|
if not ignoreAPI:
|
|
|
|
try:
|
|
|
|
response_headers = r.headers
|
|
|
|
if r.headers['X-API'] != str(API_VERSION):
|
|
|
|
raise onionrexceptions.InvalidAPIVersion
|
|
|
|
except KeyError:
|
2018-10-27 19:59:15 +00:00
|
|
|
raise onionrexceptions.InvalidAPIVersion
|
2018-06-13 22:22:48 +00:00
|
|
|
retData = r.text
|
2018-07-04 18:49:43 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise KeyboardInterrupt
|
2018-08-04 03:47:22 +00:00
|
|
|
except ValueError as e:
|
2019-06-19 20:29:27 +00:00
|
|
|
pass
|
2018-10-27 03:29:25 +00:00
|
|
|
except onionrexceptions.InvalidAPIVersion:
|
2018-11-11 03:25:40 +00:00
|
|
|
if 'X-API' in response_headers:
|
|
|
|
logger.debug('Using API version %s. Cannot communicate with node\'s API version of %s.' % (API_VERSION, response_headers['X-API']))
|
|
|
|
else:
|
|
|
|
logger.debug('Using API version %s. API version was not sent with the request.' % API_VERSION)
|
2018-06-13 22:22:48 +00:00
|
|
|
except requests.exceptions.RequestException as e:
|
2018-09-26 04:58:11 +00:00
|
|
|
if not 'ConnectTimeoutError' in str(e) and not 'Request rejected or failed' in str(e):
|
2018-09-23 04:53:09 +00:00
|
|
|
logger.debug('Error: %s' % str(e))
|
2018-06-13 22:22:48 +00:00
|
|
|
retData = False
|
2019-03-11 05:10:37 +00:00
|
|
|
if returnHeaders:
|
|
|
|
return (retData, response_headers)
|
|
|
|
else:
|
|
|
|
return retData
|
2018-06-12 07:34:33 +00:00
|
|
|
|
2019-04-27 15:43:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def strToBytes(data):
|
2018-08-10 07:03:49 +00:00
|
|
|
try:
|
|
|
|
data = data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return data
|
2019-04-27 15:43:16 +00:00
|
|
|
@staticmethod
|
|
|
|
def bytesToStr(data):
|
2018-08-10 07:03:49 +00:00
|
|
|
try:
|
|
|
|
data = data.decode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return data
|
2018-09-24 23:48:00 +00:00
|
|
|
|
2018-05-11 02:05:56 +00:00
|
|
|
def size(path='.'):
|
|
|
|
'''
|
|
|
|
Returns the size of a folder's contents in bytes
|
|
|
|
'''
|
|
|
|
total = 0
|
|
|
|
if os.path.exists(path):
|
|
|
|
if os.path.isfile(path):
|
|
|
|
total = os.path.getsize(path)
|
|
|
|
else:
|
|
|
|
for entry in os.scandir(path):
|
|
|
|
if entry.is_file():
|
|
|
|
total += entry.stat().st_size
|
|
|
|
elif entry.is_dir():
|
|
|
|
total += size(entry.path)
|
|
|
|
return total
|
|
|
|
|
|
|
|
def humanSize(num, suffix='B'):
|
2018-05-11 05:18:39 +00:00
|
|
|
'''
|
|
|
|
Converts from bytes to a human readable format.
|
|
|
|
'''
|
2018-05-11 02:05:56 +00:00
|
|
|
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
|
|
|
|
if abs(num) < 1024.0:
|
|
|
|
return "%.1f %s%s" % (num, unit, suffix)
|
|
|
|
num /= 1024.0
|
2019-02-11 23:44:39 +00:00
|
|
|
return "%.1f %s%s" % (num, 'Yi', suffix)
|