Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Remote Desktop Program for CC Tweaked
- -- Ensure rednet is enabled on both systems.
- -- Constants
- local REMOTE_PORT = 8080
- -- Function to detect and open modem on any side
- local function open_modem()
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- return true
- end
- end
- return false
- end
- -- Function to send commands to the remote computer
- local function remote_client()
- print("Enter the ID of the computer to connect to:")
- local targetID = tonumber(read())
- if not targetID then
- print("Invalid ID.")
- return
- end
- if not open_modem() then
- print("No modem found.")
- return
- end
- print("Connecting to computer " .. targetID .. "...")
- print("Waiting for connection acceptance...")
- rednet.send(targetID, "REQUEST_CONNECTION", REMOTE_PORT)
- local senderID, response, protocol = rednet.receive(REMOTE_PORT, 10)
- if senderID == targetID and response == "ACCEPT_CONNECTION" then
- print("Connection accepted.")
- else
- print("Connection denied or timeout.")
- rednet.close()
- return
- end
- while true do
- write("Remote> ")
- local command = read()
- if command:lower() == "exit" then
- print("Exiting remote session.")
- break
- end
- rednet.send(targetID, command, REMOTE_PORT)
- local senderID, response, protocol = rednet.receive(REMOTE_PORT, 5)
- if senderID == targetID and response then
- print(response)
- else
- print("No response or timeout.")
- end
- end
- rednet.close()
- end
- -- Function to listen for and execute commands from a remote computer
- local function remote_server()
- print("Starting remote desktop server...")
- if not open_modem() then
- print("No modem found.")
- return
- end
- while true do
- local senderID, command, protocol = rednet.receive(REMOTE_PORT)
- if command == "REQUEST_CONNECTION" then
- print("Connection request from computer " .. senderID .. ". Accept? (yes/no)")
- local response = read()
- if response:lower() == "yes" then
- rednet.send(senderID, "ACCEPT_CONNECTION", REMOTE_PORT)
- print("Connection accepted.")
- else
- rednet.send(senderID, "DENY_CONNECTION", REMOTE_PORT)
- print("Connection denied.")
- end
- elseif command then
- print("Command received from " .. senderID .. ": " .. command)
- local success, output = pcall(function()
- return shell.run(command)
- end)
- if not success then
- output = "Error: " .. tostring(output)
- end
- rednet.send(senderID, output, REMOTE_PORT)
- end
- end
- rednet.close()
- end
- -- Main menu
- print("Remote Desktop Program")
- print("1. Connect to a remote computer")
- print("2. Start remote desktop server")
- write("Select an option: ")
- local choice = tonumber(read())
- if choice == 1 then
- remote_client()
- elseif choice == 2 then
- remote_server()
- else
- print("Invalid option.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement