Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Remote Desktop System for ComputerCraft
- -- Usage: Run "host" on the server computer, "client" on the controlling computer
- local mode = ...
- if not mode then
- print("Usage: remote <host|client>")
- return
- end
- -- Configuration
- local PROTOCOL = "remote_desktop"
- local REFRESH_RATE = 0.5
- -- Host Mode - Computer being controlled
- if mode == "host" then
- print("Starting Remote Desktop Host...")
- print("Opening rednet...")
- -- Find and open modem
- local modem = peripheral.find("modem")
- if not modem then
- print("Error: No modem found!")
- return
- end
- rednet.open(peripheral.getName(modem))
- rednet.host(PROTOCOL, "remote_host")
- print("Host ready! ID: " .. os.getComputerID())
- print("Waiting for client connection...")
- local clientID = nil
- local running = true
- -- Function to capture screen
- local function captureScreen()
- local w, h = term.getSize()
- local screen = {}
- local oldX, oldY = term.getCursorPos()
- for y = 1, h do
- screen[y] = {}
- for x = 1, w do
- term.setCursorPos(x, y)
- local char = term.read() or " "
- screen[y][x] = char:sub(1, 1)
- end
- end
- term.setCursorPos(oldX, oldY)
- return {content = screen, width = w, height = h, cursorX = oldX, cursorY = oldY}
- end
- -- Main loop
- while running do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" then
- local senderID, message = p1, p2
- if type(message) == "table" then
- if message.type == "connect" then
- clientID = senderID
- print("Client connected: " .. clientID)
- rednet.send(clientID, {type = "connect_ack"}, PROTOCOL)
- elseif message.type == "command" and senderID == clientID then
- print("Executing: " .. message.cmd)
- shell.run(message.cmd)
- elseif message.type == "text" and senderID == clientID then
- term.write(message.content)
- elseif message.type == "screen_request" and senderID == clientID then
- local screen = captureScreen()
- rednet.send(clientID, {type = "screen_data", screen = screen}, PROTOCOL)
- elseif message.type == "disconnect" and senderID == clientID then
- print("Client disconnected")
- clientID = nil
- end
- end
- end
- end
- -- Client Mode - Controlling computer
- elseif mode == "client" then
- print("Starting Remote Desktop Client...")
- -- Find and open modem
- local modem = peripheral.find("modem")
- if not modem then
- print("Error: No modem found!")
- return
- end
- rednet.open(peripheral.getName(modem))
- -- Find host
- print("Looking for host...")
- local hostID = rednet.lookup(PROTOCOL, "remote_host")
- if not hostID then
- print("Error: No host found!")
- print("Make sure host is running.")
- return
- end
- print("Found host: " .. hostID)
- print("Connecting...")
- -- Connect to host
- rednet.send(hostID, {type = "connect"}, PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL, 5)
- if not msg or msg.type ~= "connect_ack" then
- print("Connection failed!")
- return
- end
- print("Connected!")
- print("")
- print("Commands:")
- print(" cmd <command> - Run command on host")
- print(" text <text> - Display text on host")
- print(" screen - View host screen")
- print(" exit - Disconnect")
- print("")
- -- Main client loop
- local running = true
- while running do
- write("> ")
- local input = read()
- local parts = {}
- for word in input:gmatch("%S+") do
- table.insert(parts, word)
- end
- if #parts == 0 then
- -- Empty input, continue
- elseif parts[1] == "cmd" then
- local cmd = input:match("cmd%s+(.+)")
- if cmd then
- rednet.send(hostID, {type = "command", cmd = cmd}, PROTOCOL)
- print("Command sent")
- else
- print("Usage: cmd <command>")
- end
- elseif parts[1] == "text" then
- local txt = input:match("text%s+(.+)")
- if txt then
- rednet.send(hostID, {type = "text", content = txt}, PROTOCOL)
- print("Text sent")
- else
- print("Usage: text <text>")
- end
- elseif parts[1] == "screen" then
- print("Requesting screen...")
- rednet.send(hostID, {type = "screen_request"}, PROTOCOL)
- local id, msg = rednet.receive(PROTOCOL, 5)
- if msg and msg.type == "screen_data" then
- term.clear()
- term.setCursorPos(1, 1)
- local screen = msg.screen
- for y = 1, screen.height do
- term.setCursorPos(1, y)
- for x = 1, screen.width do
- term.write(screen.content[y][x])
- end
- end
- print("")
- print("--- End of screen (Press any key) ---")
- os.pullEvent("key")
- term.clear()
- term.setCursorPos(1, 1)
- else
- print("Failed to receive screen data")
- end
- elseif parts[1] == "exit" then
- rednet.send(hostID, {type = "disconnect"}, PROTOCOL)
- print("Disconnected")
- running = false
- else
- print("Unknown command: " .. parts[1])
- end
- end
- rednet.close()
- else
- print("Invalid mode. Use 'host' or 'client'")
- end
Advertisement
Add Comment
Please, Sign In to add comment