Advertisement
Guest User

startup

a guest
Dec 19th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. local connectedClients = {}
  2. local authentications = {
  3.   {user = "admin", pass = "1234"}
  4. }
  5. local ruser = 1
  6. local pivotSender = nil
  7. local pivotID = nil
  8.  
  9. function clear()
  10.   term.clear()
  11.   term.setCursorPos(1,1)
  12. end
  13.  
  14. function out(dat)
  15.   print(dat)
  16.   --rednet.send(ruser,"print(dat)")
  17. end
  18.  
  19. function logout()
  20.   rednet.send(ruser,"print('goodbye!') return")
  21. end
  22.  
  23. function authenticate(user, pass)
  24.   for i,v in ipairs(authentications) do
  25.     if user == v.user and pass == v.pass then
  26.       return true
  27.     end
  28.   end
  29.   return false
  30. end
  31.  
  32. function isConnected(num)
  33.   for i,v in ipairs(connectedClients) do
  34.     if v == num then
  35.       return true
  36.     end
  37.   end
  38.   for i,v in ipairs(peripheral.getNames()) do
  39.     if peripheral.getType(v) == "computer" then
  40.       if num == peripheral.wrap(v).getID() then
  41.         return true
  42.       end
  43.     end
  44.   end
  45.   return false
  46. end
  47.  
  48. for i,v in ipairs(peripheral.getNames()) do
  49.   if peripheral.getType(v) == "modem" then
  50.     rednet.open(v)
  51.   end
  52. end
  53.  
  54. function fakeShell()
  55.   term.setTextColor(colors.yellow)
  56.   term.write("> ")
  57.   term.setTextColor(colors.white)
  58.   input = read()
  59.   shell.run(input)
  60. end
  61.  
  62. function network()
  63.   id,msg,d = rednet.receive()
  64.   if pivotID ~= nil and id == pivotID then
  65.     if type(msg) == "string" and msg == "breakpivot" then
  66.       pivotID = nil
  67.       pivotSender = nil
  68.     else
  69.       rednet.send(pivotSender,msg)
  70.     end
  71.   elseif isConnected(id) then
  72.     if pivotID ~= nil then
  73.       print(textutils.serialise(pivotID))
  74.       rednet.send(pivotID, msg)
  75.     elseif type(msg) == "string" and msg == "breakpivot" then
  76.       rednet.send(id,"breakpivot")
  77.     elseif type(msg) == "table" then
  78.       if msg[1] == "run" then
  79.         shell.run(msg[2])
  80.       elseif msg[1] == "rs" then
  81.         for i,v in ipairs(rs.getSides()) do
  82.           if msg[2] == "on" then
  83.             rs.setOutput(v,true)
  84.           else
  85.             rs.setOutput(v,false)
  86.           end
  87.         end
  88.       elseif msg[1] == "id" then
  89.         rednet.send(id,{"id",os.getComputerID()})
  90.       elseif msg[1] == "peak" then
  91.         if fs.exists(msg[2]) then
  92.           local lines = {}
  93.           local file = fs.open(msg[2],"r")
  94.           while true do
  95.             local line = file.readLine()
  96.             if line == nil then
  97.               break
  98.             else
  99.               table.insert(lines,line)
  100.             end
  101.           end
  102.           rednet.send(id,{"peak",lines})
  103.         else
  104.           rednet.send(id,"print('File does not exist')")
  105.         end
  106.       elseif msg[1] == "getconnected" then
  107.         local ids = {}
  108.         for i,v in ipairs(peripheral.getNames()) do
  109.           if peripheral.getType(v) == "computer" then
  110.             table.insert(ids,peripheral.wrap(v).getID())
  111.           end
  112.         end
  113.         rednet.send(id,{"getconnected",ids})
  114.       elseif msg[1] == "ls" then
  115.         local list = fs.list("/")
  116.         rednet.send(id,{"ls",list})
  117.       elseif msg[1] == "pivot" and type(msg[2]) == "number" then
  118.         print("TRYING TO PIVOT INTO: "..msg[2])
  119.         pivotID = msg[2]
  120.         print(type(pivotID))
  121.         rednet.send(pivotID,"print('hi')")
  122.         id2,msg,d = rednet.receive(.3)
  123.         if msg == nil then
  124.           pivotSender = id
  125.           rednet.send(id,"print() print('PIVOT SUCCESSFUL')")
  126.         else
  127.           pivotID = nil
  128.         end
  129.       end
  130.     else
  131.       ruser = id
  132.       f = loadstring(msg)
  133.       if not pcall(f) then
  134.         rednet.send(ruser, "print('Error with last command')")
  135.       end
  136.       print()
  137.     end
  138.   elseif type(msg) == "table" then
  139.     if msg[1] == "AUTHENTICATE" then
  140.       if authenticate(msg[2],msg[3]) then
  141.         table.insert(connectedClients,id)
  142.         rednet.send(id,"AUTHENTICATED")
  143.       end
  144.     end
  145.     clear()
  146.   else
  147.     rednet.send(id,"AUTHENTICATE")
  148.     clear()
  149.   end
  150. end
  151.  
  152. if fs.exists(".start") then
  153.   fakeShell = function()
  154.     shell.run(".start")
  155.   end
  156. end
  157.  
  158. while true do
  159.   parallel.waitForAny(fakeShell,network)
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement