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
|
2019-06-11 06:08:44 +00:00
|
|
|
import onionrexceptions, config, logger
|
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-06-25 08:21:36 +00:00
|
|
|
from . import localcommand, blockmetadata, basicrequests, validatemetadata
|
2019-06-23 17:41:07 +00:00
|
|
|
from . import stringvalidators
|
2019-06-23 07:00:27 +00:00
|
|
|
|
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-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-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
|
|
|
'''
|
2019-06-23 17:41:07 +00:00
|
|
|
return stringvalidators.validate_hash(self, data, length)
|
2018-07-31 04:41:32 +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
|
|
|
|
'''
|
2019-06-23 07:25:40 +00:00
|
|
|
return basicrequests.do_post_request(self, url, data, port, proxyType)
|
2018-07-23 07:43:10 +00:00
|
|
|
|
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
|
|
|
|
'''
|
2019-06-23 07:25:40 +00:00
|
|
|
return basicrequests.do_get_request(self, url, port, proxyType, ignoreAPI, returnHeaders)
|
2018-06-12 07:34:33 +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-06-25 08:21:36 +00:00
|
|
|
return "%.1f %s%s" % (num, 'Yi', suffix)
|
|
|
|
|
|
|
|
def has_block(core_inst, hash):
|
|
|
|
'''
|
|
|
|
Check for new block in the list
|
|
|
|
'''
|
|
|
|
conn = sqlite3.connect(core_inst.blockDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
if not stringvalidators.validate_hash(hash):
|
|
|
|
raise Exception("Invalid hash")
|
|
|
|
for result in c.execute("SELECT COUNT() FROM hashes WHERE hash = ?", (hash,)):
|
|
|
|
if result[0] >= 1:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return False
|