Advertisement
BigSHinyToys

remote access server on cahnnel 8

Apr 11th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.64 KB | None | 0 0
  1. --[[
  2.         telnet like remote access system
  3.         by BigSHinyToys
  4. ]]--
  5. --[[
  6. local oldError = error
  7. error = function(...)
  8.     local data = {...}
  9.     local file = fs.open("log","w")
  10.     for i = 1,#data do
  11.         file.write(tostring(data[1].." "))
  12.     end
  13.     file.write("\n")
  14.     file.close()
  15.     oldError(unpack(data))
  16. end
  17. ]]--
  18. local function hold() -- just used for error testing
  19.     print("HOLD")
  20.     local test
  21.     while test ~= 28 do
  22.         local _,bla = os.pullEvent("key")
  23.         test = bla
  24.     end
  25. end
  26.  
  27. local function findDevice(sType)
  28.     for _,sSide in pairs(rs.getSides()) do
  29.         if peripheral.isPresent(sSide) and peripheral.getType(sSide) == sType then
  30.             return sSide,peripheral.wrap(sSide)
  31.         end
  32.     end
  33. end
  34.  
  35. local function printTable(tInput) -- just used for error testing
  36.     if type(tInput) == "table" then
  37.         for i = 1,#tInput do
  38.             write(tostring(tInput[i]).." ")
  39.         end
  40.         print("")
  41.     else
  42.         print(tostring(tInput))
  43.     end
  44. end
  45.  
  46. local modemSide,modem = findDevice("modem")
  47.  
  48. local function wifiRedirect(sSide,freq)
  49.    
  50.     local modem = peripheral.wrap(sSide)
  51.     local freq = freq
  52.     local tFunctions = {
  53.     "scroll","setCursorBlink", -- "setCursorPos" "write"
  54.     "clear","clearLine","setTextColour","setTextColor",
  55.     "setBackgroundColour","setBackgroundColor",
  56.     }
  57.    
  58.     -- "getCursorPos","getSize","isColour","isColor",
  59.         local posX,posY = 1,1
  60.     local tRemote = {
  61.         getCursorPos = function()
  62.             return posX,posY
  63.         end,
  64.         setCursorPos = function(x,y)
  65.             posX = x
  66.             posY = y
  67.             modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
  68.         end,
  69.         getSize = function()
  70.             return 51,19
  71.         end,
  72.         write = function(line,...)
  73.         if line and type(line) == "string" then
  74.             posX = posX + #line
  75.         end
  76.             modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
  77.         end,
  78.     }
  79.     modem.transmit(freq,freq,"ISC")
  80.     local nTimer = os.startTimer(1)
  81.    
  82.     --[[while true do
  83.         local event = {os.pullEvent()}
  84.         if event[1] == "timer" and event[2] == nTimer then
  85.             error(" ERROR Remote time out")
  86.         elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  87.             local test,tInput = pcall(textutils.unserialize,event[5])
  88.             if test and type(tInput) == "table" then
  89.                 if tInput.cmd and tInput.cmd == "REPLY-SCP" then
  90.                     if tInput.test and tInput.test == true then
  91.                         tRemote.isColour = function()
  92.                             return true
  93.                         end
  94.                         tRemote.isColor = function()
  95.                             return true
  96.                         end
  97.                         break
  98.                     else
  99.                         tRemote.isColour = function()
  100.                             return false
  101.                         end
  102.                         tRemote.isColor = function()
  103.                             return false
  104.                         end
  105.                         break
  106.                     end
  107.                 end
  108.             end
  109.         end
  110.     end]]--
  111.    
  112.     -- temp
  113.     tRemote.isColor = function()
  114.         return true
  115.     end
  116.     tRemote.isColour = function()
  117.         return true
  118.     end
  119.     -- temp
  120.    
  121.     for i = 1,#tFunctions do
  122.         local funk = tFunctions[i]
  123.         tRemote[funk] = function(...)
  124.             modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
  125.         end
  126.     end
  127.    
  128.     return tRemote
  129. end
  130.  
  131. local function OldwifiRedirect(sSide,freq)
  132.    
  133.     local modem = peripheral.wrap(sSide)
  134.     local freq = freq
  135.     local tFunctions = {
  136.     "scroll","setCursorBlink", -- "setCursorPos" "write"
  137.     "clear","clearLine","setTextColour","setTextColor",
  138.     "setBackgroundColour","setBackgroundColor",
  139.     }
  140.    
  141.     -- "getCursorPos","getSize","isColour","isColor",
  142.         local posX,posY = 1,1
  143.     local tRemote = {
  144.         getCursorPos = function()
  145.             return posX,posY
  146.         end,
  147.         setCursorPos = function(x,y)
  148.             posX = x
  149.             posY = y
  150.             modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
  151.         end,
  152.         getSize = function()
  153.             return 51,19
  154.         end,
  155.         write = function(line,...)
  156.         if line and type(line) == "string" then
  157.             posX = posX + #line
  158.         end
  159.             modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
  160.         end,
  161.     }
  162.     modem.transmit(freq,freq,"ISC")
  163.     local nTimer = os.startTimer(1)
  164.    
  165.     while true do
  166.         local event = {os.pullEvent()}
  167.         if event[1] == "timer" and event[2] == nTimer then
  168.             error(" ERROR Remote time out")
  169.         elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  170.             local test,tInput = pcall(textutils.unserialize,event[5])
  171.             if test and type(tInput) == "table" then
  172.                 if tInput.cmd and tInput.cmd == "REPLY-SCP" then
  173.                     if tInput.test and tInput.test == true then
  174.                         tRemote.isColour = function()
  175.                             return true
  176.                         end
  177.                         tRemote.isColor = function()
  178.                             return true
  179.                         end
  180.                         break
  181.                     else
  182.                         tRemote.isColour = function()
  183.                             return false
  184.                         end
  185.                         tRemote.isColor = function()
  186.                             return false
  187.                         end
  188.                         break
  189.                     end
  190.                 end
  191.             end
  192.         end
  193.     end
  194.    
  195.     for i = 1,#tFunctions do
  196.         local funk = tFunctions[i]
  197.         tRemote[funk] = function(...)
  198.             modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
  199.         end
  200.     end
  201.     os.pullEvent = function(...)
  202.         local event = {os.pullEventRaw(...)}
  203.         if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  204.             local test,tInput = pcall(textutils.unserialize,event[5])
  205.             if test and type(tInput) == "table" then
  206.                 if tInput[1] == "INS" then
  207.                     table.remove(tInput,1)
  208.                     os.queueEvent(unpack(tInput))
  209.                 end
  210.             end
  211.         elseif event[1] == "terminate" then
  212.             error("Terminate")
  213.         else
  214.             return unpack(event)
  215.         end
  216.     end
  217.     term.redirect(tRemote)
  218. end
  219.  
  220. --[[
  221.     "getCursorPos","getSize","isColour","isColor"
  222. ]]--
  223.  
  224. local freq
  225. local user
  226. local pass
  227.  
  228. local findFreq = 2
  229. local tConnections = {}
  230.  
  231. local function newUser(freq)
  232.     local tEnv = {}
  233.     tConnections[freq] = {
  234.         authenticated = true,
  235.         --tEnv = {},
  236.     }
  237.     setmetatable(tEnv,{__index = _G}) -- tConnections[freq].tEnv
  238.     modem.open(freq)
  239.     tEnv.term = wifiRedirect(modemSide,freq)
  240.     tEnv.term.native = wifiRedirect(modemSide,freq)
  241.     tEnv.term.restore = function()
  242.         for name,funk in pairs(tEnv.term.native) do
  243.             tEnv.term[name] = funk
  244.         end
  245.     end
  246.     tEnv.write = write
  247.     tEnv.print = print
  248.     tEnv.read = read
  249.     setfenv(tEnv.write,tEnv)
  250.     setfenv(tEnv.print,tEnv)
  251.     setfenv(tEnv.read,tEnv)
  252.     --tEnv.term.shell = nil
  253.     print(tostring(tEnv.term))
  254.     print(tostring(term))
  255.     local function instance()
  256.         return os.run(tEnv,"rom/programs/shell")
  257.     end
  258.     tConnections[freq].co = coroutine.create(instance)
  259. end
  260. --[[
  261. --return os.run(tEnv,"rom/user/project/mouseFB/mouse_file_browser1.4.lua")
  262. local function newUser(freq)
  263.     local tEnv = {}
  264.     tConnections[freq] = {
  265.         authenticated = true,
  266.         --tEnv = {},
  267.     }
  268.     setmetatable(tEnv,{__index = _G}) -- tConnections[freq].tEnv
  269.     modem.open(freq)
  270.     tEnv.term = wifiRedirect(modemSide,freq)
  271.     tEnv.term.native = wifiRedirect(modemSide,freq)
  272.     print(tostring(tEnv.term))
  273.     print(tostring(term))
  274.     local function instance()
  275.         return os.run(tEnv,"rom/user/project/mouseFB/mouse_file_browser1.4.lua")
  276.     end
  277.     tConnections[freq].co = coroutine.create(instance)
  278. end
  279. ]]--
  280. newUser(8)
  281.  
  282. local function main()
  283.    
  284.     while true do
  285.         local event = {os.pullEvent()}
  286.         if event[1] == "modem_message" and event[2] == modemSide then
  287.             if findFreq == event[3] then
  288.             elseif tConnections[event[3]] then
  289.                 if tConnections[event[3]].authenticated then
  290.                     local test,tInput = pcall(textutils.unserialize,event[5])
  291.                     if test and type(tInput) == "table" then
  292.                         if tInput[1] == "INS" then
  293.                             table.remove(tInput,1)
  294.                             local test,err = coroutine.resume(tConnections[event[3]].co,unpack(tInput))
  295.                             if err then
  296.                                 print(err)
  297.                                 hold()
  298.                             end
  299.                         end
  300.                     end
  301.                 else
  302.                    
  303.                 end
  304.             end
  305.         end
  306.     end
  307. end
  308.  
  309. local function Oldmain()
  310.     os.startTimer(0.5)
  311.     while true do
  312.        
  313.         local event = {os.pullEvent()}
  314.         if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  315.             local test,tInput = pcall(textutils.unserialize,event[5])
  316.             if test and type(tInput) == "table" then
  317.                 if tInput.user and tInput.user == user and tInput.password and tInput.password == pass then
  318.                     print("Authenticated user!")
  319.                     wifiRedirect(modemSide,freq)
  320.                     return
  321.                 end
  322.             end
  323.         elseif event[1] == "timer" then
  324.            
  325.         end
  326.        
  327.         --printTable(event)
  328.        
  329.     end
  330. end
  331.  
  332. local tArg = {...}
  333.  
  334. if modemSide then
  335.     print(" --- Remote access server --- ")
  336.     --[[while true do
  337.         write("Choose Freq : ")
  338.         local temp = tArg[1] or read()
  339.         if string.lower(temp) == "exit" then
  340.             print(" --- Server stoping --- ")
  341.             return
  342.         end
  343.         temp = tonumber(temp)
  344.         if type(temp) == "number" and temp >= 0 and temp <= 65535 then
  345.             freq = temp
  346.             break
  347.         end
  348.         print("ERROR : Range 0 - 65535 or Exit")
  349.     end]]--
  350.     write("User : ")
  351.     user = tArg[2] or read()
  352.     write("Password : ")
  353.     pass = tArg[3] or read("*")
  354.     print("Opening modem : "..modemSide.." Freq : "..tostring(findFreq))
  355.     modem.open(findFreq)
  356.     main()
  357. else
  358.     print("ERROR : Failed to find modem ")
  359. end
  360.  
  361. --[[
  362. freq
  363. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement