Advertisement
osmarks

Really Insecure Magcard Rolldoor Controller

Jul 31st, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. local component = require "component"
  2. local net = require "internet"
  3. local event = require "event"
  4. local fs = require "filesystem"
  5. local rd = component.os_rolldoorcontroller
  6. local magcard = component.os_magreader
  7. local lamp = component.colorful_lamp
  8. magcard.setEventName "magswipe"
  9. rd.setSpeed(3)
  10. rd.close()
  11.  
  12. local function set_lamp(r, g, b)
  13.     lamp.setLampColor(r * 1024 + g * 32 + b)
  14. end
  15.  
  16. local function fetch(url)
  17.     local txt = ""
  18.     for chunk in net.request(url) do
  19.         txt = txt .. chunk
  20.     end
  21.     return txt
  22. end
  23.  
  24. local function read_file(file)
  25.     return io.open(file):read "*a"
  26. end
  27.  
  28. local function load_data(dat)
  29.     local fn, err = load("return " .. dat, "=data", "t", {})
  30.     if not fn then return false, err
  31.     else
  32.         local ok, res = pcall(fn)
  33.         if not ok then return false, res
  34.         else return true, res end
  35.     end
  36. end
  37.  
  38. local function open()
  39.     rd.open()
  40.     os.sleep(3)
  41.     rd.close()
  42. end
  43.  
  44. local ok, config = load_data(read_file "/home/conf.lua")
  45. if not ok then error(config) end
  46.  
  47. if not config.door_name then error "Door name not specified." end
  48. if not config.data_URL then error "Data URL not specified." end
  49.  
  50. local function authorized(player, card)
  51.     print("Player:", player, "Card data:", card)
  52.     local ok, sysdata = load_data(fetch(config.data_URL))
  53.     if not ok then print("DOOR DATA FORMAT ERROR:", sysdata) return false end
  54.     if player ~= card then return false end
  55.     local player_clearance = sysdata.players[player]
  56.     if not player_clearance then print("Player", player, "not found") return false end
  57.     local door_clearance = sysdata.doors[config.door_name]
  58.     return door_clearance <= player_clearance
  59. end
  60.  
  61. while true do
  62.     set_lamp(0, 0, 31)
  63.     local _, _, player_name, card_data, player_UUID = event.pull "magswipe"
  64.     if authorized(player_name, card_data) then
  65.         set_lamp(0, 31, 0)
  66.         open()
  67.     else
  68.         set_lamp(31, 0, 0)
  69.         os.sleep(1)
  70.     end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement