mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-26 20:30:59 +01:00
plugins/clnrest: Rune authentication for websocket server
This commit is contained in:
parent
9147996372
commit
6aa697ea3d
2 changed files with 30 additions and 11 deletions
|
@ -9,13 +9,13 @@ try:
|
||||||
from gunicorn.workers import sync # noqa: F401
|
from gunicorn.workers import sync # noqa: F401
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from flask import Flask
|
from flask import Flask, request
|
||||||
from flask_restx import Api
|
from flask_restx import Api
|
||||||
from gunicorn.app.base import BaseApplication
|
from gunicorn.app.base import BaseApplication
|
||||||
from multiprocessing import Process, Queue
|
from multiprocessing import Process, Queue
|
||||||
from flask_socketio import SocketIO
|
from flask_socketio import SocketIO, disconnect
|
||||||
from utilities.generate_certs import generate_certs
|
from utilities.generate_certs import generate_certs
|
||||||
from utilities.shared import set_config
|
from utilities.shared import set_config, verify_rune
|
||||||
from utilities.rpc_routes import rpcns
|
from utilities.rpc_routes import rpcns
|
||||||
from utilities.rpc_plugin import plugin
|
from utilities.rpc_plugin import plugin
|
||||||
except ModuleNotFoundError as err:
|
except ModuleNotFoundError as err:
|
||||||
|
@ -52,16 +52,30 @@ def broadcast_from_message_queue():
|
||||||
socketio.start_background_task(broadcast_from_message_queue)
|
socketio.start_background_task(broadcast_from_message_queue)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on("connect", namespace="/ws")
|
@socketio.on("message")
|
||||||
|
def handle_message(message):
|
||||||
|
plugin.log(f"Received message from client: {message}", "debug")
|
||||||
|
socketio.emit('message', {"client_message": message, "session": request.sid})
|
||||||
|
|
||||||
|
|
||||||
|
@socketio.on("connect")
|
||||||
def ws_connect():
|
def ws_connect():
|
||||||
plugin.log("Client Connected", "debug")
|
try:
|
||||||
msgq.put("Client Connected")
|
plugin.log("Client Connecting...", "debug")
|
||||||
|
is_valid_rune = verify_rune(plugin, request)
|
||||||
|
|
||||||
|
if "error" in is_valid_rune:
|
||||||
|
# Logging as error/warn emits the event for all clients
|
||||||
|
plugin.log(f"Error: {is_valid_rune}", "info")
|
||||||
|
raise Exception(is_valid_rune)
|
||||||
|
|
||||||
@socketio.on("disconnect", namespace="/ws")
|
plugin.log("Client Connected", "debug")
|
||||||
def ws_disconnect():
|
return True
|
||||||
plugin.log("Client Disconnected", "debug")
|
|
||||||
msgq.put("Client Disconnected")
|
except Exception as err:
|
||||||
|
# Logging as error/warn emits the event for all clients
|
||||||
|
plugin.log(f"{err}", "info")
|
||||||
|
disconnect()
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
def create_app():
|
||||||
|
|
|
@ -56,9 +56,14 @@ def verify_rune(plugin, request):
|
||||||
else:
|
else:
|
||||||
rpc_params = request.form.to_dict()
|
rpc_params = request.form.to_dict()
|
||||||
|
|
||||||
|
try:
|
||||||
|
rpc_method = request.view_args["rpc_method"]
|
||||||
|
except Exception:
|
||||||
|
rpc_method = ""
|
||||||
|
|
||||||
return call_rpc_method(plugin, "checkrune",
|
return call_rpc_method(plugin, "checkrune",
|
||||||
{"rune": rune,
|
{"rune": rune,
|
||||||
"method": request.view_args["rpc_method"],
|
"method": rpc_method,
|
||||||
"params": rpc_params})
|
"params": rpc_params})
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue