ozone-research commited on
Commit
9c03dbc
·
verified ·
1 Parent(s): 8c0c8d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -9
app.py CHANGED
@@ -5,13 +5,16 @@ import subprocess
5
  import shlex
6
 
7
  async def handle_terminal(websocket, path):
 
 
 
8
  try:
9
  async for message in websocket:
10
- # Extract data from WSMessage object
11
  command = shlex.split(message.data)
12
  if not command or command[0] in ['rm', 'sudo', 'halt', 'reboot']:
13
  await websocket.send_str("Command not allowed")
14
  continue
 
15
  try:
16
  process = await asyncio.create_subprocess_exec(
17
  *command,
@@ -19,7 +22,19 @@ async def handle_terminal(websocket, path):
19
  stderr=asyncio.subprocess.PIPE
20
  )
21
  async for line in process.stdout:
22
- await websocket.send_str(line.decode('utf-8').rstrip())
 
 
 
 
 
 
 
 
 
 
 
 
23
  async for line in process.stderr:
24
  await websocket.send_str(line.decode('utf-8').rstrip())
25
  await process.wait()
@@ -73,6 +88,7 @@ async def index(request):
73
  outline: none;
74
  width: 100%;
75
  caret-color: #fff;
 
76
  }
77
  </style>
78
  </head>
@@ -82,18 +98,30 @@ async def index(request):
82
  </div>
83
  <input id="input" type="text" autofocus>
84
  <script>
85
- // Use wss:// with Cloudflare handling the secure connection
86
  const ws = new WebSocket('wss://' + location.host + '/ws');
87
  const terminal = document.getElementById('terminal');
88
  const input = document.getElementById('input');
 
89
 
90
  ws.onopen = () => {
91
  input.focus();
92
  };
93
 
94
  ws.onmessage = (event) => {
95
- if (event.data === '') {
96
- // New prompt after command execution
 
 
 
 
 
 
 
 
 
 
 
 
97
  const newLine = document.createElement('div');
98
  newLine.className = 'line';
99
  newLine.textContent = 'zoyo@zoyos-MacBook-Air 10% ';
@@ -103,12 +131,11 @@ async def index(request):
103
  newLine.appendChild(prompt);
104
  terminal.appendChild(newLine);
105
  input.value = '';
106
- input.focus();
107
  } else {
108
- // Display output
109
  const output = document.createElement('div');
110
  output.className = 'line';
111
- output.textContent = event.data;
112
  terminal.appendChild(output);
113
  }
114
  terminal.scrollTop = terminal.scrollHeight;
@@ -123,7 +150,13 @@ async def index(request):
123
  };
124
 
125
  input.addEventListener('keydown', (event) => {
126
- if (event.key === 'Enter') {
 
 
 
 
 
 
127
  const command = input.value.trim();
128
  if (command) {
129
  ws.send(command);
 
5
  import shlex
6
 
7
  async def handle_terminal(websocket, path):
8
+ prompt_active = False
9
+ prompt_response = None
10
+
11
  try:
12
  async for message in websocket:
 
13
  command = shlex.split(message.data)
14
  if not command or command[0] in ['rm', 'sudo', 'halt', 'reboot']:
15
  await websocket.send_str("Command not allowed")
16
  continue
17
+
18
  try:
19
  process = await asyncio.create_subprocess_exec(
20
  *command,
 
22
  stderr=asyncio.subprocess.PIPE
23
  )
24
  async for line in process.stdout:
25
+ # Check if line contains a prompt
26
+ line_str = line.decode('utf-8').rstrip()
27
+ if line_str.endswith('[Y/n]') or line_str.endswith('[y/n]'):
28
+ await websocket.send_str(line_str)
29
+ prompt_active = True
30
+ # Wait for user response
31
+ while prompt_active:
32
+ prompt_response = await websocket.receive_str()
33
+ if prompt_response.lower() in ['y', 'n']:
34
+ prompt_active = False
35
+ await websocket.send_str("") # Signal end of prompt
36
+ else:
37
+ await websocket.send_str(line_str)
38
  async for line in process.stderr:
39
  await websocket.send_str(line.decode('utf-8').rstrip())
40
  await process.wait()
 
88
  outline: none;
89
  width: 100%;
90
  caret-color: #fff;
91
+ display: none; /* Hidden by default, shown on prompt */
92
  }
93
  </style>
94
  </head>
 
98
  </div>
99
  <input id="input" type="text" autofocus>
100
  <script>
 
101
  const ws = new WebSocket('wss://' + location.host + '/ws');
102
  const terminal = document.getElementById('terminal');
103
  const input = document.getElementById('input');
104
+ let awaitingPrompt = false;
105
 
106
  ws.onopen = () => {
107
  input.focus();
108
  };
109
 
110
  ws.onmessage = (event) => {
111
+ const data = event.data;
112
+ if (data.endsWith('[Y/n]') || data.endsWith('[y/n]')) {
113
+ // Display prompt and show input
114
+ const promptLine = document.createElement('div');
115
+ promptLine.className = 'line';
116
+ promptLine.textContent = data + ' ';
117
+ terminal.appendChild(promptLine);
118
+ input.style.display = 'block';
119
+ input.focus();
120
+ awaitingPrompt = true;
121
+ } else if (data === '') {
122
+ // End of prompt or command, hide input and show new prompt
123
+ input.style.display = 'none';
124
+ awaitingPrompt = false;
125
  const newLine = document.createElement('div');
126
  newLine.className = 'line';
127
  newLine.textContent = 'zoyo@zoyos-MacBook-Air 10% ';
 
131
  newLine.appendChild(prompt);
132
  terminal.appendChild(newLine);
133
  input.value = '';
 
134
  } else {
135
+ // Regular output
136
  const output = document.createElement('div');
137
  output.className = 'line';
138
+ output.textContent = data;
139
  terminal.appendChild(output);
140
  }
141
  terminal.scrollTop = terminal.scrollHeight;
 
150
  };
151
 
152
  input.addEventListener('keydown', (event) => {
153
+ if (event.key === 'Enter' && awaitingPrompt) {
154
+ const response = input.value.trim().toLowerCase();
155
+ if (response === 'y' || response === 'n') {
156
+ ws.send(response);
157
+ input.value = '';
158
+ }
159
+ } else if (event.key === 'Enter' && !awaitingPrompt) {
160
  const command = input.value.trim();
161
  if (command) {
162
  ws.send(command);