Guest User

remsec_controller.lua

a guest
Jul 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1.  
  2. local term = require("term")
  3. local gpu = require("tty").gpu()
  4. local thread = require("thread")
  5. local event = require("event")
  6. local component = require("component")
  7. local _, h = gpu.getResolution()
  8. local output = {}
  9. local ok
  10. local gerti
  11. local cfg
  12. local data
  13. local hash
  14. local encrypt
  15. local decrypt
  16. if not component.isAvailable("data") then
  17.   io.stderr:write("Data card unavailable.")
  18.   os.exit(1)
  19. else
  20.   data = component.data
  21.   if not data.sha256 then
  22.     io.stderr:write("SHA-256 not available.")
  23.     os.exit(1)
  24.   end
  25.   if not data.encrypt then
  26.     io.stderr:write("AES-128 encryption not available.")
  27.     os.exit(1)
  28.   end
  29.   hash = data.sha256
  30.   encrypt = data.encrypt
  31.   decrypt = data.decrypt
  32. end
  33. ok, gerti = pcall(require, "GERTiClient")
  34. if not ok then
  35.   io.stderr:write("Could not load GERTi.")
  36.   os.exit(1)
  37. end
  38. ok, cfg = pcall(require, "osu.config")
  39. if not ok then
  40.   io.stderr:write("Could not load config.")
  41.   os.exit(1)
  42. end
  43. if not cfg.remsec.enabled then
  44.   io.stderr:write("RemSec is disabled on this computer.")
  45.   os.exit(1)
  46. end
  47. if not gerti.getAddress() then
  48.   io.stderr:write("No connection to MNC.")
  49.   os.exit(1)
  50. end
  51. socket = gerti.openSocket(cfg.remsec.address)
  52. term.clear()
  53. local main = thread.create(function()
  54.   event.pull("GERTData")
  55.   txt = socket:read()
  56.   print(txt[1])
  57.   while true do
  58.     term.setCursor(1, h)
  59.     io.write("> ")
  60.     cmd = io.read()
  61.     socket:write(encrypt(hash(cfg.remsec.checksumtext), tostring(cfg.remsec.key), tostring(cfg.remsec.iv)))
  62.     socket:write(encrypt(cmd, tostring(cfg.remsec.key), tostring(cfg.remsec.iv)))
  63.     _, addr = event.pull("GERTData")
  64.     if not addr == cfg.remsec.address then
  65.       return
  66.     else
  67.       term.clear()
  68.       txt = socket:read()
  69.       output[#output+1] = txt[1]
  70.       if #output == h-1 then
  71.           for _, text in pairs(output) do
  72.         print(text)
  73.         end
  74.         output = {}
  75.       else
  76.         for _, text in pairs(output) do
  77.           print(text)
  78.         end
  79.       end
  80.     end
  81.   end
  82. end)
  83. local safeExit = thread.create(function()
  84.   event.pull("interrupted")
  85.   socket:close()
  86. end)
  87. local detectClosure = thread.create(function()
  88.   event.pull("GERTConnectionClose")
  89.   print("Server closed connection, exiting.")
  90.   socket:close()
  91. end)
  92. thread.waitForAny({safeExit, detectClosure, main})
  93. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment