Advertisement
BigSHinyToys

telnet server

Apr 9th, 2013
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --[[
  2.         wifi B****
  3.         mk2
  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.     for i = 1,#tFunctions do
  113.         local funk = tFunctions[i]
  114.         tRemote[funk] = function(...)
  115.             modem.transmit(freq,freq,textutils.serialize({tFunctions[i],...}))
  116.         end
  117.     end
  118.     os.pullEvent = function(...)
  119.         local event = {os.pullEventRaw(...)}
  120.         if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  121.             local test,tInput = pcall(textutils.unserialize,event[5])
  122.             if test and type(tInput) == "table" then
  123.                 if tInput[1] == "INS" then
  124.                     table.remove(tInput,1)
  125.                     os.queueEvent(unpack(tInput))
  126.                 end
  127.             end
  128.         elseif event[1] == "terminate" then
  129.             error("Terminate")
  130.         else
  131.             return unpack(event)
  132.         end
  133.     end
  134.     term.redirect(tRemote)
  135. end
  136.  
  137. --[[
  138.     "getCursorPos","getSize","isColour","isColor"
  139. ]]--
  140.  
  141. local freq
  142. local user
  143. local pass
  144.  
  145. local function main()
  146.     os.startTimer(0.5)
  147.     while true do
  148.        
  149.         local event = {os.pullEvent()}
  150.         if event[1] == "modem_message" and event[2] == modemSide and freq == event[3] then
  151.             local test,tInput = pcall(textutils.unserialize,event[5])
  152.             if test and type(tInput) == "table" then
  153.                 if tInput.user and tInput.user == user and tInput.password and tInput.password == pass then
  154.                     print("Authenticated user!")
  155.                     wifiRedirect(modemSide,freq)
  156.                     return
  157.                 end
  158.             end
  159.         elseif event[1] == "timer" then
  160.            
  161.         end
  162.        
  163.         --printTable(event)
  164.        
  165.     end
  166. end
  167.  
  168. local tArg = {...}
  169.  
  170. if modemSide then
  171.     print(" --- Remote access server --- ")
  172.     while true do
  173.         write("Choose Freq : ")
  174.         local temp = tArg[1] or read()
  175.         if string.lower(temp) == "exit" then
  176.             print(" --- Server stoping --- ")
  177.             return
  178.         end
  179.         temp = tonumber(temp)
  180.         if type(temp) == "number" and temp >= 0 and temp <= 65535 then
  181.             freq = temp
  182.             break
  183.         end
  184.         print("ERROR : Range 0 - 65535 or Exit")
  185.     end
  186.     write("User : ")
  187.     user = tArg[2] or read()
  188.     write("Password : ")
  189.     pass = tArg[3] or read("*")
  190.     print("Opening modem : "..modemSide.." Freq : "..tostring(freq))
  191.     modem.open(freq)
  192.     main()
  193. else
  194.     print("ERROR : Failed to find modem ")
  195. end
  196.  
  197. --[[
  198. freq
  199. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement