Advertisement
Guest User

client.lua

a guest
Sep 19th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.42 KB | None | 0 0
  1. local event = require("event")
  2. local com = require("component")
  3. local sides = require("sides")
  4. local term = require("term")
  5. local text = require("text")
  6. local r = com.redstone
  7. local mod = com.modem
  8. local dat = com.data
  9. local gpu = com.gpu
  10. local rsa = require("rsa")
  11. local classes = require("classes")
  12. local ser = require("serialization")
  13.  
  14. local message = ""
  15. local sender = ""
  16. local side = sides.back
  17. local server = {}
  18. local running = true
  19. local waiting = true
  20. local localport = math.random(35000,40000)
  21. local pubKey,prvKey = rsa.makeKeyPairs(25)
  22. print(pubKey:toString())
  23. math.randomseed(math.floor(os.time()/4)+(os.time()%4))
  24.  
  25. local function printData(...)
  26.   for i,v in ipairs(...) do
  27.     print(i..": "..v)
  28.   end
  29. end
  30.  
  31. local function foldData(...)
  32.   --
  33.   -- glue messages together for transport
  34.   --
  35.   local message = ""
  36.   for i,s in ipairs({...}) do message = message..(message ~= "" and "#" or "")..s end
  37.   return message
  38. end
  39.  
  40. local function unfoldData(message)
  41.     --
  42.     -- split data for processing
  43.     --
  44.     local data = {}
  45.     _ = message:gsub("#?[%w$]+",function(s) table.insert(data,(s:sub(1,1) == "#") and s:sub(2) or s) end)
  46.     return data
  47. end
  48.  
  49. local function sendData(encrypt,...)
  50.   --
  51.   -- process data that should be send
  52.   --
  53.   local message = foldData(...)
  54.   if encrypt then
  55.     message = server.key:encrypt(message)
  56.   end
  57.   mod.send(server.id,tonumber(server.port),message)
  58. end
  59.  
  60. local function recvData(message)
  61.   --
  62.   -- process recieved data
  63.   --
  64.   if message:sub(1,2) == "XX" then
  65.     return false
  66.   end
  67.   local dec = prvKey:decrypt(message)
  68.   print(dec)
  69.   if dec:sub(3,3) == "#" then
  70.     if dec:sub(1,2) ~= "XX" then
  71.       return unfoldData(dec)
  72.     end
  73.   end
  74.   return false
  75. end
  76.  
  77. local function handle(...)
  78.   local data = {...}
  79.   print("'"..data[3].."'|'"..data[6].."'")
  80.   sender = data[3]
  81.   message = data[6]
  82.   event.ignore("modem_message",handle)
  83.   waiting = false
  84. end
  85.  
  86. local function exit(_,_,char,_,player)
  87.     if char == ("-"):byte() and player == "JKOC" then
  88.         print("Stopping Client")
  89.         event.ignore("key_up", exit)
  90.         running = false
  91.         mod.close()
  92.     end
  93. end
  94.  
  95. -- register event
  96. event.listen("key_up",exit)
  97. while running do
  98.   message = ""
  99.   sender = ""
  100.   local tries = 0
  101. --  term.clear()
  102.   mod.open(localport)
  103.   repeat
  104.     tries = tries + 1
  105.     waiting = true
  106.     event.listen("modem_message",handle)
  107.     event.timer(10,function() waiting = false end)
  108.     mod.broadcast(80,"searchServer#"..localport)
  109.     while waiting do
  110.       os.sleep(0.5)
  111.     end    
  112.     if not running then
  113.       goto loopend
  114.     end
  115.     print("Sender: "..(sender ~= nil and sender or "nil"))
  116.     print("Message: '"..(message ~= nil and message or "nil").."'")
  117.   until (message ~= "" and message ~= nil) or tries == 10
  118.   mod.close(localport)
  119.   if message == "" or message == nil then
  120.     print("Couldn't connect to the Auth-Server...")
  121.     print("The Program will restart in 5 seconds...")
  122.     os.sleep(5)
  123.     goto loopend
  124.   end
  125.   server.id = sender
  126.   server.port = 443
  127. --  term.clear()
  128.   print("Welcome to the Password Door Security System")
  129.   print()
  130.   print("Please enter Username and Password!")
  131.   io.write("Username: ")
  132.   local username = io.read()
  133.   io.write("Password: ")
  134.   local passhash = dat.sha256(text.trim(term.read(nil, false, nil, "*")))
  135.   print("\n\nverifying credentials ... ")
  136.   message = ""
  137.   sender = ""
  138.   tries = 0
  139.   mod.open(localport)
  140.   repeat
  141.     tries = tries + 1
  142.     waiting = true
  143.     event.listen("modem_message",handle)
  144.     event.timer(10,function() waiting = false end)
  145.     sendData(false,"PK",pubKey:toString(),localport)
  146.     while waiting do
  147.       os.sleep(0.5)
  148.     end
  149.     print("'"..message.."'")
  150.     if not running then
  151.       goto loopend
  152.     end
  153.   until (message ~= "" and message ~= nil and sender == server.id) or tries == 2
  154.   mod.close(localport)
  155.   if message == "" or message == nil or sender ~= server.id then
  156.     print("Couldn't connect to the Auth-Server...")
  157.     print("The Program will restart in 5 seconds...")
  158.     os.sleep(5)
  159.     goto loopend
  160.   end
  161.   local data = recvData(message)
  162.   if data[1] ~= "PK" then
  163.     print("The Auth-Server couldn't send data...")
  164.     print("The Program will restart in 5 seconds...")
  165.     os.sleep(5)
  166.     goto loopend
  167.   end
  168.   print(ser.serialize(data))
  169.   server.key = classes.PublicKey.fromString(data[2])
  170.   server.port = tonumber(data[3])
  171.   message = ""
  172.   sender = ""
  173.   tries = 0
  174.   mod.open(localport)
  175.   repeat
  176.     tries = tries + 1
  177.     sendData(true,"VU",username,passhash)
  178.     _,_,sender,_,_,message = event.pull(5,"modem_message")
  179.   until (message ~= "" and message ~= nil and sender == server.id) or tries == 10
  180.   mod.close(localport)
  181.   if message == "" or message == nil or sender ~= server.id then
  182.     print("Couldn't connect to the Auth-Server...")
  183.     print("The Program will restart in 5 seconds...")
  184.     os.sleep(5)
  185.     goto loopend
  186.   end
  187.   local data = recvData(message)
  188.   if data[1] ~= "UV" then
  189.     print("The Auth-Server couldn't send data...")
  190.     print("The Program will restart in 5 seconds...")
  191.     os.sleep(5)
  192.     goto loopend
  193.   end
  194.   if data[2] == "granted" then
  195.     --
  196.     -- Draw picture (need verify)
  197.     --
  198.     local oldBGColor = gpu.setBackground(0x33DD33)
  199.     term.clear()
  200.     gpu.setBackground(0x118811)
  201.     local oldwidth,oldheight = gpu.setViewport(11,6)
  202.     gpu.drawRectangle(2,2,9,4)
  203.     gpu.setCursor(3,3)
  204.     gpu.write("Access")
  205.     gpu.setCursor(3,4)
  206.     gpu.write("Granted")
  207.     os.sleep(1)
  208.     r.setOutput(side,true)
  209.     os.sleep(5)
  210.     r.setOutput(side,false)
  211.     gpu.setBackground(oldBGColor)
  212.     term.clear()
  213.   end
  214.   ::loopend::
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement