Advertisement
DOGGYWOOF

Untitled

Dec 27th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. -- Remote Desktop Program for CC Tweaked
  2. -- Ensure rednet is enabled on both systems.
  3.  
  4. -- Constants
  5. local REMOTE_PORT = 8080
  6.  
  7. -- Function to detect and open modem on any side
  8. local function open_modem()
  9. local sides = {"left", "right", "top", "bottom", "front", "back"}
  10. for _, side in ipairs(sides) do
  11. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  12. rednet.open(side)
  13. return true
  14. end
  15. end
  16. return false
  17. end
  18.  
  19. -- Function to send commands to the remote computer
  20. local function remote_client()
  21. print("Enter the ID of the computer to connect to:")
  22. local targetID = tonumber(read())
  23.  
  24. if not targetID then
  25. print("Invalid ID.")
  26. return
  27. end
  28.  
  29. if not open_modem() then
  30. print("No modem found.")
  31. return
  32. end
  33.  
  34. print("Connecting to computer " .. targetID .. "...")
  35.  
  36. print("Waiting for connection acceptance...")
  37. rednet.send(targetID, "REQUEST_CONNECTION", REMOTE_PORT)
  38.  
  39. local senderID, response, protocol = rednet.receive(REMOTE_PORT, 10)
  40. if senderID == targetID and response == "ACCEPT_CONNECTION" then
  41. print("Connection accepted.")
  42. else
  43. print("Connection denied or timeout.")
  44. rednet.close()
  45. return
  46. end
  47.  
  48. while true do
  49. write("Remote> ")
  50. local command = read()
  51.  
  52. if command:lower() == "exit" then
  53. print("Exiting remote session.")
  54. break
  55. end
  56.  
  57. rednet.send(targetID, command, REMOTE_PORT)
  58.  
  59. local senderID, response, protocol = rednet.receive(REMOTE_PORT, 5)
  60. if senderID == targetID and response then
  61. print(response)
  62. else
  63. print("No response or timeout.")
  64. end
  65. end
  66.  
  67. rednet.close()
  68. end
  69.  
  70. -- Function to listen for and execute commands from a remote computer
  71. local function remote_server()
  72. print("Starting remote desktop server...")
  73. if not open_modem() then
  74. print("No modem found.")
  75. return
  76. end
  77.  
  78. while true do
  79. local senderID, command, protocol = rednet.receive(REMOTE_PORT)
  80. if command == "REQUEST_CONNECTION" then
  81. print("Connection request from computer " .. senderID .. ". Accept? (yes/no)")
  82. local response = read()
  83. if response:lower() == "yes" then
  84. rednet.send(senderID, "ACCEPT_CONNECTION", REMOTE_PORT)
  85. print("Connection accepted.")
  86. else
  87. rednet.send(senderID, "DENY_CONNECTION", REMOTE_PORT)
  88. print("Connection denied.")
  89. end
  90. elseif command then
  91. print("Command received from " .. senderID .. ": " .. command)
  92.  
  93. local success, output = pcall(function()
  94. return shell.run(command)
  95. end)
  96.  
  97. if not success then
  98. output = "Error: " .. tostring(output)
  99. end
  100.  
  101. rednet.send(senderID, output, REMOTE_PORT)
  102. end
  103. end
  104.  
  105. rednet.close()
  106. end
  107.  
  108. -- Main menu
  109. print("Remote Desktop Program")
  110. print("1. Connect to a remote computer")
  111. print("2. Start remote desktop server")
  112. write("Select an option: ")
  113. local choice = tonumber(read())
  114.  
  115. if choice == 1 then
  116. remote_client()
  117. elseif choice == 2 then
  118. remote_server()
  119. else
  120. print("Invalid option.")
  121. end
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement