2022-08-10 00:02:37 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Default example plugin for devs or to test blocks
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import locale
|
2022-10-14 18:26:07 +00:00
|
|
|
from secrets import randbelow
|
2022-08-10 00:02:37 +00:00
|
|
|
|
|
|
|
import cherrypy
|
|
|
|
|
|
|
|
locale.setlocale(locale.LC_ALL, '')
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
# import after path insert
|
|
|
|
|
2022-10-14 18:26:07 +00:00
|
|
|
from logger import log as logging
|
2022-08-10 00:02:37 +00:00
|
|
|
from utils import identifyhome
|
|
|
|
from onionrthreads import add_onionr_thread
|
2022-10-14 18:26:07 +00:00
|
|
|
import config
|
|
|
|
from onionrplugins.pluginapis import plugin_apis
|
2022-08-10 00:02:37 +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/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
plugin_name = 'rpc'
|
|
|
|
PLUGIN_VERSION = '0.0.0'
|
|
|
|
socket_file_path = identifyhome.identify_home() + 'rpc.sock'
|
|
|
|
|
|
|
|
from jsonrpc import JSONRPCResponseManager, dispatcher
|
2022-08-11 19:25:07 +00:00
|
|
|
import jsonrpc
|
|
|
|
import ujson
|
2022-11-22 05:57:14 +00:00
|
|
|
import requests_unixsocket
|
|
|
|
import requests
|
2022-08-11 19:25:07 +00:00
|
|
|
jsonrpc.manager.json = ujson
|
2022-08-10 00:02:37 +00:00
|
|
|
|
2022-11-22 05:57:14 +00:00
|
|
|
from onionrplugins import plugin_apis
|
|
|
|
|
2022-08-11 19:25:07 +00:00
|
|
|
# RPC modules map Onionr APIs to the RPC dispacher
|
2022-08-16 14:54:54 +00:00
|
|
|
from rpc import blocks, pluginrpcmethods
|
2022-08-10 00:02:37 +00:00
|
|
|
|
2022-10-14 18:26:07 +00:00
|
|
|
from rpc.addmodule import add_module_to_api
|
2023-01-12 06:23:28 +00:00
|
|
|
import longrpc
|
2022-10-14 18:26:07 +00:00
|
|
|
|
2022-11-22 05:57:14 +00:00
|
|
|
|
2022-10-14 18:26:07 +00:00
|
|
|
plugin_apis['rpc.add_module_to_api'] = add_module_to_api
|
2022-08-10 00:02:37 +00:00
|
|
|
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2023-01-12 06:23:28 +00:00
|
|
|
def _detect_cors_and_add_headers():
|
|
|
|
cherrypy.response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
|
|
|
|
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
|
|
|
|
cherrypy.response.headers['Access-Control-Allow-Methods'] = 'POST'
|
|
|
|
if cherrypy.request.method == 'OPTIONS':
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2022-08-10 00:02:37 +00:00
|
|
|
class OnionrRPC(object):
|
2023-01-12 06:23:28 +00:00
|
|
|
@cherrypy.expose
|
2023-01-13 03:51:27 +00:00
|
|
|
def threaded_rpc(self):
|
2023-01-12 06:23:28 +00:00
|
|
|
if _detect_cors_and_add_headers():
|
|
|
|
return ''
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2023-01-12 06:23:28 +00:00
|
|
|
rpc_request_json: str = cherrypy.request.body.read().decode('utf-8')
|
|
|
|
longrpc.threaded_rpc(rpc_request_json)
|
|
|
|
return 'ok'
|
|
|
|
|
|
|
|
@cherrypy.expose
|
|
|
|
def get_rpc_result(self, id=0):
|
|
|
|
if _detect_cors_and_add_headers():
|
|
|
|
return ''
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2023-01-12 06:23:28 +00:00
|
|
|
results = longrpc.get_results(id)
|
|
|
|
if not results:
|
|
|
|
return '"no result"'
|
|
|
|
return results
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2022-08-10 00:02:37 +00:00
|
|
|
@cherrypy.expose
|
|
|
|
def rpc(self):
|
2023-01-12 06:23:28 +00:00
|
|
|
# Basic RPC, intended for small amounts of work
|
|
|
|
# Use /queue_rpc for large workloads like creating blocks
|
|
|
|
# and getting results with /get_rpc_result?id=<id>
|
2022-08-10 00:02:37 +00:00
|
|
|
# Dispatcher is dictionary {<method_name>: callable}
|
2023-01-17 05:29:50 +00:00
|
|
|
|
2023-01-12 06:23:28 +00:00
|
|
|
if _detect_cors_and_add_headers():
|
2023-01-04 06:52:15 +00:00
|
|
|
return ''
|
|
|
|
|
2022-08-10 00:02:37 +00:00
|
|
|
data = cherrypy.request.body.read().decode('utf-8')
|
|
|
|
|
|
|
|
response = JSONRPCResponseManager.handle(data, dispatcher)
|
|
|
|
return response.json
|
|
|
|
|
|
|
|
|
2022-11-22 05:57:14 +00:00
|
|
|
def rpc_client(*args, **kwargs):
|
|
|
|
if config.get('rpc.use_sock_file', True):
|
|
|
|
session = requests_unixsocket.Session()
|
|
|
|
return session.post(
|
|
|
|
'http+unix://' + config.get('rpc.sock_file_path', socket_file_path).replace('/', '%2F') + '/rpc',
|
|
|
|
*args, **kwargs)
|
|
|
|
else:
|
|
|
|
return requests.post(
|
2022-12-02 21:40:33 +00:00
|
|
|
f'http://{config.get("rpc.bind_host")}:{config.get("rpc.bind_port")}/rpc',
|
2022-11-22 05:57:14 +00:00
|
|
|
*args, **kwargs)
|
2022-12-02 21:40:33 +00:00
|
|
|
|
2022-11-22 05:57:14 +00:00
|
|
|
def on_beforecmdparsing(api, data=None):
|
|
|
|
plugin_apis['rpc.rpc_client'] = rpc_client
|
|
|
|
|
2022-08-16 14:54:54 +00:00
|
|
|
def on_afterinit(api, data=None):
|
2022-10-14 18:26:07 +00:00
|
|
|
def ping():
|
|
|
|
return "pong"
|
2023-01-12 06:23:28 +00:00
|
|
|
def always_fails():
|
|
|
|
raise Exception("This always fails")
|
2022-10-14 18:26:07 +00:00
|
|
|
dispatcher['ping'] = ping
|
2023-01-12 06:23:28 +00:00
|
|
|
dispatcher['always_fails'] = always_fails
|
2022-08-16 14:54:54 +00:00
|
|
|
pluginrpcmethods.add_plugin_rpc_methods()
|
|
|
|
|
|
|
|
|
2022-10-14 18:26:07 +00:00
|
|
|
def _gen_random_loopback():
|
|
|
|
return f'127.{randbelow(256)}.{randbelow(256)}.{randbelow(256)}'
|
|
|
|
|
|
|
|
|
2023-01-11 22:51:11 +00:00
|
|
|
def on_setunixsocket_cmd(api, data=None):
|
|
|
|
config.set('rpc.use_sock_file', True, savefile=True)
|
|
|
|
logging.info('Set RPC to use unix socket')
|
|
|
|
|
|
|
|
def on_settcpsocket_cmd(api, data=None):
|
|
|
|
config.set('rpc.use_sock_file', False, savefile=True)
|
|
|
|
address = input('Enter address to bind to (default: random loopback): ').strip()
|
|
|
|
if not address:
|
|
|
|
address = _gen_random_loopback()
|
|
|
|
port = input('Enter port to bind to (default: 0 (random/picked by OS)): ').strip()
|
|
|
|
if not port:
|
|
|
|
port = 0
|
|
|
|
port = int(port)
|
|
|
|
config.set('rpc.bind_host', address, savefile=True)
|
|
|
|
config.set('rpc.bind_port', port, savefile=True)
|
|
|
|
|
|
|
|
logging.info(
|
2023-01-17 05:29:50 +00:00
|
|
|
'Set RPC to use TCP socket http://' +
|
2023-01-11 22:51:11 +00:00
|
|
|
f'{config.get("rpc.bind_host")}:{config.get("rpc.bind_port")}')
|
|
|
|
|
|
|
|
|
2022-08-10 00:02:37 +00:00
|
|
|
def on_init(api, data=None):
|
2022-10-14 18:26:07 +00:00
|
|
|
bind_config = {}
|
|
|
|
if config.get('rpc.use_sock_file', True, save=True):
|
|
|
|
bind_config['server.socket_file'] = config.get(
|
|
|
|
'rpc.sock_file_path', socket_file_path, save=True)
|
|
|
|
# create base dir if it doesn't exist
|
|
|
|
os.makedirs(
|
|
|
|
os.path.dirname(config.get('rpc.sock_file_path', socket_file_path)), exist_ok=True)
|
|
|
|
else:
|
|
|
|
# Set default bind TCP address, if not set
|
|
|
|
# We use a random loopback address to avoid browser side channel attacks
|
|
|
|
# and let the OS pick a port (0)
|
|
|
|
bind_config['server.socket_host'] = config.get(
|
|
|
|
'rpc.bind_host', _gen_random_loopback(), save=True)
|
|
|
|
bind_config['server.socket_port'] = config.get('rpc.bind_port', 0)
|
|
|
|
cherrpy_config = bind_config | {
|
2022-08-10 00:02:37 +00:00
|
|
|
'engine.autoreload.on': False
|
|
|
|
}
|
2022-10-14 18:26:07 +00:00
|
|
|
cherrypy.config.update(cherrpy_config)
|
|
|
|
|
|
|
|
logging.info("Starting RPC Server")
|
2022-08-10 00:02:37 +00:00
|
|
|
|
|
|
|
add_onionr_thread(
|
|
|
|
cherrypy.quickstart, 5, 'OnionrRPCServer',
|
|
|
|
OnionrRPC(), initial_sleep=0)
|