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. Click Start to download the latest version from https://www.torproject.org.", ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run'])
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?", [])
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.", [])
42 # save the current version to the file
43 open(self.paths['file']['version'], 'w').write(self.current_tbb_version)
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'])
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'])
58 # discover the architecture and language
59 def discover_arch_lang(self):
60 # figure out the architecture
61 self.architecture = subprocess.Popen(['arch'], stdout=subprocess.PIPE, stderr=None).stdout.read().strip('\n')
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:
72 # if language isn't available, default to english
73 if self.language not in available_languages:
74 self.language = 'en-US'
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'
84 'download': tbb_data+'/download',
85 'tbb': tbb_data+'/tbb/'+self.architecture,
86 'gpg': tbb_data+'/gpgtmp'
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'
96 'tarball': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename,
97 'tarball_sig': 'https://www.torproject.org/dist/torbrowser/linux/'+tarball_filename+'.asc'
100 'tarball': tarball_filename,
101 'tarball_sig': tarball_filename+'.asc'
105 # create directories that don't exist
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'])
112 # there are different GUIs that might appear, this sets which one we want
113 def set_gui(self, gui, message, tasks):
115 self.gui_message = message
116 self.gui_tasks = tasks
118 # build the application's UI
122 # allow buttons to have icons
124 settings = gtk.settings_get_default()
125 settings.props.gtk_button_images = True
130 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
131 self.window.set_title("Tor Browser")
132 self.window.set_position(gtk.WIN_POS_CENTER)
133 self.window.set_border_width(10)
134 self.window.connect("delete_event", self.delete_event)
135 self.window.connect("destroy", self.destroy)
137 self.box = gtk.VBox(False, 20)
138 self.window.add(self.box)
140 if self.gui == 'error':
142 self.label1 = gtk.Label( self.gui_message )
143 self.label1.set_line_wrap(True)
144 self.box.pack_start(self.label1, True, True, 0)
147 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.")
148 self.label2.set_line_wrap(True)
149 self.box.pack_start(self.label2, True, True, 0)
153 exit_image = gtk.Image()
154 exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
155 self.exit_button = gtk.Button("Exit")
156 self.exit_button.set_image(exit_image)
157 self.exit_button.connect("clicked", self.destroy, None)
158 self.box.add(self.exit_button)
159 self.exit_button.show()
161 elif self.gui == 'task':
163 self.label = gtk.Label( self.gui_message )
164 self.label.set_line_wrap(True)
165 self.box.pack_start(self.label, True, True, 0)
169 self.progressbar = gtk.ProgressBar(adjustment=None)
170 self.progressbar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
171 self.progressbar.set_pulse_step(0.01)
172 self.box.pack_start(self.progressbar, True, True, 0)
175 self.button_box = gtk.HButtonBox()
176 self.button_box.set_layout(gtk.BUTTONBOX_SPREAD)
177 self.box.pack_start(self.button_box, True, True, 0)
178 self.button_box.show()
181 start_image = gtk.Image()
182 start_image.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_BUTTON)
183 self.start_button = gtk.Button("Start")
184 self.start_button.set_image(start_image)
185 self.start_button.connect("clicked", self.start, None)
186 self.button_box.add(self.start_button)
187 self.start_button.show()
190 exit_image = gtk.Image()
191 exit_image.set_from_stock(gtk.STOCK_CANCEL, gtk.ICON_SIZE_BUTTON)
192 self.exit_button = gtk.Button("Exit")
193 self.exit_button.set_image(exit_image)
194 self.exit_button.connect("clicked", self.destroy, None)
195 self.button_box.add(self.exit_button)
196 self.exit_button.show()
201 # start button clicked, begin tasks
202 def start(self, widget, data=None):
203 # disable the start button
204 self.start_button.set_sensitive(False)
206 # start running tasks
210 # run the next task in the task list
212 if self.gui_task_i >= len(self.gui_tasks):
216 task = self.gui_tasks[self.gui_task_i]
218 # get ready for the next task
221 if task == 'download_tarball':
222 print 'Downloading '+self.paths['url']['tarball']
223 self.download('tarball', self.paths['url']['tarball'], self.paths['file']['tarball'])
225 elif task == 'download_tarball_sig':
226 print 'Downloading '+self.paths['url']['tarball_sig']
227 self.download('signature', self.paths['url']['tarball_sig'], self.paths['file']['tarball_sig'])
229 elif task == 'verify':
230 print 'Verifying signature'
233 elif task == 'extract':
234 print 'Extracting '+self.paths['filename']['tarball']
238 print 'Running '+self.paths['file']['start']
241 elif task == 'start_over':
242 print 'Starting download over again'
246 def download(self, name, url, path):
247 # initialize the progress bar
248 self.progressbar.set_fraction(0)
249 self.progressbar.set_text('Downloading '+name)
250 self.progressbar.show()
253 self.dl_response = urllib2.urlopen(url)
254 self.dl_total_size = self.dl_response.info().getheader('Content-Length').strip()
255 self.dl_total_size = int(self.dl_total_size)
256 self.dl_bytes_so_far = 0
258 # set a timer to download more chunks
259 self.timer = gobject.timeout_add(1, self.download_chunk, name)
261 # open a file to write to
262 self.file_download = open(path, 'w')
264 def download_chunk(self, name):
265 # download 10kb a time
266 chunk = self.dl_response.read(10240)
267 self.dl_bytes_so_far += len(chunk)
268 self.file_download.write(chunk)
271 self.file_download.close()
276 percent = float(self.dl_bytes_so_far) / self.dl_total_size
277 self.progressbar.set_fraction(percent)
278 percent = round(percent*100, 2)
279 self.progressbar.set_text("Downloaded %d%% of %s" % (percent, name))
281 sys.stdout.write("Downloaded %d of %d bytes (%0.2f%%)\r" % (self.dl_bytes_so_far, self.dl_total_size, percent))
283 if self.dl_bytes_so_far >= self.dl_total_size:
284 sys.stdout.write('\n')
289 # initialize the progress bar
290 self.progressbar.set_fraction(0)
291 self.progressbar.set_text('Verifying Signature')
292 self.progressbar.show()
294 p = subprocess.Popen([self.paths['file']['verify'], self.paths['dir']['gpg'], self.paths['file']['tarball_sig']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
295 self.pulse_until_process_exits(p)
297 output = p.stdout.read()
299 if 'Good signature' in output:
302 self.progressbar.hide()
303 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.")
304 self.gui_tasks = ['start_over']
306 self.start_button.set_sensitive(True)
309 # initialize the progress bar
310 self.progressbar.set_fraction(0)
311 self.progressbar.set_text('Installing')
312 self.progressbar.show()
314 p = subprocess.Popen(['tar', '-xf', self.paths['file']['tarball'], '-C', self.paths['dir']['tbb']], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
315 self.pulse_until_process_exits(p)
319 def run(self, run_next_task = True):
320 subprocess.Popen([self.paths['file']['start']])
324 # make the progress bar pulse until process p (a Popen object) finishes
325 def pulse_until_process_exits(self, p):
326 while p.poll() == None:
328 self.progressbar.pulse()
330 while gtk.events_pending():
331 gtk.main_iteration(False)
333 # start over and download TBB again
334 def start_over(self):
335 self.label.set_text("Downloading Tor Browser Bundle over again.")
336 self.gui_tasks = ['download_tarball', 'download_tarball_sig', 'verify', 'extract', 'run']
341 def delete_event(self, widget, event, data=None):
343 def destroy(self, widget, data=None):
345 gobject.source_remove(self.timer)
350 if __name__ == "__main__":
351 current_tbl_version = '0.1'
352 current_tbb_version = '2.3.25-2'
354 print 'Tor Browser Launcher'
355 print 'version %s' % (current_tbl_version)
356 print 'https://github.com/micahflee/torbrowser-launcher'
358 app = TorBrowserLauncher(current_tbb_version)