2014-06-06 21:20:57 -04:00
|
|
|
import onionshare, webapp
|
2014-06-19 21:16:22 -04:00
|
|
|
import os, sys, subprocess
|
2014-05-29 21:05:30 -04:00
|
|
|
|
2014-06-19 16:19:46 -04:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from PyQt4.QtWebKit import *
|
|
|
|
|
|
|
|
qtapp = QApplication(sys.argv)
|
2014-06-11 20:53:19 -04:00
|
|
|
|
2014-06-19 21:16:22 -04:00
|
|
|
class WebAppThread(QThread):
|
|
|
|
def __init__(self, webapp_port, onionshare_port, filename, onion_host):
|
|
|
|
QThread.__init__(self)
|
|
|
|
self.webapp_port = webapp_port
|
|
|
|
|
|
|
|
global qtapp
|
|
|
|
webapp.onionshare = onionshare
|
|
|
|
webapp.onionshare_port = onionshare_port
|
|
|
|
webapp.filename = filename
|
|
|
|
webapp.onion_host = onion_host
|
|
|
|
webapp.qtapp = qtapp
|
|
|
|
webapp.clipboard = qtapp.clipboard()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
webapp.app.run(port=self.webapp_port)
|
|
|
|
|
2014-06-19 16:51:40 -04:00
|
|
|
def alert(msg, icon=QMessageBox.NoIcon):
|
|
|
|
dialog = QMessageBox()
|
2014-06-19 20:28:55 -04:00
|
|
|
dialog.setWindowTitle("OnionShare")
|
2014-06-19 16:51:40 -04:00
|
|
|
dialog.setText(msg)
|
|
|
|
dialog.setIcon(icon)
|
|
|
|
dialog.exec_()
|
2014-05-30 20:46:24 -04:00
|
|
|
|
|
|
|
def select_file(strings):
|
|
|
|
# get filename, either from argument or file chooser dialog
|
|
|
|
if len(sys.argv) == 2:
|
|
|
|
filename = sys.argv[1]
|
|
|
|
else:
|
2014-06-19 16:36:49 -04:00
|
|
|
args = {}
|
2014-06-10 18:07:53 -04:00
|
|
|
if onionshare.get_platform() == 'Tails':
|
2014-06-19 16:36:49 -04:00
|
|
|
args['directory'] = '/home/amnesia'
|
|
|
|
|
|
|
|
filename = QFileDialog.getOpenFileName(caption=strings['choose_file'], options=QFileDialog.ReadOnly, **args)
|
|
|
|
if not filename:
|
2014-05-30 20:46:24 -04:00
|
|
|
return False, False
|
|
|
|
|
2014-06-19 16:36:49 -04:00
|
|
|
filename = str(filename)
|
|
|
|
|
2014-05-30 20:46:24 -04:00
|
|
|
# validate filename
|
|
|
|
if not os.path.isfile(filename):
|
2014-06-19 16:51:40 -04:00
|
|
|
alert(strings["not_a_file"].format(filename), QMessageBox.Warning)
|
2014-05-30 20:46:24 -04:00
|
|
|
return False, False
|
|
|
|
|
|
|
|
filename = os.path.abspath(filename)
|
|
|
|
basename = os.path.basename(filename)
|
|
|
|
return filename, basename
|
|
|
|
|
2014-06-11 21:33:52 -04:00
|
|
|
def launch_window(webapp_port, onionshare_port, basename):
|
2014-06-19 16:19:46 -04:00
|
|
|
def shutdown():
|
2014-06-06 21:20:57 -04:00
|
|
|
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
|
|
|
|
2014-06-19 16:19:46 -04:00
|
|
|
global qtapp
|
|
|
|
qtapp.connect(qtapp, SIGNAL("aboutToQuit()"), shutdown)
|
|
|
|
web = QWebView()
|
2014-06-19 20:28:55 -04:00
|
|
|
web.setWindowTitle("{0} | OnionShare".format(basename))
|
|
|
|
web.resize(550, 350)
|
|
|
|
web.setMinimumSize(550, 300)
|
|
|
|
web.setMaximumSize(550, 900)
|
2014-06-19 16:19:46 -04:00
|
|
|
web.load(QUrl("http://127.0.0.1:{0}".format(webapp_port)))
|
|
|
|
web.show()
|
|
|
|
sys.exit(qtapp.exec_())
|
2014-06-06 21:20:57 -04:00
|
|
|
|
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:
|
2014-06-19 16:51:40 -04:00
|
|
|
alert(e.args[0], QMessageBox.Warning)
|
2014-05-30 21:03:53 -04:00
|
|
|
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()
|
2014-06-19 21:16:22 -04:00
|
|
|
webapp_thread = WebAppThread(webapp_port, onionshare_port, filename, onion_host)
|
|
|
|
webapp_thread.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
|
2014-06-11 21:33:52 -04:00
|
|
|
launch_window(webapp_port, onionshare_port, basename)
|
2014-05-29 21:05:30 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|