-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathterm.py
More file actions
179 lines (146 loc) · 5.36 KB
/
term.py
File metadata and controls
179 lines (146 loc) · 5.36 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""
WebSocket terminal component for interactive shell sessions.
This module provides a WebSocket endpoint for xterm.js integration,
supporting PTY-based interactive shell sessions with terminal resize.
Each WebSocket connection creates a new PTY; disconnection destroys it.
"""
import asyncio
import os
import struct
import fcntl
import termios
import logging
import json
from typing import Optional
from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Query
from .user_manager import start_interactive_shell
logger = logging.getLogger(__name__)
router = APIRouter()
class TerminalSession:
"""Represents an active terminal session"""
def __init__(self, master_fd: int, pid: int):
self.master_fd = master_fd
self.pid = pid
self.websocket: Optional[WebSocket] = None
self._read_task: Optional[asyncio.Task] = None
self._closed = False
async def start_reader(self, websocket: WebSocket):
"""Start reading from PTY and sending to WebSocket"""
self.websocket = websocket
loop = asyncio.get_event_loop()
def read_pty():
"""Blocking read from PTY"""
try:
return os.read(self.master_fd, 4096)
except OSError:
return b""
while not self._closed:
try:
# Read from PTY in a thread to avoid blocking
data = await loop.run_in_executor(None, read_pty)
if not data:
logger.info("PTY closed")
break
# Send to WebSocket as text (xterm expects text)
await websocket.send_text(data.decode("utf-8", errors="replace"))
except WebSocketDisconnect:
logger.info("WebSocket disconnected")
break
except Exception as e:
logger.error(f"Error reading from PTY: {e}")
break
async def write(self, data: str):
"""Write data to PTY"""
try:
os.write(self.master_fd, data.encode("utf-8"))
except OSError as e:
logger.error(f"Error writing to PTY: {e}")
def resize(self, cols: int, rows: int):
"""Resize the terminal"""
try:
winsize = struct.pack("HHHH", rows, cols, 0, 0)
fcntl.ioctl(self.master_fd, termios.TIOCSWINSZ, winsize)
logger.debug(f"Resized terminal to {cols}x{rows}")
except Exception as e:
logger.error(f"Error resizing terminal: {e}")
def close(self):
"""Close the terminal session"""
if self._closed:
return
self._closed = True
try:
os.close(self.master_fd)
except OSError:
pass
# Try to terminate the child process
try:
os.kill(self.pid, 9) # SIGKILL
os.waitpid(self.pid, os.WNOHANG)
except OSError:
pass
logger.info("Closed terminal session")
@router.websocket("/ws")
async def websocket_terminal(
websocket: WebSocket,
cols: int = Query(80),
rows: int = Query(24),
):
"""
WebSocket endpoint for interactive terminal.
Query parameters:
- cols: Terminal columns (default 80)
- rows: Terminal rows (default 24)
Messages from client:
- Text: Input data to send to PTY
- JSON: {"type": "resize", "cols": <int>, "rows": <int>}
"""
await websocket.accept()
terminal: Optional[TerminalSession] = None
try:
# Start interactive shell
master_fd, pid = await start_interactive_shell(cols=cols, rows=rows)
terminal = TerminalSession(master_fd, pid)
logger.info("Terminal session started")
# Start PTY reader in background
read_task = asyncio.create_task(terminal.start_reader(websocket))
# Handle incoming WebSocket messages
while True:
try:
message = await websocket.receive()
if message["type"] == "websocket.disconnect":
break
if "text" in message:
text = message["text"]
# Check if it's a control message
if text.startswith("{"):
try:
data = json.loads(text)
if data.get("type") == "resize":
terminal.resize(
data.get("cols", 80), data.get("rows", 24)
)
continue
except json.JSONDecodeError:
pass
# Regular input data
await terminal.write(text)
elif "bytes" in message:
# Binary data (also valid input)
await terminal.write(
message["bytes"].decode("utf-8", errors="replace")
)
except WebSocketDisconnect:
break
except Exception as e:
logger.error(f"Error handling WebSocket message: {e}")
break
except Exception as e:
logger.error(f"Error in terminal WebSocket: {e}")
try:
await websocket.close(code=1011, reason=str(e))
except Exception:
pass
finally:
# Cleanup
if terminal:
terminal.close()