-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
77 lines (63 loc) · 2.06 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import logging
import asyncio
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from guacamole.client import GuacamoleClient
from guacamole.instruction import Instruction
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
WEBSOCKETS = {}
@app.get("/", response_class=HTMLResponse)
def get(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
async def guacd_to_client(websocket: WebSocket, client: GuacamoleClient):
while True:
instruction = await client.read()
if instruction.error:
logging.error(f"{instruction.short_description}-{instruction.description}")
await websocket.send_text(str(instruction))
@app.websocket("/websocket/")
async def websocket_endpoint(
websocket: WebSocket,
guacd_host: str,
guacd_port: str,
protocol: str,
remote_host: str,
remote_port: str,
username: str,
password: str,
):
await websocket.accept(subprotocol="guacamole")
client = GuacamoleClient(
guacd_host,
guacd_port,
{
"protocol": protocol,
"size": [1024, 768, 96],
"audio": [],
"video": [],
"image": [],
"args": {
"hostname": remote_host,
"port": remote_port,
"username": username,
"password": password,
},
},
debug=True,
)
await client.connect()
await client.handshake()
task = asyncio.get_event_loop().create_task(guacd_to_client(websocket, client))
try:
while True:
data = await websocket.receive_text()
instruction = Instruction.from_string(data)
await client.send(str(instruction))
except WebSocketDisconnect:
task.cancel()
await client.close()
await asyncio.wait([task])