onionshare_cli 2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onionshare_cli/__init__.py +522 -0
- onionshare_cli/common.py +296 -0
- onionshare_cli/mode_settings.py +146 -0
- onionshare_cli/onion.py +845 -0
- onionshare_cli/onionshare.py +110 -0
- onionshare_cli/resources/static/css/style.css +393 -0
- onionshare_cli/resources/static/img/ajax.gif +0 -0
- onionshare_cli/resources/static/img/favicon.ico +0 -0
- onionshare_cli/resources/static/img/logo.png +0 -0
- onionshare_cli/resources/static/img/logo_large.png +0 -0
- onionshare_cli/resources/static/img/web_file.png +0 -0
- onionshare_cli/resources/static/img/web_folder.png +0 -0
- onionshare_cli/resources/static/js/chat.js +173 -0
- onionshare_cli/resources/static/js/jquery-3.5.1.min.js +2 -0
- onionshare_cli/resources/static/js/receive.js +130 -0
- onionshare_cli/resources/static/js/send.js +75 -0
- onionshare_cli/resources/static/js/socket.io.min.js +3 -0
- onionshare_cli/resources/templates/401.html +21 -0
- onionshare_cli/resources/templates/403.html +21 -0
- onionshare_cli/resources/templates/404.html +21 -0
- onionshare_cli/resources/templates/405.html +21 -0
- onionshare_cli/resources/templates/chat.html +46 -0
- onionshare_cli/resources/templates/denied.html +14 -0
- onionshare_cli/resources/templates/listing.html +59 -0
- onionshare_cli/resources/templates/receive.html +46 -0
- onionshare_cli/resources/templates/send.html +69 -0
- onionshare_cli/resources/templates/thankyou.html +27 -0
- onionshare_cli/resources/torrc_template +8 -0
- onionshare_cli/resources/torrc_template-meek_lite_amazon +2 -0
- onionshare_cli/resources/torrc_template-meek_lite_azure +2 -0
- onionshare_cli/resources/torrc_template-obfs4 +27 -0
- onionshare_cli/resources/version.txt +1 -0
- onionshare_cli/resources/wordlist.txt +7776 -0
- onionshare_cli/settings.py +201 -0
- onionshare_cli/web/__init__.py +21 -0
- onionshare_cli/web/chat_mode.py +169 -0
- onionshare_cli/web/receive_mode.py +488 -0
- onionshare_cli/web/send_base_mode.py +338 -0
- onionshare_cli/web/share_mode.py +406 -0
- onionshare_cli/web/web.py +424 -0
- onionshare_cli/web/website_mode.py +123 -0
- onionshare_cli-2.3.dist-info/METADATA +34 -0
- onionshare_cli-2.3.dist-info/RECORD +45 -0
- onionshare_cli-2.3.dist-info/WHEEL +4 -0
- onionshare_cli-2.3.dist-info/entry_points.txt +3 -0
onionshare_cli/onion.py
ADDED
|
@@ -0,0 +1,845 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
OnionShare | https://onionshare.org/
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2014-2020 Micah Lee, et al. <micah@micahflee.com>
|
|
6
|
+
|
|
7
|
+
This program is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the GNU General Public License as published by
|
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
This program is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
GNU General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU General Public License
|
|
18
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from stem.control import Controller
|
|
22
|
+
from stem import ProtocolError, SocketClosed
|
|
23
|
+
from stem.connection import MissingPassword, UnreadableCookieFile, AuthenticationFailure
|
|
24
|
+
from Crypto.PublicKey import RSA
|
|
25
|
+
import base64
|
|
26
|
+
import os
|
|
27
|
+
import tempfile
|
|
28
|
+
import subprocess
|
|
29
|
+
import time
|
|
30
|
+
import shlex
|
|
31
|
+
import getpass
|
|
32
|
+
import psutil
|
|
33
|
+
|
|
34
|
+
from distutils.version import LooseVersion as Version
|
|
35
|
+
from .settings import Settings
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TorErrorAutomatic(Exception):
|
|
39
|
+
"""
|
|
40
|
+
OnionShare is failing to connect and authenticate to the Tor controller,
|
|
41
|
+
using automatic settings that should work with Tor Browser.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class TorErrorInvalidSetting(Exception):
|
|
46
|
+
"""
|
|
47
|
+
This exception is raised if the settings just don't make sense.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class TorErrorSocketPort(Exception):
|
|
52
|
+
"""
|
|
53
|
+
OnionShare can't connect to the Tor controller using the supplied address and port.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class TorErrorSocketFile(Exception):
|
|
58
|
+
"""
|
|
59
|
+
OnionShare can't connect to the Tor controller using the supplied socket file.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class TorErrorMissingPassword(Exception):
|
|
64
|
+
"""
|
|
65
|
+
OnionShare connected to the Tor controller, but it requires a password.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class TorErrorUnreadableCookieFile(Exception):
|
|
70
|
+
"""
|
|
71
|
+
OnionShare connected to the Tor controller, but your user does not have permission
|
|
72
|
+
to access the cookie file.
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class TorErrorAuthError(Exception):
|
|
77
|
+
"""
|
|
78
|
+
OnionShare connected to the address and port, but can't authenticate. It's possible
|
|
79
|
+
that a Tor controller isn't listening on this port.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class TorErrorProtocolError(Exception):
|
|
84
|
+
"""
|
|
85
|
+
This exception is raised if onionshare connects to the Tor controller, but it
|
|
86
|
+
isn't acting like a Tor controller (such as in Whonix).
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class TorTooOldEphemeral(Exception):
|
|
91
|
+
"""
|
|
92
|
+
This exception is raised if the version of tor doesn't support ephemeral onion services
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TorTooOldStealth(Exception):
|
|
97
|
+
"""
|
|
98
|
+
This exception is raised if the version of tor doesn't support stealth onion services
|
|
99
|
+
"""
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class BundledTorTimeout(Exception):
|
|
103
|
+
"""
|
|
104
|
+
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
105
|
+
but Tor doesn't finish connecting promptly.
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
class BundledTorCanceled(Exception):
|
|
110
|
+
"""
|
|
111
|
+
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
112
|
+
and the user cancels connecting to Tor
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class BundledTorBroken(Exception):
|
|
117
|
+
"""
|
|
118
|
+
This exception is raised if onionshare is set to use the bundled Tor binary,
|
|
119
|
+
but the process seems to fail to run.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class PortNotAvailable(Exception):
|
|
124
|
+
"""
|
|
125
|
+
There are no available ports for OnionShare to use, which really shouldn't ever happen
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class Onion(object):
|
|
130
|
+
"""
|
|
131
|
+
Onion is an abstraction layer for connecting to the Tor control port and
|
|
132
|
+
creating onion services. OnionShare supports creating onion services by
|
|
133
|
+
connecting to the Tor controller and using ADD_ONION, DEL_ONION.
|
|
134
|
+
|
|
135
|
+
stealth: Should the onion service be stealth?
|
|
136
|
+
|
|
137
|
+
settings: A Settings object. If it's not passed in, load from disk.
|
|
138
|
+
|
|
139
|
+
bundled_connection_func: If the tor connection type is bundled, optionally
|
|
140
|
+
call this function and pass in a status string while connecting to tor. This
|
|
141
|
+
is necessary for status updates to reach the GUI.
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
def __init__(self, common, use_tmp_dir=False, get_tor_paths=None):
|
|
145
|
+
self.common = common
|
|
146
|
+
self.common.log("Onion", "__init__")
|
|
147
|
+
|
|
148
|
+
self.use_tmp_dir = use_tmp_dir
|
|
149
|
+
|
|
150
|
+
# Set the path of the tor binary, for bundled tor
|
|
151
|
+
if not get_tor_paths:
|
|
152
|
+
get_tor_paths = self.common.get_tor_paths
|
|
153
|
+
(
|
|
154
|
+
self.tor_path,
|
|
155
|
+
self.tor_geo_ip_file_path,
|
|
156
|
+
self.tor_geo_ipv6_file_path,
|
|
157
|
+
self.obfs4proxy_file_path,
|
|
158
|
+
) = get_tor_paths()
|
|
159
|
+
|
|
160
|
+
# The tor process
|
|
161
|
+
self.tor_proc = None
|
|
162
|
+
|
|
163
|
+
# The Tor controller
|
|
164
|
+
self.c = None
|
|
165
|
+
|
|
166
|
+
# Start out not connected to Tor
|
|
167
|
+
self.connected_to_tor = False
|
|
168
|
+
|
|
169
|
+
# Assigned later if we are using stealth mode
|
|
170
|
+
self.auth_string = None
|
|
171
|
+
|
|
172
|
+
# Keep track of onions where it's important to gracefully close to prevent truncated downloads
|
|
173
|
+
self.graceful_close_onions = []
|
|
174
|
+
|
|
175
|
+
def connect(
|
|
176
|
+
self,
|
|
177
|
+
custom_settings=None,
|
|
178
|
+
config=None,
|
|
179
|
+
tor_status_update_func=None,
|
|
180
|
+
connect_timeout=120,
|
|
181
|
+
local_only=False,
|
|
182
|
+
):
|
|
183
|
+
if local_only:
|
|
184
|
+
self.common.log(
|
|
185
|
+
"Onion", "connect", "--local-only, so skip trying to connect"
|
|
186
|
+
)
|
|
187
|
+
return
|
|
188
|
+
|
|
189
|
+
self.common.log("Onion", "connect")
|
|
190
|
+
|
|
191
|
+
# Either use settings that are passed in, or use them from common
|
|
192
|
+
if custom_settings:
|
|
193
|
+
self.settings = custom_settings
|
|
194
|
+
elif config:
|
|
195
|
+
self.common.load_settings(config)
|
|
196
|
+
self.settings = self.common.settings
|
|
197
|
+
else:
|
|
198
|
+
self.common.load_settings()
|
|
199
|
+
self.settings = self.common.settings
|
|
200
|
+
|
|
201
|
+
# The Tor controller
|
|
202
|
+
self.c = None
|
|
203
|
+
|
|
204
|
+
if self.settings.get("connection_type") == "bundled":
|
|
205
|
+
# Create a torrc for this session
|
|
206
|
+
if self.use_tmp_dir:
|
|
207
|
+
self.tor_data_directory = tempfile.TemporaryDirectory(
|
|
208
|
+
dir=self.common.build_tmp_dir()
|
|
209
|
+
)
|
|
210
|
+
self.tor_data_directory_name = self.tor_data_directory.name
|
|
211
|
+
else:
|
|
212
|
+
self.tor_data_directory_name = self.common.build_tor_dir()
|
|
213
|
+
self.common.log(
|
|
214
|
+
"Onion",
|
|
215
|
+
"connect",
|
|
216
|
+
f"tor_data_directory_name={self.tor_data_directory_name}",
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
# Create the torrc
|
|
220
|
+
with open(self.common.get_resource_path("torrc_template")) as f:
|
|
221
|
+
torrc_template = f.read()
|
|
222
|
+
self.tor_cookie_auth_file = os.path.join(
|
|
223
|
+
self.tor_data_directory_name, "cookie"
|
|
224
|
+
)
|
|
225
|
+
try:
|
|
226
|
+
self.tor_socks_port = self.common.get_available_port(1000, 65535)
|
|
227
|
+
except:
|
|
228
|
+
print("OnionShare port not available")
|
|
229
|
+
raise PortNotAvailable()
|
|
230
|
+
self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
|
|
231
|
+
|
|
232
|
+
# If there is an existing OnionShare tor process, kill it
|
|
233
|
+
for proc in psutil.process_iter(["pid", "name", "username"]):
|
|
234
|
+
try:
|
|
235
|
+
cmdline = proc.cmdline()
|
|
236
|
+
if (
|
|
237
|
+
cmdline[0] == self.tor_path
|
|
238
|
+
and cmdline[1] == "-f"
|
|
239
|
+
and cmdline[2] == self.tor_torrc
|
|
240
|
+
):
|
|
241
|
+
self.common.log(
|
|
242
|
+
"Onion",
|
|
243
|
+
"connect",
|
|
244
|
+
"found a stale tor process, killing it",
|
|
245
|
+
)
|
|
246
|
+
proc.terminate()
|
|
247
|
+
proc.wait()
|
|
248
|
+
break
|
|
249
|
+
except:
|
|
250
|
+
pass
|
|
251
|
+
|
|
252
|
+
if self.common.platform == "Windows" or self.common.platform == "Darwin":
|
|
253
|
+
# Windows doesn't support unix sockets, so it must use a network port.
|
|
254
|
+
# macOS can't use unix sockets either because socket filenames are limited to
|
|
255
|
+
# 100 chars, and the macOS sandbox forces us to put the socket file in a place
|
|
256
|
+
# with a really long path.
|
|
257
|
+
torrc_template += "ControlPort {{control_port}}\n"
|
|
258
|
+
try:
|
|
259
|
+
self.tor_control_port = self.common.get_available_port(1000, 65535)
|
|
260
|
+
except:
|
|
261
|
+
print("OnionShare port not available")
|
|
262
|
+
raise PortNotAvailable()
|
|
263
|
+
self.tor_control_socket = None
|
|
264
|
+
else:
|
|
265
|
+
# Linux and BSD can use unix sockets
|
|
266
|
+
torrc_template += "ControlSocket {{control_socket}}\n"
|
|
267
|
+
self.tor_control_port = None
|
|
268
|
+
self.tor_control_socket = os.path.join(
|
|
269
|
+
self.tor_data_directory_name, "control_socket"
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
torrc_template = torrc_template.replace(
|
|
273
|
+
"{{data_directory}}", self.tor_data_directory_name
|
|
274
|
+
)
|
|
275
|
+
torrc_template = torrc_template.replace(
|
|
276
|
+
"{{control_port}}", str(self.tor_control_port)
|
|
277
|
+
)
|
|
278
|
+
torrc_template = torrc_template.replace(
|
|
279
|
+
"{{control_socket}}", str(self.tor_control_socket)
|
|
280
|
+
)
|
|
281
|
+
torrc_template = torrc_template.replace(
|
|
282
|
+
"{{cookie_auth_file}}", self.tor_cookie_auth_file
|
|
283
|
+
)
|
|
284
|
+
torrc_template = torrc_template.replace(
|
|
285
|
+
"{{geo_ip_file}}", self.tor_geo_ip_file_path
|
|
286
|
+
)
|
|
287
|
+
torrc_template = torrc_template.replace(
|
|
288
|
+
"{{geo_ipv6_file}}", self.tor_geo_ipv6_file_path
|
|
289
|
+
)
|
|
290
|
+
torrc_template = torrc_template.replace(
|
|
291
|
+
"{{socks_port}}", str(self.tor_socks_port)
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
with open(self.tor_torrc, "w") as f:
|
|
295
|
+
f.write(torrc_template)
|
|
296
|
+
|
|
297
|
+
# Bridge support
|
|
298
|
+
if self.settings.get("tor_bridges_use_obfs4"):
|
|
299
|
+
f.write(
|
|
300
|
+
f"ClientTransportPlugin obfs4 exec {self.obfs4proxy_file_path}\n"
|
|
301
|
+
)
|
|
302
|
+
with open(
|
|
303
|
+
self.common.get_resource_path("torrc_template-obfs4")
|
|
304
|
+
) as o:
|
|
305
|
+
for line in o:
|
|
306
|
+
f.write(line)
|
|
307
|
+
elif self.settings.get("tor_bridges_use_meek_lite_azure"):
|
|
308
|
+
f.write(
|
|
309
|
+
f"ClientTransportPlugin meek_lite exec {self.obfs4proxy_file_path}\n"
|
|
310
|
+
)
|
|
311
|
+
with open(
|
|
312
|
+
self.common.get_resource_path("torrc_template-meek_lite_azure")
|
|
313
|
+
) as o:
|
|
314
|
+
for line in o:
|
|
315
|
+
f.write(line)
|
|
316
|
+
|
|
317
|
+
if self.settings.get("tor_bridges_use_custom_bridges"):
|
|
318
|
+
if "obfs4" in self.settings.get("tor_bridges_use_custom_bridges"):
|
|
319
|
+
f.write(
|
|
320
|
+
f"ClientTransportPlugin obfs4 exec {self.obfs4proxy_file_path}\n"
|
|
321
|
+
)
|
|
322
|
+
elif "meek_lite" in self.settings.get(
|
|
323
|
+
"tor_bridges_use_custom_bridges"
|
|
324
|
+
):
|
|
325
|
+
f.write(
|
|
326
|
+
f"ClientTransportPlugin meek_lite exec {self.obfs4proxy_file_path}\n"
|
|
327
|
+
)
|
|
328
|
+
f.write(self.settings.get("tor_bridges_use_custom_bridges"))
|
|
329
|
+
f.write("\nUseBridges 1")
|
|
330
|
+
|
|
331
|
+
# Execute a tor subprocess
|
|
332
|
+
start_ts = time.time()
|
|
333
|
+
if self.common.platform == "Windows":
|
|
334
|
+
# In Windows, hide console window when opening tor.exe subprocess
|
|
335
|
+
startupinfo = subprocess.STARTUPINFO()
|
|
336
|
+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
|
337
|
+
self.tor_proc = subprocess.Popen(
|
|
338
|
+
[self.tor_path, "-f", self.tor_torrc],
|
|
339
|
+
stdout=subprocess.PIPE,
|
|
340
|
+
stderr=subprocess.PIPE,
|
|
341
|
+
startupinfo=startupinfo,
|
|
342
|
+
)
|
|
343
|
+
else:
|
|
344
|
+
self.tor_proc = subprocess.Popen(
|
|
345
|
+
[self.tor_path, "-f", self.tor_torrc],
|
|
346
|
+
stdout=subprocess.PIPE,
|
|
347
|
+
stderr=subprocess.PIPE,
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
# Wait for the tor controller to start
|
|
351
|
+
time.sleep(2)
|
|
352
|
+
|
|
353
|
+
# Connect to the controller
|
|
354
|
+
try:
|
|
355
|
+
if (
|
|
356
|
+
self.common.platform == "Windows"
|
|
357
|
+
or self.common.platform == "Darwin"
|
|
358
|
+
):
|
|
359
|
+
self.c = Controller.from_port(port=self.tor_control_port)
|
|
360
|
+
self.c.authenticate()
|
|
361
|
+
else:
|
|
362
|
+
self.c = Controller.from_socket_file(path=self.tor_control_socket)
|
|
363
|
+
self.c.authenticate()
|
|
364
|
+
except Exception as e:
|
|
365
|
+
print("OnionShare could not connect to Tor:\n{}".format(e.args[0]))
|
|
366
|
+
raise BundledTorBroken(e.args[0])
|
|
367
|
+
|
|
368
|
+
while True:
|
|
369
|
+
try:
|
|
370
|
+
res = self.c.get_info("status/bootstrap-phase")
|
|
371
|
+
except SocketClosed:
|
|
372
|
+
raise BundledTorCanceled()
|
|
373
|
+
|
|
374
|
+
res_parts = shlex.split(res)
|
|
375
|
+
progress = res_parts[2].split("=")[1]
|
|
376
|
+
summary = res_parts[4].split("=")[1]
|
|
377
|
+
|
|
378
|
+
# "\033[K" clears the rest of the line
|
|
379
|
+
print(
|
|
380
|
+
f"\rConnecting to the Tor network: {progress}% - {summary}\033[K",
|
|
381
|
+
end="",
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
if callable(tor_status_update_func):
|
|
385
|
+
if not tor_status_update_func(progress, summary):
|
|
386
|
+
# If the dialog was canceled, stop connecting to Tor
|
|
387
|
+
self.common.log(
|
|
388
|
+
"Onion",
|
|
389
|
+
"connect",
|
|
390
|
+
"tor_status_update_func returned false, canceling connecting to Tor",
|
|
391
|
+
)
|
|
392
|
+
print()
|
|
393
|
+
return False
|
|
394
|
+
|
|
395
|
+
if summary == "Done":
|
|
396
|
+
print("")
|
|
397
|
+
break
|
|
398
|
+
time.sleep(0.2)
|
|
399
|
+
|
|
400
|
+
# If using bridges, it might take a bit longer to connect to Tor
|
|
401
|
+
if (
|
|
402
|
+
self.settings.get("tor_bridges_use_custom_bridges")
|
|
403
|
+
or self.settings.get("tor_bridges_use_obfs4")
|
|
404
|
+
or self.settings.get("tor_bridges_use_meek_lite_azure")
|
|
405
|
+
):
|
|
406
|
+
# Only override timeout if a custom timeout has not been passed in
|
|
407
|
+
if connect_timeout == 120:
|
|
408
|
+
connect_timeout = 150
|
|
409
|
+
if time.time() - start_ts > connect_timeout:
|
|
410
|
+
print("")
|
|
411
|
+
try:
|
|
412
|
+
self.tor_proc.terminate()
|
|
413
|
+
print(
|
|
414
|
+
"Taking too long to connect to Tor. Maybe you aren't connected to the Internet, or have an inaccurate system clock?"
|
|
415
|
+
)
|
|
416
|
+
raise BundledTorTimeout()
|
|
417
|
+
except FileNotFoundError:
|
|
418
|
+
pass
|
|
419
|
+
|
|
420
|
+
elif self.settings.get("connection_type") == "automatic":
|
|
421
|
+
# Automatically try to guess the right way to connect to Tor Browser
|
|
422
|
+
automatic_error = "Could not connect to the Tor controller. Is Tor Browser (available from torproject.org) running in the background?"
|
|
423
|
+
|
|
424
|
+
# Try connecting to control port
|
|
425
|
+
found_tor = False
|
|
426
|
+
|
|
427
|
+
# If the TOR_CONTROL_PORT environment variable is set, use that
|
|
428
|
+
env_port = os.environ.get("TOR_CONTROL_PORT")
|
|
429
|
+
if env_port:
|
|
430
|
+
try:
|
|
431
|
+
self.c = Controller.from_port(port=int(env_port))
|
|
432
|
+
found_tor = True
|
|
433
|
+
except:
|
|
434
|
+
pass
|
|
435
|
+
|
|
436
|
+
else:
|
|
437
|
+
# Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor
|
|
438
|
+
try:
|
|
439
|
+
ports = [9151, 9153, 9051]
|
|
440
|
+
for port in ports:
|
|
441
|
+
self.c = Controller.from_port(port=port)
|
|
442
|
+
found_tor = True
|
|
443
|
+
except:
|
|
444
|
+
pass
|
|
445
|
+
|
|
446
|
+
# If this still didn't work, try guessing the default socket file path
|
|
447
|
+
socket_file_path = ""
|
|
448
|
+
if not found_tor:
|
|
449
|
+
try:
|
|
450
|
+
if self.common.platform == "Darwin":
|
|
451
|
+
socket_file_path = os.path.expanduser(
|
|
452
|
+
"~/Library/Application Support/TorBrowser-Data/Tor/control.socket"
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
self.c = Controller.from_socket_file(path=socket_file_path)
|
|
456
|
+
found_tor = True
|
|
457
|
+
except:
|
|
458
|
+
pass
|
|
459
|
+
|
|
460
|
+
# If connecting to default control ports failed, so let's try
|
|
461
|
+
# guessing the socket file name next
|
|
462
|
+
if not found_tor:
|
|
463
|
+
try:
|
|
464
|
+
if self.common.platform == "Linux" or self.common.platform == "BSD":
|
|
465
|
+
socket_file_path = (
|
|
466
|
+
f"/run/user/{os.geteuid()}/Tor/control.socket"
|
|
467
|
+
)
|
|
468
|
+
elif self.common.platform == "Darwin":
|
|
469
|
+
socket_file_path = (
|
|
470
|
+
f"/run/user/{os.geteuid()}/Tor/control.socket"
|
|
471
|
+
)
|
|
472
|
+
elif self.common.platform == "Windows":
|
|
473
|
+
# Windows doesn't support unix sockets
|
|
474
|
+
print(automatic_error)
|
|
475
|
+
raise TorErrorAutomatic()
|
|
476
|
+
|
|
477
|
+
self.c = Controller.from_socket_file(path=socket_file_path)
|
|
478
|
+
|
|
479
|
+
except:
|
|
480
|
+
print(automatic_error)
|
|
481
|
+
raise TorErrorAutomatic()
|
|
482
|
+
|
|
483
|
+
# Try authenticating
|
|
484
|
+
try:
|
|
485
|
+
self.c.authenticate()
|
|
486
|
+
except:
|
|
487
|
+
print(automatic_error)
|
|
488
|
+
raise TorErrorAutomatic()
|
|
489
|
+
|
|
490
|
+
else:
|
|
491
|
+
# Use specific settings to connect to tor
|
|
492
|
+
invalid_settings_error = "Can't connect to Tor controller because your settings don't make sense."
|
|
493
|
+
|
|
494
|
+
# Try connecting
|
|
495
|
+
try:
|
|
496
|
+
if self.settings.get("connection_type") == "control_port":
|
|
497
|
+
self.c = Controller.from_port(
|
|
498
|
+
address=self.settings.get("control_port_address"),
|
|
499
|
+
port=self.settings.get("control_port_port"),
|
|
500
|
+
)
|
|
501
|
+
elif self.settings.get("connection_type") == "socket_file":
|
|
502
|
+
self.c = Controller.from_socket_file(
|
|
503
|
+
path=self.settings.get("socket_file_path")
|
|
504
|
+
)
|
|
505
|
+
else:
|
|
506
|
+
print(invalid_settings_error)
|
|
507
|
+
raise TorErrorInvalidSetting()
|
|
508
|
+
|
|
509
|
+
except:
|
|
510
|
+
if self.settings.get("connection_type") == "control_port":
|
|
511
|
+
print(
|
|
512
|
+
"Can't connect to the Tor controller at {}:{}.".format(
|
|
513
|
+
self.settings.get("control_port_address"),
|
|
514
|
+
self.settings.get("control_port_port"),
|
|
515
|
+
)
|
|
516
|
+
)
|
|
517
|
+
raise TorErrorSocketPort(
|
|
518
|
+
self.settings.get("control_port_address"),
|
|
519
|
+
self.settings.get("control_port_port"),
|
|
520
|
+
)
|
|
521
|
+
else:
|
|
522
|
+
print(
|
|
523
|
+
"Can't connect to the Tor controller using socket file {}.".format(
|
|
524
|
+
self.settings.get("socket_file_path")
|
|
525
|
+
)
|
|
526
|
+
)
|
|
527
|
+
raise TorErrorSocketFile(self.settings.get("socket_file_path"))
|
|
528
|
+
|
|
529
|
+
# Try authenticating
|
|
530
|
+
try:
|
|
531
|
+
if self.settings.get("auth_type") == "no_auth":
|
|
532
|
+
self.c.authenticate()
|
|
533
|
+
elif self.settings.get("auth_type") == "password":
|
|
534
|
+
self.c.authenticate(self.settings.get("auth_password"))
|
|
535
|
+
else:
|
|
536
|
+
print(invalid_settings_error)
|
|
537
|
+
raise TorErrorInvalidSetting()
|
|
538
|
+
|
|
539
|
+
except MissingPassword:
|
|
540
|
+
print(
|
|
541
|
+
"Connected to Tor controller, but it requires a password to authenticate."
|
|
542
|
+
)
|
|
543
|
+
raise TorErrorMissingPassword()
|
|
544
|
+
except UnreadableCookieFile:
|
|
545
|
+
print(
|
|
546
|
+
"Connected to the Tor controller, but password may be wrong, or your user is not permitted to read the cookie file."
|
|
547
|
+
)
|
|
548
|
+
raise TorErrorUnreadableCookieFile()
|
|
549
|
+
except AuthenticationFailure:
|
|
550
|
+
print(
|
|
551
|
+
"Connected to {}:{}, but can't authenticate. Maybe this isn't a Tor controller?".format(
|
|
552
|
+
self.settings.get("control_port_address"),
|
|
553
|
+
self.settings.get("control_port_port"),
|
|
554
|
+
)
|
|
555
|
+
)
|
|
556
|
+
raise TorErrorAuthError(
|
|
557
|
+
self.settings.get("control_port_address"),
|
|
558
|
+
self.settings.get("control_port_port"),
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
# If we made it this far, we should be connected to Tor
|
|
562
|
+
self.connected_to_tor = True
|
|
563
|
+
|
|
564
|
+
# Get the tor version
|
|
565
|
+
self.tor_version = self.c.get_version().version_str
|
|
566
|
+
self.common.log("Onion", "connect", f"Connected to tor {self.tor_version}")
|
|
567
|
+
|
|
568
|
+
# Do the versions of stem and tor that I'm using support ephemeral onion services?
|
|
569
|
+
list_ephemeral_hidden_services = getattr(
|
|
570
|
+
self.c, "list_ephemeral_hidden_services", None
|
|
571
|
+
)
|
|
572
|
+
self.supports_ephemeral = (
|
|
573
|
+
callable(list_ephemeral_hidden_services) and self.tor_version >= "0.2.7.1"
|
|
574
|
+
)
|
|
575
|
+
|
|
576
|
+
# Do the versions of stem and tor that I'm using support stealth onion services?
|
|
577
|
+
try:
|
|
578
|
+
res = self.c.create_ephemeral_hidden_service(
|
|
579
|
+
{1: 1},
|
|
580
|
+
basic_auth={"onionshare": None},
|
|
581
|
+
await_publication=False,
|
|
582
|
+
key_type="NEW",
|
|
583
|
+
key_content="RSA1024",
|
|
584
|
+
)
|
|
585
|
+
tmp_service_id = res.service_id
|
|
586
|
+
self.c.remove_ephemeral_hidden_service(tmp_service_id)
|
|
587
|
+
self.supports_stealth = True
|
|
588
|
+
except:
|
|
589
|
+
# ephemeral stealth onion services are not supported
|
|
590
|
+
self.supports_stealth = False
|
|
591
|
+
|
|
592
|
+
# Does this version of Tor support next-gen ('v3') onions?
|
|
593
|
+
# Note, this is the version of Tor where this bug was fixed:
|
|
594
|
+
# https://trac.torproject.org/projects/tor/ticket/28619
|
|
595
|
+
self.supports_v3_onions = self.tor_version >= Version("0.3.5.7")
|
|
596
|
+
|
|
597
|
+
def is_authenticated(self):
|
|
598
|
+
"""
|
|
599
|
+
Returns True if the Tor connection is still working, or False otherwise.
|
|
600
|
+
"""
|
|
601
|
+
if self.c is not None:
|
|
602
|
+
return self.c.is_authenticated()
|
|
603
|
+
else:
|
|
604
|
+
return False
|
|
605
|
+
|
|
606
|
+
def start_onion_service(self, mode, mode_settings, port, await_publication):
|
|
607
|
+
"""
|
|
608
|
+
Start a onion service on port 80, pointing to the given port, and
|
|
609
|
+
return the onion hostname.
|
|
610
|
+
"""
|
|
611
|
+
self.common.log("Onion", "start_onion_service", f"port={port}")
|
|
612
|
+
|
|
613
|
+
if not self.supports_ephemeral:
|
|
614
|
+
print(
|
|
615
|
+
"Your version of Tor is too old, ephemeral onion services are not supported"
|
|
616
|
+
)
|
|
617
|
+
raise TorTooOldEphemeral()
|
|
618
|
+
if mode_settings.get("general", "client_auth") and not self.supports_stealth:
|
|
619
|
+
print(
|
|
620
|
+
"Your version of Tor is too old, stealth onion services are not supported"
|
|
621
|
+
)
|
|
622
|
+
raise TorTooOldStealth()
|
|
623
|
+
|
|
624
|
+
auth_cookie = None
|
|
625
|
+
if mode_settings.get("general", "client_auth"):
|
|
626
|
+
if mode_settings.get("onion", "hidservauth_string"):
|
|
627
|
+
auth_cookie = mode_settings.get("onion", "hidservauth_string").split()[
|
|
628
|
+
2
|
|
629
|
+
]
|
|
630
|
+
if auth_cookie:
|
|
631
|
+
basic_auth = {"onionshare": auth_cookie}
|
|
632
|
+
else:
|
|
633
|
+
# If we had neither a scheduled auth cookie or a persistent hidservauth string,
|
|
634
|
+
# set the cookie to 'None', which means Tor will create one for us
|
|
635
|
+
basic_auth = {"onionshare": None}
|
|
636
|
+
else:
|
|
637
|
+
# Not using client auth at all
|
|
638
|
+
basic_auth = None
|
|
639
|
+
|
|
640
|
+
if mode_settings.get("onion", "private_key"):
|
|
641
|
+
key_content = mode_settings.get("onion", "private_key")
|
|
642
|
+
if self.is_v2_key(key_content):
|
|
643
|
+
key_type = "RSA1024"
|
|
644
|
+
else:
|
|
645
|
+
# Assume it was a v3 key. Stem will throw an error if it's something illegible
|
|
646
|
+
key_type = "ED25519-V3"
|
|
647
|
+
else:
|
|
648
|
+
key_type = "NEW"
|
|
649
|
+
# Work out if we can support v3 onion services, which are preferred
|
|
650
|
+
if self.supports_v3_onions and not mode_settings.get("general", "legacy"):
|
|
651
|
+
key_content = "ED25519-V3"
|
|
652
|
+
else:
|
|
653
|
+
# fall back to v2 onion services
|
|
654
|
+
key_content = "RSA1024"
|
|
655
|
+
|
|
656
|
+
# v3 onions don't yet support basic auth. Our ticket:
|
|
657
|
+
# https://github.com/micahflee/onionshare/issues/697
|
|
658
|
+
if (
|
|
659
|
+
key_type == "NEW"
|
|
660
|
+
and key_content == "ED25519-V3"
|
|
661
|
+
and not mode_settings.get("general", "legacy")
|
|
662
|
+
):
|
|
663
|
+
basic_auth = None
|
|
664
|
+
|
|
665
|
+
debug_message = f"key_type={key_type}"
|
|
666
|
+
if key_type == "NEW":
|
|
667
|
+
debug_message += f", key_content={key_content}"
|
|
668
|
+
self.common.log("Onion", "start_onion_service", debug_message)
|
|
669
|
+
try:
|
|
670
|
+
res = self.c.create_ephemeral_hidden_service(
|
|
671
|
+
{80: port},
|
|
672
|
+
await_publication=await_publication,
|
|
673
|
+
basic_auth=basic_auth,
|
|
674
|
+
key_type=key_type,
|
|
675
|
+
key_content=key_content,
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
except ProtocolError as e:
|
|
679
|
+
print("Tor error: {}".format(e.args[0]))
|
|
680
|
+
raise TorErrorProtocolError(e.args[0])
|
|
681
|
+
|
|
682
|
+
onion_host = res.service_id + ".onion"
|
|
683
|
+
|
|
684
|
+
# Gracefully close share mode rendezvous circuits
|
|
685
|
+
if mode == "share":
|
|
686
|
+
self.graceful_close_onions.append(res.service_id)
|
|
687
|
+
|
|
688
|
+
# Save the service_id
|
|
689
|
+
mode_settings.set("general", "service_id", res.service_id)
|
|
690
|
+
|
|
691
|
+
# Save the private key and hidservauth string
|
|
692
|
+
if not mode_settings.get("onion", "private_key"):
|
|
693
|
+
mode_settings.set("onion", "private_key", res.private_key)
|
|
694
|
+
if mode_settings.get("general", "client_auth") and not mode_settings.get(
|
|
695
|
+
"onion", "hidservauth_string"
|
|
696
|
+
):
|
|
697
|
+
auth_cookie = list(res.client_auth.values())[0]
|
|
698
|
+
self.auth_string = f"HidServAuth {onion_host} {auth_cookie}"
|
|
699
|
+
mode_settings.set("onion", "hidservauth_string", self.auth_string)
|
|
700
|
+
|
|
701
|
+
return onion_host
|
|
702
|
+
|
|
703
|
+
def stop_onion_service(self, mode_settings):
|
|
704
|
+
"""
|
|
705
|
+
Stop a specific onion service
|
|
706
|
+
"""
|
|
707
|
+
onion_host = mode_settings.get("general", "service_id")
|
|
708
|
+
if onion_host:
|
|
709
|
+
self.common.log("Onion", "stop_onion_service", f"onion host: {onion_host}")
|
|
710
|
+
try:
|
|
711
|
+
self.c.remove_ephemeral_hidden_service(
|
|
712
|
+
mode_settings.get("general", "service_id")
|
|
713
|
+
)
|
|
714
|
+
except:
|
|
715
|
+
self.common.log(
|
|
716
|
+
"Onion", "stop_onion_service", f"failed to remove {onion_host}"
|
|
717
|
+
)
|
|
718
|
+
|
|
719
|
+
def cleanup(self, stop_tor=True, wait=True):
|
|
720
|
+
"""
|
|
721
|
+
Stop onion services that were created earlier. If there's a tor subprocess running, kill it.
|
|
722
|
+
"""
|
|
723
|
+
self.common.log("Onion", "cleanup")
|
|
724
|
+
|
|
725
|
+
# Cleanup the ephemeral onion services, if we have any
|
|
726
|
+
try:
|
|
727
|
+
onions = self.c.list_ephemeral_hidden_services()
|
|
728
|
+
for service_id in onions:
|
|
729
|
+
onion_host = f"{service_id}.onion"
|
|
730
|
+
try:
|
|
731
|
+
self.common.log(
|
|
732
|
+
"Onion", "cleanup", f"trying to remove onion {onion_host}"
|
|
733
|
+
)
|
|
734
|
+
self.c.remove_ephemeral_hidden_service(service_id)
|
|
735
|
+
except:
|
|
736
|
+
self.common.log(
|
|
737
|
+
"Onion", "cleanup", f"failed to remove onion {onion_host}"
|
|
738
|
+
)
|
|
739
|
+
pass
|
|
740
|
+
except:
|
|
741
|
+
pass
|
|
742
|
+
|
|
743
|
+
if stop_tor:
|
|
744
|
+
# Stop tor process
|
|
745
|
+
if self.tor_proc:
|
|
746
|
+
if wait:
|
|
747
|
+
# Wait for Tor rendezvous circuits to close
|
|
748
|
+
# Catch exceptions to prevent crash on Ctrl-C
|
|
749
|
+
try:
|
|
750
|
+
rendevouz_circuit_ids = []
|
|
751
|
+
for c in self.c.get_circuits():
|
|
752
|
+
if (
|
|
753
|
+
c.purpose == "HS_SERVICE_REND"
|
|
754
|
+
and c.rend_query in self.graceful_close_onions
|
|
755
|
+
):
|
|
756
|
+
rendevouz_circuit_ids.append(c.id)
|
|
757
|
+
|
|
758
|
+
symbols = [c for c in "\\|/-"]
|
|
759
|
+
symbols_i = 0
|
|
760
|
+
|
|
761
|
+
while True:
|
|
762
|
+
num_rend_circuits = 0
|
|
763
|
+
for c in self.c.get_circuits():
|
|
764
|
+
if c.id in rendevouz_circuit_ids:
|
|
765
|
+
num_rend_circuits += 1
|
|
766
|
+
|
|
767
|
+
if num_rend_circuits == 0:
|
|
768
|
+
print(
|
|
769
|
+
"\rTor rendezvous circuits have closed" + " " * 20
|
|
770
|
+
)
|
|
771
|
+
break
|
|
772
|
+
|
|
773
|
+
if num_rend_circuits == 1:
|
|
774
|
+
circuits = "circuit"
|
|
775
|
+
else:
|
|
776
|
+
circuits = "circuits"
|
|
777
|
+
print(
|
|
778
|
+
f"\rWaiting for {num_rend_circuits} Tor rendezvous {circuits} to close {symbols[symbols_i]} ",
|
|
779
|
+
end="",
|
|
780
|
+
)
|
|
781
|
+
symbols_i = (symbols_i + 1) % len(symbols)
|
|
782
|
+
time.sleep(1)
|
|
783
|
+
except:
|
|
784
|
+
pass
|
|
785
|
+
|
|
786
|
+
self.tor_proc.terminate()
|
|
787
|
+
time.sleep(0.2)
|
|
788
|
+
if self.tor_proc.poll() is None:
|
|
789
|
+
self.common.log(
|
|
790
|
+
"Onion",
|
|
791
|
+
"cleanup",
|
|
792
|
+
"Tried to terminate tor process but it's still running",
|
|
793
|
+
)
|
|
794
|
+
try:
|
|
795
|
+
self.tor_proc.kill()
|
|
796
|
+
time.sleep(0.2)
|
|
797
|
+
if self.tor_proc.poll() is None:
|
|
798
|
+
self.common.log(
|
|
799
|
+
"Onion",
|
|
800
|
+
"cleanup",
|
|
801
|
+
"Tried to kill tor process but it's still running",
|
|
802
|
+
)
|
|
803
|
+
except:
|
|
804
|
+
self.common.log(
|
|
805
|
+
"Onion", "cleanup", "Exception while killing tor process"
|
|
806
|
+
)
|
|
807
|
+
self.tor_proc = None
|
|
808
|
+
|
|
809
|
+
# Reset other Onion settings
|
|
810
|
+
self.connected_to_tor = False
|
|
811
|
+
|
|
812
|
+
try:
|
|
813
|
+
# Delete the temporary tor data directory
|
|
814
|
+
if self.use_tmp_dir:
|
|
815
|
+
self.tor_data_directory.cleanup()
|
|
816
|
+
except:
|
|
817
|
+
pass
|
|
818
|
+
|
|
819
|
+
def get_tor_socks_port(self):
|
|
820
|
+
"""
|
|
821
|
+
Returns a (address, port) tuple for the Tor SOCKS port
|
|
822
|
+
"""
|
|
823
|
+
self.common.log("Onion", "get_tor_socks_port")
|
|
824
|
+
|
|
825
|
+
if self.settings.get("connection_type") == "bundled":
|
|
826
|
+
return ("127.0.0.1", self.tor_socks_port)
|
|
827
|
+
elif self.settings.get("connection_type") == "automatic":
|
|
828
|
+
return ("127.0.0.1", 9150)
|
|
829
|
+
else:
|
|
830
|
+
return (self.settings.get("socks_address"), self.settings.get("socks_port"))
|
|
831
|
+
|
|
832
|
+
def is_v2_key(self, key):
|
|
833
|
+
"""
|
|
834
|
+
Helper function for determining if a key is RSA1024 (v2) or not.
|
|
835
|
+
"""
|
|
836
|
+
try:
|
|
837
|
+
# Import the key
|
|
838
|
+
key = RSA.importKey(base64.b64decode(key))
|
|
839
|
+
# Is this a v2 Onion key? (1024 bits) If so, we should keep using it.
|
|
840
|
+
if key.n.bit_length() == 1024:
|
|
841
|
+
return True
|
|
842
|
+
else:
|
|
843
|
+
return False
|
|
844
|
+
except:
|
|
845
|
+
return False
|