wop commited on
Commit
2ceaca7
·
verified ·
1 Parent(s): 8eb72ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -5,11 +5,13 @@ import time
5
  status_file = "status.json"
6
  command_file = "command.json"
7
 
 
 
8
  # Initialize storage files
9
  with open(status_file, "w") as f:
10
  json.dump({}, f)
11
  with open(command_file, "w") as f:
12
- json.dump({}, f)
13
 
14
  # Register PC as online
15
  def register_pc(name, password):
@@ -24,7 +26,7 @@ def register_pc(name, password):
24
  def check_pc(name):
25
  with open(status_file, "r") as f:
26
  status = json.load(f)
27
- if name in status and time.time() - status[name]["timestamp"] < 60:
28
  return "Online"
29
  return "Offline"
30
 
@@ -37,7 +39,7 @@ def send_command(name, password, command):
37
  if name in status and status[name]["password"] == password:
38
  with open(command_file, "w") as f:
39
  json.dump({"command": command}, f)
40
- return f"Command '{command}' sent to {name}."
41
  return "Authentication failed."
42
 
43
  # Get command (for PC side polling)
@@ -55,12 +57,12 @@ with gr.Blocks() as ui:
55
  check_btn.click(check_pc, inputs=name_input, outputs=check_output)
56
 
57
  with gr.Row():
58
- cmd_input = gr.Textbox(label="Command")
59
  send_btn = gr.Button("Send Command")
60
- send_output = gr.Textbox(label="Response")
61
  send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output)
62
 
63
- # Hidden components to expose the get_command API endpoint.
64
  hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False)
65
  hidden_cmd_output = gr.Textbox(visible=False)
66
  hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command")
 
5
  status_file = "status.json"
6
  command_file = "command.json"
7
 
8
+ HEARTBEAT_TIMEOUT = 20 # seconds; if no heartbeat within this interval, PC is offline
9
+
10
  # Initialize storage files
11
  with open(status_file, "w") as f:
12
  json.dump({}, f)
13
  with open(command_file, "w") as f:
14
+ json.dump({"command": ""}, f)
15
 
16
  # Register PC as online
17
  def register_pc(name, password):
 
26
  def check_pc(name):
27
  with open(status_file, "r") as f:
28
  status = json.load(f)
29
+ if name in status and time.time() - status[name]["timestamp"] < HEARTBEAT_TIMEOUT:
30
  return "Online"
31
  return "Offline"
32
 
 
39
  if name in status and status[name]["password"] == password:
40
  with open(command_file, "w") as f:
41
  json.dump({"command": command}, f)
42
+ return f"Command sent to {name}."
43
  return "Authentication failed."
44
 
45
  # Get command (for PC side polling)
 
57
  check_btn.click(check_pc, inputs=name_input, outputs=check_output)
58
 
59
  with gr.Row():
60
+ cmd_input = gr.Textbox(label="Command", lines=5) # multi-line textbox for larger scripts
61
  send_btn = gr.Button("Send Command")
62
+ send_output = gr.Textbox(label="Response", lines=2)
63
  send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output)
64
 
65
+ # Hidden endpoint to allow PC polling for commands
66
  hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False)
67
  hidden_cmd_output = gr.Textbox(visible=False)
68
  hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command")