File size: 4,385 Bytes
6fb06ea
 
 
 
 
 
fdb1b39
6fb06ea
fdb1b39
2ceaca7
6fb06ea
 
 
 
2ceaca7
fdb1b39
 
6fb06ea
 
 
 
 
 
 
 
 
 
 
 
 
 
2ceaca7
6fb06ea
 
 
fdb1b39
6fb06ea
2e33767
 
6fb06ea
 
 
 
 
2ceaca7
6fb06ea
 
2e33767
6fb06ea
 
 
 
 
eeb6441
 
fdb1b39
 
 
eeb6441
2e87049
eeb6441
 
 
 
 
 
 
fdb1b39
eeb6441
fdb1b39
 
 
 
 
 
 
 
 
6fb06ea
2e87049
6fb06ea
 
 
 
 
 
 
2e87049
6fb06ea
fdb1b39
6fb06ea
2ceaca7
6fb06ea
8eb72ef
2e87049
 
 
 
 
 
fdb1b39
8eb72ef
 
 
fdb1b39
2e87049
fdb1b39
eeb6441
fdb1b39
 
eeb6441
 
 
 
 
 
6fb06ea
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import gradio as gr
import json
import time

status_file = "status.json"
command_file = "command.json"
output_file = "output.json"

HEARTBEAT_TIMEOUT = 20  # seconds

# Initialize storage files
with open(status_file, "w") as f:
    json.dump({}, f)
with open(command_file, "w") as f:
    json.dump({"command": ""}, f)
with open(output_file, "w") as f:
    json.dump({"output": ""}, f)

# Register PC as online
def register_pc(name, password):
    with open(status_file, "r") as f:
        status = json.load(f)
    status[name] = {"password": password, "timestamp": time.time()}
    with open(status_file, "w") as f:
        json.dump(status, f)
    return f"PC '{name}' registered as online."

# Check if PC is online
def check_pc(name):
    with open(status_file, "r") as f:
        status = json.load(f)
    if name in status and time.time() - status[name]["timestamp"] < HEARTBEAT_TIMEOUT:
        return "Online"
    return "Offline"

# Send command (or register if command is "REGISTER")
def send_command(name, password, command):
    if command == "REGISTER":
        return register_pc(name, password)
    with open(status_file, "r") as f:
        status = json.load(f)
    if name in status and status[name]["password"] == password:
        with open(command_file, "w") as f:
            json.dump({"command": command}, f)
        return f"Command sent to {name}."
    return "Authentication failed."

# Get command (for PC side polling)
def get_command():
    with open(command_file, "r") as f:
        cmd_data = json.load(f)
    return cmd_data.get("command", "")

# Upload command output from PC with timestamps
def upload_output(name, password, output, sent_time):
    with open(status_file, "r") as f:
        status = json.load(f)
    if name in status and status[name]["password"] == password:
        received_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        # Format the output with "Time Sent", a divider, then "Time Received", another divider, and the actual output.
        combined_output = (
            f"Time Sent: {sent_time}\n"
            "--------------------\n"
            f"Time Received: {received_time}\n"
            "--------------------\n"
            f"{output}"
        )
        with open(output_file, "w") as f:
            json.dump({"output": combined_output}, f)
        return "Output uploaded successfully."
    return "Authentication failed."

# Retrieve command output (to view on Hugging Face)
def get_output():
    with open(output_file, "r") as f:
        data = json.load(f)
    return data.get("output", "")

with gr.Blocks() as ui:
    # PC info and status
    with gr.Row():
        name_input = gr.Textbox(label="PC Name")
        pass_input = gr.Textbox(label="Password", type="password")
    check_btn = gr.Button("Check Online Status")
    check_output = gr.Textbox(label="Status")
    check_btn.click(check_pc, inputs=name_input, outputs=check_output)

    # Command sending row
    with gr.Row():
        cmd_input = gr.Textbox(label="Command", lines=5)  # Multi-line for larger scripts
        send_btn = gr.Button("Send Command")
        send_output = gr.Textbox(label="Response", lines=2)
    send_btn.click(send_command, inputs=[name_input, pass_input, cmd_input], outputs=send_output)
    
    # Visible output display row
    with gr.Row():
        get_output_btn = gr.Button("Get Command Output")
        output_display = gr.Textbox(label="Command Output", lines=10)
    get_output_btn.click(get_output, inputs=[], outputs=output_display)
    
    # Hidden endpoints for PC polling and uploading output:
    hidden_get_cmd_btn = gr.Button("Hidden Get Command", visible=False)
    hidden_cmd_output = gr.Textbox(visible=False)
    hidden_get_cmd_btn.click(get_command, inputs=[], outputs=hidden_cmd_output, api_name="/get_command")
    
    # Hidden components for uploading output: includes a textbox for sent_time.
    output_input = gr.Textbox(label="Output", visible=False)
    sent_time_input = gr.Textbox(label="Sent Time", visible=False)
    hidden_upload_btn = gr.Button("Hidden Upload Output", visible=False)
    hidden_upload_output = gr.Textbox(visible=False)
    hidden_upload_btn.click(
        upload_output, 
        inputs=[name_input, pass_input, output_input, sent_time_input],
        outputs=hidden_upload_output, 
        api_name="/upload_output"
    )

ui.launch(share=True)