Advertisement
Guest User

net.lua

a guest
Nov 22nd, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. local c = require("component")
  2. local term = require("term")
  3. local fs = require("filesystem")
  4. local shell = require("shell")
  5. local event = require("event")
  6. local s = require("serialization")
  7. local text = require("text")
  8. local m
  9. local gpu
  10. local logged = false
  11.  
  12. local port = 121
  13.  
  14. term.clear()
  15.  
  16. if not c.isAvailable("gpu") then
  17.   c.computer.beep(1500,2)
  18.   os.exit()
  19. else
  20.   gpu = c.gpu
  21. end
  22.  
  23. if c.isAvailable("modem") then
  24.   if c.isAvailable("tunnel") then
  25.     print("Linked card and wireless card discovered! Use Linked[1] or Wireless[2]?")
  26.     local valid = false
  27.     while not valid do
  28.       local choice = io.read()
  29.       if choice == "1" then
  30.         print("Linked card selected for network")
  31.         m = c.tunnel
  32.         valid = true
  33.         mode = "tunnel"
  34.       elseif choice == "2" then
  35.         print("Wireless card selected for network")
  36.         m = c.modem
  37.         m.open(port)
  38.         mode = "wireless"
  39.         valid = true
  40.       else
  41.         print("Invalid input. please enter [1] or [2].")
  42.       end
  43.     end
  44.   else
  45.     m = c.modem
  46.     mode = "wireless"
  47.     m.open(port)
  48.     print("Wireless card selected for network")
  49.   end
  50. elseif c.isAvailable("tunnel") then
  51.   m = c.tunnel
  52.   mode = "tunnel"
  53.   print("Linked card selected for network")
  54. end
  55.  
  56. function ping(time)
  57.   if mode == "wireless" then
  58.     print("sending wireless request")
  59.     m.broadcast(port, s.serialize({"ping"}))
  60.   elseif mode == "tunnel" then
  61.     print("Sending linked request")
  62.     m.send(s.serialize({"ping"}))
  63.   end
  64.  
  65.   local Type, _, _, _, _, message = event.pull(10,"modem_message")
  66.   if not Type then
  67.     print("No response from server, check server is online and you are in range")
  68.     os.exit()
  69.   elseif message == "pong" then
  70.     print("Response recieved!")
  71.   end
  72. end
  73.  
  74. function login(time, user)
  75.   if mode == "wireless" then
  76.     m.broadcast(port, s.serialize(user))
  77.   elseif mode == "tunnel" then
  78.     m.send(s.serialize(user))
  79.   end
  80.  
  81.   local Type, _, _, _, _, message = event.pull(10,"modem_message")
  82.   if not Type then
  83.     print("\nRequest timed out.")
  84.     os.exit()
  85.   elseif message == "valid" then
  86.     print("\nCredentials valid!")
  87.     logged = true
  88.   elseif message == "invalid" then
  89.     print("\nCredentials invalid!")
  90.   end
  91. end
  92.  
  93. gpu.setForeground(0xFF00FF)
  94. print("Attempting to contact authorisation servers.")
  95. ping(10)
  96. gpu.setForeground(0x00FF00)
  97.  
  98. while not logged do
  99.   print("Enter username: ")
  100.   local x, y = term.getCursor()
  101.   term.setCursor(17, y-1)
  102.   local username = io.read()
  103.  
  104.   print("\nEnter Password: ")
  105.   local x, y = term.getCursor()
  106.   term.setCursor(17, y-1)
  107.   local password = text.trim(term.read(nil, false, nil, "*"))
  108.  
  109.   local user = {username, password}
  110.   login(5, user)
  111. end
  112.  
  113. while true do
  114.   while logged do
  115.     gpu.setForeground(0xFF0000)
  116.     local wd = shell.getWorkingDirectory()
  117.     print(wd.." # ")
  118.     gpu.setForeground(0xFFFFFF)
  119.     local x, y = term.getCursor()
  120.     term.setCursor(string.len(wd)+4, y-1)  
  121.    
  122.     local input = io.read()
  123.     os.execute(input)
  124.   end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement