Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,14 +4,17 @@ import time
|
|
4 |
|
5 |
status_file = "status.json"
|
6 |
command_file = "command.json"
|
|
|
7 |
|
8 |
-
HEARTBEAT_TIMEOUT = 20 # seconds
|
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):
|
@@ -30,7 +33,7 @@ def check_pc(name):
|
|
30 |
return "Online"
|
31 |
return "Offline"
|
32 |
|
33 |
-
# Send command or register if command is "REGISTER"
|
34 |
def send_command(name, password, command):
|
35 |
if command == "REGISTER":
|
36 |
return register_pc(name, password)
|
@@ -48,6 +51,22 @@ def get_command():
|
|
48 |
cmd_data = json.load(f)
|
49 |
return cmd_data.get("command", "")
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
with gr.Blocks() as ui:
|
52 |
with gr.Row():
|
53 |
name_input = gr.Textbox(label="PC Name")
|
@@ -57,14 +76,23 @@ with gr.Blocks() as ui:
|
|
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) #
|
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
|
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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
ui.launch(share=True)
|
|
|
4 |
|
5 |
status_file = "status.json"
|
6 |
command_file = "command.json"
|
7 |
+
output_file = "output.json"
|
8 |
|
9 |
+
HEARTBEAT_TIMEOUT = 20 # seconds
|
10 |
|
11 |
# Initialize storage files
|
12 |
with open(status_file, "w") as f:
|
13 |
json.dump({}, f)
|
14 |
with open(command_file, "w") as f:
|
15 |
json.dump({"command": ""}, f)
|
16 |
+
with open(output_file, "w") as f:
|
17 |
+
json.dump({"output": ""}, f)
|
18 |
|
19 |
# Register PC as online
|
20 |
def register_pc(name, password):
|
|
|
33 |
return "Online"
|
34 |
return "Offline"
|
35 |
|
36 |
+
# Send command (or register if command is "REGISTER")
|
37 |
def send_command(name, password, command):
|
38 |
if command == "REGISTER":
|
39 |
return register_pc(name, password)
|
|
|
51 |
cmd_data = json.load(f)
|
52 |
return cmd_data.get("command", "")
|
53 |
|
54 |
+
# Upload command output from PC
|
55 |
+
def upload_output(name, password, output):
|
56 |
+
with open(status_file, "r") as f:
|
57 |
+
status = json.load(f)
|
58 |
+
if name in status and status[name]["password"] == password:
|
59 |
+
with open(output_file, "w") as f:
|
60 |
+
json.dump({"output": output}, f)
|
61 |
+
return "Output uploaded successfully."
|
62 |
+
return "Authentication failed."
|
63 |
+
|
64 |
+
# Retrieve command output (to view on Hugging Face)
|
65 |
+
def get_output():
|
66 |
+
with open(output_file, "r") as f:
|
67 |
+
data = json.load(f)
|
68 |
+
return data.get("output", "")
|
69 |
+
|
70 |
with gr.Blocks() as ui:
|
71 |
with gr.Row():
|
72 |
name_input = gr.Textbox(label="PC Name")
|
|
|
76 |
check_btn.click(check_pc, inputs=name_input, outputs=check_output)
|
77 |
|
78 |
with gr.Row():
|
79 |
+
cmd_input = gr.Textbox(label="Command", lines=5) # Multi-line for larger scripts
|
80 |
send_btn = gr.Button("Send Command")
|
81 |
send_output = gr.Textbox(label="Response", lines=2)
|
82 |
send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output)
|
83 |
|
84 |
+
# Hidden endpoints for PC polling and uploading output:
|
85 |
hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False)
|
86 |
hidden_cmd_output = gr.Textbox(visible=False)
|
87 |
hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command")
|
88 |
+
|
89 |
+
output_input = gr.Textbox(label="Output", visible=False)
|
90 |
+
hidden_upload_btn = gr.Button("Hidden Upload Output", visible=False)
|
91 |
+
hidden_upload_output = gr.Textbox(visible=False)
|
92 |
+
hidden_upload_btn.click(upload_output, inputs=[name_input, pass_input, output_input], outputs=hidden_upload_output, api_name="/upload_output")
|
93 |
+
|
94 |
+
hidden_get_output_btn = gr.Button("Hidden Get Output", visible=False)
|
95 |
+
hidden_get_output = gr.Textbox(visible=False)
|
96 |
+
hidden_get_output_btn.click(get_output, inputs=[], outputs=hidden_get_output, api_name="/get_output")
|
97 |
|
98 |
ui.launch(share=True)
|