]> git.lizzy.rs Git - torbrowser-launcher.git/blob - torbrowser-launcher
Tor Browser is out of date, the launcher version is not in question (https://github...
[torbrowser-launcher.git] / torbrowser-launcher
1 #!/usr/bin/env python
2
3 import os, sys, subprocess, locale, urllib2, gobject, time
4
5 import pygtk
6 pygtk.require('2.0')
7 import gtk
8
9 class TorBrowserLauncher:
10   def __init__(self, current_tbb_version):
11     # initialize the app
12     self.current_tbb_version = current_tbb_version
13     self.discover_arch_lang();
14     self.build_paths();
15     self.mkdirs();
16
17     launch_gui = True
18
19     # is TBB already installed?
20     if os.path.isfile(self.paths['file']['start']) and os.access(self.paths['file']['start'], os.X_OK):
21       # does the version file exist?
22       if os.path.isfile(self.paths['file']['version']):
23         installed_tbb_version = open(self.paths['file']['version']).read().strip()
24
25         if installed_tbb_version == current_tbb_version:
26           # current version is tbb is installed, launch it
27           self.run(False)
28           launch_gui = False
29         elif installed_tbb_version < self.current_tbb_version:
30           # there is a tbb upgrade available
31           self.set_gui('task', "Your Tor Browser is out of date. Click Start to download the latest version from https://www.torproject.org.", ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run'])
32         else:
33           # for some reason the installed tbb is newer than the current version?
34           self.set_gui('error', "Something is wrong. The version of Tor Browser Bundle you have installed is newer than the current version?", [])
35
36       else:
37         # if tbb is installed but the version file doesn't exist, something is wrong
38         self.set_gui('error', "Something is wrong. You have the Tor Browser Bundle installed, but the version file is missing.", [])
39
40     # not installed
41     else:
42       # save the current version to the file
43       open(self.paths['file']['version'], 'w').write(self.current_tbb_version)
44
45       # are the tarball and sig already downloaded?
46       if os.path.isfile(self.paths['file']['tarball']) and os.path.isfile(self.paths['file']['tarball_sig']):
47         # start the gui with verify
48         self.set_gui('task', "You already have Tor Browser Bundle downloaded, but it isn't installed yet.", ['verify', 'extract', 'run'])
49
50       # first run
51       else:
52         self.set_gui('task', "The first time you run the Tor Browser Launcher you need to download the Tor Browser Bundle. Click Start to download it now from https://www.torproject.org/.", ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run'])
53
54     if launch_gui:
55       self.build_ui()
56       gtk.main()
57   
58   # discover the architecture and language
59   def discover_arch_lang(self):
60     # figure out the architecture
61     self.architecture = subprocess.check_output(['arch']).strip('\n')
62
63     # figure out the language
64     available_languages = ['en-US', 'ar', 'de', 'es-ES', 'fa', 'fr', 'it', 'ko', 'nl', 'pl', 'pt-PT', 'ru', 'vi', 'zh-CN']
65     self.language = locale.getdefaultlocale()[0].replace('_', '-')
66     if self.language not in available_languages:
67       self.language = self.language.split('-')[0]
68       if self.language not in available_languages:
69         for l in available_languages:
70           if l[0:2] == self.language:
71             self.language = l
72     # if language isn't available, default to english
73     if self.language not in available_languages:
74       self.language = 'en-US'
75
76   # build all relevant paths
77   def build_paths(self):
78     tbb_data = os.getenv('HOME')+'/.torbrowser'
79     tarball_filename = 'tor-browser-gnu-linux-'+self.architecture+'-'+self.current_tbb_version+'-dev-'+self.language+'.tar.gz'
80
81     self.paths = {
82       'dir': {
83         'data': tbb_data,
84         'download': tbb_data+'/download',
85         'tbb': tbb_data+'/tbb/'+self.architecture,
86         'gpg': tbb_data+'/gpgtmp'
87       },
88       'file': {
89         'version': tbb_data+'/version',
90         'start': tbb_data+'/tbb/'+self.architecture+'/tor-browser_'+self.language+'/start-tor-browser',
91         'tarball': tbb_data+'/download/'+tarball_filename,
92         'tarball_sig': tbb_data+'/download/'+tarball_filename+'.asc',
93         'verify': '/usr/share/torbrowser-launcher/verify.sh'
94       },
95       'url': {
96         'tarball': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename,
97         'tarball_sig': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename+'.asc'
98       },
99       'filename': {
100         'tarball': tarball_filename,
101         'tarball_sig': tarball_filename+'.asc'
102       }
103     }
104
105   # create directories that don't exist
106   def mkdirs(self):
107     if os.path.exists(self.paths['dir']['download']) == False:
108       os.makedirs(self.paths['dir']['download'])
109     if os.path.exists(self.paths['dir']['tbb']) == False:
110       os.makedirs(self.paths['dir']['tbb'])
111
112   # there are different GUIs that might appear, this sets which one we want
113   def set_gui(self, gui, message, tasks):
114     self.gui = gui
115     self.gui_message = message
116     self.gui_tasks = tasks
117
118   # build the application's UI
119   def build_ui(self):
120     self.timer = False
121
122     # allow buttons to have icons
123     settings = gtk.settings_get_default()
124     settings.props.gtk_button_images = True
125
126     # set up the window
127     self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
128     self.window.set_title("Tor Browser Launcher")
129     self.window.set_position(gtk.WIN_POS_CENTER)
130     self.window.set_border_width(10)
131     self.window.connect("delete_event", self.delete_event)
132     self.window.connect("destroy", self.destroy)
133
134     self.box = gtk.VBox(False, 20)
135     self.window.add(self.box)
136
137     if self.gui == 'error':
138       # labels
139       self.label1 = gtk.Label( self.gui_message ); 
140       self.label1.set_line_wrap(True)
141       self.box.pack_start(self.label1, True, True, 0)
142       self.label1.show()
143
144       self.label2 = gtk.Label("You can fix the problem by deleting:\n"+self.paths['dir']['data']+"\n\nHowever, you will lose all your bookmarks and other Tor Browser preferences."); 
145       self.label2.set_line_wrap(True)
146       self.box.pack_start(self.label2, True, True, 0)
147       self.label2.show()
148
149       # exit button
150       exit_image = gtk.Image()
151       exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
152       self.exit_button = gtk.Button("Exit")
153       self.exit_button.set_image(exit_image)
154       self.exit_button.connect("clicked", self.destroy, None)
155       self.box.add(self.exit_button)
156       self.exit_button.show()
157
158     elif self.gui == 'task':
159       # label
160       self.label = gtk.Label( self.gui_message ); 
161       self.label.set_line_wrap(True)
162       self.box.pack_start(self.label, True, True, 0)
163       self.label.show()
164       
165       # progress bar
166       self.progressbar = gtk.ProgressBar(adjustment=None)
167       self.progressbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
168       self.progressbar.set_pulse_step(0.01)
169       self.box.pack_start(self.progressbar, True, True, 0)
170
171       # button box
172       self.button_box = gtk.HButtonBox()
173       self.button_box.set_layout(gtk.BUTTONBOX_SPREAD)
174       self.box.pack_start(self.button_box, True, True, 0)
175       self.button_box.show()
176
177       # start button
178       start_image = gtk.Image()
179       start_image.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_BUTTON)
180       self.start_button = gtk.Button("Start")
181       self.start_button.set_image(start_image)
182       self.start_button.connect("clicked", self.start, None)
183       self.button_box.add(self.start_button)
184       self.start_button.show()
185
186       # exit button
187       exit_image = gtk.Image()
188       exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
189       self.exit_button = gtk.Button("Exit")
190       self.exit_button.set_image(exit_image)
191       self.exit_button.connect("clicked", self.destroy, None)
192       self.button_box.add(self.exit_button)
193       self.exit_button.show()
194
195     self.box.show()
196     self.window.show();
197
198   # start button clicked, begin tasks
199   def start(self, widget, data=None):
200     # disable the start button
201     self.start_button.set_sensitive(False)
202
203     # start running tasks
204     self.gui_task_i = 0
205     self.run_task()
206     
207   # run the next task in the task list
208   def run_task(self):
209     if self.gui_task_i >= len(self.gui_tasks):
210       self.destroy(False)
211       return
212
213     task = self.gui_tasks[self.gui_task_i]
214     
215     # get ready for the next task
216     self.gui_task_i += 1
217
218     if task == 'download_tarball':
219       print 'Downloading '+self.paths['url']['tarball']
220       self.download('tarball', self.paths['url']['tarball'], self.paths['file']['tarball'])
221
222     elif task == 'download_tarball_sig':
223       print 'Downloading '+self.paths['url']['tarball_sig']
224       self.download('signature', self.paths['url']['tarball_sig'], self.paths['file']['tarball_sig'])
225
226     elif task == 'verify':
227       print 'Verifying signature'
228       self.verify()
229
230     elif task == 'extract':
231       print 'Extracting '+self.paths['filename']['tarball']
232       self.extract()
233
234     elif task == 'run':
235       print 'Running '+self.paths['file']['start']
236       self.run()
237     
238     elif task == 'start_over':
239       print 'Starting download over again'
240       self.start_over()
241
242
243   def download(self, name, url, path):
244     # initialize the progress bar
245     self.progressbar.set_fraction(0) 
246     self.progressbar.set_text('Downloading '+name)
247     self.progressbar.show()
248
249     # start the download
250     self.dl_response = urllib2.urlopen(url);
251     self.dl_total_size = self.dl_response.info().getheader('Content-Length').strip()
252     self.dl_total_size = int(self.dl_total_size)
253     self.dl_bytes_so_far = 0
254
255     # set a timer to download more chunks
256     self.timer = gobject.timeout_add(1, self.download_chunk, name)
257
258     # open a file to write to
259     self.file_download = open(path, 'w')
260
261   def download_chunk(self, name):
262     # download 10kb a time
263     chunk = self.dl_response.read(10240)
264     self.dl_bytes_so_far += len(chunk)
265     self.file_download.write(chunk)
266
267     if not chunk:
268       self.file_download.close()
269       # next task!
270       self.run_task()
271       return False
272
273     percent = float(self.dl_bytes_so_far) / self.dl_total_size
274     self.progressbar.set_fraction(percent)
275     percent = round(percent*100, 2)
276     self.progressbar.set_text("Downloaded %d%% of %s" % (percent, name))
277     
278     sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % (self.dl_bytes_so_far, self.dl_total_size, percent))
279
280     if self.dl_bytes_so_far >= self.dl_total_size:
281       sys.stdout.write('\n')
282
283     return True
284
285   def verify(self):
286     # initialize the progress bar
287     self.progressbar.set_fraction(0) 
288     self.progressbar.set_text('Verifying Signature')
289     self.progressbar.show()
290
291     p = subprocess.Popen([self.paths['file']['verify'], self.paths['dir']['gpg'], self.paths['file']['tarball_sig']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
292     self.pulse_until_process_exits(p)
293
294     output = p.stdout.read()
295     
296     if 'Good signature' in output:
297       self.run_task();
298     else:
299       self.progressbar.hide()
300       self.label.set_text("SIGNATURE VERIFICATION FAILED!\n\nYou might be under attack, or there might just be a networking problem. Click Start try the download again.")
301       self.gui_tasks = ['start_over']
302       self.gui_task_i = 0
303       self.start_button.set_sensitive(True)
304
305   def extract(self):
306     # initialize the progress bar
307     self.progressbar.set_fraction(0) 
308     self.progressbar.set_text('Installing')
309     self.progressbar.show()
310
311     p = subprocess.Popen(['tar', '-xf', self.paths['file']['tarball'], '-C', self.paths['dir']['tbb']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
312     self.pulse_until_process_exits(p)
313
314     self.run_task();
315
316   def run(self, run_next_task = True):
317     subprocess.Popen([self.paths['file']['start']])
318     if run_next_task:
319       self.run_task();
320
321   # make the progress bar pulse until process p (a Popen object) finishes
322   def pulse_until_process_exits(self, p):
323     while p.poll() == None:
324       time.sleep(0.01)
325       self.progressbar.pulse()
326       # redraw gtk
327       while gtk.events_pending():
328          gtk.main_iteration(False)
329
330   # start over and download TBB again
331   def start_over(self):
332     self.label.set_text("Downloading Tor Browser Bundle over again.")
333     self.gui_tasks = ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run']
334     self.gui_task_i = 0
335     self.start(None)
336   
337   # exit
338   def delete_event(self, widget, event, data=None):
339     return False
340   def destroy(self, widget, data=None):
341     if self.timer:
342       gobject.source_remove(self.timer)
343     self.timer = False
344
345     gtk.main_quit()
346
347 if __name__ == "__main__":
348   print 'Tor Browser Launcher'
349   print 'https://github.com/micahflee/torbrowser-launcher'
350
351   current_tbb_version = '2.3.25-2'
352   app = TorBrowserLauncher(current_tbb_version)
353