Onionr/onionr/onionrservices/connectionserver.py

76 lines
3.3 KiB
Python
Raw Normal View History

'''
Onionr - P2P Anonymous Storage Network
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/>.
'''
import threading, time
from gevent.pywsgi import WSGIServer, WSGIHandler
from stem.control import Controller
from flask import Flask
2019-03-29 17:37:51 +00:00
import core, logger, httpapi
from netcontroller import getOpenPort
2019-03-25 23:46:25 +00:00
import api
2019-03-27 17:38:46 +00:00
from . import httpheaders
class ConnectionServer:
2019-03-19 05:09:53 +00:00
def __init__(self, peer, address, core_inst=None):
if core_inst is None:
self.core_inst = core.Core()
else:
self.core_inst = core_inst
if not core_inst._utils.validatePubKey(peer):
raise ValueError('Peer must be valid base32 ed25519 public key')
socks = core_inst.config.get('tor.socksport') # Load config for Tor socks port for proxy
service_app = Flask(__name__) # Setup Flask app for server.
2019-03-19 05:09:53 +00:00
service_port = getOpenPort()
2019-03-25 23:46:25 +00:00
service_ip = api.setBindIP()
2019-03-19 05:09:53 +00:00
http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None)
2019-03-26 04:25:46 +00:00
core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server)
# 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-04-25 05:38:15 +00:00
@service_app.route('/close')
def shutdown_server():
core_inst.onionrInst.communicatorInst.service_greenlets.remove(http_server)
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-03-19 05:09:53 +00:00
with Controller.from_port(port=core_inst.config.get('tor.controlPort')) as controller:
# Connect to the Tor process for Onionr
controller.authenticate(core_inst.config.get('tor.controlpassword'))
# Create the v3 onion service
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-03-29 17:37:51 +00:00
self.core_inst.keyStore.put('dc-' + response.service_id, self.core_inst._utils.bytesToStr(peer))
self.core_inst._utils.doPostRequest('http://' + address + '/bs/' + response.service_id, port=socks)
logger.info('hosting on %s with %s' % (response.service_id, peer))
2019-03-25 23:46:25 +00:00
http_server.serve_forever()
2019-03-29 17:37:51 +00:00
self.core_inst.keyStore.delete('dc-' + response.service_id)
http_server.stop()