DorianH935

Untitled

Jan 5th, 2026
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. -- Remote Desktop System for ComputerCraft
  2. -- Usage: Run "host" on the server computer, "client" on the controlling computer
  3.  
  4. local mode = ...
  5. if not mode then
  6. print("Usage: remote <host|client>")
  7. return
  8. end
  9.  
  10. -- Configuration
  11. local PROTOCOL = "remote_desktop"
  12. local REFRESH_RATE = 0.5
  13.  
  14. -- Host Mode - Computer being controlled
  15. if mode == "host" then
  16. print("Starting Remote Desktop Host...")
  17. print("Opening rednet...")
  18.  
  19. -- Find and open modem
  20. local modem = peripheral.find("modem")
  21. if not modem then
  22. print("Error: No modem found!")
  23. return
  24. end
  25.  
  26. rednet.open(peripheral.getName(modem))
  27. rednet.host(PROTOCOL, "remote_host")
  28.  
  29. print("Host ready! ID: " .. os.getComputerID())
  30. print("Waiting for client connection...")
  31.  
  32. local clientID = nil
  33. local running = true
  34.  
  35. -- Function to capture screen
  36. local function captureScreen()
  37. local w, h = term.getSize()
  38. local screen = {}
  39. local oldX, oldY = term.getCursorPos()
  40.  
  41. for y = 1, h do
  42. screen[y] = {}
  43. for x = 1, w do
  44. term.setCursorPos(x, y)
  45. local char = term.read() or " "
  46. screen[y][x] = char:sub(1, 1)
  47. end
  48. end
  49.  
  50. term.setCursorPos(oldX, oldY)
  51. return {content = screen, width = w, height = h, cursorX = oldX, cursorY = oldY}
  52. end
  53.  
  54. -- Main loop
  55. while running do
  56. local event, p1, p2, p3 = os.pullEvent()
  57.  
  58. if event == "rednet_message" then
  59. local senderID, message = p1, p2
  60.  
  61. if type(message) == "table" then
  62. if message.type == "connect" then
  63. clientID = senderID
  64. print("Client connected: " .. clientID)
  65. rednet.send(clientID, {type = "connect_ack"}, PROTOCOL)
  66.  
  67. elseif message.type == "command" and senderID == clientID then
  68. print("Executing: " .. message.cmd)
  69. shell.run(message.cmd)
  70.  
  71. elseif message.type == "text" and senderID == clientID then
  72. term.write(message.content)
  73.  
  74. elseif message.type == "screen_request" and senderID == clientID then
  75. local screen = captureScreen()
  76. rednet.send(clientID, {type = "screen_data", screen = screen}, PROTOCOL)
  77.  
  78. elseif message.type == "disconnect" and senderID == clientID then
  79. print("Client disconnected")
  80. clientID = nil
  81. end
  82. end
  83. end
  84. end
  85.  
  86. -- Client Mode - Controlling computer
  87. elseif mode == "client" then
  88. print("Starting Remote Desktop Client...")
  89.  
  90. -- Find and open modem
  91. local modem = peripheral.find("modem")
  92. if not modem then
  93. print("Error: No modem found!")
  94. return
  95. end
  96.  
  97. rednet.open(peripheral.getName(modem))
  98.  
  99. -- Find host
  100. print("Looking for host...")
  101. local hostID = rednet.lookup(PROTOCOL, "remote_host")
  102.  
  103. if not hostID then
  104. print("Error: No host found!")
  105. print("Make sure host is running.")
  106. return
  107. end
  108.  
  109. print("Found host: " .. hostID)
  110. print("Connecting...")
  111.  
  112. -- Connect to host
  113. rednet.send(hostID, {type = "connect"}, PROTOCOL)
  114. local id, msg = rednet.receive(PROTOCOL, 5)
  115.  
  116. if not msg or msg.type ~= "connect_ack" then
  117. print("Connection failed!")
  118. return
  119. end
  120.  
  121. print("Connected!")
  122. print("")
  123. print("Commands:")
  124. print(" cmd <command> - Run command on host")
  125. print(" text <text> - Display text on host")
  126. print(" screen - View host screen")
  127. print(" exit - Disconnect")
  128. print("")
  129.  
  130. -- Main client loop
  131. local running = true
  132. while running do
  133. write("> ")
  134. local input = read()
  135. local parts = {}
  136. for word in input:gmatch("%S+") do
  137. table.insert(parts, word)
  138. end
  139.  
  140. if #parts == 0 then
  141. -- Empty input, continue
  142. elseif parts[1] == "cmd" then
  143. local cmd = input:match("cmd%s+(.+)")
  144. if cmd then
  145. rednet.send(hostID, {type = "command", cmd = cmd}, PROTOCOL)
  146. print("Command sent")
  147. else
  148. print("Usage: cmd <command>")
  149. end
  150.  
  151. elseif parts[1] == "text" then
  152. local txt = input:match("text%s+(.+)")
  153. if txt then
  154. rednet.send(hostID, {type = "text", content = txt}, PROTOCOL)
  155. print("Text sent")
  156. else
  157. print("Usage: text <text>")
  158. end
  159.  
  160. elseif parts[1] == "screen" then
  161. print("Requesting screen...")
  162. rednet.send(hostID, {type = "screen_request"}, PROTOCOL)
  163.  
  164. local id, msg = rednet.receive(PROTOCOL, 5)
  165. if msg and msg.type == "screen_data" then
  166. term.clear()
  167. term.setCursorPos(1, 1)
  168.  
  169. local screen = msg.screen
  170. for y = 1, screen.height do
  171. term.setCursorPos(1, y)
  172. for x = 1, screen.width do
  173. term.write(screen.content[y][x])
  174. end
  175. end
  176.  
  177. print("")
  178. print("--- End of screen (Press any key) ---")
  179. os.pullEvent("key")
  180. term.clear()
  181. term.setCursorPos(1, 1)
  182. else
  183. print("Failed to receive screen data")
  184. end
  185.  
  186. elseif parts[1] == "exit" then
  187. rednet.send(hostID, {type = "disconnect"}, PROTOCOL)
  188. print("Disconnected")
  189. running = false
  190.  
  191. else
  192. print("Unknown command: " .. parts[1])
  193. end
  194. end
  195.  
  196. rednet.close()
  197. else
  198. print("Invalid mode. Use 'host' or 'client'")
  199. end
Advertisement
Add Comment
Please, Sign In to add comment