2014-06-06 21:20:57 -04:00
|
|
|
from flask import Flask, render_template
|
2014-06-20 01:42:53 -04:00
|
|
|
import threading, json, os, time, platform, sys
|
2014-06-06 21:20:57 -04:00
|
|
|
|
|
|
|
onionshare = None
|
|
|
|
onionshare_port = None
|
|
|
|
filename = None
|
|
|
|
onion_host = None
|
2014-06-19 16:19:46 -04:00
|
|
|
qtapp = None
|
|
|
|
clipboard = None
|
2014-06-26 14:31:29 -04:00
|
|
|
stay_open = None
|
2014-06-06 21:20:57 -04:00
|
|
|
|
|
|
|
url = None
|
|
|
|
|
2014-06-26 19:02:59 -04:00
|
|
|
app = Flask(__name__, template_folder='./templates')
|
2014-06-19 16:59:36 -04:00
|
|
|
|
2014-06-26 19:02:59 -04:00
|
|
|
def debug_mode():
|
|
|
|
import logging
|
|
|
|
global app
|
2014-06-19 16:59:36 -04:00
|
|
|
|
2014-06-26 19:02:59 -04:00
|
|
|
if platform.system() == 'Windows':
|
|
|
|
temp_dir = os.environ['Temp'].replace('\\', '/')
|
|
|
|
else:
|
|
|
|
temp_dir = '/tmp/'
|
2014-06-19 16:59:36 -04:00
|
|
|
|
2014-06-26 19:02:59 -04:00
|
|
|
log_handler = logging.FileHandler('{0}/onionshare.web.log'.format(temp_dir))
|
|
|
|
log_handler.setLevel(logging.WARNING)
|
|
|
|
app.logger.addHandler(log_handler)
|
2014-06-06 21:20:57 -04:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
|
|
|
return render_template('index.html')
|
|
|
|
|
2014-06-09 21:07:18 -04:00
|
|
|
@app.route("/init_info")
|
|
|
|
def init_info():
|
2014-06-26 14:31:29 -04:00
|
|
|
global onionshare, filename, stay_open
|
2014-06-09 21:07:18 -04:00
|
|
|
basename = os.path.basename(filename)
|
|
|
|
|
|
|
|
return json.dumps({
|
|
|
|
'strings': onionshare.strings,
|
2014-06-26 14:31:29 -04:00
|
|
|
'basename': basename,
|
|
|
|
'stay_open': stay_open
|
2014-06-09 21:07:18 -04:00
|
|
|
})
|
|
|
|
|
2014-06-06 21:20:57 -04:00
|
|
|
@app.route("/start_onionshare")
|
|
|
|
def start_onionshare():
|
|
|
|
global onionshare, onionshare_port, filename, onion_host, url
|
|
|
|
|
|
|
|
url = 'http://{0}/{1}'.format(onion_host, onionshare.slug)
|
|
|
|
|
|
|
|
filehash, filesize = onionshare.file_crunching(filename)
|
|
|
|
onionshare.set_file_info(filename, filehash, filesize)
|
|
|
|
|
|
|
|
# start onionshare service in new thread
|
|
|
|
t = threading.Thread(target=onionshare.app.run, kwargs={'port': onionshare_port})
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
return json.dumps({
|
|
|
|
'filehash': filehash,
|
|
|
|
'filesize': filesize,
|
|
|
|
'url': url
|
|
|
|
})
|
|
|
|
|
|
|
|
@app.route("/copy_url")
|
|
|
|
def copy_url():
|
2014-06-21 20:29:26 -04:00
|
|
|
if platform.system() == 'Windows':
|
|
|
|
# Qt's QClipboard isn't working in Windows
|
|
|
|
# https://github.com/micahflee/onionshare/issues/46
|
|
|
|
import ctypes
|
|
|
|
GMEM_DDESHARE = 0x2000
|
|
|
|
ctypes.windll.user32.OpenClipboard(None)
|
|
|
|
ctypes.windll.user32.EmptyClipboard()
|
|
|
|
hcd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(url))+1)
|
|
|
|
pch_data = ctypes.windll.kernel32.GlobalLock(hcd)
|
|
|
|
ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pch_data), bytes(url))
|
|
|
|
ctypes.windll.kernel32.GlobalUnlock(hcd)
|
|
|
|
ctypes.windll.user32.SetClipboardData(1, hcd)
|
|
|
|
ctypes.windll.user32.CloseClipboard()
|
|
|
|
else:
|
|
|
|
global clipboard
|
|
|
|
clipboard.setText(url)
|
2014-06-06 21:20:57 -04:00
|
|
|
return ''
|
|
|
|
|
2014-06-26 14:45:18 -04:00
|
|
|
@app.route("/stay_open_true")
|
|
|
|
def stay_open_true():
|
|
|
|
global onionshare
|
|
|
|
onionshare.set_stay_open(True)
|
|
|
|
|
|
|
|
@app.route("/stay_open_false")
|
|
|
|
def stay_open_false():
|
|
|
|
global onionshare
|
|
|
|
onionshare.set_stay_open(False)
|
|
|
|
|
2014-06-11 19:55:28 -04:00
|
|
|
@app.route("/heartbeat")
|
2014-06-06 21:54:08 -04:00
|
|
|
def check_for_requests():
|
|
|
|
global onionshare
|
2014-06-11 19:55:28 -04:00
|
|
|
events = []
|
|
|
|
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
try:
|
|
|
|
r = onionshare.q.get(False)
|
|
|
|
events.append(r)
|
|
|
|
except onionshare.Queue.Empty:
|
|
|
|
done = True
|
|
|
|
|
|
|
|
return json.dumps(events)
|
2014-06-06 21:54:08 -04:00
|
|
|
|
2014-06-11 20:53:19 -04:00
|
|
|
@app.route("/close")
|
|
|
|
def close():
|
2014-06-19 16:19:46 -04:00
|
|
|
global qtapp
|
|
|
|
time.sleep(1)
|
|
|
|
qtapp.closeAllWindows()
|
2014-06-11 20:53:19 -04:00
|
|
|
return ''
|
|
|
|
|