Advertisement
Guest User

server

a guest
Sep 12th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 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.   print("File not found: "..file)
  49.   sleep(0.1)
  50.   return {}
  51.  end
  52. end
  53.  
  54. function saveTable(file, table)
  55.  --Save table contents
  56.  f = fs.open(file, "w")
  57.  f.write(textutils.serialise(table))
  58.  f.close()
  59. end
  60.  
  61. function filterResponse(filterID)
  62.  rid = -1
  63.  while rid ~= filterID do
  64.   rid, msg = rednet.receive()
  65.  end
  66.  return msg
  67. end
  68.  
  69. function checkWhitelist(compID)
  70.  for i=1, #allowedID do
  71.   if allowedID[i] == compID then
  72.    return true
  73.   elseif i == #allowedID and allowedID[i] ~= compID then
  74.    return false
  75.   end
  76.  end
  77. end
  78.  
  79. --Main Code
  80. init()
  81. while true do
  82.  term.clear()
  83.  term.setCursorPos(1,1)
  84.  print("Choose an Option:")
  85.  print("1: Start Server")
  86.  print("2: Add User")
  87.  print("3: Remove User")
  88.  print("4: Add Whitelist ID")
  89.  print("5: Revoke Whitelist ID")
  90.  print("6: Exit Program")
  91.  print("")
  92.  write("Your Choice: ")
  93.  x = read()
  94. --------------------------------------------------
  95.  if tonumber(x) == 1 then
  96.   --Server Code
  97.   print("Server started...")
  98.   while true do
  99.    id, msg = rednet.receive()
  100.    if checkWhitelist(id) then
  101.     sleep(0.1)
  102.     rednet.send(id, "ready")
  103.     subUname = filterResponse(id)
  104.     subPword = filterResponse(id)
  105.     chkUname = checkTable(username,subUname)
  106.     if password[chkUname] == subPword then
  107.      rednet.send(id, "auth")
  108.     else
  109.      rednet.send(id, "nauth")
  110.     end
  111.    else
  112.     rednet.send(id,"400")
  113.    end
  114.   end
  115. --------------------------------------------------
  116.  elseif tonumber(x) == 2 then
  117.   write("Username?: ")
  118.   uname = read()
  119.   print("")
  120.   write("Password?: ")
  121.   pword = read()
  122.   print("")
  123.   print("Creating user...")
  124.   table.insert(username, uname)
  125.   table.insert(password, pword)
  126.   print("User created, saving tables...")
  127.   saveTable("uname", username)
  128.   saveTable("pword", password)
  129.   print("Tables saved successfully!")
  130.  elseif tonumber(x) == 3 then
  131.   write("Username of user to delete?: ")
  132.   uname = read()
  133.   for i=1, #username do
  134.    if username[i] == uname then
  135.     table.remove(username, i)
  136.     table.remove(password, i)
  137.     print("User removed, saving tables...")
  138.     saveTable("uname", username)
  139.     saveTable("pword", password)
  140.     print("Tables saved successfully!")
  141.    elseif i == #username and uname ~= username[i] then
  142.     print("Error: User not found.")
  143.    end
  144.   end
  145.   --
  146.  elseif tonumber(x) == 4 then
  147.   write("Computer ID to whitelist?: ")
  148.   compID = tonumber(read())
  149.   table.insert(allowedID, compID)
  150.   saveTable("whitelist", allowedID)
  151.   print("Computer whitelisted successfully!")
  152.   --
  153.  elseif tonumber(x) == 5 then
  154.   write("Computer ID to dewhitelist?: ")
  155.   compID = tonumber(read())
  156.   for i=1, #allowedID do
  157.    if allowedID[i] == compID then
  158.     table.remove(allowedID, i)
  159.     saveTable("whitelist", allowedID)
  160.     print("Computer dewhitelisted successfully!")
  161.    elseif i == #allowedID and allowedID[i] ~= compID then
  162.     print("Error: Computer ID not found.")
  163.    end
  164.   end
  165.   --
  166.  elseif tonumber(x) == 6 then
  167.   print("Exiting...")
  168.   error()
  169.  end
  170.  sleep(1)
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement