2014-09-02 20:30:01 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-02 15:10:42 -04:00
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2022-01-16 19:15:49 -05:00
|
|
|
Copyright (C) 2014-2022 Micah Lee, et al. <micah@micahflee.com>
|
2014-09-02 15:10:42 -04: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 <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
2017-05-24 23:20:07 -04:00
|
|
|
import base64
|
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import random
|
2021-10-14 23:24:27 -04:00
|
|
|
import requests
|
2017-05-24 23:20:07 -04:00
|
|
|
import socket
|
|
|
|
import sys
|
2017-11-08 04:25:59 -05:00
|
|
|
import threading
|
2017-05-24 23:20:07 -04:00
|
|
|
import time
|
2020-04-06 22:49:49 -04:00
|
|
|
import shutil
|
2021-11-29 18:01:14 -05:00
|
|
|
import re
|
2020-11-08 16:34:19 -05:00
|
|
|
from pkg_resources import resource_filename
|
2014-09-15 20:08:27 -04:00
|
|
|
|
2021-05-02 18:13:50 -04:00
|
|
|
import colorama
|
|
|
|
from colorama import Fore, Back, Style
|
|
|
|
|
2018-03-13 06:28:47 -04:00
|
|
|
from .settings import Settings
|
|
|
|
|
2018-05-19 23:51:01 -04:00
|
|
|
|
2020-11-29 14:08:46 -05:00
|
|
|
class CannotFindTor(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare can't find a tor binary
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2019-10-20 23:01:09 -04:00
|
|
|
class Common:
|
2018-03-08 13:18:31 -05:00
|
|
|
"""
|
|
|
|
The Common object is shared amongst all parts of OnionShare.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2019-04-18 22:53:21 -04:00
|
|
|
def __init__(self, verbose=False):
|
|
|
|
self.verbose = verbose
|
2018-03-08 13:18:31 -05:00
|
|
|
|
2021-05-02 18:13:50 -04:00
|
|
|
colorama.init(autoreset=True)
|
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
# The platform OnionShare is running on
|
|
|
|
self.platform = platform.system()
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.platform.endswith("BSD") or self.platform == "DragonFly":
|
|
|
|
self.platform = "BSD"
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
# The current version of OnionShare
|
2019-10-13 00:01:25 -04:00
|
|
|
with open(self.get_resource_path("version.txt")) as f:
|
2018-03-08 13:18:31 -05:00
|
|
|
self.version = f.read().strip()
|
|
|
|
|
2021-04-26 22:29:18 -04:00
|
|
|
def display_banner(self):
|
2021-04-28 19:08:20 -04:00
|
|
|
"""
|
|
|
|
Raw ASCII art example:
|
2021-04-28 23:25:42 -04:00
|
|
|
╭───────────────────────────────────────────╮
|
|
|
|
│ * ▄▄█████▄▄ * │
|
|
|
|
│ ▄████▀▀▀████▄ * │
|
|
|
|
│ ▀▀█▀ ▀██▄ │
|
|
|
|
│ * ▄█▄ ▀██▄ │
|
|
|
|
│ ▄█████▄ ███ -+- │
|
|
|
|
│ ███ ▀█████▀ │
|
|
|
|
│ ▀██▄ ▀█▀ │
|
|
|
|
│ * ▀██▄ ▄█▄▄ * │
|
|
|
|
│ * ▀████▄▄▄████▀ │
|
|
|
|
│ ▀▀█████▀▀ │
|
|
|
|
│ -+- * │
|
|
|
|
│ ▄▀▄ ▄▀▀ █ │
|
|
|
|
│ █ █ ▀ ▀▄ █ │
|
|
|
|
│ █ █ █▀▄ █ ▄▀▄ █▀▄ ▀▄ █▀▄ ▄▀▄ █▄▀ ▄█▄ │
|
|
|
|
│ ▀▄▀ █ █ █ ▀▄▀ █ █ ▄▄▀ █ █ ▀▄█ █ ▀▄▄ │
|
|
|
|
│ │
|
|
|
|
│ v2.3.1 │
|
|
|
|
│ │
|
|
|
|
│ https://onionshare.org/ │
|
|
|
|
╰───────────────────────────────────────────╯
|
2021-04-28 19:08:20 -04:00
|
|
|
"""
|
|
|
|
|
2021-11-15 21:16:10 -05:00
|
|
|
try:
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "╭───────────────────────────────────────────╮"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "▄▄█████▄▄"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▄████▀▀▀████▄"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▀▀█▀ ▀██▄ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "▄█▄ ▀██▄ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▄█████▄ ███"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " -+- "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ███ ▀█████▀ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▀██▄ ▀█▀ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "▀██▄ ▄█▄▄"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "▀████▄▄▄████▀ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▀▀█████▀▀ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.LIGHTMAGENTA_EX
|
|
|
|
+ " -+- * "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▄▀▄ ▄▀▀ █ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " █ █ ▀ ▀▄ █ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " █ █ █▀▄ █ ▄▀▄ █▀▄ ▀▄ █▀▄ ▄▀▄ █▄▀ ▄█▄ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " ▀▄▀ █ █ █ ▀▄▀ █ █ ▄▄▀ █ █ ▀▄█ █ ▀▄▄ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│ │"
|
|
|
|
)
|
|
|
|
left_spaces = (43 - len(self.version) - 1) // 2
|
|
|
|
right_spaces = left_spaces
|
|
|
|
if left_spaces + len(self.version) + 1 + right_spaces < 43:
|
|
|
|
right_spaces += 1
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ f"{' '*left_spaces}v{self.version}{' '*right_spaces}"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│ │"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ " https://onionshare.org/ "
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "│"
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
Back.MAGENTA
|
|
|
|
+ Fore.WHITE
|
|
|
|
+ "╰───────────────────────────────────────────╯"
|
|
|
|
)
|
|
|
|
print()
|
|
|
|
except:
|
|
|
|
# If anything fails, print a boring banner
|
|
|
|
print(f"OnionShare v{self.version}")
|
|
|
|
print("https://onionshare.org/")
|
|
|
|
print()
|
2021-04-26 22:29:18 -04:00
|
|
|
|
2018-03-13 06:28:47 -04:00
|
|
|
def load_settings(self, config=None):
|
|
|
|
"""
|
|
|
|
Loading settings, optionally from a custom config json file.
|
|
|
|
"""
|
|
|
|
self.settings = Settings(self, config)
|
|
|
|
self.settings.load()
|
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
def log(self, module, func, msg=None):
|
|
|
|
"""
|
2019-04-18 22:53:21 -04:00
|
|
|
If verbose mode is on, log error messages to stdout
|
2018-03-08 13:18:31 -05:00
|
|
|
"""
|
2019-04-18 22:53:21 -04:00
|
|
|
if self.verbose:
|
2018-03-08 13:18:31 -05:00
|
|
|
timestamp = time.strftime("%b %d %Y %X")
|
2021-05-02 18:49:43 -04:00
|
|
|
final_msg = f"{Fore.LIGHTBLACK_EX + Style.DIM}[{timestamp}]{Style.RESET_ALL} {Fore.WHITE + Style.DIM}{module}.{func}{Style.RESET_ALL}"
|
2018-03-08 13:18:31 -05:00
|
|
|
if msg:
|
2021-05-02 18:13:50 -04:00
|
|
|
final_msg = (
|
|
|
|
f"{final_msg}{Fore.WHITE + Style.DIM}: {msg}{Style.RESET_ALL}"
|
|
|
|
)
|
2018-03-08 13:18:31 -05:00
|
|
|
print(final_msg)
|
|
|
|
|
|
|
|
def get_resource_path(self, filename):
|
|
|
|
"""
|
2020-10-13 01:40:55 -04:00
|
|
|
Returns the absolute path of a resource
|
2018-03-08 13:18:31 -05:00
|
|
|
"""
|
2020-11-08 16:34:19 -05:00
|
|
|
path = resource_filename("onionshare_cli", os.path.join("resources", filename))
|
2020-11-08 17:20:31 -05:00
|
|
|
self.log("Common", "get_resource_path", f"filename={filename}, path={path}")
|
2020-11-04 18:57:14 -05:00
|
|
|
return path
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
def get_tor_paths(self):
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.platform == "Linux":
|
2021-10-24 20:35:24 -04:00
|
|
|
tor_path = shutil.which("tor")
|
|
|
|
if not tor_path:
|
|
|
|
raise CannotFindTor()
|
|
|
|
obfs4proxy_file_path = shutil.which("obfs4proxy")
|
|
|
|
snowflake_file_path = shutil.which("snowflake-client")
|
2021-10-24 23:26:36 -04:00
|
|
|
meek_client_file_path = shutil.which("meek-client")
|
2021-10-24 20:35:24 -04:00
|
|
|
prefix = os.path.dirname(os.path.dirname(tor_path))
|
|
|
|
tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip")
|
|
|
|
tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6")
|
2019-10-13 00:01:25 -04:00
|
|
|
elif self.platform == "Windows":
|
2021-12-21 17:15:52 -05:00
|
|
|
# In Windows, the Tor binaries are in the onionshare package, not the onionshare_cli package
|
2020-10-15 01:36:31 -04:00
|
|
|
base_path = self.get_resource_path("tor")
|
2021-12-21 17:15:52 -05:00
|
|
|
base_path = base_path.replace("onionshare_cli", "onionshare")
|
2020-10-15 01:36:31 -04:00
|
|
|
tor_path = os.path.join(base_path, "Tor", "tor.exe")
|
2021-12-21 17:15:52 -05:00
|
|
|
|
|
|
|
# If tor.exe isn't there, mayber we're running from the source tree
|
|
|
|
if not os.path.exists(tor_path):
|
|
|
|
base_path = os.path.join(os.getcwd(), "onionshare", "resources", "tor")
|
|
|
|
|
2021-12-22 16:08:01 -05:00
|
|
|
tor_path = os.path.join(base_path, "Tor", "tor.exe")
|
2021-12-21 17:15:52 -05:00
|
|
|
if not os.path.exists(tor_path):
|
2021-12-22 16:08:01 -05:00
|
|
|
raise CannotFindTor()
|
2021-12-21 17:15:52 -05:00
|
|
|
|
2020-10-15 01:36:31 -04:00
|
|
|
obfs4proxy_file_path = os.path.join(base_path, "Tor", "obfs4proxy.exe")
|
2021-10-11 23:45:28 -04:00
|
|
|
snowflake_file_path = os.path.join(base_path, "Tor", "snowflake-client.exe")
|
2021-10-18 02:17:47 -04:00
|
|
|
meek_client_file_path = os.path.join(base_path, "Tor", "meek-client.exe")
|
2020-10-15 01:36:31 -04:00
|
|
|
tor_geo_ip_file_path = os.path.join(base_path, "Data", "Tor", "geoip")
|
|
|
|
tor_geo_ipv6_file_path = os.path.join(base_path, "Data", "Tor", "geoip6")
|
2021-12-22 16:08:01 -05:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
elif self.platform == "Darwin":
|
2021-12-22 16:08:01 -05:00
|
|
|
# Let's see if we have tor binaries in the onionshare GUI package
|
|
|
|
base_path = self.get_resource_path("tor")
|
|
|
|
base_path = base_path.replace("onionshare_cli", "onionshare")
|
|
|
|
tor_path = os.path.join(base_path, "tor")
|
|
|
|
if os.path.exists(tor_path):
|
|
|
|
obfs4proxy_file_path = os.path.join(base_path, "obfs4proxy")
|
|
|
|
snowflake_file_path = os.path.join(base_path, "snowflake-client")
|
|
|
|
meek_client_file_path = os.path.join(base_path, "meek-client")
|
|
|
|
tor_geo_ip_file_path = os.path.join(base_path, "geoip")
|
|
|
|
tor_geo_ipv6_file_path = os.path.join(base_path, "geoip6")
|
|
|
|
else:
|
|
|
|
# Fallback to looking in the path
|
|
|
|
tor_path = shutil.which("tor")
|
|
|
|
if not os.path.exists(tor_path):
|
|
|
|
raise CannotFindTor()
|
|
|
|
|
|
|
|
obfs4proxy_file_path = shutil.which("obfs4proxy")
|
|
|
|
snowflake_file_path = shutil.which("snowflake-client")
|
|
|
|
meek_client_file_path = shutil.which("meek-client")
|
|
|
|
prefix = os.path.dirname(os.path.dirname(tor_path))
|
|
|
|
tor_geo_ip_file_path = os.path.join(prefix, "share/tor/geoip")
|
|
|
|
tor_geo_ipv6_file_path = os.path.join(prefix, "share/tor/geoip6")
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
elif self.platform == "BSD":
|
|
|
|
tor_path = "/usr/local/bin/tor"
|
|
|
|
tor_geo_ip_file_path = "/usr/local/share/tor/geoip"
|
|
|
|
tor_geo_ipv6_file_path = "/usr/local/share/tor/geoip6"
|
|
|
|
obfs4proxy_file_path = "/usr/local/bin/obfs4proxy"
|
2021-10-11 23:45:28 -04:00
|
|
|
snowflake_file_path = "/usr/local/bin/snowflake-client"
|
2021-10-18 02:17:47 -04:00
|
|
|
meek_client_file_path = "/usr/local/bin/meek-client"
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
tor_path,
|
|
|
|
tor_geo_ip_file_path,
|
|
|
|
tor_geo_ipv6_file_path,
|
|
|
|
obfs4proxy_file_path,
|
2021-10-11 23:45:28 -04:00
|
|
|
snowflake_file_path,
|
2021-10-18 02:17:47 -04:00
|
|
|
meek_client_file_path,
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2018-03-08 13:18:31 -05:00
|
|
|
|
2018-11-25 20:17:56 -05:00
|
|
|
def build_data_dir(self):
|
|
|
|
"""
|
|
|
|
Returns the path of the OnionShare data directory.
|
|
|
|
"""
|
2019-11-08 03:44:21 -05:00
|
|
|
if self.platform == "Windows":
|
|
|
|
try:
|
|
|
|
appdata = os.environ["APPDATA"]
|
|
|
|
onionshare_data_dir = f"{appdata}\\OnionShare"
|
2021-04-28 19:08:20 -04:00
|
|
|
except Exception:
|
2019-11-08 03:44:21 -05:00
|
|
|
# If for some reason we don't have the 'APPDATA' environment variable
|
|
|
|
# (like running tests in Linux while pretending to be in Windows)
|
2021-04-28 01:45:37 -04:00
|
|
|
try:
|
|
|
|
xdg_config_home = os.environ["XDG_CONFIG_HOME"]
|
|
|
|
onionshare_data_dir = f"{xdg_config_home}/onionshare"
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2021-04-28 01:45:37 -04:00
|
|
|
onionshare_data_dir = os.path.expanduser("~/.config/onionshare")
|
2019-11-08 03:44:21 -05:00
|
|
|
elif self.platform == "Darwin":
|
|
|
|
onionshare_data_dir = os.path.expanduser(
|
|
|
|
"~/Library/Application Support/OnionShare"
|
|
|
|
)
|
|
|
|
else:
|
2021-04-28 01:45:37 -04:00
|
|
|
try:
|
|
|
|
xdg_config_home = os.environ["XDG_CONFIG_HOME"]
|
|
|
|
onionshare_data_dir = f"{xdg_config_home}/onionshare"
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2021-04-28 01:45:37 -04:00
|
|
|
onionshare_data_dir = os.path.expanduser("~/.config/onionshare")
|
2019-11-08 03:44:21 -05:00
|
|
|
|
|
|
|
# Modify the data dir if running tests
|
|
|
|
if getattr(sys, "onionshare_test_mode", False):
|
|
|
|
onionshare_data_dir += "-testdata"
|
2018-12-19 19:04:31 -05:00
|
|
|
|
|
|
|
os.makedirs(onionshare_data_dir, 0o700, True)
|
|
|
|
return onionshare_data_dir
|
2018-11-25 20:17:56 -05:00
|
|
|
|
2019-11-28 17:02:00 -05:00
|
|
|
def build_tmp_dir(self):
|
|
|
|
"""
|
|
|
|
Returns path to a folder that can hold temporary files
|
|
|
|
"""
|
|
|
|
tmp_dir = os.path.join(self.build_data_dir(), "tmp")
|
|
|
|
os.makedirs(tmp_dir, 0o700, True)
|
|
|
|
return tmp_dir
|
|
|
|
|
2019-11-03 00:00:23 -04:00
|
|
|
def build_persistent_dir(self):
|
2018-03-08 13:18:31 -05:00
|
|
|
"""
|
2019-11-03 00:00:23 -04:00
|
|
|
Returns the path to the folder that holds persistent files
|
|
|
|
"""
|
2019-11-28 17:02:00 -05:00
|
|
|
persistent_dir = os.path.join(self.build_data_dir(), "persistent")
|
2019-11-03 00:00:23 -04:00
|
|
|
os.makedirs(persistent_dir, 0o700, True)
|
|
|
|
return persistent_dir
|
|
|
|
|
2019-11-28 17:02:00 -05:00
|
|
|
def build_tor_dir(self):
|
|
|
|
"""
|
|
|
|
Returns path to the tor data directory
|
|
|
|
"""
|
|
|
|
tor_dir = os.path.join(self.build_data_dir(), "tor_data")
|
|
|
|
os.makedirs(tor_dir, 0o700, True)
|
|
|
|
return tor_dir
|
|
|
|
|
2019-11-03 00:00:23 -04:00
|
|
|
def build_password(self, word_count=2):
|
|
|
|
"""
|
|
|
|
Returns a random string made of words from the wordlist, such as "deter-trig".
|
2018-03-08 13:18:31 -05:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
with open(self.get_resource_path("wordlist.txt")) as f:
|
2018-03-08 13:18:31 -05:00
|
|
|
wordlist = f.read().split()
|
|
|
|
|
|
|
|
r = random.SystemRandom()
|
2019-11-03 00:00:23 -04:00
|
|
|
return "-".join(r.choice(wordlist) for _ in range(word_count))
|
2018-03-08 13:18:31 -05:00
|
|
|
|
2020-03-08 05:21:43 -04:00
|
|
|
def build_username(self, word_count=2):
|
|
|
|
"""
|
|
|
|
Returns a random string made of words from the wordlist, such as "deter-trig".
|
|
|
|
"""
|
|
|
|
with open(self.get_resource_path("wordlist.txt")) as f:
|
|
|
|
wordlist = f.read().split()
|
|
|
|
|
|
|
|
r = random.SystemRandom()
|
|
|
|
return "-".join(r.choice(wordlist) for _ in range(word_count))
|
|
|
|
|
2021-11-29 18:01:14 -05:00
|
|
|
def check_bridges_valid(self, bridges):
|
|
|
|
"""
|
|
|
|
Does a regex check against a supplied list of bridges, to make sure they
|
|
|
|
are valid strings depending on the bridge type.
|
|
|
|
"""
|
|
|
|
valid_bridges = []
|
|
|
|
self.log("Common", "check_bridges_valid", "Checking bridge syntax")
|
|
|
|
for bridge in bridges:
|
|
|
|
if bridge != "":
|
|
|
|
# Check the syntax of the custom bridge to make sure it looks legitimate
|
|
|
|
ipv4_pattern = re.compile(
|
|
|
|
"(obfs4\s+)?(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):([0-9]+)(\s+)([A-Z0-9]+)(.+)$"
|
|
|
|
)
|
|
|
|
ipv6_pattern = re.compile(
|
|
|
|
"(obfs4\s+)?\[(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\]:[0-9]+\s+[A-Z0-9]+(.+)$"
|
|
|
|
)
|
|
|
|
meek_lite_pattern = re.compile(
|
|
|
|
"(meek_lite)(\s)+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+)(\s)+([0-9A-Z]+)(\s)+url=(.+)(\s)+front=(.+)"
|
|
|
|
)
|
|
|
|
snowflake_pattern = re.compile(
|
|
|
|
"(snowflake)(\s)+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+)(\s)+([0-9A-Z]+)"
|
|
|
|
)
|
|
|
|
if (
|
|
|
|
ipv4_pattern.match(bridge)
|
|
|
|
or ipv6_pattern.match(bridge)
|
|
|
|
or meek_lite_pattern.match(bridge)
|
|
|
|
or snowflake_pattern.match(bridge)
|
|
|
|
):
|
|
|
|
valid_bridges.append(bridge)
|
|
|
|
if valid_bridges:
|
|
|
|
return valid_bridges
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2021-11-14 20:16:21 -05:00
|
|
|
def is_flatpak(self):
|
|
|
|
"""
|
|
|
|
Returns True if OnionShare is running in a Flatpak sandbox
|
|
|
|
"""
|
|
|
|
return os.environ.get("FLATPAK_ID") == "org.onionshare.OnionShare"
|
2021-11-15 21:16:10 -05:00
|
|
|
|
2021-11-14 23:53:22 -05:00
|
|
|
def is_snapcraft(self):
|
|
|
|
"""
|
2021-11-16 21:10:25 -05:00
|
|
|
Returns True if OnionShare is running in a Snapcraft sandbox
|
2021-11-14 23:53:22 -05:00
|
|
|
"""
|
|
|
|
return os.environ.get("SNAP_INSTANCE_NAME") == "onionshare"
|
2021-11-14 20:16:21 -05:00
|
|
|
|
2021-11-29 18:54:17 -05:00
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
@staticmethod
|
|
|
|
def random_string(num_bytes, output_len=None):
|
|
|
|
"""
|
|
|
|
Returns a random string with a specified number of bytes.
|
|
|
|
"""
|
|
|
|
b = os.urandom(num_bytes)
|
|
|
|
h = hashlib.sha256(b).digest()[:16]
|
2019-10-13 00:01:25 -04:00
|
|
|
s = base64.b32encode(h).lower().replace(b"=", b"").decode("utf-8")
|
2018-03-08 13:18:31 -05:00
|
|
|
if not output_len:
|
|
|
|
return s
|
|
|
|
return s[:output_len]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def human_readable_filesize(b):
|
|
|
|
"""
|
|
|
|
Returns filesize in a human readable format.
|
|
|
|
"""
|
|
|
|
thresh = 1024.0
|
|
|
|
if b < thresh:
|
2019-10-20 21:14:00 -04:00
|
|
|
return "{:.1f} B".format(b)
|
2019-10-13 00:01:25 -04:00
|
|
|
units = ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
|
2018-03-08 13:18:31 -05:00
|
|
|
u = 0
|
2014-08-26 21:22:59 -04:00
|
|
|
b /= thresh
|
2018-03-08 13:18:31 -05:00
|
|
|
while b >= thresh:
|
|
|
|
b /= thresh
|
|
|
|
u += 1
|
2019-10-13 00:01:25 -04:00
|
|
|
return "{:.1f} {}".format(b, units[u])
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def format_seconds(seconds):
|
|
|
|
"""Return a human-readable string of the format 1d2h3m4s"""
|
|
|
|
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))
|
2019-10-13 00:01:25 -04:00
|
|
|
return "".join(human_readable)
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def estimated_time_remaining(bytes_downloaded, total_bytes, started):
|
|
|
|
now = time.time()
|
|
|
|
time_elapsed = now - started # in seconds
|
|
|
|
download_rate = bytes_downloaded / time_elapsed
|
|
|
|
remaining_bytes = total_bytes - bytes_downloaded
|
|
|
|
eta = remaining_bytes / download_rate
|
2018-03-13 05:22:26 -04:00
|
|
|
return Common.format_seconds(eta)
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_available_port(min_port, max_port):
|
|
|
|
"""
|
|
|
|
Find a random available port within the given range.
|
|
|
|
"""
|
|
|
|
with socket.socket() as tmpsock:
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
tmpsock.bind(("127.0.0.1", random.randint(min_port, max_port)))
|
|
|
|
break
|
2021-04-28 19:08:20 -04:00
|
|
|
except OSError:
|
2018-09-28 00:22:10 -04:00
|
|
|
pass
|
2018-03-08 13:18:31 -05:00
|
|
|
_, port = tmpsock.getsockname()
|
|
|
|
return port
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def dir_size(start_path):
|
|
|
|
"""
|
|
|
|
Calculates the total size, in bytes, of all of the files in a directory.
|
|
|
|
"""
|
|
|
|
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
|
2014-08-26 21:22:59 -04:00
|
|
|
|
2014-11-18 12:29:32 -05:00
|
|
|
|
2019-03-25 00:05:54 -04:00
|
|
|
class AutoStopTimer(threading.Thread):
|
2017-11-08 04:25:59 -05:00
|
|
|
"""
|
|
|
|
Background thread sleeps t hours and returns.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2018-03-08 13:18:31 -05:00
|
|
|
def __init__(self, common, time):
|
2017-11-08 04:25:59 -05:00
|
|
|
threading.Thread.__init__(self)
|
2018-03-08 13:18:31 -05:00
|
|
|
|
|
|
|
self.common = common
|
|
|
|
|
2017-11-08 04:25:59 -05:00
|
|
|
self.setDaemon(True)
|
|
|
|
self.time = time
|
|
|
|
|
|
|
|
def run(self):
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log(
|
2019-10-20 13:15:16 -04:00
|
|
|
"AutoStopTimer", f"Server will shut down after {self.time} seconds"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2017-11-08 19:29:55 -05:00
|
|
|
time.sleep(self.time)
|
2017-11-08 04:25:59 -05:00
|
|
|
return 1
|