BigSHinyToys

Remote terminal Server mk2

May 11th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. --[[
  2.         wifi server mk2
  3.         by BigSHinyToys
  4. ]]--
  5.  
  6. local oldError = error
  7. error = function(...)
  8.     local data = {...}
  9.     local file = fs.open("log","a")
  10.     if file then
  11.         for i = 1,#data do
  12.             file.write(tostring(data[i]).." ")
  13.         end
  14.         file.write("\n")
  15.         file.close()
  16.     end
  17.     oldError(unpack(data))
  18. end
  19.  
  20. local function hold() -- just used for error testing
  21.     print("HOLD")
  22.     local test
  23.     while test ~= 28 do
  24.         local _,bla = os.pullEvent("key")
  25.         test = bla
  26.     end
  27. end
  28.  
  29. local function findDevice(sType)
  30.     for _,sSide in pairs(rs.getSides()) do
  31.         if peripheral.isPresent(sSide) and peripheral.getType(sSide) == sType then
  32.             return sSide,peripheral.wrap(sSide)
  33.         end
  34.     end
  35. end
  36.  
  37. local function printTable(tInput) -- just used for error testing
  38.     if type(tInput) == "table" then
  39.         for i = 1,#tInput do
  40.             write(tostring(tInput[i]).." ")
  41.         end
  42.         print("")
  43.     else
  44.         print(tostring(tInput))
  45.     end
  46. end
  47.  
  48. local modemSide,modem = findDevice("modem")
  49. local tDraw = {}
  50.  
  51. local oldEvent = os.pullEventRaw
  52.  
  53. local speed = 0.1
  54.  
  55. local function wifiRedirect(sSide,freq)
  56.    
  57.     local modem = peripheral.wrap(sSide)
  58.     local freq = freq
  59.     local tFunctions = {
  60.     "scroll","setCursorBlink", -- "setCursorPos" "write"
  61.     "clear","clearLine","setTextColour","setTextColor",
  62.     "setBackgroundColour","setBackgroundColor",
  63.     }
  64.    
  65.     -- "getCursorPos","getSize","isColour","isColor",
  66.     local posX,posY = 1,1
  67.     local tRemote = {
  68.         getCursorPos = function()
  69.             return posX,posY
  70.         end,
  71.         setCursorPos = function(x,y)
  72.             posX = x
  73.             posY = y
  74.             --modem.transmit(freq,freq,textutils.serialize({"setCursorPos",x,y}))
  75.             if not sendTimer then
  76.                 sendTimer = os.startTimer(speed)
  77.             end
  78.             table.insert(tDraw,textutils.serialize({"setCursorPos",x,y}))
  79.         end,
  80.         write = function(line,...)
  81.         if line and type(line) == "string" then
  82.             posX = posX + #line
  83.         end
  84.             --modem.transmit(freq,freq,textutils.serialize({"write",line,...}))
  85.             if not sendTimer then
  86.                 sendTimer = os.startTimer(speed)
  87.             end
  88.             table.insert(tDraw,textutils.serialize({"write",line,...}))
  89.         end,
  90.     }
  91.     modem.transmit(freq,freq,"ISC")
  92.     local nTimer = os.startTimer(1)
  93.    
  94.     while true do
  95.         local event = {os.pullEvent()}
  96.         if event[1] == "timer" and event[2] == nTimer then
  97.             error(" ERROR Remote time out")
  98.         elseif event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  99.             local test,tInput = pcall(textutils.unserialize,event[5])
  100.             if test and type(tInput) == "table" then
  101.                 if tInput.cmd and tInput.cmd == "REPLY-SCP" then
  102.                     if tInput.test and tInput.test == true then
  103.                         tRemote.isColour = function()
  104.                             return true
  105.                         end
  106.                         tRemote.isColor = function()
  107.                             return true
  108.                         end
  109.                     else
  110.                         tRemote.isColour = function()
  111.                             return false
  112.                         end
  113.                         tRemote.isColor = function()
  114.                             return false
  115.                         end
  116.                     end
  117.                     if tInput.h and type(tInput.h) == "number" and tInput.w and type(tInput.w) == "number" then
  118.                         local w,h = tInput.w,tInput.h
  119.                         tRemote.getSize = function()
  120.                             return w,h
  121.                         end
  122.                     else
  123.                         error("Somthing went wrong / wrong ver")
  124.                     end
  125.                     break
  126.                 end
  127.             end
  128.         end
  129.     end
  130.    
  131.     for i = 1,#tFunctions do
  132.         local funk = tFunctions[i]
  133.         tRemote[funk] = function(...)
  134.             if not sendTimer then
  135.                 sendTimer = os.startTimer(speed)
  136.             end
  137.             table.insert(tDraw,textutils.serialize({tFunctions[i],...}))
  138.             --modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
  139.         end
  140.     end
  141.     os.pullEventRaw = function(...)
  142.         while true do
  143.             local event = {coroutine.yield(...)}
  144.             if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  145.                 local test,tInput = pcall(textutils.unserialize,event[5])
  146.                 if test and type(tInput) == "table" then
  147.                     if tInput[1] == "INS" then
  148.                         table.remove(tInput,1)
  149.                         return unpack(tInput)
  150.                     end
  151.                 end
  152.             elseif event[1] == "timer" and event[2] == sendTimer then
  153.                 sendTimer = false
  154.                 modem.transmit(freq,freq,textutils.serialize(tDraw))
  155.                 tDraw = {}
  156.             else
  157.                 return unpack(event)
  158.             end
  159.         end
  160.     end
  161.     term.redirect(tRemote)
  162. end
  163.  
  164. --[[
  165.     "getCursorPos","getSize","isColour","isColor"
  166. ]]--
  167.  
  168. local freq
  169. local user
  170. local pass
  171.  
  172. local function main()
  173.     os.startTimer(0.5)
  174.     while true do
  175.        
  176.         local event = {os.pullEvent()}
  177.         if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  178.             local test,tInput = pcall(textutils.unserialize,event[5])
  179.             if test and type(tInput) == "table" then
  180.                 if tInput.user and tInput.user == user and tInput.password and tInput.password == pass then
  181.                     print("Authenticated user!")
  182.                     wifiRedirect(modemSide,freq)
  183.                     return
  184.                 end
  185.             end
  186.         elseif event[1] == "timer" then
  187.            
  188.         end
  189.        
  190.         --printTable(event)
  191.        
  192.     end
  193. end
  194.  
  195. local tArg = {...}
  196.  
  197. if modemSide then
  198.     print(" --- Remote access server --- ")
  199.     while true do
  200.         write("Choose Freq : ")
  201.         local temp = tArg[1] or read()
  202.         if string.lower(temp) == "exit" then
  203.             print(" --- Server stoping --- ")
  204.             return
  205.         end
  206.         temp = tonumber(temp)
  207.         if type(temp) == "number" and temp >= 0 and temp <= 65535 then
  208.             freq = temp
  209.             break
  210.         end
  211.         print("ERROR : Range 0 - 65535 or Exit")
  212.     end
  213.     write("User : ")
  214.     user = tArg[2] or read()
  215.     write("Password : ")
  216.     pass = tArg[3] or read("*")
  217.     print("Opening modem : "..modemSide.." Freq : "..tostring(freq))
  218.     modem.open(freq)
  219.     modem.transmit(65535,freq,"Telnet server")
  220.     main()
  221. else
  222.     print("ERROR : Failed to find modem ")
  223. end
Advertisement
Add Comment
Please, Sign In to add comment