Advertisement
osmarks

DoorController

Jan 2nd, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local function fetch(URL)
  2.     local h = http.get(URL)
  3.     local text = h.readAll()
  4.     h.close()
  5.     return text
  6. end
  7.  
  8. local f = fs.open("startup", "w")
  9. f.write(fetch "https://pastebin.com/raw/WzK8zVpq")
  10. f.close()
  11.  
  12. local ECC = loadstring(fetch "https://pastebin.com/raw/Sc0DU3rA")() "ecc"
  13.  
  14. local door_name = settings.get "door.name"
  15. local door_data = settings.get "door.data"
  16. local integrator_output_side = settings.get "door.iout"
  17. local door_side = settings.get "door.side"
  18. if not door_name or not door_data then
  19.     error "The settings 'door.name' and 'door.data' must be set."
  20. end
  21.  
  22. local function hexize(key)
  23.     local out = ""
  24.     for _, v in pairs(key) do
  25.         out = out .. string.format("%.2x", v)
  26.     end
  27.     return out
  28. end
  29.  
  30. local function unhexize(key)
  31.     local out = {}
  32.     for i = 1, #key, 2 do
  33.         local pair = key:sub(i, i + 1)
  34.         table.insert(out, tonumber(pair, 16))
  35.     end
  36.     return out
  37. end
  38.  
  39. local function check_data(dat)
  40.     local private_key = unhexize(dat)
  41.     local door_data, err = loadstring(fetch(door_data))()
  42.     if not door_data then error(err) end
  43.     local public_key = hexize(ECC.publicKey(private_key))
  44.     local user_clearance = door_data.users[public_key]
  45.     local door_clearance = door_data.doors[door_name]
  46.     print(user_clearance, door_clearance)
  47.     local data = {
  48.         settings = door_data,
  49.         required_clearance = door_clearance,
  50.         user_clearance = user_clearance,
  51.         public_key = public_key,
  52.         private_key = private_key,
  53.         door = door_name
  54.     }
  55.     if not user_clearance then error(public_key .. " - User invalid.") end
  56.     if not door_clearance then error(door_name .. " - Door name invalid.") end
  57.     if type(door_clearance) == "function" and door_clearance(data) then
  58.         return true
  59.     elseif type(user_clearance) == "function" and user_clearance(data) then -- allow programmatic access control
  60.         return true
  61.     else
  62.         return user_clearance >= door_clearance
  63.     end
  64. end
  65.  
  66. while true do
  67.     local _, side = os.pullEvent "disk"
  68.     local mount = disk.getMountPath(side)
  69.     if mount then
  70.         local path = fs.combine(mount, "keyfile")
  71.         if fs.exists(path) then
  72.             local f = fs.open(path, "r")
  73.             local data = f.readAll()
  74.             f.close()
  75.             disk.eject(side)
  76.             local ok, res = pcall(check_data, data)
  77.             if ok and res then
  78.                 print "Opening!"
  79.                 peripheral.find("redstone_integrator", function(_, p) p.setOutput(integrator_output_side, true) end)
  80.                 redstone.setOutput(door_side, true)
  81.                 sleep(3)
  82.                 peripheral.find("redstone_integrator", function(_, p) p.setOutput(integrator_output_side, false) end)
  83.                 redstone.setOutput(door_side, false)
  84.             else
  85.                 printError(res)
  86.             end
  87.         else
  88.             print "Invalid keycard. No keyfile."
  89.             disk.eject(side)
  90.         end
  91.     else
  92.         disk.eject(side)
  93.         print "Invalid keycard. This is not a disk."
  94.     end
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement