mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-12-15 00:26:23 +00:00
Update AppImage
This commit is contained in:
@@ -43,6 +43,17 @@ export function ScriptTerminalModal({
|
|||||||
const [interactionInput, setInteractionInput] = useState("")
|
const [interactionInput, setInteractionInput] = useState("")
|
||||||
const terminalRef = useRef<any>(null)
|
const terminalRef = useRef<any>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open) {
|
||||||
|
console.log("[v0] ScriptTerminalModal opened with:", {
|
||||||
|
scriptPath,
|
||||||
|
scriptName,
|
||||||
|
params,
|
||||||
|
sessionId,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [open, scriptPath, scriptName, params, sessionId])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!open) return
|
if (!open) return
|
||||||
|
|
||||||
@@ -63,6 +74,13 @@ export function ScriptTerminalModal({
|
|||||||
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sessionId}`
|
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sessionId}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const wsUrl = getScriptWebSocketUrl()
|
||||||
|
console.log("[v0] ScriptTerminalModal WebSocket URL:", wsUrl)
|
||||||
|
console.log("[v0] ScriptTerminalModal initMessage:", {
|
||||||
|
script_path: scriptPath,
|
||||||
|
params: params,
|
||||||
|
})
|
||||||
|
|
||||||
const handleInteractionResponse = (value: string) => {
|
const handleInteractionResponse = (value: string) => {
|
||||||
if (!terminalRef.current || !currentInteraction) return
|
if (!terminalRef.current || !currentInteraction) return
|
||||||
|
|
||||||
@@ -109,7 +127,7 @@ export function ScriptTerminalModal({
|
|||||||
<div className="flex-1 overflow-hidden">
|
<div className="flex-1 overflow-hidden">
|
||||||
<TerminalPanel
|
<TerminalPanel
|
||||||
ref={terminalRef}
|
ref={terminalRef}
|
||||||
websocketUrl={getScriptWebSocketUrl()}
|
websocketUrl={wsUrl}
|
||||||
initMessage={{
|
initMessage={{
|
||||||
script_path: scriptPath,
|
script_path: scriptPath,
|
||||||
params: params,
|
params: params,
|
||||||
|
|||||||
@@ -240,26 +240,51 @@ def terminal_websocket(ws):
|
|||||||
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"""
|
||||||
|
|
||||||
|
print(f"[DEBUG] Script WebSocket connected for session: {session_id}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
print(f"[DEBUG] Waiting for init data...")
|
||||||
init_data = ws.receive(timeout=10)
|
init_data = ws.receive(timeout=10)
|
||||||
|
print(f"[DEBUG] Received init data: {init_data}")
|
||||||
|
|
||||||
if not init_data:
|
if not init_data:
|
||||||
ws.send('{"type": "error", "message": "No script data received"}\r\n')
|
error_msg = '{"type": "error", "message": "No script data received"}\r\n'
|
||||||
|
print(f"[DEBUG] Error: No init data received")
|
||||||
|
ws.send(error_msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
script_data = json.loads(init_data)
|
script_data = json.loads(init_data)
|
||||||
|
print(f"[DEBUG] Parsed script data: {script_data}")
|
||||||
|
|
||||||
script_path = script_data.get('script_path')
|
script_path = script_data.get('script_path')
|
||||||
params = script_data.get('params', {})
|
params = script_data.get('params', {})
|
||||||
|
|
||||||
|
print(f"[DEBUG] Script path: {script_path}")
|
||||||
|
print(f"[DEBUG] Params: {params}")
|
||||||
|
|
||||||
if not script_path:
|
if not script_path:
|
||||||
ws.send('{"type": "error", "message": "No script_path provided"}\r\n')
|
error_msg = '{"type": "error", "message": "No script_path provided"}\r\n'
|
||||||
|
print(f"[DEBUG] Error: No script_path in data")
|
||||||
|
ws.send(error_msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if not os.path.exists(script_path):
|
||||||
|
error_msg = f'{{"type": "error", "message": "Script not found: {script_path}"}}\r\n'
|
||||||
|
print(f"[DEBUG] Error: Script file not found: {script_path}")
|
||||||
|
ws.send(error_msg)
|
||||||
|
return
|
||||||
|
|
||||||
|
print(f"[DEBUG] Script file exists, starting execution...")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ws.send(f'{{"type": "error", "message": "Invalid init data: {str(e)}"}}\r\n')
|
error_msg = f'{{"type": "error", "message": "Invalid init data: {str(e)}"}}\r\n'
|
||||||
|
print(f"[DEBUG] Exception parsing init data: {e}")
|
||||||
|
ws.send(error_msg)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Create pseudo-terminal for script execution
|
# Create pseudo-terminal for script execution
|
||||||
master_fd, slave_fd = pty.openpty()
|
master_fd, slave_fd = pty.openpty()
|
||||||
|
print(f"[DEBUG] Created PTY: master_fd={master_fd}, slave_fd={slave_fd}")
|
||||||
|
|
||||||
# Build environment variables from params
|
# Build environment variables from params
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
@@ -267,6 +292,7 @@ def script_websocket(ws, session_id):
|
|||||||
env[key] = str(value)
|
env[key] = str(value)
|
||||||
|
|
||||||
# Start script process with PTY
|
# Start script process with PTY
|
||||||
|
print(f"[DEBUG] Starting script process: {script_path}")
|
||||||
script_process = subprocess.Popen(
|
script_process = subprocess.Popen(
|
||||||
['/bin/bash', script_path],
|
['/bin/bash', script_path],
|
||||||
stdin=slave_fd,
|
stdin=slave_fd,
|
||||||
@@ -275,6 +301,7 @@ def script_websocket(ws, session_id):
|
|||||||
preexec_fn=os.setsid,
|
preexec_fn=os.setsid,
|
||||||
env=env
|
env=env
|
||||||
)
|
)
|
||||||
|
print(f"[DEBUG] Script process started with PID: {script_process.pid}")
|
||||||
|
|
||||||
# Set non-blocking mode for master_fd
|
# Set non-blocking mode for master_fd
|
||||||
flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
|
flags = fcntl.fcntl(master_fd, fcntl.F_GETFL)
|
||||||
@@ -285,6 +312,7 @@ def script_websocket(ws, session_id):
|
|||||||
|
|
||||||
# Thread to read script output and forward to WebSocket
|
# Thread to read script output and forward to WebSocket
|
||||||
def read_script_output():
|
def read_script_output():
|
||||||
|
print(f"[DEBUG] Output reader thread started")
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
r, _, _ = select.select([master_fd], [], [], 0.01)
|
r, _, _ = select.select([master_fd], [], [], 0.01)
|
||||||
@@ -292,33 +320,39 @@ def script_websocket(ws, session_id):
|
|||||||
try:
|
try:
|
||||||
data = os.read(master_fd, 4096)
|
data = os.read(master_fd, 4096)
|
||||||
if not data:
|
if not data:
|
||||||
|
print(f"[DEBUG] No more data from script")
|
||||||
break
|
break
|
||||||
|
|
||||||
text = data.decode('utf-8', errors='ignore')
|
text = data.decode('utf-8', errors='ignore')
|
||||||
|
print(f"[DEBUG] Read {len(text)} chars from script")
|
||||||
|
|
||||||
# Send raw text to terminal (TerminalPanel expects plain text)
|
# Send raw text to terminal (TerminalPanel expects plain text)
|
||||||
try:
|
try:
|
||||||
ws.send(text)
|
ws.send(text)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
|
print(f"[DEBUG] Error sending to WebSocket: {e}")
|
||||||
break
|
break
|
||||||
|
|
||||||
except OSError:
|
except OSError as e:
|
||||||
|
print(f"[DEBUG] OSError reading from PTY: {e}")
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error reading script output: {e}")
|
print(f"[DEBUG] Error reading script output: {e}")
|
||||||
break
|
break
|
||||||
|
|
||||||
script_process.wait()
|
script_process.wait()
|
||||||
exit_code = script_process.returncode if script_process.returncode is not None else 0
|
exit_code = script_process.returncode if script_process.returncode is not None else 0
|
||||||
|
print(f"[DEBUG] Script exited with code: {exit_code}")
|
||||||
|
|
||||||
# Only send exit message if WebSocket is still open
|
# Only send exit message if WebSocket is still open
|
||||||
try:
|
try:
|
||||||
ws.send(f'\r\n[Script exited with code {exit_code}]\r\n')
|
ws.send(f'\r\n[Script exited with code {exit_code}]\r\n')
|
||||||
except Exception:
|
except Exception as e:
|
||||||
pass
|
print(f"[DEBUG] Could not send exit message: {e}")
|
||||||
|
|
||||||
output_thread = threading.Thread(target=read_script_output, daemon=True)
|
output_thread = threading.Thread(target=read_script_output, daemon=True)
|
||||||
output_thread.start()
|
output_thread.start()
|
||||||
|
print(f"[DEBUG] Output thread started")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -326,8 +360,11 @@ def script_websocket(ws, session_id):
|
|||||||
data = ws.receive(timeout=None)
|
data = ws.receive(timeout=None)
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
|
print(f"[DEBUG] WebSocket closed by client")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
print(f"[DEBUG] Received from client: {data[:100] if len(data) > 100 else data}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg = json.loads(data)
|
msg = json.loads(data)
|
||||||
|
|
||||||
@@ -336,22 +373,27 @@ def script_websocket(ws, session_id):
|
|||||||
cols = int(msg.get('cols', 120))
|
cols = int(msg.get('cols', 120))
|
||||||
rows = int(msg.get('rows', 30))
|
rows = int(msg.get('rows', 30))
|
||||||
set_winsize(master_fd, rows, cols)
|
set_winsize(master_fd, rows, cols)
|
||||||
|
print(f"[DEBUG] Resized terminal to {cols}x{rows}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
# Raw text input, send to script
|
# Raw text input, send to script
|
||||||
try:
|
try:
|
||||||
os.write(master_fd, data.encode('utf-8'))
|
os.write(master_fd, data.encode('utf-8'))
|
||||||
except OSError:
|
print(f"[DEBUG] Sent input to script")
|
||||||
|
except OSError as e:
|
||||||
|
print(f"[DEBUG] Error writing to script: {e}")
|
||||||
break
|
break
|
||||||
|
|
||||||
# Check if process is still alive
|
# Check if process is still alive
|
||||||
if script_process.poll() is not None:
|
if script_process.poll() is not None:
|
||||||
|
print(f"[DEBUG] Script process terminated")
|
||||||
break
|
break
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Script session error: Connection closed: {e}")
|
print(f"[DEBUG] Script session error: {e}")
|
||||||
finally:
|
finally:
|
||||||
|
print(f"[DEBUG] Cleaning up script session")
|
||||||
# Cleanup
|
# Cleanup
|
||||||
try:
|
try:
|
||||||
script_process.terminate()
|
script_process.terminate()
|
||||||
|
|||||||
Reference in New Issue
Block a user