2019-03-18 05:22:31 +00:00
|
|
|
'''
|
|
|
|
Onionr - P2P Anonymous Storage Network
|
|
|
|
|
|
|
|
Bootstrap onion direct connections for the clients
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2019-03-25 23:46:25 +00:00
|
|
|
import time, threading, uuid
|
2019-03-18 05:22:31 +00:00
|
|
|
from gevent.pywsgi import WSGIServer, WSGIHandler
|
|
|
|
from stem.control import Controller
|
2019-03-25 23:46:25 +00:00
|
|
|
from flask import Flask, Response
|
2019-03-18 05:22:31 +00:00
|
|
|
import core
|
2019-03-25 03:32:17 +00:00
|
|
|
from netcontroller import getOpenPort
|
2019-03-27 17:38:46 +00:00
|
|
|
from . import httpheaders
|
2019-03-18 05:22:31 +00:00
|
|
|
|
2019-03-23 02:58:09 +00:00
|
|
|
def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300):
|
2019-03-18 05:22:31 +00:00
|
|
|
'''
|
|
|
|
Bootstrap client services
|
|
|
|
'''
|
|
|
|
if core_inst is None:
|
|
|
|
core_inst = core.Core()
|
|
|
|
|
|
|
|
if not core_inst._utils.validatePubKey(peer):
|
|
|
|
raise ValueError('Peer must be valid base32 ed25519 public key')
|
2019-03-19 05:09:53 +00:00
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
bootstrap_port = getOpenPort()
|
2019-03-19 05:09:53 +00:00
|
|
|
bootstrap_app = Flask(__name__)
|
|
|
|
http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None)
|
2019-03-26 04:25:46 +00:00
|
|
|
try:
|
2019-03-31 17:16:09 +00:00
|
|
|
assert core_inst.onionrInst.communicatorInst is not None
|
|
|
|
except (AttributeError, AssertionError) as e:
|
2019-03-26 04:25:46 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server)
|
2019-03-18 05:22:31 +00:00
|
|
|
|
|
|
|
bootstrap_address = ''
|
2019-03-25 03:32:17 +00:00
|
|
|
shutdown = False
|
2019-03-25 23:46:25 +00:00
|
|
|
bs_id = str(uuid.uuid4())
|
2019-03-18 05:22:31 +00:00
|
|
|
|
|
|
|
@bootstrap_app.route('/ping')
|
|
|
|
def get_ping():
|
|
|
|
return "pong!"
|
|
|
|
|
2019-03-27 17:38:46 +00:00
|
|
|
@bootstrap_app.after_request
|
|
|
|
def afterReq(resp):
|
|
|
|
# Security headers
|
|
|
|
resp = httpheaders.set_default_onionr_http_headers(resp)
|
|
|
|
return resp
|
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
@bootstrap_app.route('/bs/<address>', methods=['POST'])
|
|
|
|
def get_bootstrap(address):
|
2019-03-25 23:46:25 +00:00
|
|
|
if core_inst._utils.validateID(address + '.onion'):
|
2019-03-18 05:22:31 +00:00
|
|
|
# Set the bootstrap address then close the server
|
2019-03-25 23:46:25 +00:00
|
|
|
bootstrap_address = address + '.onion'
|
|
|
|
core_inst.keyStore.put(bs_id, bootstrap_address)
|
|
|
|
http_server.stop()
|
|
|
|
return Response("success")
|
|
|
|
else:
|
|
|
|
return Response("")
|
2019-03-18 05:22:31 +00:00
|
|
|
|
2019-03-19 05:09:53 +00:00
|
|
|
with Controller.from_port(port=core_inst.config.get('tor.controlPort')) as controller:
|
2019-03-18 05:22:31 +00:00
|
|
|
# Connect to the Tor process for Onionr
|
2019-03-19 05:09:53 +00:00
|
|
|
controller.authenticate(core_inst.config.get('tor.controlpassword'))
|
2019-03-18 05:22:31 +00:00
|
|
|
# Create the v3 onion service
|
2019-03-25 23:46:25 +00:00
|
|
|
response = controller.create_ephemeral_hidden_service({80: bootstrap_port}, key_type = 'NEW', key_content = 'ED25519-V3', await_publication = True)
|
2019-03-19 05:09:53 +00:00
|
|
|
core_inst.insertBlock(response.service_id, header='con', sign=True, encryptType='asym',
|
|
|
|
asymPeer=peer, disableForward=True, expire=(core_inst._utils.getEpoch() + bootstrap_timeout))
|
2019-03-18 05:22:31 +00:00
|
|
|
# Run the bootstrap server
|
2019-03-25 23:46:25 +00:00
|
|
|
try:
|
|
|
|
http_server.serve_forever()
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2019-03-18 05:22:31 +00:00
|
|
|
# This line reached when server is shutdown by being bootstrapped
|
|
|
|
|
|
|
|
# Now that the bootstrap server has received a server, return the address
|
2019-03-25 23:46:25 +00:00
|
|
|
return core_inst.keyStore.get(bs_id)
|