mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-12-14 16:16:21 +00:00
Update AppImage
This commit is contained in:
@@ -91,36 +91,44 @@ class ScriptRunner:
|
||||
|
||||
lines_read = [0] # Lista para compartir entre threads
|
||||
|
||||
# Monitor output for interactions
|
||||
def monitor_output():
|
||||
print(f"[DEBUG] monitor_output thread started for session {session_id}", file=sys.stderr, flush=True)
|
||||
print(f"[DEBUG] Will monitor log file: {log_file}", file=sys.stderr, flush=True)
|
||||
|
||||
try:
|
||||
with open(log_file, 'a') as log_f:
|
||||
while True:
|
||||
line = process.stdout.readline()
|
||||
if not line:
|
||||
print(f"[DEBUG] No more lines from stdout (EOF reached)", file=sys.stderr, flush=True)
|
||||
break
|
||||
|
||||
decoded_line = line.decode('utf-8', errors='replace').rstrip()
|
||||
lines_read[0] += 1
|
||||
|
||||
print(f"[DEBUG] Read line {lines_read[0]}: {decoded_line[:100]}...", file=sys.stderr, flush=True)
|
||||
|
||||
log_f.write(decoded_line + '\n')
|
||||
log_f.flush()
|
||||
|
||||
# Check for interaction requests
|
||||
try:
|
||||
if decoded_line.strip().startswith('{'):
|
||||
data = json.loads(decoded_line.strip())
|
||||
if data.get('type') == 'interaction_request':
|
||||
session['pending_interaction'] = data
|
||||
print(f"[DEBUG] Detected interaction request: {data}", file=sys.stderr, flush=True)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
# Read log file in real-time (similar to tail -f)
|
||||
last_position = 0
|
||||
|
||||
# Wait a moment for script to start writing
|
||||
time.sleep(0.5)
|
||||
|
||||
while process.poll() is None or last_position < os.path.getsize(log_file):
|
||||
try:
|
||||
if os.path.exists(log_file):
|
||||
with open(log_file, 'r') as log_f:
|
||||
log_f.seek(last_position)
|
||||
new_lines = log_f.readlines()
|
||||
|
||||
for line in new_lines:
|
||||
decoded_line = line.rstrip()
|
||||
if decoded_line: # Skip empty lines
|
||||
lines_read[0] += 1
|
||||
print(f"[DEBUG] Read line {lines_read[0]} from log: {decoded_line[:100]}...", file=sys.stderr, flush=True)
|
||||
|
||||
# Check for interaction requests in the line
|
||||
if 'WEB_INTERACTION:' in decoded_line:
|
||||
print(f"[DEBUG] Detected WEB_INTERACTION line: {decoded_line}", file=sys.stderr, flush=True)
|
||||
session['pending_interaction'] = decoded_line
|
||||
|
||||
last_position = log_f.tell()
|
||||
|
||||
except Exception as e:
|
||||
print(f"[DEBUG ERROR] Error reading log file: {e}", file=sys.stderr, flush=True)
|
||||
|
||||
time.sleep(0.1) # Poll every 100ms
|
||||
|
||||
print(f"[DEBUG] monitor_output thread finished. Total lines read: {lines_read[0]}", file=sys.stderr, flush=True)
|
||||
|
||||
except Exception as e:
|
||||
print(f"[DEBUG ERROR] Exception in monitor_output: {e}", file=sys.stderr, flush=True)
|
||||
|
||||
|
||||
@@ -236,6 +236,169 @@ def terminal_websocket(ws):
|
||||
if session_id in active_sessions:
|
||||
del active_sessions[session_id]
|
||||
|
||||
@sock.route('/ws/script/<session_id>')
|
||||
def script_websocket(ws, session_id):
|
||||
"""WebSocket endpoint for executing scripts with hybrid web mode"""
|
||||
|
||||
# Receive initial script execution request
|
||||
try:
|
||||
init_data = ws.receive(timeout=5)
|
||||
if not init_data:
|
||||
ws.send(json.dumps({'type': 'error', 'message': 'No script data received'}))
|
||||
return
|
||||
|
||||
script_data = json.loads(init_data)
|
||||
script_path = script_data.get('script_path')
|
||||
params = script_data.get('params', {})
|
||||
|
||||
if not script_path:
|
||||
ws.send(json.dumps({'type': 'error', 'message': 'No script_path provided'}))
|
||||
return
|
||||
|
||||
except Exception as e:
|
||||
ws.send(json.dumps({'type': 'error', 'message': f'Invalid init data: {str(e)}'}))
|
||||
return
|
||||
|
||||
# Create pseudo-terminal for script execution
|
||||
master_fd, slave_fd = pty.openpty()
|
||||
|
||||
# Build environment variables from params
|
||||
env = os.environ.copy()
|
||||
for key, value in params.items():
|
||||
env[key] = str(value)
|
||||
|
||||
# Start script process with PTY
|
||||
script_process = subprocess.Popen(
|
||||
['/bin/bash', script_path],
|
||||
stdin=slave_fd,
|
||||
stdout=slave_fd,
|
||||
stderr=slave_fd,
|
||||
preexec_fn=os.setsid,
|
||||
env=env
|
||||
)
|
||||
|
||||
# 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 terminal size
|
||||
set_winsize(master_fd, 30, 120)
|
||||
|
||||
# Thread to read script output and forward to WebSocket
|
||||
def read_script_output():
|
||||
buffer = ""
|
||||
while True:
|
||||
try:
|
||||
r, _, _ = select.select([master_fd], [], [], 0.01)
|
||||
if master_fd in r:
|
||||
try:
|
||||
data = os.read(master_fd, 4096)
|
||||
if not data:
|
||||
break
|
||||
|
||||
text = data.decode('utf-8', errors='ignore')
|
||||
buffer += text
|
||||
|
||||
# Process line by line to detect WEB_INTERACTION
|
||||
lines = buffer.split('\n')
|
||||
buffer = lines[-1] # Keep incomplete line in buffer
|
||||
|
||||
for line in lines[:-1]:
|
||||
# Detect WEB_INTERACTION lines
|
||||
if line.strip().startswith('WEB_INTERACTION:'):
|
||||
# Parse interaction: WEB_INTERACTION:type:id:title_b64:text_b64:data_b64
|
||||
try:
|
||||
parts = line.strip().split(':', 6)
|
||||
if len(parts) >= 5:
|
||||
interaction = {
|
||||
'type': 'interaction',
|
||||
'interaction_type': parts[1],
|
||||
'interaction_id': parts[2],
|
||||
'title': parts[3],
|
||||
'text': parts[4],
|
||||
'data': parts[5] if len(parts) > 5 else ''
|
||||
}
|
||||
ws.send(json.dumps(interaction))
|
||||
continue
|
||||
except Exception as e:
|
||||
print(f"Error parsing WEB_INTERACTION: {e}")
|
||||
|
||||
# Regular output line
|
||||
ws.send(json.dumps({
|
||||
'type': 'output',
|
||||
'data': line + '\n'
|
||||
}))
|
||||
except OSError:
|
||||
break
|
||||
except Exception as e:
|
||||
print(f"Error reading script output: {e}")
|
||||
break
|
||||
|
||||
# Send completion message
|
||||
exit_code = script_process.poll()
|
||||
ws.send(json.dumps({
|
||||
'type': 'exit',
|
||||
'code': exit_code if exit_code is not None else 0
|
||||
}))
|
||||
|
||||
output_thread = threading.Thread(target=read_script_output, daemon=True)
|
||||
output_thread.start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Receive user input or interaction responses
|
||||
data = ws.receive(timeout=None)
|
||||
|
||||
if data is None:
|
||||
break
|
||||
|
||||
try:
|
||||
msg = json.loads(data)
|
||||
|
||||
# Handle resize
|
||||
if msg.get('type') == 'resize':
|
||||
cols = int(msg.get('cols', 120))
|
||||
rows = int(msg.get('rows', 30))
|
||||
set_winsize(master_fd, rows, cols)
|
||||
continue
|
||||
|
||||
# Handle interaction response
|
||||
if msg.get('type') == 'response':
|
||||
response_value = msg.get('value', '')
|
||||
# Write response to script's stdin
|
||||
os.write(master_fd, (str(response_value) + '\n').encode('utf-8'))
|
||||
continue
|
||||
|
||||
except json.JSONDecodeError:
|
||||
# Raw text input, send to script
|
||||
os.write(master_fd, data.encode('utf-8'))
|
||||
|
||||
# Check if process is still alive
|
||||
if script_process.poll() is not None:
|
||||
break
|
||||
|
||||
except Exception as e:
|
||||
print(f"Script session error: {e}")
|
||||
finally:
|
||||
# Cleanup
|
||||
try:
|
||||
script_process.terminate()
|
||||
script_process.wait(timeout=1)
|
||||
except:
|
||||
try:
|
||||
script_process.kill()
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
os.close(master_fd)
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
os.close(slave_fd)
|
||||
except:
|
||||
pass
|
||||
|
||||
def init_terminal_routes(app):
|
||||
"""Initialize terminal routes with Flask app"""
|
||||
|
||||
Reference in New Issue
Block a user