install tor if needed, fixes #11

This commit is contained in:
j 2016-01-15 18:34:07 +05:30
parent 90b788e48b
commit b66e76c01a

View file

@ -4,6 +4,7 @@ import subprocess
from threading import Thread from threading import Thread
import distutils import distutils
import ox
import stem import stem
from stem.control import Controller from stem.control import Controller
import settings import settings
@ -49,7 +50,9 @@ DirReqStatistics 0
tor = get_tor() tor = get_tor()
if not tor: if not tor:
self._status.append('No tor binary found. Please install TorBrowser or tor') self._status.append('No tor binary found. Please install TorBrowser or tor')
else: install_tor()
tor = get_tor()
if tor:
cmd = [tor, '--defaults-torrc', defaults, '-f', torrc] cmd = [tor, '--defaults-torrc', defaults, '-f', torrc]
self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1,
universal_newlines=True, close_fds=True, start_new_session=True) universal_newlines=True, close_fds=True, start_new_session=True)
@ -185,7 +188,7 @@ def torbrowser_url():
base_url = 'https://dist.torproject.org/torbrowser/' base_url = 'https://dist.torproject.org/torbrowser/'
r = re.compile('href="(\d\.\d\.\d/)"') r = re.compile('href="(\d\.\d\.\d/)"')
current = sorted(r.findall(read_url(base_url).decode()))[-1] current = sorted(r.findall(read_url(base_url, timeout=3*24*60*60).decode()))[-1]
url = base_url + current url = base_url + current
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
import platform import platform
@ -236,36 +239,43 @@ def install_tor():
import shutil import shutil
# only install if tor can not be found # only install if tor can not be found
if get_tor(): if get_tor():
print('found existing tor installation') logger.debug('found existing tor installation')
return
url = torbrowser_url() url = torbrowser_url()
target = os.path.normpath(os.path.join(settings.base_dir, '..', 'tor')) target = os.path.normpath(os.path.join(settings.base_dir, '..', 'tor'))
if url: if url:
print('downloading and installing tor') logger.debug('downloading and installing tor')
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
os.makedirs(target) ox.makedirs(target)
tar_file = os.path.join(target, os.path.basename(url)) try:
update.get(url, tar_file) tar_file = os.path.join(target, os.path.basename(url))
tar = tarfile.open(tar_file) update.get(url, tar_file)
tar.extractall(target) tar = tarfile.open(tar_file)
desktop = os.path.join(target, tar.members[0].path, 'start-tor-browser.desktop') tar.extractall(target)
tar.close() desktop = os.path.join(target, tar.members[0].path, 'start-tor-browser.desktop')
subprocess.call([desktop, '--register-app'], cwd=target) tar.close()
os.unlink(tar_file) subprocess.call([desktop, '--register-app'], cwd=os.path.dirname(desktop))
os.unlink(tar_file)
except:
logger.debug('tor installation failed', exc_info=1)
elif sys.platform == 'darwin': elif sys.platform == 'darwin':
os.makedirs(target) ox.makedirs(target)
dmg = os.path.join(target, os.path.basename(url)) dmg = os.path.join(target, os.path.basename(url))
update.get(url, dmg)
subprocess.call(['hdiutil', 'mount', dmg])
volume = '/Volumes/Tor Browser'
name = 'TorBrowser.app' name = 'TorBrowser.app'
app = os.path.join(volume, name)
if os.access('/Applications', os.W_OK): if os.access('/Applications', os.W_OK):
target = os.path.join('/Applications', name) target = os.path.join('/Applications', name)
else: else:
os.makedirs(target)
target = os.path.join(target, name) target = os.path.join(target, name)
if not os.path.exists(target): if not os.path.exists(target):
shutil.copy(app, target) try:
subprocess.call(['hdiutil', 'unmount', volume]) update.get(url, dmg)
r = subprocess.check_output(['hdiutil', 'mount', dmg])
volume = r.decode().strip().split('\t')[-1]
app = os.path.join(volume, name)
shutil.copytree(app, target)
subprocess.call(['hdiutil', 'unmount', volume])
os.unlink(dmg)
except:
logger.debug('tor installation failed', exc_info=1)
else: else:
logger.debug('no way to install TorBrowser on %s so far', sys.platform) logger.debug('no way to install TorBrowser on %s so far', sys.platform)