Code:
# mini webserver via Tornet -- source : https://stem.torproject.org/tutorials/over_the_river.html
# improved tutorial example 2016 : re-use .onion for tiny flask webserver
# release site anon gist : https://gist.github.com/anonymous/19193e9aed0ffc68a87a0e2a94437aba
# file name on https://gist.github.com : re-useOnion_Flask.py
# main published gist release : https://gist.github.com/anonymous/a4b798b1a31a2a0d8caaf4770387b540
import os
from stem.control import Controller
################################## flask webserver main part ######### just 30 lines of python code for a complete webServer !
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "<h1> This is a tiny webserver demo (version 1.0) </h1>"
#######################################################################
print(' * Flask is up soon. Now Connecting to tor * ')
#key_path = os.path.expanduser('~/my_service_key')
key_path = os.path.expanduser('./my_service_key-onion-data')
with Controller.from_port() as controller:
controller.authenticate()
print(" wait 1 min. for HS resuming") ### ****.onion
if not os.path.exists(key_path):
service = controller.create_ephemeral_hidden_service({80: 5000}, await_publication = True)
print("Started a new hidden service with the address of %s.onion" % service.service_id)
with open(key_path, 'w') as key_file:
key_file.write('%s:%s' % (service.private_key_type, service.private_key))
else:
with open(key_path) as key_file:
key_type, key_content = key_file.read().split(':', 1)
service = controller.create_ephemeral_hidden_service({80: 5000}, key_type = key_type, key_content = key_content, await_publication = True)
print("Resumed %s.onion" % service.service_id)
try:
app.run()
# raw_input(' app ran - press any key then RETURN to shut the service down...')
controller.remove_ephemeral_hidden_service(service.service_id)
finally:
print(" Now service is down, but the above .onion can be re-used. ")