Merge branch 'enotodden-master'

This commit is contained in:
Micah Lee 2014-05-23 22:53:18 -04:00
commit 31235367c5
3 changed files with 50 additions and 12 deletions

View file

@ -66,8 +66,8 @@
<p><a class="button" href='/{{ slug }}/download'>{{ filename }} &#x25BC;</a></p> <p><a class="button" href='/{{ slug }}/download'>{{ filename }} &#x25BC;</a></p>
<div class="metadata"> <div class="metadata">
<p>File size: <strong>{{ filesize }} bytes</strong></p> <p>{{strings.filesize}}: <strong>{{ filesize }} bytes</strong></p>
<p>SHA1 checksum: <strong>{{ filehash }}</strong></p> <p>{{strings.sha1_checksum}}: <strong>{{ filehash }}</strong></p>
</div> </div>
</body> </body>
</html> </html>

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, subprocess, time, hashlib, platform import os, sys, subprocess, time, hashlib, platform, json, locale
from random import randint from random import randint
from functools import wraps from functools import wraps
@ -12,6 +12,8 @@ from stem import SocketError
from flask import Flask, Markup, Response, request, make_response, send_from_directory, render_template_string from flask import Flask, Markup, Response, request, make_response, send_from_directory, render_template_string
app = Flask(__name__) app = Flask(__name__)
strings = {}
# generate an unguessable string # generate an unguessable string
slug = os.urandom(16).encode('hex') slug = os.urandom(16).encode('hex')
@ -22,7 +24,7 @@ filename = filehash = filesize = ''
def index(): def index():
global filename, filesize, filehash, slug global filename, filesize, filehash, slug
return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(), return render_template_string(open('{0}/index.html'.format(os.path.dirname(__file__))).read(),
slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize) slug=slug, filename=os.path.basename(filename), filehash=filehash, filesize=filesize, strings=strings)
@app.route("/{0}/download".format(slug)) @app.route("/{0}/download".format(slug))
def download(): def download():
@ -57,24 +59,37 @@ def get_hidden_service_hostname(port):
def tails_open_port(port): def tails_open_port(port):
if get_platform() == 'Tails': if get_platform() == 'Tails':
print 'Punching a hole in the firewall' print strings["punching_a_hole"]
subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT']) subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'ACCEPT'])
def tails_close_port(port): def tails_close_port(port):
if get_platform() == 'Tails': if get_platform() == 'Tails':
print 'Closing hole in firewall' print strings["closing_hole"]
subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'REJECT']) subprocess.call(['/sbin/iptables', '-I', 'OUTPUT', '-o', 'lo', '-p', 'tcp', '--dport', str(port), '-j', 'REJECT'])
def load_strings(default="en"):
global strings
translated = json.loads(open('{0}/strings.json'.format(
os.path.dirname(__file__))).read())
strings = translated[default]
lc, enc = locale.getdefaultlocale()
if lc:
lang = lc[:2]
if lang in translated:
strings = translated[lang]
if __name__ == '__main__': if __name__ == '__main__':
load_strings()
# validate filename # validate filename
if len(sys.argv) != 2: if len(sys.argv) != 2:
sys.exit('Usage: {0} [filename]'.format(sys.argv[0])); sys.exit('Usage: {0} [filename]'.format(sys.argv[0]));
filename = sys.argv[1] filename = sys.argv[1]
if not os.path.isfile(filename): if not os.path.isfile(filename):
sys.exit('{0} is not a file'.format(filename)) sys.exit(strings["not_a_file"].format(filename))
# calculate filehash, file size # calculate filehash, file size
print 'Calculate sha1 checksum' print strings["calculating_sha1"]
BLOCKSIZE = 65536 BLOCKSIZE = 65536
hasher = hashlib.sha1() hasher = hashlib.sha1()
with open(filename, 'rb') as f: with open(filename, 'rb') as f:
@ -89,7 +104,7 @@ if __name__ == '__main__':
port = randint(1025, 65535) port = randint(1025, 65535)
# connect to the tor controlport # connect to the tor controlport
print 'Connecting to Tor control port to set up hidden service on port {0}'.format(port) print strings["connecting_ctrlport"].format(port)
controlports = [9051, 9151] controlports = [9051, 9151]
controller = False controller = False
for controlport in controlports: for controlport in controlports:
@ -98,7 +113,7 @@ if __name__ == '__main__':
except SocketError: except SocketError:
pass pass
if not controller: if not controller:
sys.exit('Cannot connect to Tor control port on ports {0}. Is Tor running?'.format(controlports)) sys.exit(strings["cant_connect_ctrlport"].format(controlports))
controller.authenticate() controller.authenticate()
# set up hidden service # set up hidden service
@ -112,10 +127,10 @@ if __name__ == '__main__':
tails_open_port(port) tails_open_port(port)
# instructions # instructions
print '\nGive this URL to the person you\'re sending the file to:' print '\n' + strings["give_this_url"]
print 'http://{0}/{1}'.format(onion_host, slug) print 'http://{0}/{1}'.format(onion_host, slug)
print '' print ''
print 'Press Ctrl-C to stop server\n' print strings["ctrlc_to_stop"]
# start the web server # start the web server
app.run(port=port) app.run(port=port)

23
strings.json Normal file
View file

@ -0,0 +1,23 @@
{ "en": {
"punching_a_hole": "Punching a hole in the firewall.",
"closing_hole": "Closing hole in firewall.",
"calculating_sha1": "Calculating SHA1 checksum.",
"connecting_ctrlport": "Connecting to Tor control port to set up hidden service on port {0}.",
"cant_connect_ctrlport": "Cannot connect to Tor control port on ports {0}. Is Tor running?",
"give_this_url": "Give this URL to the person you're sending the file to:",
"ctrlc_to_stop": "Press Ctrl-C to stop server",
"not_a_file": "{0} is not a file.",
"filesize": "File size",
"sha1_checksum": "SHA1 checksum"
}, "no": {
"punching_a_hole": "Åpner port i brannmuren.",
"closing_hole": "Lukker port i brannmuren.",
"calculating_sha1": "Kalkulerer SHA1 sjekksum.",
"connecting_ctrlport": "Kobler til Tors kontroll-port for å sette opp en gjemt tjeneste på port {0}.",
"cant_connect_ctrlport": "Klarte ikke å koble til Tors kontroll-porter {0}. Sjekk at Tor kjører.",
"give_this_url": "Gi personen du vil sende filen til denne URL-en:",
"ctrlc_to_stop": "Trykk Ctrl+C for å stoppe serveren.",
"not_a_file": "{0} er ikke en fil.",
"filesize": "Filstørrelse",
"sha1_checksum": "SHA1 sjekksum"
}}