3 import os, sys, subprocess, locale, urllib2, gobject, time
9 class TorBrowserLauncher:
10 def __init__(self, current_tbb_version):
12 self.current_tbb_version = current_tbb_version
13 self.discover_arch_lang()
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()
25 if installed_tbb_version == current_tbb_version:
26 # current version is tbb is installed, launch it
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.",
33 'download_tarball_sig',
38 # for some reason the installed tbb is newer than the current version?
39 self.set_gui('error', "Something is wrong. The version of Tor Browser Bundle you have installed is newer than the current version?", [])
42 # if tbb is installed but the version file doesn't exist, something is wrong
43 self.set_gui('error', "Something is wrong. You have the Tor Browser Bundle installed, but the version file is missing.", [])
47 # save the current version to the file
48 open(self.paths['file']['version'], 'w').write(self.current_tbb_version)
50 # are the tarball and sig already downloaded?
51 if os.path.isfile(self.paths['file']['tarball']) and os.path.isfile(self.paths['file']['tarball_sig']):
52 # start the gui with verify
53 self.set_gui('task', "Installing Tor Browser.",
60 self.set_gui('task', "Downloading and installing Tor Browser.",
62 'download_tarball_sig',
71 # discover the architecture and language
72 def discover_arch_lang(self):
73 # figure out the architecture
74 self.architecture = subprocess.Popen(['uname', '-m'], stdout=subprocess.PIPE, stderr=None).stdout.read().strip('\n')
76 # figure out the language
77 available_languages = ['en-US', 'ar', 'de', 'es-ES', 'fa', 'fr', 'it', 'ko', 'nl', 'pl', 'pt-PT', 'ru', 'vi', 'zh-CN']
78 default_locale = locale.getdefaultlocale()[0]
79 if default_locale == None:
80 self.language = 'en-US'
82 self.language = default_locale.replace('_', '-')
83 if self.language not in available_languages:
84 self.language = self.language.split('-')[0]
85 if self.language not in available_languages:
86 for l in available_languages:
87 if l[0:2] == self.language:
89 # if language isn't available, default to english
90 if self.language not in available_languages:
91 self.language = 'en-US'
93 # build all relevant paths
94 def build_paths(self):
95 tbb_data = os.getenv('HOME')+'/.torbrowser'
96 tarball_filename = 'tor-browser-gnu-linux-'+self.architecture+'-'+self.current_tbb_version+'-dev-'+self.language+'.tar.gz'
101 'download': tbb_data+'/download',
102 'tbb': tbb_data+'/tbb/'+self.architecture,
103 'gpg': tbb_data+'/gpgtmp'
106 'version': tbb_data+'/version',
107 'start': tbb_data+'/tbb/'+self.architecture+'/tor-browser_'+self.language+'/start-tor-browser',
108 'tarball': tbb_data+'/download/'+tarball_filename,
109 'tarball_sig': tbb_data+'/download/'+tarball_filename+'.asc',
110 'verify': '/usr/share/torbrowser-launcher/verify.sh'
113 #'tarball': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename,
114 #'tarball_sig': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename+'.asc'
115 'tarball': 'http://localhost/'+tarball_filename,
116 'tarball_sig': 'http://localhost/'+tarball_filename+'.asc'
119 'tarball': tarball_filename,
120 'tarball_sig': tarball_filename+'.asc'
124 # create directories that don't exist
126 if os.path.exists(self.paths['dir']['download']) == False:
127 os.makedirs(self.paths['dir']['download'])
128 if os.path.exists(self.paths['dir']['tbb']) == False:
129 os.makedirs(self.paths['dir']['tbb'])
131 # there are different GUIs that might appear, this sets which one we want
132 def set_gui(self, gui, message, tasks, autostart=True):
134 self.gui_message = message
135 self.gui_tasks = tasks
136 self.gui_autostart = autostart
138 # build the application's UI
142 # allow buttons to have icons
144 settings = gtk.settings_get_default()
145 settings.props.gtk_button_images = True
150 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
151 self.window.set_title("Tor Browser")
152 self.window.set_position(gtk.WIN_POS_CENTER)
153 self.window.set_border_width(10)
154 self.window.connect("delete_event", self.delete_event)
155 self.window.connect("destroy", self.destroy)
157 self.box = gtk.VBox(False, 20)
158 self.window.add(self.box)
160 if self.gui == 'error':
162 self.label1 = gtk.Label( self.gui_message )
163 self.label1.set_line_wrap(True)
164 self.box.pack_start(self.label1, True, True, 0)
167 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.")
168 self.label2.set_line_wrap(True)
169 self.box.pack_start(self.label2, True, True, 0)
173 exit_image = gtk.Image()
174 exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
175 self.exit_button = gtk.Button("Exit")
176 self.exit_button.set_image(exit_image)
177 self.exit_button.connect("clicked", self.destroy, None)
178 self.box.add(self.exit_button)
179 self.exit_button.show()
181 elif self.gui == 'task':
183 self.label = gtk.Label( self.gui_message )
184 self.label.set_line_wrap(True)
185 self.box.pack_start(self.label, True, True, 0)
189 self.progressbar = gtk.ProgressBar(adjustment=None)
190 self.progressbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
191 self.progressbar.set_pulse_step(0.01)
192 self.box.pack_start(self.progressbar, True, True, 0)
195 self.button_box = gtk.HButtonBox()
196 self.button_box.set_layout(gtk.BUTTONBOX_SPREAD)
197 self.box.pack_start(self.button_box, True, True, 0)
198 self.button_box.show()
201 start_image = gtk.Image()
202 start_image.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_BUTTON)
203 self.start_button = gtk.Button("Start")
204 self.start_button.set_image(start_image)
205 self.start_button.connect("clicked", self.start, None)
206 self.button_box.add(self.start_button)
207 if not self.gui_autostart:
208 self.start_button.show()
211 exit_image = gtk.Image()
212 exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
213 self.exit_button = gtk.Button("Exit")
214 self.exit_button.set_image(exit_image)
215 self.exit_button.connect("clicked", self.destroy, None)
216 self.button_box.add(self.exit_button)
217 self.exit_button.show()
222 if self.gui_autostart:
225 # start button clicked, begin tasks
226 def start(self, widget, data=None):
227 # disable the start button
228 self.start_button.set_sensitive(False)
230 # start running tasks
234 # run the next task in the task list
236 if self.gui_task_i >= len(self.gui_tasks):
240 task = self.gui_tasks[self.gui_task_i]
242 # get ready for the next task
245 if task == 'download_tarball':
246 print 'Downloading '+self.paths['url']['tarball']
247 self.download('tarball', self.paths['url']['tarball'], self.paths['file']['tarball'])
249 elif task == 'download_tarball_sig':
250 print 'Downloading '+self.paths['url']['tarball_sig']
251 self.download('signature', self.paths['url']['tarball_sig'], self.paths['file']['tarball_sig'])
253 elif task == 'verify':
254 print 'Verifying signature'
257 elif task == 'extract':
258 print 'Extracting '+self.paths['filename']['tarball']
262 print 'Running '+self.paths['file']['start']
265 elif task == 'start_over':
266 print 'Starting download over again'
270 def download(self, name, url, path):
271 # initialize the progress bar
272 self.progressbar.set_fraction(0)
273 self.progressbar.set_text('Downloading '+name)
274 self.progressbar.show()
278 self.dl_response = urllib2.urlopen(url)
279 self.dl_total_size = self.dl_response.info().getheader('Content-Length').strip()
280 self.dl_total_size = int(self.dl_total_size)
281 self.dl_bytes_so_far = 0
283 # set a timer to download more chunks
284 self.timer = gobject.timeout_add(1, self.download_chunk, name)
286 # open a file to write to
287 self.file_download = open(path, 'w')
289 def download_chunk(self, name):
290 # download 10kb a time
291 chunk = self.dl_response.read(10240)
292 self.dl_bytes_so_far += len(chunk)
293 self.file_download.write(chunk)
296 self.file_download.close()
301 percent = float(self.dl_bytes_so_far) / self.dl_total_size
302 self.progressbar.set_fraction(percent)
303 percent = round(percent*100, 2)
304 self.progressbar.set_text("Downloaded %d%% of %s" % (percent, name))
307 sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % (self.dl_bytes_so_far, self.dl_total_size, percent))
309 if self.dl_bytes_so_far >= self.dl_total_size:
310 sys.stdout.write('\n')
315 # initialize the progress bar
316 self.progressbar.set_fraction(0)
317 self.progressbar.set_text('Verifying Signature')
318 self.progressbar.show()
320 p = subprocess.Popen([self.paths['file']['verify'], self.paths['dir']['gpg'], self.paths['file']['tarball_sig']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
321 self.pulse_until_process_exits(p)
323 output = p.stdout.read()
325 if 'Good signature' in output:
328 self.progressbar.hide()
329 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.")
330 self.gui_tasks = ['start_over']
332 self.start_button.show()
333 self.start_button.set_sensitive(True)
336 # initialize the progress bar
337 self.progressbar.set_fraction(0)
338 self.progressbar.set_text('Installing')
339 self.progressbar.show()
341 p = subprocess.Popen(['tar', '-xf', self.paths['file']['tarball'], '-C', self.paths['dir']['tbb']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
342 self.pulse_until_process_exits(p)
346 def run(self, run_next_task = True):
347 subprocess.Popen([self.paths['file']['start']])
351 # make the progress bar pulse until process p (a Popen object) finishes
352 def pulse_until_process_exits(self, p):
353 while p.poll() == None:
355 self.progressbar.pulse()
358 # start over and download TBB again
359 def start_over(self):
360 self.label.set_text("Downloading Tor Browser Bundle over again.")
361 self.gui_tasks = ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run']
366 def refresh_gtk(self):
367 while gtk.events_pending():
368 gtk.main_iteration(False)
371 def delete_event(self, widget, event, data=None):
373 def destroy(self, widget, data=None):
375 gobject.source_remove(self.timer)
380 if __name__ == "__main__":
381 current_tbl_version = '0.1'
382 current_tbb_version = '2.3.25-2'
384 print 'Tor Browser Launcher'
385 print 'version %s' % (current_tbl_version)
386 print 'https://github.com/micahflee/torbrowser-launcher'
388 app = TorBrowserLauncher(current_tbb_version)