Advertisement
Trystan_C_C

Rednet Remote Shell (Edited from Lan)

Nov 27th, 2012
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. local tArgs = { ... } -- Capture arguments inputted at the top level shell.
  2. --[[
  3.         LAN Cable Remote Shell          PaymentOption
  4.                                         Black Wolf Server
  5.                                         27 November 2012
  6.                                        
  7.         This program allows for the connection of one
  8.         computer to another and redirecting terminal
  9.         output to the receving machine. Both the receiving
  10.         machine and transmitting machine will be able
  11.         to provide input to the transmitting machine
  12.         through this program.
  13.        
  14.         Remember, this program will NOT check if the connection is secure
  15.         or the receiver/transmitter is still running this program. It is
  16.         the responisibility of the user to be aware of this information.
  17.         Results of the aforementioned situation are unknown, so be careful.
  18. ]]--
  19.  
  20. --[[
  21. local shellThread = coroutine.create(function() shell.run("rom/programs/shell") end)
  22. coroutine.resume(shellThread)
  23. while true do
  24.         local event = {os.pullEvent()} -- Get an event from the system.
  25.         coroutine.resume(shellThread, unpack(event)) -- Resume the shell using the event that was given.
  26. end
  27. ]]--
  28.  
  29. local connection = {} -- This will be the connection table for the connection that
  30.                       -- is made to this particular computer.
  31. local session = {}    -- This will be the shell session that has been started
  32.                       -- over the starting shell that will capture and
  33.                       -- resend events to the receiving computer.
  34. local modemSide = nil -- This will be the side on which the lan cable exists.
  35. local receiving = false -- Whether or not this particular computer is receiving
  36.                         -- the terminal output or is transmitting it.
  37.  
  38.  
  39.  
  40. -- Parses the arguments passed from the top level shell. The proper useage
  41. -- of the program is printed if the arguments are in some way incorrect.
  42. -- Params : nil
  43. -- Returns: true or false - Depending on whehter or not the arguments were correct.
  44. function processArguments()
  45.         if #tArgs == 2 then
  46.                 if tArgs[1] == "transmit" or tArgs[1] == "receive" then
  47.                         if tArgs[1] == "receive" then
  48.                                 receiving = true
  49.                         end
  50.                        
  51.                         if tonumber(tArgs[2]) then
  52.                                 connection.id = tonumber(tArgs[2])
  53.                                 return true
  54.                         end
  55.                 end
  56.         end
  57.        
  58.         print("Useage: " .. fs.getName(shell.getRunningProgram()) .. " <transmit/receive> <connectionID>")
  59.         return false
  60. end
  61.                        
  62. -- Attempts to locate a lan cable on any side of this particular computer.
  63. -- This function will wrap the lan cable if one is found and return it, along
  64. -- with the side the cable was found on.
  65. -- Params : nil
  66. -- Returns: rednet, rednetSide - The wrapped lan cable and the side its on.
  67. function locateAndWrapLanCable()
  68.         local sides = rs.getSides()
  69.         for sideIndex, side in ipairs(sides) do
  70.                 if peripheral.getType(side) == "modem" then
  71.                         modemSide = side
  72.                         rednet.open(side)
  73.                         return
  74.                 end
  75.         end
  76. end
  77.  
  78. -- Creates a new local shell session in a thread.
  79. -- Params : nil
  80. -- Returns: shellThread
  81. function newShellThread()
  82.         local shellThread = coroutine.create(function() shell.run("rom/programs/shell") end)
  83.         return shellThread
  84. end
  85.  
  86. -- Wraps all of the native terminal functions into new functionst that will
  87. -- transmit themselves over lan to the receiving computer in a serialized table.
  88. -- Params : nil
  89. -- Returns: wrappedTerminal - The new native terminal functions that redirect output.
  90. function wrapNativeTerminalFunctions()
  91.         local oldTerminal = {}
  92.         local wrappedTerminal = {}
  93.        
  94.         local function wrapFunction(functionName)
  95.                 return function( ... )
  96.                         rednet.send(connection.id, textutils.serialize({functionName = functionName, params = { ... }}))
  97.                         return oldTerminal[functionName]( ... )
  98.                 end
  99.         end
  100.        
  101.         for functionName, functionObject in pairs(term.native) do
  102.                 oldTerminal[functionName] = functionObject
  103.         end
  104.        
  105.         for functionName, functionObject in pairs(term.native) do
  106.                 wrappedTerminal[functionName] = wrapFunction(functionName)
  107.         end
  108.        
  109.         return wrappedTerminal
  110. end
  111.  
  112. -- Transmits information from this particular computer to the receiving computer
  113. -- over lan in a serialized table. Also, this program handles the output on the
  114. -- terminal after the computer has wrapped the native terminal output to be
  115. -- sent over lan.
  116. -- Params : nil
  117. -- Returns: nil
  118. function transmitTerminalOutput()
  119.         -- Wrap all of the native terminal functions into a new table that
  120.         -- will transmit their information to the receiving machine. Then
  121.         -- redirect terminal output through aforementioned table.
  122.         local redirectedTerminal = wrapNativeTerminalFunctions()
  123.         term.redirect(redirectedTerminal)
  124.        
  125.         -- Create a new shell instance and resume it, all the while handling
  126.         -- messages passed from the receiving computer.
  127.         local shellThread = newShellThread()
  128.         coroutine.resume(shellThread)
  129.        
  130.         while true do
  131.                 -- Wait for an event and handle it accordingly. Use a table to
  132.                 -- capture all parameters returned by any event.
  133.                 local event = {os.pullEvent()}
  134.                
  135.                 -- If the event was a cable message, then go ahead and check
  136.                 -- to see if it was from the receiving machine. In the case
  137.                 -- that it is, then handle it like an event.
  138.                 if event[1] == "rednet_message" and event[2] == connection.id then
  139.                         event = textutils.unserialize(event[3])
  140.                         -- The message should have been an event table. Swap
  141.                         -- the sent event for the current one and update the
  142.                         -- screen.
  143.                 end
  144.                
  145.                 -- Update the current shell thread with the event that was
  146.                 -- captured this iteration.
  147.                 coroutine.resume(shellThread, unpack(event))
  148.         end
  149. end
  150.  
  151. -- Receives terminal output from the transmitting machine and draws it to the
  152. -- screen. This function also handles local events and redirects them to the
  153. -- transmitting machine. However, rednet events are not redirected, nor are
  154. -- lan cable events to prevent confusion on the transmitter's side.
  155. -- Params : nil
  156. -- Returns: nil
  157. function receiveTerminalOutput()
  158.         -- Runs any function from the transmitting machine from within the
  159.         -- native terminal functions.
  160.         local function runTerminalFunction(functionTable)
  161.                 if term.native[functionTable.functionName] then
  162.                         return term.native[functionTable.functionName](unpack(functionTable.params))
  163.                 end
  164.         end
  165.        
  166.         -- Create a new shell instance and resume it, all the while handling
  167.         -- messages passed from the transmitting computer. Also, send
  168.         -- user interaction events that are passsed.
  169.         local shellThread = newShellThread()
  170.         coroutine.resume(shellThread)
  171.        
  172.         while true do
  173.                 -- Wait for an event and handle it accordingly. Use a table to
  174.                 -- capture all parameters returned by any event.
  175.                 local event = {os.pullEvent()}
  176.                
  177.                         -- If the event was a cable message from the transmitting computer,
  178.                         -- then go ahead and attempt to process it as a function call.
  179.                         if event[1] == "rednet_message" and event[2] == connection.id then
  180.                                 -- Get the function table from the message by
  181.                                 -- unserializing the message, then execute it.
  182.                                 local functionTable = textutils.unserialize(event[3])
  183.                                 runTerminalFunction(functionTable)
  184.                         -- Any other event can be serialized and sent to the
  185.                         -- transmitting machine.
  186.                         else
  187.                                 rednet.send(connection.id, textutils.serialize(event))
  188.                         end
  189.                
  190.                 -- Update the current shell thread with the event that was
  191.                 -- captured this iteration.
  192.                 -- coroutine.resume(shellThread, unpack(event))
  193.         end
  194. end
  195.  
  196. -- Proccess the given arguments to make sure the program has the necessary information
  197. -- to execute properly.
  198. if not processArguments() then
  199.         return
  200. end
  201. -- Attempt to locate and wrap any land cable that can be found.
  202. locateAndWrapLanCable()
  203. if not modemSide then
  204.         print("No modem found.")
  205.         return
  206. end
  207.  
  208. -- If we are not to be the receiving computer, then begin transmitting terminal
  209. -- output to the receiving computer and listen for events from the receiving computer.
  210. if not receiving then
  211.         transmitTerminalOutput()
  212. else
  213.         receiveTerminalOutput()
  214. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement