Advertisement
Tatantyler

RPC-Based SSH Server

Oct 28th, 2012
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. local args = {...}
  2.  
  3. function generateRemoteTermTable(target)
  4.     local remoteTerm = {
  5.         ["getCursorPos"] = function() return rpc.remoteCall(target, "term.getCursorPos") end,
  6.         ["setCursorPos"] = function(x,y) return rpc.remoteCall(target, "term.setCursorPos",x,y) end,
  7.         ["getSize"] = function() return rpc.remoteCall(target, "term.getSize") end,
  8.         ["write"] = function(text) return rpc.remoteCall(target, "term.write", text) end,
  9.         ["setBackgroundColor"] = function(color) return rpc.remoteCall(target, "term.setBackgroundColor", color) end,
  10.         ["clear"] = function() return rpc.remoteCall(target, "term.clear") end,
  11.         ["setBackgroundColour"] = function(color) return rpc.remoteCall(target, "term.setBackgroundColour", color) end,
  12.         ["setTextColor"] = function(color) return rpc.remoteCall(target, "term.setTextColor", color) end,
  13.         ["scroll"] = function(n) return rpc.remoteCall(target, "term.scroll", n) end,
  14.         ["setTextColour"] = function(color) return rpc.remoteCall(target, "term.setTextColour", color) end,
  15.         ["setCursorBlink"] = function(blink) return rpc.remoteCall(target, "term.setCursorBlink", blink) end,
  16.         ["isColour"] = function() return rpc.remoteCall(target, "term.isColour") end,
  17.         ["isColor"] = function() return rpc.remoteCall(target, "term.isColor") end,
  18.         ["clearLine"] = function() return rpc.remoteCall(target, "term.clearLine") end,
  19.     }
  20.     return remoteTerm
  21. end
  22.  
  23. for i,v in ipairs(rs.getSides()) do
  24.     if peripheral.getType(v) == "modem" then
  25.         rednet.open(v)
  26.     end
  27. end
  28.  
  29. local function genEnv(api, target)
  30.     local oldAPI = _G[api]
  31.     local newAPI = {}
  32.     for fName, fBody in pairs(oldAPI) do
  33.         if type(fBody) == "function" and fName ~= "serialize" and fName ~= "unserialize" then
  34.             fullFName = api.."."..fName
  35.             newAPI[fName] = loadstring("local args = {...} return rpc.remoteCall("..tostring(target)..", \""..fullFName.."\", unpack(args))")
  36.         elseif type(fBody) == "function" then
  37.             newAPI[fName] = fBody
  38.         end
  39.     end
  40.     return newAPI
  41. end
  42.  
  43. while true do
  44.     local id, msg = rednet.receive()
  45.     if msg == "connect" then
  46.         print("Client connection request: "..id)
  47.         local terminal = generateRemoteTermTable(id)
  48.         local newTextutils = genEnv("textutils", id)
  49.         local newIO = genEnv("io", id)
  50.         local oldTerm = term
  51.         local oldPrint = print
  52.         local oldRead = read
  53.         local oldWrite = write
  54.         local oldIO = io
  55.         local oldTUtils = textutils
  56.         read = function()
  57.             return rpc.remoteCall(id, "read")
  58.         end
  59.         print = function(...)
  60.             return rpc.remoteCall(id, "print", ...)
  61.         end
  62.         write = function(...)
  63.             return rpc.remoteCall(id, "write", ...)
  64.         end
  65.         term = terminal
  66.         textutils = newTextutils
  67.         io = newIO
  68.         local normalTextColor = colors.white
  69.         local remoteIDColor = colors.white
  70.         local inputTextColor = colors.white
  71.         local promptTextColor = colors.white
  72.         local remoteHighlightColor = colors.white
  73.        
  74.         if terminal.isColor() then
  75.             normalTextColor = colors.white
  76.             remoteIDColor = colors.lime
  77.             inputTextColor = colors.grey
  78.             promptTextColor = colors.yellow
  79.             remoteHighlightColor = colors.lightBlue
  80.         end
  81.        
  82.         local loggedIn = false
  83.         local password = ""
  84.         print("Connecting to computer "..os.computerID().."...")
  85.         if args[1] then
  86.             print("Please enter a password: ")
  87.             password = read()
  88.             if password == args[1] then
  89.                 loggedIn = true
  90.                 print("Success!")
  91.             else
  92.                 print("Incorrect password.")
  93.             end
  94.         else
  95.             loggedIn = true
  96.         end
  97.         os.sleep(1.5)
  98.        
  99.         if loggedIn then
  100.             term.clear()
  101.             term.setCursorPos(1,1)
  102.            
  103.             print(os.version().." [REMOTE]")
  104.            
  105.             while true do
  106.                 term.setTextColor(remoteHighlightColor)
  107.                 write("remote ")
  108.                 term.setTextColor(normalTextColor)
  109.                 write("@ ")
  110.                 term.setTextColor(remoteIDColor)
  111.                 write(os.computerID())
  112.                 term.setTextColor(promptTextColor)
  113.                 write(" "..shell.dir()..">")
  114.                 term.setTextColor(inputTextColor)
  115.                 local sLine = rpc.remoteCall(id, "read")
  116.                 local words = {}
  117.                 for word in sLine:gmatch("(%S+)") do
  118.                     table.insert(words, word)
  119.                 end
  120.                 if words[1] == "exit" or words[1] == "disconnect" then
  121.                     break
  122.                 else
  123.                     shell.run(unpack(words))
  124.                 end
  125.             end
  126.         end
  127.         print("Disconnecting from server...")
  128.         term = oldTerm
  129.         textutils = oldTUtils
  130.         io = oldIO
  131.         --[[
  132.         local env = {}
  133.         setmetatable(env, {__index = _G})
  134.         env["remoteID"] = id
  135.         print("Client connected.")
  136.         terminal = generateRemoteTermTable(id)
  137.         terminal.write("Logged in.")
  138.         terminal.setCursorPos(1,2)
  139.         print("Generated remote terminal.")
  140.         env["term"] = terminal
  141.         print("Generating TextUtils API...")
  142.         local oldTextutils = genEnv("textutils", id)
  143.         print("Generating IO API...")
  144.         env["io"] = genEnv("io", id)
  145.         rpc.setDebug(true)
  146.         env["read"] = function()
  147.             local output = rpc.remoteCall(remoteID, "read")
  148.             return output
  149.         end
  150.         print("Generating environment...")
  151.         print("Environment built.")
  152.         env["write"] = function(text)
  153.             return rpc.remoteCall(remoteID, "write", text)
  154.         end
  155.         env["print"] = function(...)
  156.             return rpc.remoteCall(remoteID, "print", unpack(arg))
  157.         end
  158.         env["printLol"] = function() print("lol") end
  159.         local function fn()
  160.             print(type(rpc))
  161.             parallel.waitForAll(
  162.             function()
  163.                 print("Connection from "..remoteID)
  164.                 local tCommandHistory = {}
  165.                 while not bExit do
  166.                     write("remote> " )
  167.                     local sLine = rpc.remoteCall(remoteID, "read")
  168.                     print(sLine)
  169.                     table.insert( tCommandHistory, sLine )
  170.                     local words = {}
  171.                     for word in string.gmatch(sLine, "(%S+)") do
  172.                         table.insert(words, word)
  173.                     end
  174.                     os.run({}, unpack(words) )
  175.                 end
  176.             end, rpc.run)
  177.         end
  178.         setfenv(fn, env)
  179.         env.printLol()
  180.         fn()
  181.         print("Client disconnected.")
  182.         term = oldTerm
  183.         textutils = oldTextutils
  184.         io = oldIO
  185.         read = oldRead
  186.         write = oldWrite]]
  187.     end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement