2015-09-08 01:34:54 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
2022-01-16 19:15:49 -05:00
|
|
|
Copyright (C) 2014-2022 Micah Lee, et al. <micah@micahflee.com>
|
2015-09-08 01:34:54 -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/>.
|
|
|
|
"""
|
|
|
|
|
2021-11-22 23:11:50 -05:00
|
|
|
from .censorship import CensorshipCircumvention
|
|
|
|
from .meek import Meek
|
2015-09-08 01:34:54 -04:00
|
|
|
from stem.control import Controller
|
2017-04-17 23:49:06 -04:00
|
|
|
from stem import ProtocolError, SocketClosed
|
2017-01-07 20:31:26 -05:00
|
|
|
from stem.connection import MissingPassword, UnreadableCookieFile, AuthenticationFailure
|
2020-11-17 20:45:31 -05:00
|
|
|
import base64
|
2021-05-03 20:02:02 -04:00
|
|
|
import nacl.public
|
2020-11-17 20:45:31 -05:00
|
|
|
import os
|
2021-11-24 01:55:47 -05:00
|
|
|
import psutil
|
|
|
|
import shlex
|
2020-11-17 20:45:31 -05:00
|
|
|
import subprocess
|
2021-11-24 01:55:47 -05:00
|
|
|
import tempfile
|
2020-11-17 20:45:31 -05:00
|
|
|
import time
|
2021-11-14 23:53:22 -05:00
|
|
|
import traceback
|
2015-09-08 01:34:54 -04:00
|
|
|
|
2018-08-21 05:31:02 -04:00
|
|
|
from distutils.version import LooseVersion as Version
|
2015-09-08 01:34:54 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2016-12-29 15:57:58 -05:00
|
|
|
class TorErrorAutomatic(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare is failing to connect and authenticate to the Tor controller,
|
|
|
|
using automatic settings that should work with Tor Browser.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
class TorErrorInvalidSetting(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if the settings just don't make sense.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
class TorErrorSocketPort(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare can't connect to the Tor controller using the supplied address and port.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
class TorErrorSocketFile(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare can't connect to the Tor controller using the supplied socket file.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
class TorErrorMissingPassword(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare connected to the Tor controller, but it requires a password.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
class TorErrorUnreadableCookieFile(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare connected to the Tor controller, but your user does not have permission
|
|
|
|
to access the cookie file.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2017-01-07 20:31:26 -05:00
|
|
|
class TorErrorAuthError(Exception):
|
|
|
|
"""
|
|
|
|
OnionShare connected to the address and port, but can't authenticate. It's possible
|
|
|
|
that a Tor controller isn't listening on this port.
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2017-02-22 19:45:52 -05:00
|
|
|
class TorErrorProtocolError(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if onionshare connects to the Tor controller, but it
|
|
|
|
isn't acting like a Tor controller (such as in Whonix).
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2020-11-29 15:25:09 -05:00
|
|
|
class TorTooOldEphemeral(Exception):
|
2016-12-22 19:56:39 -05:00
|
|
|
"""
|
2020-11-29 15:25:09 -05:00
|
|
|
This exception is raised if the version of tor doesn't support ephemeral onion services
|
2016-12-22 19:56:39 -05:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
|
2020-11-29 15:25:09 -05:00
|
|
|
class TorTooOldStealth(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if the version of tor doesn't support stealth onion services
|
2016-12-22 19:56:39 -05:00
|
|
|
"""
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2017-04-14 13:00:56 -04:00
|
|
|
class BundledTorTimeout(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
|
|
but Tor doesn't finish connecting promptly.
|
|
|
|
"""
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2017-04-17 23:49:06 -04:00
|
|
|
class BundledTorCanceled(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
|
|
and the user cancels connecting to Tor
|
|
|
|
"""
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2017-05-17 15:00:42 -04:00
|
|
|
class BundledTorBroken(Exception):
|
|
|
|
"""
|
|
|
|
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
|
|
but the process seems to fail to run.
|
|
|
|
"""
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2020-11-29 15:25:09 -05:00
|
|
|
class PortNotAvailable(Exception):
|
|
|
|
"""
|
|
|
|
There are no available ports for OnionShare to use, which really shouldn't ever happen
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2016-09-05 14:16:54 -04:00
|
|
|
class Onion(object):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
2016-09-05 14:16:54 -04:00
|
|
|
Onion is an abstraction layer for connecting to the Tor control port and
|
2017-02-22 17:10:06 -05:00
|
|
|
creating onion services. OnionShare supports creating onion services by
|
|
|
|
connecting to the Tor controller and using ADD_ONION, DEL_ONION.
|
2017-04-14 01:56:47 -04:00
|
|
|
|
|
|
|
stealth: Should the onion service be stealth?
|
|
|
|
|
|
|
|
settings: A Settings object. If it's not passed in, load from disk.
|
|
|
|
|
|
|
|
bundled_connection_func: If the tor connection type is bundled, optionally
|
|
|
|
call this function and pass in a status string while connecting to tor. This
|
|
|
|
is necessary for status updates to reach the GUI.
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2020-10-15 22:09:46 -04:00
|
|
|
def __init__(self, common, use_tmp_dir=False, get_tor_paths=None):
|
2018-03-08 13:18:31 -05:00
|
|
|
self.common = common
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("Onion", "__init__")
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2019-11-28 17:02:00 -05:00
|
|
|
self.use_tmp_dir = use_tmp_dir
|
|
|
|
|
2017-04-14 01:22:34 -04:00
|
|
|
# Set the path of the tor binary, for bundled tor
|
2020-10-15 22:09:46 -04:00
|
|
|
if not get_tor_paths:
|
|
|
|
get_tor_paths = self.common.get_tor_paths
|
2019-10-13 00:01:25 -04:00
|
|
|
(
|
|
|
|
self.tor_path,
|
|
|
|
self.tor_geo_ip_file_path,
|
|
|
|
self.tor_geo_ipv6_file_path,
|
|
|
|
self.obfs4proxy_file_path,
|
2021-10-11 23:45:28 -04:00
|
|
|
self.snowflake_file_path,
|
2021-10-18 02:17:47 -04:00
|
|
|
self.meek_client_file_path,
|
2020-10-15 22:09:46 -04:00
|
|
|
) = get_tor_paths()
|
2017-04-14 01:22:34 -04:00
|
|
|
|
|
|
|
# The tor process
|
2017-04-15 14:43:19 -04:00
|
|
|
self.tor_proc = None
|
2017-04-14 01:22:34 -04:00
|
|
|
|
2018-12-06 02:05:25 -05:00
|
|
|
# The Tor controller
|
|
|
|
self.c = None
|
|
|
|
|
2017-05-16 16:09:27 -04:00
|
|
|
# Start out not connected to Tor
|
|
|
|
self.connected_to_tor = False
|
|
|
|
|
2020-08-27 01:02:23 -04:00
|
|
|
# Assigned later if we are using stealth mode
|
|
|
|
self.auth_string = None
|
|
|
|
|
2020-11-27 14:22:29 -05:00
|
|
|
# Keep track of onions where it's important to gracefully close to prevent truncated downloads
|
|
|
|
self.graceful_close_onions = []
|
|
|
|
|
2021-05-03 20:02:02 -04:00
|
|
|
def key_str(self, key):
|
|
|
|
"""
|
|
|
|
Returns a base32 decoded string of a key.
|
|
|
|
"""
|
|
|
|
# bytes to base 32
|
|
|
|
key_bytes = bytes(key)
|
|
|
|
key_b32 = base64.b32encode(key_bytes)
|
|
|
|
# strip trailing ====
|
2021-10-11 23:45:28 -04:00
|
|
|
assert key_b32[-4:] == b"===="
|
2021-05-03 20:02:02 -04:00
|
|
|
key_b32 = key_b32[:-4]
|
|
|
|
# change from b'ASDF' to ASDF
|
2021-10-11 23:45:28 -04:00
|
|
|
s = key_b32.decode("utf-8")
|
2021-05-03 20:02:02 -04:00
|
|
|
return s
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
def connect(
|
|
|
|
self,
|
2019-11-02 20:13:06 -04:00
|
|
|
custom_settings=None,
|
|
|
|
config=None,
|
2019-10-13 00:01:25 -04:00
|
|
|
tor_status_update_func=None,
|
|
|
|
connect_timeout=120,
|
2019-11-02 17:56:40 -04:00
|
|
|
local_only=False,
|
2019-10-13 00:01:25 -04:00
|
|
|
):
|
2019-11-02 17:56:40 -04:00
|
|
|
if local_only:
|
|
|
|
self.common.log(
|
|
|
|
"Onion", "connect", "--local-only, so skip trying to connect"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2018-03-13 06:28:47 -04:00
|
|
|
# Either use settings that are passed in, or use them from common
|
|
|
|
if custom_settings:
|
|
|
|
self.settings = custom_settings
|
2019-04-20 00:43:04 -04:00
|
|
|
elif config:
|
|
|
|
self.common.load_settings(config)
|
|
|
|
self.settings = self.common.settings
|
2017-04-17 22:13:53 -04:00
|
|
|
else:
|
2019-04-19 20:31:34 -04:00
|
|
|
self.common.load_settings()
|
2018-03-13 06:28:47 -04:00
|
|
|
self.settings = self.common.settings
|
2017-04-17 22:13:53 -04:00
|
|
|
|
2021-10-24 23:03:19 -04:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"connect",
|
|
|
|
f"connection_type={self.settings.get('connection_type')}",
|
|
|
|
)
|
|
|
|
|
2017-04-17 22:13:53 -04:00
|
|
|
# The Tor controller
|
2016-12-29 12:58:13 -05:00
|
|
|
self.c = None
|
2016-12-22 16:39:32 -05:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.settings.get("connection_type") == "bundled":
|
2017-04-14 01:22:34 -04:00
|
|
|
# Create a torrc for this session
|
2019-11-28 17:02:00 -05:00
|
|
|
if self.use_tmp_dir:
|
|
|
|
self.tor_data_directory = tempfile.TemporaryDirectory(
|
|
|
|
dir=self.common.build_tmp_dir()
|
|
|
|
)
|
|
|
|
self.tor_data_directory_name = self.tor_data_directory.name
|
|
|
|
else:
|
|
|
|
self.tor_data_directory_name = self.common.build_tor_dir()
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log(
|
2019-12-08 13:13:56 -05:00
|
|
|
"Onion",
|
|
|
|
"connect",
|
|
|
|
f"tor_data_directory_name={self.tor_data_directory_name}",
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2017-04-14 21:33:44 -04:00
|
|
|
|
2018-11-25 19:50:20 -05:00
|
|
|
# Create the torrc
|
2019-10-13 00:01:25 -04:00
|
|
|
with open(self.common.get_resource_path("torrc_template")) as f:
|
2018-11-25 19:50:20 -05:00
|
|
|
torrc_template = f.read()
|
2019-10-13 00:01:25 -04:00
|
|
|
self.tor_cookie_auth_file = os.path.join(
|
2019-11-28 17:02:00 -05:00
|
|
|
self.tor_data_directory_name, "cookie"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2018-11-25 19:50:20 -05:00
|
|
|
try:
|
|
|
|
self.tor_socks_port = self.common.get_available_port(1000, 65535)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-29 15:25:09 -05:00
|
|
|
print("OnionShare port not available")
|
|
|
|
raise PortNotAvailable()
|
2019-11-28 17:02:00 -05:00
|
|
|
self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
|
2018-11-25 19:50:20 -05:00
|
|
|
|
2020-11-17 21:06:36 -05:00
|
|
|
# If there is an existing OnionShare tor process, kill it
|
|
|
|
for proc in psutil.process_iter(["pid", "name", "username"]):
|
2020-11-17 21:46:22 -05:00
|
|
|
try:
|
2020-11-17 21:06:36 -05:00
|
|
|
cmdline = proc.cmdline()
|
|
|
|
if (
|
|
|
|
cmdline[0] == self.tor_path
|
|
|
|
and cmdline[1] == "-f"
|
2020-11-17 21:08:59 -05:00
|
|
|
and cmdline[2] == self.tor_torrc
|
2020-11-17 21:06:36 -05:00
|
|
|
):
|
|
|
|
self.common.log(
|
2021-11-22 23:11:50 -05:00
|
|
|
"Onion", "connect", "found a stale tor process, killing it"
|
2020-11-17 21:06:36 -05:00
|
|
|
)
|
|
|
|
proc.terminate()
|
|
|
|
proc.wait()
|
2020-11-17 21:46:22 -05:00
|
|
|
break
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-17 21:46:22 -05:00
|
|
|
pass
|
2020-11-17 21:06:36 -05:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.common.platform == "Windows" or self.common.platform == "Darwin":
|
2018-11-25 19:50:20 -05:00
|
|
|
# Windows doesn't support unix sockets, so it must use a network port.
|
|
|
|
# macOS can't use unix sockets either because socket filenames are limited to
|
|
|
|
# 100 chars, and the macOS sandbox forces us to put the socket file in a place
|
|
|
|
# with a really long path.
|
2019-10-13 00:01:25 -04:00
|
|
|
torrc_template += "ControlPort {{control_port}}\n"
|
2017-12-06 16:49:10 -05:00
|
|
|
try:
|
2018-03-08 13:18:31 -05:00
|
|
|
self.tor_control_port = self.common.get_available_port(1000, 65535)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-29 15:25:09 -05:00
|
|
|
print("OnionShare port not available")
|
|
|
|
raise PortNotAvailable()
|
2017-04-14 21:33:44 -04:00
|
|
|
self.tor_control_socket = None
|
|
|
|
else:
|
2018-11-25 19:50:20 -05:00
|
|
|
# Linux and BSD can use unix sockets
|
2019-10-13 00:01:25 -04:00
|
|
|
torrc_template += "ControlSocket {{control_socket}}\n"
|
2017-04-14 21:33:44 -04:00
|
|
|
self.tor_control_port = None
|
2019-10-13 00:01:25 -04:00
|
|
|
self.tor_control_socket = os.path.join(
|
2019-11-28 17:02:00 -05:00
|
|
|
self.tor_data_directory_name, "control_socket"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
torrc_template = torrc_template.replace(
|
2019-11-28 17:02:00 -05:00
|
|
|
"{{data_directory}}", self.tor_data_directory_name
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{control_port}}", str(self.tor_control_port)
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{control_socket}}", str(self.tor_control_socket)
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{cookie_auth_file}}", self.tor_cookie_auth_file
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{geo_ip_file}}", self.tor_geo_ip_file_path
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{geo_ipv6_file}}", self.tor_geo_ipv6_file_path
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{socks_port}}", str(self.tor_socks_port)
|
|
|
|
)
|
2021-10-15 12:17:03 -04:00
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{obfs4proxy_path}}", str(self.obfs4proxy_file_path)
|
|
|
|
)
|
|
|
|
torrc_template = torrc_template.replace(
|
|
|
|
"{{snowflake_path}}", str(self.snowflake_file_path)
|
|
|
|
)
|
2019-10-13 00:01:25 -04:00
|
|
|
|
|
|
|
with open(self.tor_torrc, "w") as f:
|
2021-11-24 01:55:47 -05:00
|
|
|
self.common.log("Onion", "connect", "Writing torrc template file")
|
2017-05-23 18:16:27 -04:00
|
|
|
f.write(torrc_template)
|
2017-04-14 01:22:34 -04:00
|
|
|
|
2017-12-10 22:53:13 -05:00
|
|
|
# Bridge support
|
2021-10-27 00:06:05 -04:00
|
|
|
if self.settings.get("bridges_enabled"):
|
2021-11-22 23:11:50 -05:00
|
|
|
f.write("\nUseBridges 1\n")
|
2021-10-27 00:06:05 -04:00
|
|
|
if self.settings.get("bridges_type") == "built-in":
|
2021-11-24 01:55:47 -05:00
|
|
|
use_torrc_bridge_templates = False
|
|
|
|
builtin_bridge_type = self.settings.get("bridges_builtin_pt")
|
|
|
|
# Use built-inbridges stored in settings, if they are there already.
|
|
|
|
# They are probably newer than that of our hardcoded copies.
|
|
|
|
if self.settings.get("bridges_builtin"):
|
|
|
|
try:
|
|
|
|
for line in self.settings.get("bridges_builtin")[
|
|
|
|
builtin_bridge_type
|
|
|
|
]:
|
|
|
|
if line.strip() != "":
|
|
|
|
f.write(f"Bridge {line}\n")
|
2021-11-22 23:36:41 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"connect",
|
2021-11-24 01:55:47 -05:00
|
|
|
"Wrote in the built-in bridges from OnionShare settings",
|
2021-11-22 23:36:41 -05:00
|
|
|
)
|
2021-11-24 01:55:47 -05:00
|
|
|
except KeyError:
|
|
|
|
# Somehow we had built-in bridges in our settings, but
|
|
|
|
# not for this bridge type. Fall back to using the hard-
|
|
|
|
# coded templates.
|
|
|
|
use_torrc_bridge_templates = True
|
2021-11-22 23:11:50 -05:00
|
|
|
else:
|
2021-11-24 01:55:47 -05:00
|
|
|
use_torrc_bridge_templates = True
|
|
|
|
if use_torrc_bridge_templates:
|
|
|
|
if builtin_bridge_type == "obfs4":
|
|
|
|
with open(
|
|
|
|
self.common.get_resource_path(
|
|
|
|
"torrc_template-obfs4"
|
|
|
|
)
|
|
|
|
) as o:
|
|
|
|
f.write(o.read())
|
|
|
|
elif builtin_bridge_type == "meek-azure":
|
|
|
|
with open(
|
|
|
|
self.common.get_resource_path(
|
|
|
|
"torrc_template-meek_lite_azure"
|
|
|
|
)
|
|
|
|
) as o:
|
|
|
|
f.write(o.read())
|
|
|
|
elif builtin_bridge_type == "snowflake":
|
|
|
|
with open(
|
|
|
|
self.common.get_resource_path(
|
|
|
|
"torrc_template-snowflake"
|
|
|
|
)
|
|
|
|
) as o:
|
|
|
|
f.write(o.read())
|
2021-11-22 23:11:50 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"connect",
|
2021-11-24 01:55:47 -05:00
|
|
|
"Wrote in the built-in bridges from torrc templates",
|
2021-11-22 23:11:50 -05:00
|
|
|
)
|
2021-10-27 00:06:05 -04:00
|
|
|
elif self.settings.get("bridges_type") == "moat":
|
|
|
|
for line in self.settings.get("bridges_moat").split("\n"):
|
|
|
|
if line.strip() != "":
|
|
|
|
f.write(f"Bridge {line}\n")
|
|
|
|
|
|
|
|
elif self.settings.get("bridges_type") == "custom":
|
|
|
|
for line in self.settings.get("bridges_custom").split("\n"):
|
|
|
|
if line.strip() != "":
|
|
|
|
f.write(f"Bridge {line}\n")
|
2017-12-10 22:53:13 -05:00
|
|
|
|
2017-04-15 14:34:03 -04:00
|
|
|
# Execute a tor subprocess
|
2021-11-22 23:11:50 -05:00
|
|
|
self.common.log("Onion", "connect", f"starting {self.tor_path} subprocess")
|
2017-04-14 13:00:56 -04:00
|
|
|
start_ts = time.time()
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.common.platform == "Windows":
|
2017-04-15 14:34:03 -04:00
|
|
|
# In Windows, hide console window when opening tor.exe subprocess
|
|
|
|
startupinfo = subprocess.STARTUPINFO()
|
|
|
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
2019-10-13 00:01:25 -04:00
|
|
|
self.tor_proc = subprocess.Popen(
|
|
|
|
[self.tor_path, "-f", self.tor_torrc],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
startupinfo=startupinfo,
|
|
|
|
)
|
2017-04-15 14:34:03 -04:00
|
|
|
else:
|
2021-11-14 23:53:22 -05:00
|
|
|
if self.common.is_snapcraft():
|
|
|
|
env = None
|
|
|
|
else:
|
|
|
|
env = {"LD_LIBRARY_PATH": os.path.dirname(self.tor_path)}
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
self.tor_proc = subprocess.Popen(
|
|
|
|
[self.tor_path, "-f", self.tor_torrc],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
2021-11-14 23:53:22 -05:00
|
|
|
env=env,
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2017-04-15 14:34:03 -04:00
|
|
|
|
|
|
|
# Wait for the tor controller to start
|
2021-11-22 23:11:50 -05:00
|
|
|
self.common.log("Onion", "connect", f"tor pid: {self.tor_proc.pid}")
|
2017-05-18 20:34:36 -04:00
|
|
|
time.sleep(2)
|
2017-04-14 01:22:34 -04:00
|
|
|
|
2022-01-16 14:44:30 -05:00
|
|
|
return_code = self.tor_proc.poll()
|
|
|
|
if return_code != None:
|
|
|
|
self.common.log("Onion", "connect", f"tor process has terminated early: {return_code}")
|
|
|
|
|
2017-04-14 01:22:34 -04:00
|
|
|
# Connect to the controller
|
2021-11-22 23:11:50 -05:00
|
|
|
self.common.log("Onion", "connect", "authenticating to tor controller")
|
2021-11-14 23:53:22 -05:00
|
|
|
try:
|
|
|
|
if (
|
|
|
|
self.common.platform == "Windows"
|
|
|
|
or self.common.platform == "Darwin"
|
|
|
|
):
|
|
|
|
self.c = Controller.from_port(port=self.tor_control_port)
|
|
|
|
self.c.authenticate()
|
|
|
|
else:
|
|
|
|
self.c = Controller.from_socket_file(path=self.tor_control_socket)
|
|
|
|
self.c.authenticate()
|
|
|
|
except Exception as e:
|
|
|
|
print("OnionShare could not connect to Tor:\n{}".format(e.args[0]))
|
|
|
|
print(traceback.format_exc())
|
|
|
|
raise BundledTorBroken(e.args[0])
|
2017-04-14 01:22:34 -04:00
|
|
|
|
|
|
|
while True:
|
2017-04-17 23:49:06 -04:00
|
|
|
try:
|
|
|
|
res = self.c.get_info("status/bootstrap-phase")
|
|
|
|
except SocketClosed:
|
|
|
|
raise BundledTorCanceled()
|
|
|
|
|
2017-04-14 01:22:34 -04:00
|
|
|
res_parts = shlex.split(res)
|
2019-10-13 00:01:25 -04:00
|
|
|
progress = res_parts[2].split("=")[1]
|
|
|
|
summary = res_parts[4].split("=")[1]
|
2017-04-14 01:22:34 -04:00
|
|
|
|
|
|
|
# "\033[K" clears the rest of the line
|
2020-08-27 18:21:03 -04:00
|
|
|
print(
|
|
|
|
f"\rConnecting to the Tor network: {progress}% - {summary}\033[K",
|
|
|
|
end="",
|
|
|
|
)
|
2017-04-14 01:56:47 -04:00
|
|
|
|
2017-04-17 23:26:35 -04:00
|
|
|
if callable(tor_status_update_func):
|
2017-05-14 21:30:45 -04:00
|
|
|
if not tor_status_update_func(progress, summary):
|
|
|
|
# If the dialog was canceled, stop connecting to Tor
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"connect",
|
|
|
|
"tor_status_update_func returned false, canceling connecting to Tor",
|
|
|
|
)
|
2017-05-14 21:30:45 -04:00
|
|
|
print()
|
|
|
|
return False
|
2017-04-14 01:56:47 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if summary == "Done":
|
2017-04-14 01:22:34 -04:00
|
|
|
print("")
|
|
|
|
break
|
2017-05-16 14:47:18 -04:00
|
|
|
time.sleep(0.2)
|
2017-04-14 01:22:34 -04:00
|
|
|
|
2017-12-14 01:31:15 -05:00
|
|
|
# If using bridges, it might take a bit longer to connect to Tor
|
2021-10-27 00:07:38 -04:00
|
|
|
if self.settings.get("bridges_enabled"):
|
2019-10-13 00:01:25 -04:00
|
|
|
# Only override timeout if a custom timeout has not been passed in
|
|
|
|
if connect_timeout == 120:
|
|
|
|
connect_timeout = 150
|
2017-12-14 01:31:15 -05:00
|
|
|
if time.time() - start_ts > connect_timeout:
|
2017-04-14 13:00:56 -04:00
|
|
|
print("")
|
2019-03-12 00:29:07 -04:00
|
|
|
try:
|
|
|
|
self.tor_proc.terminate()
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Taking too long to connect to Tor. Maybe you aren't connected to the Internet, or have an inaccurate system clock?"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise BundledTorTimeout()
|
2019-03-12 00:29:07 -04:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2017-04-14 13:00:56 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
elif self.settings.get("connection_type") == "automatic":
|
2016-12-29 12:58:13 -05:00
|
|
|
# Automatically try to guess the right way to connect to Tor Browser
|
2020-11-29 15:25:09 -05:00
|
|
|
automatic_error = "Could not connect to the Tor controller. Is Tor Browser (available from torproject.org) running in the background?"
|
2016-12-29 12:58:13 -05:00
|
|
|
|
2016-12-29 16:36:29 -05:00
|
|
|
# Try connecting to control port
|
|
|
|
found_tor = False
|
2016-12-29 15:57:58 -05:00
|
|
|
|
2016-12-29 16:36:29 -05:00
|
|
|
# If the TOR_CONTROL_PORT environment variable is set, use that
|
2019-10-13 00:01:25 -04:00
|
|
|
env_port = os.environ.get("TOR_CONTROL_PORT")
|
2016-12-29 16:36:29 -05:00
|
|
|
if env_port:
|
|
|
|
try:
|
|
|
|
self.c = Controller.from_port(port=int(env_port))
|
|
|
|
found_tor = True
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2016-12-29 16:36:29 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor
|
|
|
|
try:
|
|
|
|
ports = [9151, 9153, 9051]
|
|
|
|
for port in ports:
|
|
|
|
self.c = Controller.from_port(port=port)
|
|
|
|
found_tor = True
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2016-12-29 16:36:29 -05:00
|
|
|
pass
|
|
|
|
|
2017-01-06 14:46:41 -05:00
|
|
|
# If this still didn't work, try guessing the default socket file path
|
2019-10-13 00:01:25 -04:00
|
|
|
socket_file_path = ""
|
2017-01-06 14:46:41 -05:00
|
|
|
if not found_tor:
|
2017-01-06 14:54:42 -05:00
|
|
|
try:
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.common.platform == "Darwin":
|
|
|
|
socket_file_path = os.path.expanduser(
|
|
|
|
"~/Library/Application Support/TorBrowser-Data/Tor/control.socket"
|
|
|
|
)
|
2017-01-06 14:46:41 -05:00
|
|
|
|
2017-01-06 14:54:42 -05:00
|
|
|
self.c = Controller.from_socket_file(path=socket_file_path)
|
|
|
|
found_tor = True
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2017-01-06 14:54:42 -05:00
|
|
|
pass
|
2017-01-06 14:46:41 -05:00
|
|
|
|
2016-12-29 16:36:29 -05:00
|
|
|
# If connecting to default control ports failed, so let's try
|
|
|
|
# guessing the socket file name next
|
|
|
|
if not found_tor:
|
|
|
|
try:
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.common.platform == "Linux" or self.common.platform == "BSD":
|
2019-10-20 13:15:16 -04:00
|
|
|
socket_file_path = (
|
|
|
|
f"/run/user/{os.geteuid()}/Tor/control.socket"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
|
|
|
elif self.common.platform == "Darwin":
|
2019-10-20 13:15:16 -04:00
|
|
|
socket_file_path = (
|
|
|
|
f"/run/user/{os.geteuid()}/Tor/control.socket"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
|
|
|
elif self.common.platform == "Windows":
|
2016-12-29 16:36:29 -05:00
|
|
|
# Windows doesn't support unix sockets
|
2020-11-29 15:25:09 -05:00
|
|
|
print(automatic_error)
|
|
|
|
raise TorErrorAutomatic()
|
2016-12-29 15:57:58 -05:00
|
|
|
|
2016-12-29 16:36:29 -05:00
|
|
|
self.c = Controller.from_socket_file(path=socket_file_path)
|
|
|
|
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(automatic_error)
|
|
|
|
raise TorErrorAutomatic()
|
2016-12-29 15:57:58 -05:00
|
|
|
|
|
|
|
# Try authenticating
|
|
|
|
try:
|
|
|
|
self.c.authenticate()
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(automatic_error)
|
|
|
|
raise TorErrorAutomatic()
|
2016-12-22 16:39:32 -05:00
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
else:
|
|
|
|
# Use specific settings to connect to tor
|
2020-11-29 15:25:09 -05:00
|
|
|
invalid_settings_error = "Can't connect to Tor controller because your settings don't make sense."
|
2016-12-29 12:58:13 -05:00
|
|
|
|
|
|
|
# Try connecting
|
2015-09-08 01:34:54 -04:00
|
|
|
try:
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.settings.get("connection_type") == "control_port":
|
|
|
|
self.c = Controller.from_port(
|
|
|
|
address=self.settings.get("control_port_address"),
|
|
|
|
port=self.settings.get("control_port_port"),
|
|
|
|
)
|
|
|
|
elif self.settings.get("connection_type") == "socket_file":
|
|
|
|
self.c = Controller.from_socket_file(
|
|
|
|
path=self.settings.get("socket_file_path")
|
|
|
|
)
|
2016-12-29 12:58:13 -05:00
|
|
|
else:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(invalid_settings_error)
|
|
|
|
raise TorErrorInvalidSetting()
|
2016-12-29 12:58:13 -05:00
|
|
|
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.settings.get("connection_type") == "control_port":
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Can't connect to the Tor controller at {}:{}.".format(
|
2019-10-13 00:01:25 -04:00
|
|
|
self.settings.get("control_port_address"),
|
|
|
|
self.settings.get("control_port_port"),
|
|
|
|
)
|
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise TorErrorSocketPort(
|
|
|
|
self.settings.get("control_port_address"),
|
|
|
|
self.settings.get("control_port_port"),
|
|
|
|
)
|
2021-03-10 03:40:06 -05:00
|
|
|
print(
|
|
|
|
"Can't connect to the Tor controller using socket file {}.".format(
|
|
|
|
self.settings.get("socket_file_path")
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2021-03-10 03:40:06 -05:00
|
|
|
)
|
|
|
|
raise TorErrorSocketFile(self.settings.get("socket_file_path"))
|
2017-01-07 20:31:26 -05:00
|
|
|
|
2016-12-29 12:58:13 -05:00
|
|
|
# Try authenticating
|
|
|
|
try:
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.settings.get("auth_type") == "no_auth":
|
2016-12-29 12:58:13 -05:00
|
|
|
self.c.authenticate()
|
2019-10-13 00:01:25 -04:00
|
|
|
elif self.settings.get("auth_type") == "password":
|
|
|
|
self.c.authenticate(self.settings.get("auth_password"))
|
2016-12-29 12:58:13 -05:00
|
|
|
else:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(invalid_settings_error)
|
|
|
|
raise TorErrorInvalidSetting()
|
2016-12-29 12:58:13 -05:00
|
|
|
|
2016-12-22 16:39:32 -05:00
|
|
|
except MissingPassword:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Connected to Tor controller, but it requires a password to authenticate."
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise TorErrorMissingPassword()
|
2016-12-22 16:39:32 -05:00
|
|
|
except UnreadableCookieFile:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Connected to the Tor controller, but password may be wrong, or your user is not permitted to read the cookie file."
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise TorErrorUnreadableCookieFile()
|
2017-01-07 20:31:26 -05:00
|
|
|
except AuthenticationFailure:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Connected to {}:{}, but can't authenticate. Maybe this isn't a Tor controller?".format(
|
2019-10-13 00:01:25 -04:00
|
|
|
self.settings.get("control_port_address"),
|
|
|
|
self.settings.get("control_port_port"),
|
|
|
|
)
|
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise TorErrorAuthError(
|
|
|
|
self.settings.get("control_port_address"),
|
|
|
|
self.settings.get("control_port_port"),
|
|
|
|
)
|
2016-12-29 12:58:13 -05:00
|
|
|
|
2017-05-16 16:09:27 -04:00
|
|
|
# If we made it this far, we should be connected to Tor
|
|
|
|
self.connected_to_tor = True
|
|
|
|
|
2017-02-22 17:10:06 -05:00
|
|
|
# Get the tor version
|
2016-12-29 13:03:29 -05:00
|
|
|
self.tor_version = self.c.get_version().version_str
|
2019-10-20 20:59:12 -04:00
|
|
|
self.common.log("Onion", "connect", f"Connected to tor {self.tor_version}")
|
2015-09-08 01:34:54 -04:00
|
|
|
|
2017-02-22 17:10:06 -05:00
|
|
|
# Do the versions of stem and tor that I'm using support ephemeral onion services?
|
2019-10-13 00:01:25 -04:00
|
|
|
list_ephemeral_hidden_services = getattr(
|
|
|
|
self.c, "list_ephemeral_hidden_services", None
|
|
|
|
)
|
|
|
|
self.supports_ephemeral = (
|
|
|
|
callable(list_ephemeral_hidden_services) and self.tor_version >= "0.2.7.1"
|
|
|
|
)
|
2015-09-08 01:34:54 -04:00
|
|
|
|
2021-05-03 20:02:02 -04:00
|
|
|
# Do the versions of stem and tor that I'm using support v3 stealth onion services?
|
|
|
|
try:
|
|
|
|
res = self.c.create_ephemeral_hidden_service(
|
|
|
|
{1: 1},
|
|
|
|
basic_auth=None,
|
|
|
|
await_publication=False,
|
|
|
|
key_type="NEW",
|
|
|
|
key_content="ED25519-V3",
|
|
|
|
client_auth_v3="E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA",
|
|
|
|
)
|
|
|
|
tmp_service_id = res.service_id
|
|
|
|
self.c.remove_ephemeral_hidden_service(tmp_service_id)
|
2021-05-06 00:26:00 -04:00
|
|
|
self.supports_stealth = True
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2016-12-22 19:56:39 -05:00
|
|
|
# ephemeral stealth onion services are not supported
|
2021-05-06 00:26:00 -04:00
|
|
|
self.supports_stealth = False
|
2021-05-03 20:02:02 -04:00
|
|
|
|
2018-09-25 01:26:19 -04:00
|
|
|
# Does this version of Tor support next-gen ('v3') onions?
|
2018-12-05 23:33:45 -05:00
|
|
|
# Note, this is the version of Tor where this bug was fixed:
|
|
|
|
# https://trac.torproject.org/projects/tor/ticket/28619
|
2019-10-13 00:01:25 -04:00
|
|
|
self.supports_v3_onions = self.tor_version >= Version("0.3.5.7")
|
2017-12-20 20:15:17 -05:00
|
|
|
|
2021-11-24 01:55:47 -05:00
|
|
|
# Now that we are connected to Tor, if we are using built-in bridges,
|
|
|
|
# update them with the latest copy available from the Tor API
|
|
|
|
if (
|
|
|
|
self.settings.get("bridges_enabled")
|
|
|
|
and self.settings.get("bridges_type") == "built-in"
|
|
|
|
):
|
|
|
|
self.update_builtin_bridges()
|
|
|
|
|
2017-12-20 17:22:53 -05:00
|
|
|
def is_authenticated(self):
|
|
|
|
"""
|
|
|
|
Returns True if the Tor connection is still working, or False otherwise.
|
|
|
|
"""
|
2017-12-20 22:08:13 -05:00
|
|
|
if self.c is not None:
|
2017-12-20 20:15:17 -05:00
|
|
|
return self.c.is_authenticated()
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2020-11-27 14:22:29 -05:00
|
|
|
def start_onion_service(self, mode, mode_settings, port, await_publication):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
2016-09-04 20:23:06 -04:00
|
|
|
Start a onion service on port 80, pointing to the given port, and
|
2015-11-15 22:01:20 -05:00
|
|
|
return the onion hostname.
|
|
|
|
"""
|
2019-11-10 20:32:34 -05:00
|
|
|
self.common.log("Onion", "start_onion_service", f"port={port}")
|
2019-03-11 00:55:17 -04:00
|
|
|
|
2017-02-22 17:10:06 -05:00
|
|
|
if not self.supports_ephemeral:
|
2020-11-29 15:25:09 -05:00
|
|
|
print(
|
2020-10-13 01:40:55 -04:00
|
|
|
"Your version of Tor is too old, ephemeral onion services are not supported"
|
|
|
|
)
|
2020-11-29 15:25:09 -05:00
|
|
|
raise TorTooOldEphemeral()
|
2015-12-08 02:47:13 -05:00
|
|
|
|
2019-12-08 13:13:56 -05:00
|
|
|
if mode_settings.get("onion", "private_key"):
|
|
|
|
key_content = mode_settings.get("onion", "private_key")
|
2021-05-06 00:26:00 -04:00
|
|
|
key_type = "ED25519-V3"
|
2017-12-06 20:45:29 -05:00
|
|
|
else:
|
2021-05-06 00:26:00 -04:00
|
|
|
key_content = "ED25519-V3"
|
2018-10-11 22:38:05 -04:00
|
|
|
key_type = "NEW"
|
2017-12-08 14:49:34 -05:00
|
|
|
|
2019-10-20 13:15:16 -04:00
|
|
|
debug_message = f"key_type={key_type}"
|
2018-10-11 22:38:05 -04:00
|
|
|
if key_type == "NEW":
|
2019-10-20 13:15:16 -04:00
|
|
|
debug_message += f", key_content={key_content}"
|
|
|
|
self.common.log("Onion", "start_onion_service", debug_message)
|
2021-05-06 00:26:00 -04:00
|
|
|
|
2021-08-27 01:52:29 -04:00
|
|
|
if mode_settings.get("general", "public"):
|
|
|
|
client_auth_priv_key = None
|
|
|
|
client_auth_pub_key = None
|
|
|
|
else:
|
2021-05-06 00:26:00 -04:00
|
|
|
if not self.supports_stealth:
|
|
|
|
print(
|
|
|
|
"Your version of Tor is too old, stealth onion services are not supported"
|
|
|
|
)
|
|
|
|
raise TorTooOldStealth()
|
|
|
|
else:
|
2021-10-11 23:45:28 -04:00
|
|
|
if key_type == "NEW" or not mode_settings.get(
|
|
|
|
"onion", "client_auth_priv_key"
|
|
|
|
):
|
2021-05-06 00:26:00 -04:00
|
|
|
# Generate a new key pair for Client Auth on new onions, or if
|
|
|
|
# it's a persistent onion but for some reason we don't them
|
2021-05-06 04:02:40 -04:00
|
|
|
client_auth_priv_key_raw = nacl.public.PrivateKey.generate()
|
|
|
|
client_auth_priv_key = self.key_str(client_auth_priv_key_raw)
|
2021-10-11 23:45:28 -04:00
|
|
|
client_auth_pub_key = self.key_str(
|
|
|
|
client_auth_priv_key_raw.public_key
|
|
|
|
)
|
2021-05-06 00:26:00 -04:00
|
|
|
else:
|
|
|
|
# These should have been saved in settings from the previous run of a persistent onion
|
2021-10-11 23:45:28 -04:00
|
|
|
client_auth_priv_key = mode_settings.get(
|
|
|
|
"onion", "client_auth_priv_key"
|
|
|
|
)
|
|
|
|
client_auth_pub_key = mode_settings.get(
|
|
|
|
"onion", "client_auth_pub_key"
|
|
|
|
)
|
2021-05-06 00:26:00 -04:00
|
|
|
|
2017-02-22 19:45:52 -05:00
|
|
|
try:
|
2021-05-06 00:35:11 -04:00
|
|
|
if not self.supports_stealth:
|
|
|
|
res = self.c.create_ephemeral_hidden_service(
|
|
|
|
{80: port},
|
|
|
|
await_publication=await_publication,
|
|
|
|
basic_auth=None,
|
|
|
|
key_type=key_type,
|
|
|
|
key_content=key_content,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
res = self.c.create_ephemeral_hidden_service(
|
|
|
|
{80: port},
|
|
|
|
await_publication=await_publication,
|
|
|
|
basic_auth=None,
|
|
|
|
key_type=key_type,
|
|
|
|
key_content=key_content,
|
2021-05-06 04:02:40 -04:00
|
|
|
client_auth_v3=client_auth_pub_key,
|
2021-05-06 00:35:11 -04:00
|
|
|
)
|
2017-02-22 19:45:52 -05:00
|
|
|
|
2018-09-18 20:17:25 -04:00
|
|
|
except ProtocolError as e:
|
2020-11-29 15:25:09 -05:00
|
|
|
print("Tor error: {}".format(e.args[0]))
|
|
|
|
raise TorErrorProtocolError(e.args[0])
|
2015-09-08 20:42:08 -04:00
|
|
|
|
2019-11-10 20:32:34 -05:00
|
|
|
onion_host = res.service_id + ".onion"
|
2015-09-08 20:42:08 -04:00
|
|
|
|
2020-11-27 14:22:29 -05:00
|
|
|
# Gracefully close share mode rendezvous circuits
|
|
|
|
if mode == "share":
|
|
|
|
self.graceful_close_onions.append(res.service_id)
|
|
|
|
|
2019-11-10 20:32:34 -05:00
|
|
|
# Save the service_id
|
2019-11-29 02:40:45 -05:00
|
|
|
mode_settings.set("general", "service_id", res.service_id)
|
2015-09-08 20:42:08 -04:00
|
|
|
|
2019-12-08 13:13:56 -05:00
|
|
|
# Save the private key and hidservauth string
|
|
|
|
if not mode_settings.get("onion", "private_key"):
|
|
|
|
mode_settings.set("onion", "private_key", res.private_key)
|
2017-02-22 17:10:06 -05:00
|
|
|
|
2021-05-03 20:02:02 -04:00
|
|
|
# If using V3 onions and Client Auth, save both the private and public key
|
2021-05-06 00:26:00 -04:00
|
|
|
# because we need to send the public key to ADD_ONION (if we restart this
|
|
|
|
# same share at a later date), and the private key to the other user for
|
|
|
|
# their Tor Browser.
|
2021-08-27 01:52:29 -04:00
|
|
|
if not mode_settings.get("general", "public"):
|
2021-05-06 04:02:40 -04:00
|
|
|
mode_settings.set("onion", "client_auth_priv_key", client_auth_priv_key)
|
|
|
|
mode_settings.set("onion", "client_auth_pub_key", client_auth_pub_key)
|
2021-05-03 20:02:02 -04:00
|
|
|
# If we were pasting the client auth directly into the filesystem behind a Tor client,
|
|
|
|
# it would need to be in the format below. However, let's just set the private key
|
|
|
|
# by itself, as this can be pasted directly into Tor Browser, which is likely to
|
|
|
|
# be the most common use case.
|
2021-05-06 04:02:40 -04:00
|
|
|
# self.auth_string = f"{onion_host}:x25519:{client_auth_priv_key}"
|
|
|
|
self.auth_string = client_auth_priv_key
|
2021-05-03 20:02:02 -04:00
|
|
|
|
2019-11-10 20:32:34 -05:00
|
|
|
return onion_host
|
|
|
|
|
|
|
|
def stop_onion_service(self, mode_settings):
|
|
|
|
"""
|
|
|
|
Stop a specific onion service
|
|
|
|
"""
|
|
|
|
onion_host = mode_settings.get("general", "service_id")
|
2020-08-21 19:14:40 -04:00
|
|
|
if onion_host:
|
|
|
|
self.common.log("Onion", "stop_onion_service", f"onion host: {onion_host}")
|
|
|
|
try:
|
|
|
|
self.c.remove_ephemeral_hidden_service(
|
|
|
|
mode_settings.get("general", "service_id")
|
|
|
|
)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-08-21 19:14:40 -04:00
|
|
|
self.common.log(
|
|
|
|
"Onion", "stop_onion_service", f"failed to remove {onion_host}"
|
|
|
|
)
|
2015-09-08 20:42:08 -04:00
|
|
|
|
2020-11-27 17:50:07 -05:00
|
|
|
def cleanup(self, stop_tor=True, wait=True):
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
2017-04-14 01:22:34 -04:00
|
|
|
Stop onion services that were created earlier. If there's a tor subprocess running, kill it.
|
2015-11-15 22:01:20 -05:00
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("Onion", "cleanup")
|
2017-05-16 14:23:18 -04:00
|
|
|
|
2018-02-25 01:44:27 -05:00
|
|
|
# Cleanup the ephemeral onion services, if we have any
|
|
|
|
try:
|
|
|
|
onions = self.c.list_ephemeral_hidden_services()
|
2019-11-10 20:32:34 -05:00
|
|
|
for service_id in onions:
|
|
|
|
onion_host = f"{service_id}.onion"
|
2018-02-25 01:44:27 -05:00
|
|
|
try:
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log(
|
2019-11-10 20:32:34 -05:00
|
|
|
"Onion", "cleanup", f"trying to remove onion {onion_host}"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2019-11-10 20:32:34 -05:00
|
|
|
self.c.remove_ephemeral_hidden_service(service_id)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log(
|
2019-11-10 20:32:34 -05:00
|
|
|
"Onion", "cleanup", f"failed to remove onion {onion_host}"
|
2019-10-13 00:01:25 -04:00
|
|
|
)
|
2018-02-25 01:44:27 -05:00
|
|
|
pass
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2018-02-25 01:44:27 -05:00
|
|
|
pass
|
2017-04-14 01:22:34 -04:00
|
|
|
|
2019-11-29 00:38:34 -05:00
|
|
|
if stop_tor:
|
|
|
|
# Stop tor process
|
|
|
|
if self.tor_proc:
|
2020-11-27 17:50:07 -05:00
|
|
|
if wait:
|
|
|
|
# Wait for Tor rendezvous circuits to close
|
|
|
|
# Catch exceptions to prevent crash on Ctrl-C
|
|
|
|
try:
|
2021-02-09 01:16:30 -05:00
|
|
|
rendezvous_circuit_ids = []
|
2020-11-27 14:08:58 -05:00
|
|
|
for c in self.c.get_circuits():
|
2020-11-27 17:50:07 -05:00
|
|
|
if (
|
|
|
|
c.purpose == "HS_SERVICE_REND"
|
|
|
|
and c.rend_query in self.graceful_close_onions
|
|
|
|
):
|
2021-02-09 01:16:30 -05:00
|
|
|
rendezvous_circuit_ids.append(c.id)
|
2020-11-27 17:50:07 -05:00
|
|
|
|
2021-03-10 03:40:06 -05:00
|
|
|
symbols = list("\\|/-")
|
2020-11-27 17:50:07 -05:00
|
|
|
symbols_i = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
num_rend_circuits = 0
|
|
|
|
for c in self.c.get_circuits():
|
2021-02-09 01:16:30 -05:00
|
|
|
if c.id in rendezvous_circuit_ids:
|
2020-11-27 17:50:07 -05:00
|
|
|
num_rend_circuits += 1
|
|
|
|
|
|
|
|
if num_rend_circuits == 0:
|
|
|
|
print(
|
|
|
|
"\rTor rendezvous circuits have closed" + " " * 20
|
|
|
|
)
|
|
|
|
break
|
|
|
|
|
|
|
|
if num_rend_circuits == 1:
|
|
|
|
circuits = "circuit"
|
|
|
|
else:
|
|
|
|
circuits = "circuits"
|
|
|
|
print(
|
|
|
|
f"\rWaiting for {num_rend_circuits} Tor rendezvous {circuits} to close {symbols[symbols_i]} ",
|
|
|
|
end="",
|
|
|
|
)
|
|
|
|
symbols_i = (symbols_i + 1) % len(symbols)
|
|
|
|
time.sleep(1)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2020-11-27 17:50:07 -05:00
|
|
|
pass
|
2020-11-25 14:25:32 -05:00
|
|
|
|
2019-11-29 00:38:34 -05:00
|
|
|
self.tor_proc.terminate()
|
|
|
|
time.sleep(0.2)
|
2020-08-29 10:49:10 -04:00
|
|
|
if self.tor_proc.poll() is None:
|
2019-12-08 13:13:56 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"cleanup",
|
|
|
|
"Tried to terminate tor process but it's still running",
|
|
|
|
)
|
2019-11-29 00:38:34 -05:00
|
|
|
try:
|
|
|
|
self.tor_proc.kill()
|
|
|
|
time.sleep(0.2)
|
2020-08-29 10:49:10 -04:00
|
|
|
if self.tor_proc.poll() is None:
|
2019-12-08 13:13:56 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"cleanup",
|
|
|
|
"Tried to kill tor process but it's still running",
|
|
|
|
)
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2019-12-08 13:13:56 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion", "cleanup", "Exception while killing tor process"
|
|
|
|
)
|
2019-11-29 00:38:34 -05:00
|
|
|
self.tor_proc = None
|
2017-04-14 21:33:44 -04:00
|
|
|
|
2019-11-29 00:38:34 -05:00
|
|
|
# Reset other Onion settings
|
|
|
|
self.connected_to_tor = False
|
2017-05-16 19:50:33 -04:00
|
|
|
|
2019-11-29 00:38:34 -05:00
|
|
|
try:
|
|
|
|
# Delete the temporary tor data directory
|
|
|
|
if self.use_tmp_dir:
|
|
|
|
self.tor_data_directory.cleanup()
|
2021-04-29 20:13:05 -04:00
|
|
|
except Exception:
|
2019-11-29 00:38:34 -05:00
|
|
|
pass
|
2017-07-13 20:14:49 -04:00
|
|
|
|
2017-04-15 19:33:41 -04:00
|
|
|
def get_tor_socks_port(self):
|
|
|
|
"""
|
|
|
|
Returns a (address, port) tuple for the Tor SOCKS port
|
|
|
|
"""
|
2019-10-13 00:01:25 -04:00
|
|
|
self.common.log("Onion", "get_tor_socks_port")
|
2017-05-16 14:44:34 -04:00
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if self.settings.get("connection_type") == "bundled":
|
|
|
|
return ("127.0.0.1", self.tor_socks_port)
|
|
|
|
elif self.settings.get("connection_type") == "automatic":
|
|
|
|
return ("127.0.0.1", 9150)
|
2017-04-15 19:33:41 -04:00
|
|
|
else:
|
2019-10-13 00:01:25 -04:00
|
|
|
return (self.settings.get("socks_address"), self.settings.get("socks_port"))
|
2021-11-24 01:55:47 -05:00
|
|
|
|
|
|
|
def update_builtin_bridges(self):
|
|
|
|
"""
|
|
|
|
Use the CensorshipCircumvention API to fetch the latest built-in bridges
|
|
|
|
and update them in settings.
|
|
|
|
"""
|
2021-11-26 18:35:25 -05:00
|
|
|
builtin_bridges = False
|
|
|
|
meek = None
|
2021-11-24 01:55:47 -05:00
|
|
|
# Try obtaining bridges over Tor, if we're connected to it.
|
|
|
|
if self.is_authenticated:
|
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"update_builtin_bridges",
|
|
|
|
"Updating the built-in bridges. Trying over Tor first",
|
|
|
|
)
|
2021-11-26 18:35:25 -05:00
|
|
|
self.censorship_circumvention = CensorshipCircumvention(
|
|
|
|
self.common, None, self
|
2021-11-24 01:55:47 -05:00
|
|
|
)
|
2021-11-26 18:35:25 -05:00
|
|
|
builtin_bridges = self.censorship_circumvention.request_builtin_bridges()
|
2021-11-24 01:55:47 -05:00
|
|
|
|
2021-11-26 18:35:25 -05:00
|
|
|
if not builtin_bridges:
|
|
|
|
# Tor was not running or it failed to hit the Tor API.
|
|
|
|
# Fall back to using Meek (domain-fronting).
|
2021-11-24 01:55:47 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"update_builtin_bridges",
|
|
|
|
"Updating the built-in bridges. Trying via Meek (no Tor)",
|
|
|
|
)
|
|
|
|
meek = Meek(self.common)
|
|
|
|
meek.start()
|
2021-11-26 18:35:25 -05:00
|
|
|
self.censorship_circumvention = CensorshipCircumvention(
|
|
|
|
self.common, meek, None
|
2021-11-24 01:55:47 -05:00
|
|
|
)
|
2021-11-26 18:35:25 -05:00
|
|
|
builtin_bridges = self.censorship_circumvention.request_builtin_bridges()
|
2021-11-24 01:55:47 -05:00
|
|
|
meek.cleanup()
|
|
|
|
|
2021-11-26 18:35:25 -05:00
|
|
|
if builtin_bridges:
|
|
|
|
# If we got to this point, we have bridges
|
2021-11-24 01:55:47 -05:00
|
|
|
self.common.log(
|
|
|
|
"Onion",
|
|
|
|
"update_builtin_bridges",
|
2021-11-26 18:35:25 -05:00
|
|
|
f"Obtained bridges: {builtin_bridges}",
|
2021-11-24 01:55:47 -05:00
|
|
|
)
|
|
|
|
# Save the new settings
|
2021-11-26 18:35:25 -05:00
|
|
|
self.settings.set("bridges_builtin", builtin_bridges)
|
2021-11-24 01:55:47 -05:00
|
|
|
self.settings.save()
|
|
|
|
else:
|
|
|
|
self.common.log(
|
|
|
|
"Onion", "update_builtin_bridges", "Error getting built-in bridges"
|
|
|
|
)
|
|
|
|
return False
|