2020-01-27 23:30:22 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-07-02 23:16:04 +00:00
|
|
|
|
2020-01-27 23:30:22 +00:00
|
|
|
Misc client API endpoints too small to need their own file and that need access to the client api inst
|
|
|
|
"""
|
2019-10-14 00:56:14 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2020-02-06 02:31:38 +00:00
|
|
|
import platform
|
2019-10-14 00:56:14 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
from flask import Response, Blueprint, request, send_from_directory, abort
|
2020-02-12 01:49:44 +00:00
|
|
|
from flask import g
|
2020-11-13 08:17:48 +00:00
|
|
|
from gevent import sleep
|
2019-09-10 06:05:59 +00:00
|
|
|
import unpaddedbase32
|
2019-09-06 22:22:33 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
from httpapi import apiutils
|
2020-01-27 23:30:22 +00:00
|
|
|
import onionrcrypto
|
|
|
|
import config
|
2019-08-04 04:52:57 +00:00
|
|
|
from netcontroller import NetController
|
2020-01-27 08:20:09 +00:00
|
|
|
from onionrstatistics.serializeddata import SerializedData
|
2019-09-06 20:09:39 +00:00
|
|
|
from onionrutils import mnemonickeys
|
|
|
|
from onionrutils import bytesconverter
|
2019-10-14 00:56:14 +00:00
|
|
|
from etc import onionrvalues
|
2019-09-17 01:16:06 +00:00
|
|
|
from utils import reconstructhash
|
2020-06-21 22:54:30 +00:00
|
|
|
from utils.gettransports import get as get_tor
|
2020-10-03 22:43:18 +00:00
|
|
|
from .addpeer import add_peer
|
2020-01-27 23:30:22 +00:00
|
|
|
"""
|
|
|
|
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-09-06 20:09:39 +00:00
|
|
|
|
2019-09-06 22:22:33 +00:00
|
|
|
pub_key = onionrcrypto.pub_key.replace('=', '')
|
|
|
|
|
2020-01-27 23:30:22 +00:00
|
|
|
SCRIPT_NAME = os.path.dirname(os.path.realpath(__file__)) + \
|
|
|
|
f'/../../../{onionrvalues.SCRIPT_NAME}'
|
2019-10-14 00:56:14 +00:00
|
|
|
|
2020-06-30 22:16:33 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
class PrivateEndpoints:
|
|
|
|
def __init__(self, client_api):
|
|
|
|
private_endpoints_bp = Blueprint('privateendpoints', __name__)
|
|
|
|
self.private_endpoints_bp = private_endpoints_bp
|
|
|
|
|
2020-10-03 22:43:18 +00:00
|
|
|
@private_endpoints_bp.route('/addpeer/<name>', methods=['post'])
|
|
|
|
def add_peer_endpoint(name):
|
|
|
|
result = add_peer(name)
|
|
|
|
if result == "success":
|
|
|
|
return Response("success")
|
|
|
|
else:
|
2020-10-15 23:24:25 +00:00
|
|
|
if "already" in result:
|
|
|
|
return Response(result, 409)
|
|
|
|
else:
|
|
|
|
return Response(result, 400)
|
2020-10-03 22:43:18 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
@private_endpoints_bp.route('/www/<path:path>', endpoint='www')
|
|
|
|
def wwwPublic(path):
|
|
|
|
if not config.get("www.private.run", True):
|
|
|
|
abort(403)
|
2020-08-10 03:13:06 +00:00
|
|
|
return send_from_directory(config.get('www.private.path',
|
|
|
|
'static-data/www/private/'), path)
|
2019-07-02 23:16:04 +00:00
|
|
|
|
|
|
|
@private_endpoints_bp.route('/hitcount')
|
|
|
|
def get_hit_count():
|
|
|
|
return Response(str(client_api.publicAPI.hitCount))
|
|
|
|
|
|
|
|
@private_endpoints_bp.route('/ping')
|
|
|
|
def ping():
|
|
|
|
# Used to check if client api is working
|
|
|
|
return Response("pong!")
|
|
|
|
|
|
|
|
@private_endpoints_bp.route('/lastconnect')
|
2020-08-10 03:13:06 +00:00
|
|
|
def last_connect():
|
2019-07-02 23:16:04 +00:00
|
|
|
return Response(str(client_api.publicAPI.lastRequest))
|
|
|
|
|
|
|
|
@private_endpoints_bp.route('/waitforshare/<name>', methods=['post'])
|
2020-01-27 23:30:22 +00:00
|
|
|
def wait_for_share(name):
|
|
|
|
"""Prevent the **public** api from sharing blocks.
|
|
|
|
|
|
|
|
Used for blocks we created usually
|
|
|
|
"""
|
|
|
|
if not name.isalnum():
|
|
|
|
raise ValueError('block hash needs to be alpha numeric')
|
2019-09-17 01:16:06 +00:00
|
|
|
name = reconstructhash.reconstruct_hash(name)
|
2019-07-02 23:16:04 +00:00
|
|
|
if name in client_api.publicAPI.hideBlocks:
|
2019-12-27 07:53:18 +00:00
|
|
|
return Response("will be removed")
|
2019-07-02 23:16:04 +00:00
|
|
|
else:
|
2019-07-03 00:35:59 +00:00
|
|
|
client_api.publicAPI.hideBlocks.append(name)
|
2019-07-02 23:16:04 +00:00
|
|
|
return Response("added")
|
|
|
|
|
|
|
|
@private_endpoints_bp.route('/shutdown')
|
|
|
|
def shutdown():
|
|
|
|
return apiutils.shutdown.shutdown(client_api)
|
2019-09-20 17:53:42 +00:00
|
|
|
|
|
|
|
@private_endpoints_bp.route('/restartclean')
|
|
|
|
def restart_clean():
|
2019-10-14 00:56:14 +00:00
|
|
|
subprocess.Popen([SCRIPT_NAME, 'restart'])
|
2019-09-20 17:53:42 +00:00
|
|
|
return Response("bye")
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-11-27 19:39:48 +00:00
|
|
|
@private_endpoints_bp.route('/gethidden')
|
|
|
|
def get_hidden_blocks():
|
|
|
|
return Response('\n'.join(client_api.publicAPI.hideBlocks))
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
@private_endpoints_bp.route('/getstats')
|
2020-01-27 23:30:22 +00:00
|
|
|
def get_stats():
|
|
|
|
"""Return serialized node statistics."""
|
2019-07-02 23:16:04 +00:00
|
|
|
while True:
|
2019-12-22 19:42:10 +00:00
|
|
|
try:
|
2020-01-27 23:30:22 +00:00
|
|
|
return Response(client_api._too_many.get(
|
|
|
|
SerializedData).get_stats())
|
|
|
|
except AttributeError:
|
2019-07-02 23:16:04 +00:00
|
|
|
pass
|
2020-11-16 06:57:38 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
@private_endpoints_bp.route('/getuptime')
|
2020-08-10 03:13:06 +00:00
|
|
|
def show_uptime():
|
2019-07-02 23:16:04 +00:00
|
|
|
return Response(str(client_api.getUptime()))
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
@private_endpoints_bp.route('/getActivePubkey')
|
2020-08-10 03:13:06 +00:00
|
|
|
def get_active_pubkey():
|
2019-07-21 00:29:08 +00:00
|
|
|
return Response(pub_key)
|
2019-07-02 23:16:04 +00:00
|
|
|
|
2019-09-06 20:09:39 +00:00
|
|
|
@private_endpoints_bp.route('/getHumanReadable')
|
2020-08-10 03:13:06 +00:00
|
|
|
def get_human_readable_default():
|
2019-09-06 20:09:39 +00:00
|
|
|
return Response(mnemonickeys.get_human_readable_ID())
|
|
|
|
|
2019-07-02 23:16:04 +00:00
|
|
|
@private_endpoints_bp.route('/getHumanReadable/<name>')
|
2020-08-10 03:13:06 +00:00
|
|
|
def get_human_readable(name):
|
2019-09-10 06:05:59 +00:00
|
|
|
name = unpaddedbase32.repad(bytesconverter.str_to_bytes(name))
|
2019-08-04 04:52:57 +00:00
|
|
|
return Response(mnemonickeys.get_human_readable_ID(name))
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2019-09-06 20:09:39 +00:00
|
|
|
@private_endpoints_bp.route('/getBase32FromHumanReadable/<words>')
|
|
|
|
def get_base32_from_human_readable(words):
|
2020-08-10 03:13:06 +00:00
|
|
|
return Response(
|
|
|
|
bytesconverter.bytes_to_str(mnemonickeys.get_base32(words)))
|
2019-09-06 20:09:39 +00:00
|
|
|
|
2019-11-30 08:42:49 +00:00
|
|
|
@private_endpoints_bp.route('/setonboarding', methods=['POST'])
|
|
|
|
def set_onboarding():
|
2020-08-10 03:13:06 +00:00
|
|
|
return Response(
|
|
|
|
config.onboarding.set_config_from_onboarding(request.get_json()))
|
2020-02-12 01:49:44 +00:00
|
|
|
|
2020-02-06 02:31:38 +00:00
|
|
|
@private_endpoints_bp.route('/os')
|
|
|
|
def get_os_system():
|
|
|
|
return Response(platform.system().lower())
|
2019-12-22 19:42:10 +00:00
|
|
|
|
2020-11-13 08:17:48 +00:00
|
|
|
@private_endpoints_bp.route('/gettorsocks')
|
|
|
|
def get_tor_socks():
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
return Response(
|
|
|
|
str(
|
|
|
|
g.too_many.get_by_string(
|
|
|
|
'NetController').socksPort))
|
|
|
|
except KeyError:
|
|
|
|
sleep(0.1)
|
|
|
|
|
2020-02-12 01:49:44 +00:00
|
|
|
@private_endpoints_bp.route('/torready')
|
|
|
|
def is_tor_ready():
|
|
|
|
"""If Tor is starting up, the web UI is not ready to be used."""
|
2020-11-13 08:17:48 +00:00
|
|
|
try:
|
|
|
|
return Response(
|
|
|
|
str(g.too_many.get_by_string('NetController').readyState).lower())
|
|
|
|
except KeyError:
|
|
|
|
return Response("false")
|
2020-06-30 23:34:13 +00:00
|
|
|
|
2020-06-21 22:54:30 +00:00
|
|
|
@private_endpoints_bp.route('/gettoraddress')
|
|
|
|
def get_tor_address():
|
|
|
|
"""Return public Tor v3 Onion address for this node"""
|
2020-06-27 23:05:04 +00:00
|
|
|
if not config.get('general.security_level', 0) == 0:
|
2020-06-25 08:29:27 +00:00
|
|
|
abort(404)
|
2020-06-21 22:54:30 +00:00
|
|
|
return Response(get_tor()[0])
|
2020-08-10 03:13:06 +00:00
|
|
|
|
|
|
|
@private_endpoints_bp.route('/getgeneratingblocks')
|
|
|
|
def get_generating_blocks() -> Response:
|
|
|
|
return Response(
|
|
|
|
','.join(
|
|
|
|
g.too_many.get_by_string('DeadSimpleKV').get(
|
|
|
|
'generating_blocks'
|
|
|
|
))
|
|
|
|
)
|
2020-08-10 06:03:32 +00:00
|
|
|
|
|
|
|
@private_endpoints_bp.route('/getblockstoupload')
|
|
|
|
def get_blocks_to_upload() -> Response:
|
|
|
|
return Response(
|
|
|
|
','.join(
|
|
|
|
g.too_many.get_by_string('DeadSimpleKV').get('blocksToUpload')
|
|
|
|
)
|
|
|
|
)
|