2014-06-06 21:20:57 -04:00
|
|
|
import onionshare, webapp
|
2014-06-10 18:07:53 -04:00
|
|
|
import threading, gtk, gobject, webkit, os, sys, subprocess
|
2014-05-29 21:05:30 -04:00
|
|
|
|
2014-05-30 20:46:24 -04:00
|
|
|
def alert(msg, type=gtk.MESSAGE_INFO):
|
|
|
|
dialog = gtk.MessageDialog(
|
|
|
|
parent=None,
|
|
|
|
flags=gtk.DIALOG_MODAL,
|
|
|
|
type=type,
|
|
|
|
buttons=gtk.BUTTONS_OK,
|
|
|
|
message_format=msg)
|
|
|
|
response = dialog.run()
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
def select_file(strings):
|
|
|
|
# get filename, either from argument or file chooser dialog
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
else:
|
|
|
|
canceled = False
|
|
|
|
chooser = gtk.FileChooserDialog(
|
|
|
|
title="Choose a file to share",
|
|
|
|
action=gtk.FILE_CHOOSER_ACTION_OPEN,
|
|
|
|
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
|
2014-06-10 18:07:53 -04:00
|
|
|
if onionshare.get_platform() == 'Tails':
|
|
|
|
chooser.set_current_folder('/home/amnesia/')
|
2014-05-30 20:46:24 -04:00
|
|
|
response = chooser.run()
|
|
|
|
if response == gtk.RESPONSE_OK:
|
|
|
|
filename = chooser.get_filename()
|
|
|
|
elif response == gtk.RESPONSE_CANCEL:
|
|
|
|
canceled = True
|
|
|
|
chooser.destroy()
|
|
|
|
|
|
|
|
if canceled:
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
# validate filename
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
alert(strings["not_a_file"].format(filename), gtk.MESSAGE_ERROR)
|
|
|
|
return False, False
|
|
|
|
|
|
|
|
filename = os.path.abspath(filename)
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
return filename, basename
|
|
|
|
|
2014-06-06 21:20:57 -04:00
|
|
|
def start_webapp(webapp_port, onionshare_port, filename, onion_host):
|
|
|
|
webapp.onionshare = onionshare
|
|
|
|
webapp.onionshare_port = onionshare_port
|
|
|
|
webapp.filename = filename
|
|
|
|
webapp.onion_host = onion_host
|
|
|
|
webapp.app.run(port=webapp_port)
|
|
|
|
|
|
|
|
def launch_window(webapp_port, onionshare_port):
|
|
|
|
def on_destroy(widget, data=None):
|
|
|
|
onionshare.tails_close_port(onionshare_port)
|
2014-06-10 18:10:40 -04:00
|
|
|
onionshare.tails_close_port(webapp_port)
|
2014-06-06 21:20:57 -04:00
|
|
|
gtk.main_quit()
|
|
|
|
|
|
|
|
window = gtk.Window()
|
|
|
|
window.set_title('OnionShare')
|
2014-06-08 13:15:39 -04:00
|
|
|
window.resize(520, 400)
|
2014-06-06 21:20:57 -04:00
|
|
|
window.set_resizable(False)
|
|
|
|
window.connect('destroy', on_destroy)
|
|
|
|
|
|
|
|
box = gtk.VBox(homogeneous=False, spacing=0)
|
|
|
|
window.add(box)
|
|
|
|
|
|
|
|
browser = webkit.WebView()
|
|
|
|
box.pack_start(browser, expand=True, fill=True, padding=0)
|
|
|
|
|
|
|
|
window.show_all()
|
|
|
|
|
|
|
|
# wait half a second for server to start
|
|
|
|
gobject.timeout_add(500, browser.open, 'http://127.0.0.1:{0}/'.format(webapp_port))
|
|
|
|
|
|
|
|
gtk.main()
|
|
|
|
|
2014-05-29 21:05:30 -04:00
|
|
|
def main():
|
2014-06-06 21:20:57 -04:00
|
|
|
onionshare.strings = onionshare.load_strings()
|
2014-05-30 20:46:24 -04:00
|
|
|
|
2014-06-10 18:07:53 -04:00
|
|
|
# check for root in Tails
|
|
|
|
if onionshare.get_platform() == 'Tails' and not onionshare.is_root():
|
|
|
|
subprocess.call(['/usr/bin/gksudo']+sys.argv)
|
|
|
|
return
|
|
|
|
|
2014-05-30 21:03:53 -04:00
|
|
|
# try starting hidden service
|
2014-06-06 21:20:57 -04:00
|
|
|
onionshare_port = onionshare.choose_port()
|
2014-05-30 21:03:53 -04:00
|
|
|
try:
|
2014-06-06 21:20:57 -04:00
|
|
|
onion_host = onionshare.start_hidden_service(onionshare_port)
|
2014-05-30 21:03:53 -04:00
|
|
|
except onionshare.NoTor as e:
|
|
|
|
alert(e.args[0], gtk.MESSAGE_ERROR)
|
|
|
|
return
|
2014-06-06 21:20:57 -04:00
|
|
|
onionshare.tails_open_port(onionshare_port)
|
2014-05-30 21:03:53 -04:00
|
|
|
|
2014-05-30 20:46:24 -04:00
|
|
|
# select file to share
|
2014-06-06 21:20:57 -04:00
|
|
|
filename, basename = select_file(onionshare.strings)
|
2014-05-29 22:31:28 -04:00
|
|
|
if not filename:
|
2014-05-29 21:05:30 -04:00
|
|
|
return
|
|
|
|
|
2014-06-06 21:20:57 -04:00
|
|
|
# start the gui web server
|
|
|
|
webapp_port = onionshare.choose_port()
|
|
|
|
t = threading.Thread(target=start_webapp, kwargs={
|
|
|
|
'webapp_port': webapp_port,
|
|
|
|
'onionshare_port': onionshare_port,
|
|
|
|
'filename': filename,
|
|
|
|
'onion_host': onion_host
|
|
|
|
})
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
2014-06-10 18:10:40 -04:00
|
|
|
onionshare.tails_open_port(webapp_port)
|
2014-06-06 21:20:57 -04:00
|
|
|
|
|
|
|
# launch the window
|
|
|
|
launch_window(webapp_port, onionshare_port)
|
2014-05-29 21:05:30 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|