Advertisement
1lann

authserver.lua

May 20th, 2020
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. local chan = 15432
  2.  
  3. local k = require("k")
  4. local secret = "secretword"
  5. local mon = peripheral.wrap("right")
  6. local mod = peripheral.wrap("top")
  7.  
  8. mod.open(chan)
  9.  
  10. local function authSuccess(dist)
  11.     mon.setBackgroundColor(colors.black)
  12.     mon.clear()
  13.     mon.setBackgroundColor(colors.green)
  14.     mon.setTextColor(colors.white)
  15.     mon.setCursorPos(1,1)
  16.     mon.write("succ")
  17.     mon.setCursorPos(1,2)
  18.     mon.write("dist:")
  19.     mon.setCursorPos(1,3)
  20.     mon.write(tostring(dist))
  21.     rs.setOutput("left", true)
  22. end
  23.  
  24. local function authFail()
  25.     mon.setBackgroundColor(colors.black)
  26.     mon.clear()
  27.     mon.setBackgroundColor(colors.red)
  28.     mon.setTextColor(colors.white)
  29.     mon.setCursorPos(1,1)
  30.     mon.write("fail")
  31.     rs.setOutput("left", false)
  32. end
  33.  
  34. local timer = os.startTimer(1)
  35. local lastChall = ""
  36. local lastChallTime = os.epoch("utc")
  37. local gotResponse = false
  38. while true do
  39.     local e, p1, p2, p3, p4, p5 = os.pullEvent()
  40.     if e == "timer" and p1 == timer then
  41.         if not gotResponse then
  42.             print("auth timeout/too far away")
  43.             authFail()
  44.         end
  45.         gotResponse = false
  46.  
  47.         lastChallTime = os.epoch("utc")
  48.         lastChall = "authchall:" .. tostring(os.getComputerID()) .. ":" .. tostring(os.epoch("utc"))
  49.         mod.transmit(chan, chan, lastChall)
  50.         timer = os.startTimer(1)
  51.     elseif e == "modem_message" and p2 == chan and type(p4) == "string" and p5 and
  52.         p4:sub(1, 9) == "authresp:" and p5 < 5 and (os.epoch("utc") - lastChallTime < 100) then
  53.         local resp = p4:sub(10)
  54.         local expected = k.sha256(lastChall .. ":" .. secret .. ":" .. tostring(p5))
  55.         if resp == expected then
  56.             print("received valid response")
  57.             gotResponse = true
  58.             authSuccess(p5)
  59.         else
  60.             print("received invalid response")
  61.             authFail()
  62.         end
  63.     end
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement