You must connect to a socket file instead of a port for Tor Browser 6.5a6. Make automatic settings fallback to socket file if the port doesn't work (only for Linux so far, have not tested in OS X, and is not supported in Windows)

This commit is contained in:
Micah Lee 2016-12-29 13:36:29 -08:00
parent a0abab3653
commit 50835b67f8
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73

View File

@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from stem.control import Controller from stem.control import Controller
from stem import SocketError from stem import SocketError
from stem.connection import MissingPassword, UnreadableCookieFile from stem.connection import MissingPassword, UnreadableCookieFile
import os, sys, tempfile, shutil, urllib import os, sys, tempfile, shutil, urllib, platform
from . import socks from . import socks
from . import helpers, strings from . import helpers, strings
@ -109,28 +109,46 @@ class Onion(object):
if self.settings.get('connection_type') == 'automatic': if self.settings.get('connection_type') == 'automatic':
# Automatically try to guess the right way to connect to Tor Browser # Automatically try to guess the right way to connect to Tor Browser
# Try connecting # Try connecting to control port
try: found_tor = False
# If the TOR_CONTROL_PORT environment variable is set, use that
env_port = os.environ.get('TOR_CONTROL_PORT')
if env_port:
self.c = Controller.from_port(port=int(env_port))
else: # If the TOR_CONTROL_PORT environment variable is set, use that
# Otherwise, try default ports for Tor Browser, Tor Messenger, and system tor env_port = os.environ.get('TOR_CONTROL_PORT')
found_tor = False if env_port:
try: try:
ports = [9151, 9153, 9051] self.c = Controller.from_port(port=int(env_port))
for port in ports: found_tor = True
self.c = Controller.from_port(port=port) except:
found_tor = True pass
except:
pass else:
if not found_tor: # 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
except:
pass
# If connecting to default control ports failed, so let's try
# guessing the socket file name next
if not found_tor:
try:
p = platform.system()
if p == 'Linux':
socket_file_path = '/run/user/{}/Tor/control.socket'.format(os.geteuid())
elif p == 'Darwin':
# TODO: figure out the unix socket path in OS X
socket_file_path = '/run/user/{}/Tor/control.socket'.format(os.geteuid())
elif p == 'Windows':
# Windows doesn't support unix sockets
raise TorErrorAutomatic(strings._('settings_error_automatic')) raise TorErrorAutomatic(strings._('settings_error_automatic'))
except TorErrorAutomatic: self.c = Controller.from_socket_file(path=socket_file_path)
raise TorErrorAutomatic(strings._('settings_error_automatic'))
except:
raise TorErrorAutomatic(strings._('settings_error_automatic'))
# Try authenticating # Try authenticating
try: try: