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
import distutils
import ox
import stem
from stem.control import Controller
import settings
@ -49,7 +50,9 @@ DirReqStatistics 0
tor = get_tor()
if not 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]
self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1,
universal_newlines=True, close_fds=True, start_new_session=True)
@ -185,7 +188,7 @@ def torbrowser_url():
base_url = 'https://dist.torproject.org/torbrowser/'
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
if sys.platform.startswith('linux'):
import platform
@ -236,36 +239,43 @@ def install_tor():
import shutil
# only install if tor can not be found
if get_tor():
print('found existing tor installation')
logger.debug('found existing tor installation')
return
url = torbrowser_url()
target = os.path.normpath(os.path.join(settings.base_dir, '..', 'tor'))
if url:
print('downloading and installing tor')
logger.debug('downloading and installing tor')
if sys.platform.startswith('linux'):
os.makedirs(target)
ox.makedirs(target)
try:
tar_file = os.path.join(target, os.path.basename(url))
update.get(url, tar_file)
tar = tarfile.open(tar_file)
tar.extractall(target)
desktop = os.path.join(target, tar.members[0].path, 'start-tor-browser.desktop')
tar.close()
subprocess.call([desktop, '--register-app'], cwd=target)
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':
os.makedirs(target)
ox.makedirs(target)
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'
app = os.path.join(volume, name)
if os.access('/Applications', os.W_OK):
target = os.path.join('/Applications', name)
else:
os.makedirs(target)
target = os.path.join(target, name)
if not os.path.exists(target):
shutil.copy(app, target)
try:
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:
logger.debug('no way to install TorBrowser on %s so far', sys.platform)