2019-03-25 03:32:17 +00:00
|
|
|
'''
|
2019-06-12 00:05:15 +00:00
|
|
|
Onionr - Private P2P Communication
|
2019-03-25 03:32:17 +00:00
|
|
|
|
|
|
|
This module does the second part of the bootstrap block handshake and creates the API server
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
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-06-12 00:05:15 +00:00
|
|
|
from gevent.pywsgi import WSGIServer
|
2019-03-25 03:32:17 +00:00
|
|
|
from stem.control import Controller
|
|
|
|
from flask import Flask
|
2019-07-19 19:49:56 +00:00
|
|
|
import logger, httpapi
|
2019-07-28 18:05:32 +00:00
|
|
|
import onionrexceptions, config, filepaths
|
2019-07-14 06:51:43 +00:00
|
|
|
from netcontroller import get_open_port
|
2019-07-02 06:32:26 +00:00
|
|
|
from httpapi import apiutils
|
2019-06-29 18:18:31 +00:00
|
|
|
from onionrutils import stringvalidators, basicrequests, bytesconverter
|
2019-03-27 17:38:46 +00:00
|
|
|
from . import httpheaders
|
2019-07-28 18:05:32 +00:00
|
|
|
import deadsimplekv as simplekv
|
2019-03-25 03:32:17 +00:00
|
|
|
|
2019-03-18 05:22:31 +00:00
|
|
|
class ConnectionServer:
|
2019-07-28 05:33:26 +00:00
|
|
|
def __init__(self, peer, address, comm_inst=None):
|
2019-03-19 05:09:53 +00:00
|
|
|
|
2019-06-25 08:21:36 +00:00
|
|
|
if not stringvalidators.validate_pub_key(peer):
|
2019-03-19 05:09:53 +00:00
|
|
|
raise ValueError('Peer must be valid base32 ed25519 public key')
|
|
|
|
|
2019-07-28 05:33:26 +00:00
|
|
|
socks = config.get('tor.socksport') # Load config for Tor socks port for proxy
|
2019-03-25 03:32:17 +00:00
|
|
|
service_app = Flask(__name__) # Setup Flask app for server.
|
2019-07-14 06:51:43 +00:00
|
|
|
service_port = get_open_port()
|
2019-07-19 19:49:56 +00:00
|
|
|
service_ip = apiutils.setbindip.set_bind_IP()
|
2019-03-19 05:09:53 +00:00
|
|
|
http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None)
|
2019-07-28 05:33:26 +00:00
|
|
|
comm_inst.service_greenlets.append(http_server)
|
2019-07-28 18:05:32 +00:00
|
|
|
key_store = simplekv.DeadSimpleKV(filepaths.cached_storage)
|
2019-03-25 03:32:17 +00:00
|
|
|
|
|
|
|
# TODO define basic endpoints useful for direct connections like stats
|
2019-03-29 17:37:51 +00:00
|
|
|
|
|
|
|
httpapi.load_plugin_blueprints(service_app, blueprint='direct_blueprint')
|
|
|
|
|
2019-03-19 05:09:53 +00:00
|
|
|
@service_app.route('/ping')
|
|
|
|
def get_ping():
|
|
|
|
return "pong!"
|
2019-03-31 17:16:09 +00:00
|
|
|
|
2019-04-25 05:38:15 +00:00
|
|
|
@service_app.route('/close')
|
2019-03-31 17:16:09 +00:00
|
|
|
def shutdown_server():
|
2019-07-28 05:33:26 +00:00
|
|
|
comm_inst.service_greenlets.remove(http_server)
|
2019-03-31 17:16:09 +00:00
|
|
|
http_server.stop()
|
|
|
|
return Response('goodbye')
|
2019-03-19 05:09:53 +00:00
|
|
|
|
2019-03-27 17:38:46 +00:00
|
|
|
@service_app.after_request
|
|
|
|
def afterReq(resp):
|
|
|
|
# Security headers
|
|
|
|
resp = httpheaders.set_default_onionr_http_headers(resp)
|
|
|
|
return resp
|
|
|
|
|
2019-07-28 05:33:26 +00:00
|
|
|
with Controller.from_port(port=config.get('tor.controlPort')) as controller:
|
2019-03-19 05:09:53 +00:00
|
|
|
# Connect to the Tor process for Onionr
|
2019-07-28 05:33:26 +00:00
|
|
|
controller.authenticate(config.get('tor.controlpassword'))
|
2019-05-15 23:25:36 +00:00
|
|
|
# Create the v3 onion service for the peer to connect to
|
2019-03-25 23:46:25 +00:00
|
|
|
response = controller.create_ephemeral_hidden_service({80: service_port}, await_publication = True, key_type='NEW', key_content = 'ED25519-V3')
|
2019-05-15 23:25:36 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
for x in range(3):
|
2019-08-06 21:19:26 +00:00
|
|
|
attempt = basicrequests.do_post_request('http://' + address + '/bs/' + response.service_id, port=socks)
|
2019-05-15 23:25:36 +00:00
|
|
|
if attempt == 'success':
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ConnectionError
|
|
|
|
except ConnectionError:
|
|
|
|
# Re-raise
|
|
|
|
raise ConnectionError('Could not reach %s bootstrap address %s' % (peer, address))
|
|
|
|
else:
|
|
|
|
# If no connection error, create the service and save it to local global key store
|
2019-08-16 20:41:56 +00:00
|
|
|
peer = bytesconverter.bytes_to_str(peer)
|
|
|
|
key_store.put('dc-' + peer, response.service_id)
|
|
|
|
key_store.flush()
|
2019-05-15 23:25:36 +00:00
|
|
|
logger.info('hosting on %s with %s' % (response.service_id, peer))
|
|
|
|
http_server.serve_forever()
|
|
|
|
http_server.stop()
|
2019-08-16 20:41:56 +00:00
|
|
|
key_store.delete('dc-' + peer)
|