Onionr/static-data/official-plugins/rpc/main.py

101 lines
3.1 KiB
Python
Raw Normal View History

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
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
from logger import log as logging
2022-08-10 00:02:37 +00:00
from utils import identifyhome
from onionrthreads import add_onionr_thread
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
jsonrpc.manager.json = ujson
2022-08-10 00:02:37 +00:00
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
from rpc.addmodule import add_module_to_api
plugin_apis['rpc.add_module_to_api'] = add_module_to_api
2022-08-10 00:02:37 +00:00
class OnionrRPC(object):
@cherrypy.expose
@cherrypy.tools.json_out()
def rpc(self):
# Dispatcher is dictionary {<method_name>: callable}
data = cherrypy.request.body.read().decode('utf-8')
response = JSONRPCResponseManager.handle(data, dispatcher)
return response.json
2022-08-16 14:54:54 +00:00
def on_afterinit(api, data=None):
def ping():
return "pong"
dispatcher['ping'] = ping
2022-08-16 14:54:54 +00:00
pluginrpcmethods.add_plugin_rpc_methods()
def _gen_random_loopback():
return f'127.{randbelow(256)}.{randbelow(256)}.{randbelow(256)}'
2022-08-10 00:02:37 +00:00
def on_init(api, data=None):
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
}
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)