Advertisement
Guest User

servertest.lua

a guest
Sep 11th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -----------------------------------------------
  2. --Copyright 2018 by Dusk the Dutch Angel Dragon
  3. -----------------------------------------------
  4. sides = {"top", "back", "left", "right", "bottom"}
  5. allowedID = {}
  6. username = {}
  7. password = {}
  8. modem = "null"
  9.  
  10. --Define Functions
  11. function init()
  12.  print("Initalizing Server...")
  13.  print("Initalizing modem...")
  14.  for i=1, #sides do
  15.   if peripheral.getType(sides[i]) == "modem" then
  16.    modem = sides[i]
  17.    print("Modem found on "..sides[i].. " side.")
  18.    rednet.open(modem)
  19.    break
  20.   elseif i == #sides and modem == "null" then
  21.    error("Fatal Error: No Modem found.")
  22.   end
  23.  end
  24.  
  25.  username = loadTable("uname")
  26.  password = loadTable("pword")
  27.  allowedID = loadTable("whitelist")
  28. end
  29.  
  30. function checkTable(table, chkv)
  31.  for i=1, #table do
  32.   if table[i] == chkv then
  33.    return i
  34.   elseif i == #table and table[i] ~= chkv then
  35.    return "nomatch"
  36.   end
  37.  end
  38. end
  39.  
  40. function loadTable(file)
  41.  --Load table contents
  42.  if fs.exists(file) then
  43.   f = fs.open(file, "r")
  44.   d = f.readAll()
  45.   f.close()
  46.   return(textutils.unserialise(d))
  47.  else
  48.   error("File not Found")
  49.  end
  50. end
  51.  
  52. function saveTable(file, table)
  53.  --Save table contents
  54.  f = fs.open(file, "w")
  55.  f.write(textutils.serialise(table))
  56.  f.close()
  57. end
  58.  
  59. function filterResponse(filterID)
  60.  rid = -1
  61.  while rid ~= filterID do
  62.   rid, msg = rednet.receive()
  63.  end
  64.  return msg
  65. end
  66.  
  67. function checkWhitelist(compID)
  68.  for i=1, #allowedID do
  69.   if allowedID[i] == compID then
  70.    return true
  71.   elseif i == #allowedID and allowedID[i] ~= compID then
  72.    return false
  73.   end
  74.  end
  75. end
  76.  
  77. --Main Code
  78. init()
  79. while true do
  80.  term.clear()
  81.  term.setCursorPos(1,1)
  82.  print("Choose an Option:")
  83.  print("1: Start Server")
  84.  print("2: Add User")
  85.  print("3: Remove User")
  86.  print("4: Add Whitelist ID")
  87.  print("5: Revoke Whitelist ID")
  88.  print("6: Exit Program")
  89.  print("")
  90.  write("Your Choice: ")
  91.  x = read()
  92. --------------------------------------------------
  93.  if tonumber(x) == 1 then
  94.   --Server Code
  95.   print("Server started...")
  96.   while true do
  97.    id, msg = rednet.receive()
  98.    if checkWhitelist(id) then
  99.     sleep(0.1)
  100.     rednet.send(id, "ready")
  101.     subUname = filterResponse(id)
  102.     subPword = filterResponse(id)
  103.     chkUname = checkTable(username,subUname)
  104.     if password[chkUname] == subPword then
  105.      rednet.send(id, "auth")
  106.     else
  107.      rednet.send(id, "nauth")
  108.     end
  109.    else
  110.     rednet.send(id, 400)
  111.    end
  112.   end
  113. --------------------------------------------------
  114.  elseif tonumber(x) == 2 then
  115.   write("Username?: ")
  116.   uname = read()
  117.   print("")
  118.   write("Password?: ")
  119.   pword = read()
  120.   print("")
  121.   print("Creating user...")
  122.   table.insert(username, uname)
  123.   table.insert(password, pword)
  124.   print("User created, saving tables...")
  125.   saveTable("uname", username)
  126.   saveTable("pword", password)
  127.   print("Tables saved successfully!")
  128.  elseif tonumber(x) == 3 then
  129.   write("Username of user to delete?: ")
  130.   uname = read()
  131.   for i=1, #username do
  132.    if username[i] == uname then
  133.     table.remove(username, i)
  134.     table.remove(password, i)
  135.     print("User removed, saving tables...")
  136.     saveTable("uname", username)
  137.     saveTable("pword", password)
  138.     print("Tables saved successfully!")
  139.    elseif i == #username and uname ~= username[i] then
  140.     print("Error: User not found.")
  141.    end
  142.   end
  143.   --
  144.  elseif tonumber(x) == 4 then
  145.   write("Computer ID to whitelist?: ")
  146.   compID = tonumber(read())
  147.   table.insert(allowedID, compID)
  148.   saveTable("whitelist", allowedID)
  149.   print("Computer whitelisted successfully!")
  150.   --
  151.  elseif tonumber(x) == 5 then
  152.   write("Computer ID to dewhitelist?: ")
  153.   compID = tonumber(read())
  154.   for i=1, #allowedID do
  155.    if allowedID[i] == compID then
  156.     table.remove(allowedID, i)
  157.     saveTable("whitelist", allowedID)
  158.     print("Computer dewhitelisted successfully!")
  159.    elseif i == #allowedID and allowedID[i] ~= compID then
  160.     print("Error: Computer ID not found.")
  161.    end
  162.   end
  163.   --
  164.  elseif tonumber(x) == 6 then
  165.   print("Exiting...")
  166.   error()
  167.  end
  168.  sleep(1)
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement