mirror of
https://github.com/lnbits/lnbits.git
synced 2025-06-21 06:12:03 +02:00
feat: basic tunnel
This commit is contained in:
parent
3e1e408999
commit
31078f95ea
@ -60,3 +60,18 @@ def enable_ws_tunnel_for_routers(routers: APIRouter):
|
|||||||
await websocket.send_text(json.dumps(resp))
|
await websocket.send_text(json.dumps(resp))
|
||||||
except WebSocketDisconnect as exc:
|
except WebSocketDisconnect as exc:
|
||||||
logger.warning(exc)
|
logger.warning(exc)
|
||||||
|
|
||||||
|
|
||||||
|
@routers.websocket("/api/v1/feeder")
|
||||||
|
async def websocket_feeder(websocket: WebSocket):
|
||||||
|
try:
|
||||||
|
await websocket.accept()
|
||||||
|
|
||||||
|
while settings.lnbits_running:
|
||||||
|
req = await websocket.receive_text()
|
||||||
|
|
||||||
|
resp = await HTTPInternalCall(routers)(req)
|
||||||
|
|
||||||
|
await websocket.send_text(json.dumps(resp))
|
||||||
|
except WebSocketDisconnect as exc:
|
||||||
|
logger.warning(exc)
|
||||||
|
@ -10,7 +10,7 @@ from loguru import logger
|
|||||||
|
|
||||||
class HTTPTunnelClient:
|
class HTTPTunnelClient:
|
||||||
|
|
||||||
def __init__(self, websocket: WebSocket):
|
def __init__(self, websocket: Optional[WebSocket] = None):
|
||||||
self.ws = websocket
|
self.ws = websocket
|
||||||
|
|
||||||
def reconect_ws(self, websocket: WebSocket):
|
def reconect_ws(self, websocket: WebSocket):
|
||||||
@ -28,11 +28,13 @@ class HTTPTunnelClient:
|
|||||||
timeout: Optional[int] = None,
|
timeout: Optional[int] = None,
|
||||||
) -> "HTTPTunnelResponse":
|
) -> "HTTPTunnelResponse":
|
||||||
try:
|
try:
|
||||||
|
assert self.ws, "Websocket connection not established."
|
||||||
body = data
|
body = data
|
||||||
if json:
|
if json:
|
||||||
body = json.dumps(json)
|
body = json.dumps(json)
|
||||||
self.ws.send_json(
|
self.ws.send_json(
|
||||||
{
|
{
|
||||||
|
"request_id": "abc-123",
|
||||||
"method": method,
|
"method": method,
|
||||||
"url": url,
|
"url": url,
|
||||||
"body": body,
|
"body": body,
|
||||||
@ -168,11 +170,13 @@ class HTTPTunnelClient:
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def aclose(self) -> None:
|
async def aclose(self) -> None:
|
||||||
pass
|
if self.ws:
|
||||||
|
self.ws.close()
|
||||||
|
|
||||||
|
|
||||||
class HTTPTunnelResponse:
|
class HTTPTunnelResponse:
|
||||||
|
|
||||||
|
# status code, detail
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -3,9 +3,13 @@ from __future__ import annotations
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import TYPE_CHECKING, AsyncGenerator, Coroutine, NamedTuple, Optional
|
from typing import TYPE_CHECKING, AsyncGenerator, Coroutine, NamedTuple, Optional
|
||||||
|
|
||||||
|
from lnbits.utils.gateway import HTTPTunnelClient
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from lnbits.nodes.base import Node
|
from lnbits.nodes.base import Node
|
||||||
|
|
||||||
|
http_tunnel_client = HTTPTunnelClient()
|
||||||
|
|
||||||
|
|
||||||
class StatusResponse(NamedTuple):
|
class StatusResponse(NamedTuple):
|
||||||
error_message: Optional[str]
|
error_message: Optional[str]
|
||||||
|
@ -16,6 +16,7 @@ from .base import (
|
|||||||
PaymentSuccessStatus,
|
PaymentSuccessStatus,
|
||||||
StatusResponse,
|
StatusResponse,
|
||||||
Wallet,
|
Wallet,
|
||||||
|
http_tunnel_client,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -37,7 +38,8 @@ class LNbitsWallet(Wallet):
|
|||||||
)
|
)
|
||||||
self.endpoint = self.normalize_endpoint(settings.lnbits_endpoint)
|
self.endpoint = self.normalize_endpoint(settings.lnbits_endpoint)
|
||||||
self.headers = {"X-Api-Key": key, "User-Agent": settings.user_agent}
|
self.headers = {"X-Api-Key": key, "User-Agent": settings.user_agent}
|
||||||
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.headers)
|
# self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.headers)
|
||||||
|
self.client = http_tunnel_client
|
||||||
|
|
||||||
async def cleanup(self):
|
async def cleanup(self):
|
||||||
try:
|
try:
|
||||||
@ -47,6 +49,9 @@ class LNbitsWallet(Wallet):
|
|||||||
|
|
||||||
async def status(self) -> StatusResponse:
|
async def status(self) -> StatusResponse:
|
||||||
try:
|
try:
|
||||||
|
if not self.client.ws:
|
||||||
|
return StatusResponse(None, 333)
|
||||||
|
|
||||||
r = await self.client.get(url="/api/v1/wallet", timeout=15)
|
r = await self.client.get(url="/api/v1/wallet", timeout=15)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user