Update terminal modal

This commit is contained in:
MacRimi
2026-01-31 17:19:50 +01:00
parent 964538eb43
commit 3c3c902087
2 changed files with 137 additions and 37 deletions

View File

@@ -24,9 +24,9 @@ interface LxcTerminalModalProps {
vmName: string vmName: string
} }
function getWebSocketUrl(): string { function getWebSocketUrl(vmid: number): string {
if (typeof window === "undefined") { if (typeof window === "undefined") {
return "ws://localhost:8008/ws/terminal" return `ws://localhost:8008/ws/terminal/lxc/${vmid}`
} }
const { protocol, hostname, port } = window.location const { protocol, hostname, port } = window.location
@@ -34,9 +34,9 @@ function getWebSocketUrl(): string {
const wsProtocol = protocol === "https:" ? "wss:" : "ws:" const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
if (isStandardPort) { if (isStandardPort) {
return `${wsProtocol}//${hostname}/ws/terminal` return `${wsProtocol}//${hostname}/ws/terminal/lxc/${vmid}`
} else { } else {
return `${wsProtocol}//${hostname}:${API_PORT}/ws/terminal` return `${wsProtocol}//${hostname}:${API_PORT}/ws/terminal/lxc/${vmid}`
} }
} }
@@ -98,15 +98,11 @@ export function LxcTerminalModal({
// Small delay to ensure Dialog content is rendered // Small delay to ensure Dialog content is rendered
const initTimeout = setTimeout(() => { const initTimeout = setTimeout(() => {
if (!terminalContainerRef.current) { if (!terminalContainerRef.current) return
console.log("[v0] Terminal container not ready")
return
}
initTerminal() initTerminal()
}, 100) }, 100)
const initTerminal = async () => { const initTerminal = async () => {
console.log("[v0] Initializing LXC terminal for vmid:", vmid)
const [TerminalClass, FitAddonClass] = await Promise.all([ const [TerminalClass, FitAddonClass] = await Promise.all([
import("xterm").then((mod) => mod.Terminal), import("xterm").then((mod) => mod.Terminal),
import("xterm-addon-fit").then((mod) => mod.FitAddon), import("xterm-addon-fit").then((mod) => mod.FitAddon),
@@ -160,14 +156,12 @@ export function LxcTerminalModal({
termRef.current = term termRef.current = term
fitAddonRef.current = fitAddon fitAddonRef.current = fitAddon
// Connect WebSocket // Connect WebSocket - direct connection to LXC container
const wsUrl = getWebSocketUrl() const wsUrl = getWebSocketUrl(vmid)
console.log("[v0] LXC Terminal connecting to:", wsUrl)
const ws = new WebSocket(wsUrl) const ws = new WebSocket(wsUrl)
wsRef.current = ws wsRef.current = ws
ws.onopen = () => { ws.onopen = () => {
console.log("[v0] LXC Terminal WebSocket connected")
setConnectionStatus("online") setConnectionStatus("online")
// Start heartbeat ping // Start heartbeat ping
@@ -181,22 +175,13 @@ export function LxcTerminalModal({
} }
}, 25000) }, 25000)
// Sync terminal size first // Sync terminal size
fitAddon.fit() fitAddon.fit()
ws.send(JSON.stringify({ ws.send(JSON.stringify({
type: "resize", type: "resize",
cols: term.cols, cols: term.cols,
rows: term.rows, rows: term.rows,
})) }))
// Auto-execute pct enter command after terminal is ready
// Wait for shell prompt to appear before sending command
setTimeout(() => {
if (ws.readyState === WebSocket.OPEN) {
// Send the command as plain text like user would type
ws.send(`pct enter ${vmid}\r`)
}
}, 800)
} }
ws.onmessage = (event) => { ws.onmessage = (event) => {
@@ -207,14 +192,12 @@ export function LxcTerminalModal({
term.write(event.data) term.write(event.data)
} }
ws.onerror = (error) => { ws.onerror = () => {
console.log("[v0] LXC Terminal WebSocket error:", error)
setConnectionStatus("offline") setConnectionStatus("offline")
term.writeln("\r\n\x1b[31m[ERROR] WebSocket connection error\x1b[0m") term.writeln("\r\n\x1b[31m[ERROR] WebSocket connection error\x1b[0m")
} }
ws.onclose = (event) => { ws.onclose = () => {
console.log("[v0] LXC Terminal WebSocket closed:", event.code, event.reason)
setConnectionStatus("offline") setConnectionStatus("offline")
if (pingIntervalRef.current) { if (pingIntervalRef.current) {
clearInterval(pingIntervalRef.current) clearInterval(pingIntervalRef.current)
@@ -397,13 +380,13 @@ export function LxcTerminalModal({
{/* Mobile/Tablet control buttons */} {/* Mobile/Tablet control buttons */}
{showMobileControls && ( {showMobileControls && (
<div className="px-3 py-2 bg-zinc-900 border-t border-zinc-800"> <div className="px-2 py-2 bg-zinc-900 border-t border-zinc-800">
<div className="flex items-center justify-center gap-2 flex-wrap"> <div className="flex items-center justify-center gap-1.5">
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendEsc} onClick={sendEsc}
className="h-9 px-3 bg-zinc-800 border-zinc-700 text-zinc-300" className="h-8 px-2 text-xs bg-zinc-800 border-zinc-700 text-zinc-300"
> >
ESC ESC
</Button> </Button>
@@ -411,7 +394,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendTab} onClick={sendTab}
className="h-9 px-3 bg-zinc-800 border-zinc-700 text-zinc-300" className="h-8 px-2 text-xs bg-zinc-800 border-zinc-700 text-zinc-300"
> >
TAB TAB
</Button> </Button>
@@ -419,7 +402,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendArrowUp} onClick={sendArrowUp}
className="h-9 w-9 p-0 bg-zinc-800 border-zinc-700" className="h-8 w-8 p-0 bg-zinc-800 border-zinc-700"
> >
<ArrowUp className="h-4 w-4" /> <ArrowUp className="h-4 w-4" />
</Button> </Button>
@@ -427,7 +410,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendArrowDown} onClick={sendArrowDown}
className="h-9 w-9 p-0 bg-zinc-800 border-zinc-700" className="h-8 w-8 p-0 bg-zinc-800 border-zinc-700"
> >
<ArrowDown className="h-4 w-4" /> <ArrowDown className="h-4 w-4" />
</Button> </Button>
@@ -435,7 +418,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendArrowLeft} onClick={sendArrowLeft}
className="h-9 w-9 p-0 bg-zinc-800 border-zinc-700" className="h-8 w-8 p-0 bg-zinc-800 border-zinc-700"
> >
<ArrowLeft className="h-4 w-4" /> <ArrowLeft className="h-4 w-4" />
</Button> </Button>
@@ -443,7 +426,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendArrowRight} onClick={sendArrowRight}
className="h-9 w-9 p-0 bg-zinc-800 border-zinc-700" className="h-8 w-8 p-0 bg-zinc-800 border-zinc-700"
> >
<ArrowRight className="h-4 w-4" /> <ArrowRight className="h-4 w-4" />
</Button> </Button>
@@ -451,7 +434,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={sendEnter} onClick={sendEnter}
className="h-9 px-3 bg-blue-600/20 border-blue-600/50 text-blue-400 hover:bg-blue-600/30" className="h-8 px-2 text-xs bg-blue-600/20 border-blue-600/50 text-blue-400 hover:bg-blue-600/30"
> >
<CornerDownLeft className="h-4 w-4 mr-1" /> <CornerDownLeft className="h-4 w-4 mr-1" />
Enter Enter
@@ -460,7 +443,7 @@ export function LxcTerminalModal({
variant="outline" variant="outline"
size="sm" size="sm"
onClick={handleCtrlPress} onClick={handleCtrlPress}
className={`h-9 px-3 ${ctrlPressed className={`h-8 px-2 text-xs ${ctrlPressed
? "bg-yellow-600/30 border-yellow-600/50 text-yellow-400" ? "bg-yellow-600/30 border-yellow-600/50 text-yellow-400"
: "bg-zinc-800 border-zinc-700 text-zinc-300"}`} : "bg-zinc-800 border-zinc-700 text-zinc-300"}`}
> >

View File

@@ -250,6 +250,123 @@ def terminal_websocket(ws):
if session_id in active_sessions: if session_id in active_sessions:
del active_sessions[session_id] del active_sessions[session_id]
@sock.route('/ws/terminal/lxc/<int:vmid>')
def lxc_terminal_websocket(ws, vmid):
"""WebSocket endpoint for direct LXC container terminal sessions"""
# Create pseudo-terminal
master_fd, slave_fd = pty.openpty()
# Start pct enter directly to connect to the LXC container
shell_process = subprocess.Popen(
['/usr/bin/pct', 'enter', str(vmid)],
stdin=slave_fd,
stdout=slave_fd,
stderr=slave_fd,
preexec_fn=os.setsid,
cwd='/',
env=dict(os.environ, TERM='xterm-256color')
)
session_id = id(ws)
active_sessions[session_id] = {
'process': shell_process,
'master_fd': master_fd,
'vmid': vmid
}
# Set non-blocking mode for master_fd
flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
fcntl.fcntl(master_fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
# Set initial terminal size
set_winsize(master_fd, 30, 120)
# Start thread to read PTY output and forward to WebSocket
output_thread = threading.Thread(
target=read_and_forward_output,
args=(master_fd, ws),
daemon=True
)
output_thread.start()
try:
while True:
data = ws.receive(timeout=None)
if data is None:
break
handled = False
if isinstance(data, str):
try:
msg = json.loads(data)
except Exception:
msg = None
if isinstance(msg, dict):
msg_type = msg.get('type')
if msg_type == 'ping':
try:
ws.send(json.dumps({'type': 'pong'}))
except:
pass
handled = True
elif msg_type == 'resize':
cols = int(msg.get('cols', 120))
rows = int(msg.get('rows', 30))
set_winsize(master_fd, rows, cols)
handled = True
if handled:
continue
if isinstance(data, str) and data.startswith('\x1b[8;'):
try:
parts = data[4:-1].split(';')
rows, cols = int(parts[0]), int(parts[1])
set_winsize(master_fd, rows, cols)
continue
except Exception:
pass
try:
os.write(master_fd, data.encode('utf-8'))
except OSError as e:
print(f"Error writing to LXC PTY: {e}")
break
if shell_process.poll() is not None:
break
except Exception as e:
print(f"LXC terminal session error: {e}")
finally:
try:
shell_process.terminate()
shell_process.wait(timeout=1)
except:
try:
shell_process.kill()
except:
pass
try:
os.close(master_fd)
except:
pass
try:
os.close(slave_fd)
except:
pass
if session_id in active_sessions:
del active_sessions[session_id]
@sock.route('/ws/script/<session_id>') @sock.route('/ws/script/<session_id>')
def script_websocket(ws, session_id): def script_websocket(ws, session_id):
"""WebSocket endpoint for executing scripts with hybrid web mode""" """WebSocket endpoint for executing scripts with hybrid web mode"""