Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- function generateRemoteTermTable(target)
- local remoteTerm = {
- ["getCursorPos"] = function() return rpc.remoteCall(target, "term.getCursorPos") end,
- ["setCursorPos"] = function(x,y) return rpc.remoteCall(target, "term.setCursorPos",x,y) end,
- ["getSize"] = function() return rpc.remoteCall(target, "term.getSize") end,
- ["write"] = function(text) return rpc.remoteCall(target, "term.write", text) end,
- ["setBackgroundColor"] = function(color) return rpc.remoteCall(target, "term.setBackgroundColor", color) end,
- ["clear"] = function() return rpc.remoteCall(target, "term.clear") end,
- ["setBackgroundColour"] = function(color) return rpc.remoteCall(target, "term.setBackgroundColour", color) end,
- ["setTextColor"] = function(color) return rpc.remoteCall(target, "term.setTextColor", color) end,
- ["scroll"] = function(n) return rpc.remoteCall(target, "term.scroll", n) end,
- ["setTextColour"] = function(color) return rpc.remoteCall(target, "term.setTextColour", color) end,
- ["setCursorBlink"] = function(blink) return rpc.remoteCall(target, "term.setCursorBlink", blink) end,
- ["isColour"] = function() return rpc.remoteCall(target, "term.isColour") end,
- ["isColor"] = function() return rpc.remoteCall(target, "term.isColor") end,
- ["clearLine"] = function() return rpc.remoteCall(target, "term.clearLine") end,
- }
- return remoteTerm
- end
- for i,v in ipairs(rs.getSides()) do
- if peripheral.getType(v) == "modem" then
- rednet.open(v)
- end
- end
- local function genEnv(api, target)
- local oldAPI = _G[api]
- local newAPI = {}
- for fName, fBody in pairs(oldAPI) do
- if type(fBody) == "function" and fName ~= "serialize" and fName ~= "unserialize" then
- fullFName = api.."."..fName
- newAPI[fName] = loadstring("local args = {...} return rpc.remoteCall("..tostring(target)..", \""..fullFName.."\", unpack(args))")
- elseif type(fBody) == "function" then
- newAPI[fName] = fBody
- end
- end
- return newAPI
- end
- while true do
- local id, msg = rednet.receive()
- if msg == "connect" then
- print("Client connection request: "..id)
- local terminal = generateRemoteTermTable(id)
- local newTextutils = genEnv("textutils", id)
- local newIO = genEnv("io", id)
- local oldTerm = term
- local oldPrint = print
- local oldRead = read
- local oldWrite = write
- local oldIO = io
- local oldTUtils = textutils
- read = function()
- return rpc.remoteCall(id, "read")
- end
- print = function(...)
- return rpc.remoteCall(id, "print", ...)
- end
- write = function(...)
- return rpc.remoteCall(id, "write", ...)
- end
- term = terminal
- textutils = newTextutils
- io = newIO
- local normalTextColor = colors.white
- local remoteIDColor = colors.white
- local inputTextColor = colors.white
- local promptTextColor = colors.white
- local remoteHighlightColor = colors.white
- if terminal.isColor() then
- normalTextColor = colors.white
- remoteIDColor = colors.lime
- inputTextColor = colors.grey
- promptTextColor = colors.yellow
- remoteHighlightColor = colors.lightBlue
- end
- local loggedIn = false
- local password = ""
- print("Connecting to computer "..os.computerID().."...")
- if args[1] then
- print("Please enter a password: ")
- password = read()
- if password == args[1] then
- loggedIn = true
- print("Success!")
- else
- print("Incorrect password.")
- end
- else
- loggedIn = true
- end
- os.sleep(1.5)
- if loggedIn then
- term.clear()
- term.setCursorPos(1,1)
- print(os.version().." [REMOTE]")
- while true do
- term.setTextColor(remoteHighlightColor)
- write("remote ")
- term.setTextColor(normalTextColor)
- write("@ ")
- term.setTextColor(remoteIDColor)
- write(os.computerID())
- term.setTextColor(promptTextColor)
- write(" "..shell.dir()..">")
- term.setTextColor(inputTextColor)
- local sLine = rpc.remoteCall(id, "read")
- local words = {}
- for word in sLine:gmatch("(%S+)") do
- table.insert(words, word)
- end
- if words[1] == "exit" or words[1] == "disconnect" then
- break
- else
- shell.run(unpack(words))
- end
- end
- end
- print("Disconnecting from server...")
- term = oldTerm
- textutils = oldTUtils
- io = oldIO
- --[[
- local env = {}
- setmetatable(env, {__index = _G})
- env["remoteID"] = id
- print("Client connected.")
- terminal = generateRemoteTermTable(id)
- terminal.write("Logged in.")
- terminal.setCursorPos(1,2)
- print("Generated remote terminal.")
- env["term"] = terminal
- print("Generating TextUtils API...")
- local oldTextutils = genEnv("textutils", id)
- print("Generating IO API...")
- env["io"] = genEnv("io", id)
- rpc.setDebug(true)
- env["read"] = function()
- local output = rpc.remoteCall(remoteID, "read")
- return output
- end
- print("Generating environment...")
- print("Environment built.")
- env["write"] = function(text)
- return rpc.remoteCall(remoteID, "write", text)
- end
- env["print"] = function(...)
- return rpc.remoteCall(remoteID, "print", unpack(arg))
- end
- env["printLol"] = function() print("lol") end
- local function fn()
- print(type(rpc))
- parallel.waitForAll(
- function()
- print("Connection from "..remoteID)
- local tCommandHistory = {}
- while not bExit do
- write("remote> " )
- local sLine = rpc.remoteCall(remoteID, "read")
- print(sLine)
- table.insert( tCommandHistory, sLine )
- local words = {}
- for word in string.gmatch(sLine, "(%S+)") do
- table.insert(words, word)
- end
- os.run({}, unpack(words) )
- end
- end, rpc.run)
- end
- setfenv(fn, env)
- env.printLol()
- fn()
- print("Client disconnected.")
- term = oldTerm
- textutils = oldTextutils
- io = oldIO
- read = oldRead
- write = oldWrite]]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement