Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function fetch(URL)
- local h = http.get(URL)
- local text = h.readAll()
- h.close()
- return text
- end
- local f = fs.open("startup", "w")
- f.write(fetch "https://pastebin.com/raw/WzK8zVpq")
- f.close()
- local ECC = loadstring(fetch "https://pastebin.com/raw/Sc0DU3rA")() "ecc"
- local door_name = settings.get "door.name"
- local door_data = settings.get "door.data"
- local integrator_output_side = settings.get "door.iout"
- local door_side = settings.get "door.side"
- if not door_name or not door_data then
- error "The settings 'door.name' and 'door.data' must be set."
- end
- local function hexize(key)
- local out = ""
- for _, v in pairs(key) do
- out = out .. string.format("%.2x", v)
- end
- return out
- end
- local function unhexize(key)
- local out = {}
- for i = 1, #key, 2 do
- local pair = key:sub(i, i + 1)
- table.insert(out, tonumber(pair, 16))
- end
- return out
- end
- local function check_data(dat)
- local private_key = unhexize(dat)
- local door_data, err = loadstring(fetch(door_data))()
- if not door_data then error(err) end
- local public_key = hexize(ECC.publicKey(private_key))
- local user_clearance = door_data.users[public_key]
- local door_clearance = door_data.doors[door_name]
- print(user_clearance, door_clearance)
- local data = {
- settings = door_data,
- required_clearance = door_clearance,
- user_clearance = user_clearance,
- public_key = public_key,
- private_key = private_key,
- door = door_name
- }
- if not user_clearance then error(public_key .. " - User invalid.") end
- if not door_clearance then error(door_name .. " - Door name invalid.") end
- if type(door_clearance) == "function" and door_clearance(data) then
- return true
- elseif type(user_clearance) == "function" and user_clearance(data) then -- allow programmatic access control
- return true
- else
- return user_clearance >= door_clearance
- end
- end
- while true do
- local _, side = os.pullEvent "disk"
- local mount = disk.getMountPath(side)
- if mount then
- local path = fs.combine(mount, "keyfile")
- if fs.exists(path) then
- local f = fs.open(path, "r")
- local data = f.readAll()
- f.close()
- disk.eject(side)
- local ok, res = pcall(check_data, data)
- if ok and res then
- print "Opening!"
- peripheral.find("redstone_integrator", function(_, p) p.setOutput(integrator_output_side, true) end)
- redstone.setOutput(door_side, true)
- sleep(3)
- peripheral.find("redstone_integrator", function(_, p) p.setOutput(integrator_output_side, false) end)
- redstone.setOutput(door_side, false)
- else
- printError(res)
- end
- else
- print "Invalid keycard. No keyfile."
- disk.eject(side)
- end
- else
- disk.eject(side)
- print "Invalid keycard. This is not a disk."
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement