Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- telnet like remote access system
- by BigSHinyToys
- ]]--
- --[[
- local oldError = error
- error = function(...)
- local data = {...}
- local file = fs.open("log","w")
- for i = 1,#data do
- file.write(tostring(data[1].." "))
- end
- file.write("\n")
- file.close()
- oldError(unpack(data))
- end
- ]]--
- local function hold() -- just used for error testing
- print("HOLD")
- local test
- while test ~= 28 do
- local _,bla = os.pullEvent("key")
- test = bla
- end
- end
- local function findDevice(sType)
- for _,sSide in pairs(rs.getSides()) do
- if peripheral.isPresent(sSide) and peripheral.getType(sSide) == sType then
- return sSide,peripheral.wrap(sSide)
- end
- end
- end
- local function printTable(tInput) -- just used for error testing
- if type(tInput) == "table" then
- for i = 1,#tInput do
- write(tostring(tInput[i]).." ")
- end
- print("")
- else
- print(tostring(tInput))
- end
- end
- local modemSide,modem = findDevice("modem")
- local function wifiRedirect(sSide,freq)
- local modem = peripheral.wrap(sSide)
- local freq = freq
- local tFunctions = {
- "scroll","setCursorBlink", -- "setCursorPos" "write"
- "clear","clearLine","setTextColour","setTextColor",
- "setBackgroundColour","setBackgroundColor",
- }
- -- "getCursorPos","getSize","isColour","isColor",
- local posX,posY = 1,1
- local tRemote = {
- getCursorPos = function()
- return posX,posY
- end,
- setCursorPos = function(x,y)
- posX = x
- posY = y
- modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
- end,
- getSize = function()
- return 51,19
- end,
- write = function(line,...)
- if line and type(line) == "string" then
- posX = posX + #line
- end
- modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
- end,
- }
- modem.transmit(freq,freq,"ISC")
- local nTimer = os.startTimer(1)
- --[[while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == nTimer then
- error(" ERROR Remote time out")
- elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput.cmd and tInput.cmd == "REPLY-SCP" then
- if tInput.test and tInput.test == true then
- tRemote.isColour = function()
- return true
- end
- tRemote.isColor = function()
- return true
- end
- break
- else
- tRemote.isColour = function()
- return false
- end
- tRemote.isColor = function()
- return false
- end
- break
- end
- end
- end
- end
- end]]--
- -- temp
- tRemote.isColor = function()
- return true
- end
- tRemote.isColour = function()
- return true
- end
- -- temp
- for i = 1,#tFunctions do
- local funk = tFunctions[i]
- tRemote[funk] = function(...)
- modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
- end
- end
- return tRemote
- end
- local function OldwifiRedirect(sSide,freq)
- local modem = peripheral.wrap(sSide)
- local freq = freq
- local tFunctions = {
- "scroll","setCursorBlink", -- "setCursorPos" "write"
- "clear","clearLine","setTextColour","setTextColor",
- "setBackgroundColour","setBackgroundColor",
- }
- -- "getCursorPos","getSize","isColour","isColor",
- local posX,posY = 1,1
- local tRemote = {
- getCursorPos = function()
- return posX,posY
- end,
- setCursorPos = function(x,y)
- posX = x
- posY = y
- modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
- end,
- getSize = function()
- return 51,19
- end,
- write = function(line,...)
- if line and type(line) == "string" then
- posX = posX + #line
- end
- modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
- end,
- }
- modem.transmit(freq,freq,"ISC")
- local nTimer = os.startTimer(1)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == nTimer then
- error(" ERROR Remote time out")
- elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput.cmd and tInput.cmd == "REPLY-SCP" then
- if tInput.test and tInput.test == true then
- tRemote.isColour = function()
- return true
- end
- tRemote.isColor = function()
- return true
- end
- break
- else
- tRemote.isColour = function()
- return false
- end
- tRemote.isColor = function()
- return false
- end
- break
- end
- end
- end
- end
- end
- for i = 1,#tFunctions do
- local funk = tFunctions[i]
- tRemote[funk] = function(...)
- modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
- end
- end
- os.pullEvent = function(...)
- local event = {os.pullEventRaw(...)}
- if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput[1] == "INS" then
- table.remove(tInput,1)
- os.queueEvent(unpack(tInput))
- end
- end
- elseif event[1] == "terminate" then
- error("Terminate")
- else
- return unpack(event)
- end
- end
- term.redirect(tRemote)
- end
- --[[
- "getCursorPos","getSize","isColour","isColor"
- ]]--
- local freq
- local user
- local pass
- local findFreq = 2
- local tConnections = {}
- local function newUser(freq)
- local tEnv = {}
- tConnections[freq] = {
- authenticated = true,
- --tEnv = {},
- }
- setmetatable(tEnv,{__index = _G}) -- tConnections[freq].tEnv
- modem.open(freq)
- tEnv.term = wifiRedirect(modemSide,freq)
- tEnv.term.native = wifiRedirect(modemSide,freq)
- tEnv.term.restore = function()
- for name,funk in pairs(tEnv.term.native) do
- tEnv.term[name] = funk
- end
- end
- tEnv.write = write
- tEnv.print = print
- tEnv.read = read
- setfenv(tEnv.write,tEnv)
- setfenv(tEnv.print,tEnv)
- setfenv(tEnv.read,tEnv)
- --tEnv.term.shell = nil
- print(tostring(tEnv.term))
- print(tostring(term))
- local function instance()
- return os.run(tEnv,"rom/programs/shell")
- end
- tConnections[freq].co = coroutine.create(instance)
- end
- --[[
- --return os.run(tEnv,"rom/user/project/mouseFB/mouse_file_browser1.4.lua")
- local function newUser(freq)
- local tEnv = {}
- tConnections[freq] = {
- authenticated = true,
- --tEnv = {},
- }
- setmetatable(tEnv,{__index = _G}) -- tConnections[freq].tEnv
- modem.open(freq)
- tEnv.term = wifiRedirect(modemSide,freq)
- tEnv.term.native = wifiRedirect(modemSide,freq)
- print(tostring(tEnv.term))
- print(tostring(term))
- local function instance()
- return os.run(tEnv,"rom/user/project/mouseFB/mouse_file_browser1.4.lua")
- end
- tConnections[freq].co = coroutine.create(instance)
- end
- ]]--
- newUser(8)
- local function main()
- while true do
- local event = {os.pullEvent()}
- if event[1] == "modem_message" and event[2] == modemSide then
- if findFreq == event[3] then
- elseif tConnections[event[3]] then
- if tConnections[event[3]].authenticated then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput[1] == "INS" then
- table.remove(tInput,1)
- local test,err = coroutine.resume(tConnections[event[3]].co,unpack(tInput))
- if err then
- print(err)
- hold()
- end
- end
- end
- else
- end
- end
- end
- end
- end
- local function Oldmain()
- os.startTimer(0.5)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
- local test,tInput = pcall(textutils.unserialize,event[5])
- if test and type(tInput) == "table" then
- if tInput.user and tInput.user == user and tInput.password and tInput.password == pass then
- print("Authenticated user!")
- wifiRedirect(modemSide,freq)
- return
- end
- end
- elseif event[1] == "timer" then
- end
- --printTable(event)
- end
- end
- local tArg = {...}
- if modemSide then
- print(" --- Remote access server --- ")
- --[[while true do
- write("Choose Freq : ")
- local temp = tArg[1] or read()
- if string.lower(temp) == "exit" then
- print(" --- Server stoping --- ")
- return
- end
- temp = tonumber(temp)
- if type(temp) == "number" and temp >= 0 and temp <= 65535 then
- freq = temp
- break
- end
- print("ERROR : Range 0 - 65535 or Exit")
- end]]--
- write("User : ")
- user = tArg[2] or read()
- write("Password : ")
- pass = tArg[3] or read("*")
- print("Opening modem : "..modemSide.." Freq : "..tostring(findFreq))
- modem.open(findFreq)
- main()
- else
- print("ERROR : Failed to find modem ")
- end
- --[[
- freq
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement