mirror of
https://github.com/lnbits/lnbits.git
synced 2025-06-02 03:02:35 +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))
|
||||
except WebSocketDisconnect as 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:
|
||||
|
||||
def __init__(self, websocket: WebSocket):
|
||||
def __init__(self, websocket: Optional[WebSocket] = None):
|
||||
self.ws = websocket
|
||||
|
||||
def reconect_ws(self, websocket: WebSocket):
|
||||
@ -28,11 +28,13 @@ class HTTPTunnelClient:
|
||||
timeout: Optional[int] = None,
|
||||
) -> "HTTPTunnelResponse":
|
||||
try:
|
||||
assert self.ws, "Websocket connection not established."
|
||||
body = data
|
||||
if json:
|
||||
body = json.dumps(json)
|
||||
self.ws.send_json(
|
||||
{
|
||||
"request_id": "abc-123",
|
||||
"method": method,
|
||||
"url": url,
|
||||
"body": body,
|
||||
@ -168,11 +170,13 @@ class HTTPTunnelClient:
|
||||
)
|
||||
|
||||
async def aclose(self) -> None:
|
||||
pass
|
||||
if self.ws:
|
||||
self.ws.close()
|
||||
|
||||
|
||||
class HTTPTunnelResponse:
|
||||
|
||||
# status code, detail
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
@ -3,9 +3,13 @@ from __future__ import annotations
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import TYPE_CHECKING, AsyncGenerator, Coroutine, NamedTuple, Optional
|
||||
|
||||
from lnbits.utils.gateway import HTTPTunnelClient
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from lnbits.nodes.base import Node
|
||||
|
||||
http_tunnel_client = HTTPTunnelClient()
|
||||
|
||||
|
||||
class StatusResponse(NamedTuple):
|
||||
error_message: Optional[str]
|
||||
|
@ -16,6 +16,7 @@ from .base import (
|
||||
PaymentSuccessStatus,
|
||||
StatusResponse,
|
||||
Wallet,
|
||||
http_tunnel_client,
|
||||
)
|
||||
|
||||
|
||||
@ -37,7 +38,8 @@ class LNbitsWallet(Wallet):
|
||||
)
|
||||
self.endpoint = self.normalize_endpoint(settings.lnbits_endpoint)
|
||||
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):
|
||||
try:
|
||||
@ -47,6 +49,9 @@ class LNbitsWallet(Wallet):
|
||||
|
||||
async def status(self) -> StatusResponse:
|
||||
try:
|
||||
if not self.client.ws:
|
||||
return StatusResponse(None, 333)
|
||||
|
||||
r = await self.client.get(url="/api/v1/wallet", timeout=15)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
|
Loading…
x
Reference in New Issue
Block a user