Refactoring, added docstrings
This commit is contained in:
@@ -4,16 +4,6 @@ import shutil
|
||||
import sys
|
||||
|
||||
|
||||
def dir_size(start_path):
|
||||
total_size = 0
|
||||
for dirpath, dirnames, filenames in os.walk(start_path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dirpath, f)
|
||||
if not os.path.islink(fp):
|
||||
total_size += os.path.getsize(fp)
|
||||
return total_size
|
||||
|
||||
|
||||
def get_resource_path(filename):
|
||||
application_path = 'resources'
|
||||
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import os
|
||||
import hashlib
|
||||
import base64
|
||||
import time
|
||||
|
||||
|
||||
def random_string(num_bytes, output_len=None):
|
||||
b = os.urandom(num_bytes)
|
||||
h = hashlib.sha256(b).digest()[:16]
|
||||
s = base64.b32encode(h).lower().replace(b"=", b"").decode("utf-8")
|
||||
if not output_len:
|
||||
return s
|
||||
return s[:output_len]
|
||||
|
||||
|
||||
def human_readable_filesize(b):
|
||||
thresh = 1024.0
|
||||
if b < thresh:
|
||||
return "{:.1f} B".format(b)
|
||||
units = ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
|
||||
u = 0
|
||||
b /= thresh
|
||||
while b >= thresh:
|
||||
b /= thresh
|
||||
u += 1
|
||||
return "{:.1f} {}".format(b, units[u])
|
||||
|
||||
|
||||
def format_seconds(seconds):
|
||||
days, seconds = divmod(seconds, 86400)
|
||||
hours, seconds = divmod(seconds, 3600)
|
||||
minutes, seconds = divmod(seconds, 60)
|
||||
|
||||
human_readable = []
|
||||
if days:
|
||||
human_readable.append("{:.0f}d".format(days))
|
||||
if hours:
|
||||
human_readable.append("{:.0f}h".format(hours))
|
||||
if minutes:
|
||||
human_readable.append("{:.0f}m".format(minutes))
|
||||
if seconds or not human_readable:
|
||||
human_readable.append("{:.0f}s".format(seconds))
|
||||
return "".join(human_readable)
|
||||
|
||||
|
||||
def estimated_time_remaining(bytes_downloaded, total_bytes, started):
|
||||
now = time.time()
|
||||
time_elapsed = now - started
|
||||
download_rate = bytes_downloaded / time_elapsed
|
||||
remaining_bytes = total_bytes - bytes_downloaded
|
||||
eta = remaining_bytes / download_rate
|
||||
return format_seconds(eta)
|
||||
@@ -2,6 +2,11 @@ import sqlitedict
|
||||
|
||||
|
||||
class AuthFile(sqlitedict.SqliteDict):
|
||||
"""
|
||||
Valid AuthFile has fields:
|
||||
host - .onion url of service
|
||||
auth - v3 onion auth string in format, that can be written to .auth_private file
|
||||
"""
|
||||
def __init__(self, service):
|
||||
super().__init__(
|
||||
filename=f'{service}.auth',
|
||||
|
||||
@@ -134,6 +134,12 @@ class Onion(object):
|
||||
|
||||
@staticmethod
|
||||
def write_onion_service(name: str, port: int):
|
||||
"""
|
||||
Writes onion service to config
|
||||
:param name: Name of service
|
||||
:param port: Port of real service on local machine to proxy
|
||||
:return: ServiceModel object
|
||||
"""
|
||||
if name in services.keys():
|
||||
service: config.models.ServiceModel = services[name]
|
||||
service.port = port
|
||||
|
||||
@@ -8,6 +8,10 @@ from typing import Literal
|
||||
|
||||
|
||||
def get_latest_version() -> str:
|
||||
"""
|
||||
Gets latest non-alfa version name from dist.torproject.org
|
||||
:return:
|
||||
"""
|
||||
r = requests.get('https://dist.torproject.org/torbrowser/').text
|
||||
|
||||
results = re.findall(r'<a href=".+/">(.+)/</a>', r)
|
||||
@@ -22,6 +26,10 @@ def get_build() -> Literal[
|
||||
'macos-x86_64',
|
||||
'macos-aarch64'
|
||||
]:
|
||||
"""
|
||||
Gets proper build name for your system
|
||||
:return:
|
||||
"""
|
||||
if sys.platform == 'win32':
|
||||
return 'windows-x86_64'
|
||||
elif sys.platform == 'linux':
|
||||
@@ -38,18 +46,32 @@ def get_build() -> Literal[
|
||||
|
||||
def get_tor_expert_bundles(version: str = get_latest_version(),
|
||||
platform: str = get_build()):
|
||||
"""
|
||||
Returns a link for downloading tor expert bundle by version and platform
|
||||
:param version: Tor expert bundle version that exists in dist.torproject.org
|
||||
:param platform: Build type based on platform and arch, can be generated using
|
||||
get_build()
|
||||
:return:
|
||||
"""
|
||||
return f'https://dist.torproject.org/torbrowser/{version}/tor-expert-bundle-' \
|
||||
f'{version}-{platform}.tar.gz'
|
||||
|
||||
|
||||
def download_tor(url: str = get_tor_expert_bundles(), dist: str = 'tor'):
|
||||
"""
|
||||
Downloads tor from url and unpacks it to specified directory. Note, that
|
||||
it doesn't unpack only tor executable to dist folder, but creates there
|
||||
tor folder, where tor executable and libs are stored
|
||||
:param url: Direct link for downloading
|
||||
:param dist: Directory where to unpack archive (tor folder will appear there)
|
||||
:return:
|
||||
"""
|
||||
if not os.path.exists(dist):
|
||||
os.makedirs(dist)
|
||||
|
||||
(tar := tarfile.open(fileobj=io.BytesIO(requests.get(url).content),
|
||||
mode='r:gz')).extractall(
|
||||
members=
|
||||
[
|
||||
members=[
|
||||
tarinfo
|
||||
for tarinfo
|
||||
in tar.getmembers()
|
||||
|
||||
Reference in New Issue
Block a user