Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... } -- Capture arguments inputted at the top level shell.
- --[[
- LAN Cable Remote Shell PaymentOption
- Black Wolf Server
- 27 November 2012
- This program allows for the connection of one
- computer to another and redirecting terminal
- output to the receving machine. Both the receiving
- machine and transmitting machine will be able
- to provide input to the transmitting machine
- through this program.
- Remember, this program will NOT check if the connection is secure
- or the receiver/transmitter is still running this program. It is
- the responisibility of the user to be aware of this information.
- Results of the aforementioned situation are unknown, so be careful.
- ]]--
- --[[
- local shellThread = coroutine.create(function() shell.run("rom/programs/shell") end)
- coroutine.resume(shellThread)
- while true do
- local event = {os.pullEvent()} -- Get an event from the system.
- coroutine.resume(shellThread, unpack(event)) -- Resume the shell using the event that was given.
- end
- ]]--
- local connection = {} -- This will be the connection table for the connection that
- -- is made to this particular computer.
- local session = {} -- This will be the shell session that has been started
- -- over the starting shell that will capture and
- -- resend events to the receiving computer.
- local modemSide = nil -- This will be the side on which the lan cable exists.
- local receiving = false -- Whether or not this particular computer is receiving
- -- the terminal output or is transmitting it.
- -- Parses the arguments passed from the top level shell. The proper useage
- -- of the program is printed if the arguments are in some way incorrect.
- -- Params : nil
- -- Returns: true or false - Depending on whehter or not the arguments were correct.
- function processArguments()
- if #tArgs == 2 then
- if tArgs[1] == "transmit" or tArgs[1] == "receive" then
- if tArgs[1] == "receive" then
- receiving = true
- end
- if tonumber(tArgs[2]) then
- connection.id = tonumber(tArgs[2])
- return true
- end
- end
- end
- print("Useage: " .. fs.getName(shell.getRunningProgram()) .. " <transmit/receive> <connectionID>")
- return false
- end
- -- Attempts to locate a lan cable on any side of this particular computer.
- -- This function will wrap the lan cable if one is found and return it, along
- -- with the side the cable was found on.
- -- Params : nil
- -- Returns: rednet, rednetSide - The wrapped lan cable and the side its on.
- function locateAndWrapLanCable()
- local sides = rs.getSides()
- for sideIndex, side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- modemSide = side
- rednet.open(side)
- return
- end
- end
- end
- -- Creates a new local shell session in a thread.
- -- Params : nil
- -- Returns: shellThread
- function newShellThread()
- local shellThread = coroutine.create(function() shell.run("rom/programs/shell") end)
- return shellThread
- end
- -- Wraps all of the native terminal functions into new functionst that will
- -- transmit themselves over lan to the receiving computer in a serialized table.
- -- Params : nil
- -- Returns: wrappedTerminal - The new native terminal functions that redirect output.
- function wrapNativeTerminalFunctions()
- local oldTerminal = {}
- local wrappedTerminal = {}
- local function wrapFunction(functionName)
- return function( ... )
- rednet.send(connection.id, textutils.serialize({functionName = functionName, params = { ... }}))
- return oldTerminal[functionName]( ... )
- end
- end
- for functionName, functionObject in pairs(term.native) do
- oldTerminal[functionName] = functionObject
- end
- for functionName, functionObject in pairs(term.native) do
- wrappedTerminal[functionName] = wrapFunction(functionName)
- end
- return wrappedTerminal
- end
- -- Transmits information from this particular computer to the receiving computer
- -- over lan in a serialized table. Also, this program handles the output on the
- -- terminal after the computer has wrapped the native terminal output to be
- -- sent over lan.
- -- Params : nil
- -- Returns: nil
- function transmitTerminalOutput()
- -- Wrap all of the native terminal functions into a new table that
- -- will transmit their information to the receiving machine. Then
- -- redirect terminal output through aforementioned table.
- local redirectedTerminal = wrapNativeTerminalFunctions()
- term.redirect(redirectedTerminal)
- -- Create a new shell instance and resume it, all the while handling
- -- messages passed from the receiving computer.
- local shellThread = newShellThread()
- coroutine.resume(shellThread)
- while true do
- -- Wait for an event and handle it accordingly. Use a table to
- -- capture all parameters returned by any event.
- local event = {os.pullEvent()}
- -- If the event was a cable message, then go ahead and check
- -- to see if it was from the receiving machine. In the case
- -- that it is, then handle it like an event.
- if event[1] == "rednet_message" and event[2] == connection.id then
- event = textutils.unserialize(event[3])
- -- The message should have been an event table. Swap
- -- the sent event for the current one and update the
- -- screen.
- end
- -- Update the current shell thread with the event that was
- -- captured this iteration.
- coroutine.resume(shellThread, unpack(event))
- end
- end
- -- Receives terminal output from the transmitting machine and draws it to the
- -- screen. This function also handles local events and redirects them to the
- -- transmitting machine. However, rednet events are not redirected, nor are
- -- lan cable events to prevent confusion on the transmitter's side.
- -- Params : nil
- -- Returns: nil
- function receiveTerminalOutput()
- -- Runs any function from the transmitting machine from within the
- -- native terminal functions.
- local function runTerminalFunction(functionTable)
- if term.native[functionTable.functionName] then
- return term.native[functionTable.functionName](unpack(functionTable.params))
- end
- end
- -- Create a new shell instance and resume it, all the while handling
- -- messages passed from the transmitting computer. Also, send
- -- user interaction events that are passsed.
- local shellThread = newShellThread()
- coroutine.resume(shellThread)
- while true do
- -- Wait for an event and handle it accordingly. Use a table to
- -- capture all parameters returned by any event.
- local event = {os.pullEvent()}
- -- If the event was a cable message from the transmitting computer,
- -- then go ahead and attempt to process it as a function call.
- if event[1] == "rednet_message" and event[2] == connection.id then
- -- Get the function table from the message by
- -- unserializing the message, then execute it.
- local functionTable = textutils.unserialize(event[3])
- runTerminalFunction(functionTable)
- -- Any other event can be serialized and sent to the
- -- transmitting machine.
- else
- rednet.send(connection.id, textutils.serialize(event))
- end
- -- Update the current shell thread with the event that was
- -- captured this iteration.
- -- coroutine.resume(shellThread, unpack(event))
- end
- end
- -- Proccess the given arguments to make sure the program has the necessary information
- -- to execute properly.
- if not processArguments() then
- return
- end
- -- Attempt to locate and wrap any land cable that can be found.
- locateAndWrapLanCable()
- if not modemSide then
- print("No modem found.")
- return
- end
- -- If we are not to be the receiving computer, then begin transmitting terminal
- -- output to the receiving computer and listen for events from the receiving computer.
- if not receiving then
- transmitTerminalOutput()
- else
- receiveTerminalOutput()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement