Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chan = 15432
- local k = require("k")
- local secret = "secretword"
- local mon = peripheral.wrap("right")
- local mod = peripheral.wrap("top")
- mod.open(chan)
- local function authSuccess(dist)
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setBackgroundColor(colors.green)
- mon.setTextColor(colors.white)
- mon.setCursorPos(1,1)
- mon.write("succ")
- mon.setCursorPos(1,2)
- mon.write("dist:")
- mon.setCursorPos(1,3)
- mon.write(tostring(dist))
- rs.setOutput("left", true)
- end
- local function authFail()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.white)
- mon.setCursorPos(1,1)
- mon.write("fail")
- rs.setOutput("left", false)
- end
- local timer = os.startTimer(1)
- local lastChall = ""
- local lastChallTime = os.epoch("utc")
- local gotResponse = false
- while true do
- local e, p1, p2, p3, p4, p5 = os.pullEvent()
- if e == "timer" and p1 == timer then
- if not gotResponse then
- print("auth timeout/too far away")
- authFail()
- end
- gotResponse = false
- lastChallTime = os.epoch("utc")
- lastChall = "authchall:" .. tostring(os.getComputerID()) .. ":" .. tostring(os.epoch("utc"))
- mod.transmit(chan, chan, lastChall)
- timer = os.startTimer(1)
- elseif e == "modem_message" and p2 == chan and type(p4) == "string" and p5 and
- p4:sub(1, 9) == "authresp:" and p5 < 5 and (os.epoch("utc") - lastChallTime < 100) then
- local resp = p4:sub(10)
- local expected = k.sha256(lastChall .. ":" .. secret .. ":" .. tostring(p5))
- if resp == expected then
- print("received valid response")
- gotResponse = true
- authSuccess(p5)
- else
- print("received invalid response")
- authFail()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement