Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local event = require("event")
- local com = require("component")
- local sides = require("sides")
- local term = require("term")
- local text = require("text")
- local r = com.redstone
- local mod = com.modem
- local dat = com.data
- local gpu = com.gpu
- local rsa = require("rsa")
- local classes = require("classes")
- local ser = require("serialization")
- local message = ""
- local sender = ""
- local side = sides.back
- local server = {}
- local running = true
- local waiting = true
- local localport = math.random(35000,40000)
- local pubKey,prvKey = rsa.makeKeyPairs(25)
- print(pubKey:toString())
- math.randomseed(math.floor(os.time()/4)+(os.time()%4))
- local function printData(...)
- for i,v in ipairs(...) do
- print(i..": "..v)
- end
- end
- local function foldData(...)
- --
- -- glue messages together for transport
- --
- local message = ""
- for i,s in ipairs({...}) do message = message..(message ~= "" and "#" or "")..s end
- return message
- end
- local function unfoldData(message)
- --
- -- split data for processing
- --
- local data = {}
- _ = message:gsub("#?[%w$]+",function(s) table.insert(data,(s:sub(1,1) == "#") and s:sub(2) or s) end)
- return data
- end
- local function sendData(encrypt,...)
- --
- -- process data that should be send
- --
- local message = foldData(...)
- if encrypt then
- message = server.key:encrypt(message)
- end
- mod.send(server.id,tonumber(server.port),message)
- end
- local function recvData(message)
- --
- -- process recieved data
- --
- if message:sub(1,2) == "XX" then
- return false
- end
- local dec = prvKey:decrypt(message)
- print(dec)
- if dec:sub(3,3) == "#" then
- if dec:sub(1,2) ~= "XX" then
- return unfoldData(dec)
- end
- end
- return false
- end
- local function handle(...)
- local data = {...}
- print("'"..data[3].."'|'"..data[6].."'")
- sender = data[3]
- message = data[6]
- event.ignore("modem_message",handle)
- waiting = false
- end
- local function exit(_,_,char,_,player)
- if char == ("-"):byte() and player == "JKOC" then
- print("Stopping Client")
- event.ignore("key_up", exit)
- running = false
- mod.close()
- end
- end
- -- register event
- event.listen("key_up",exit)
- while running do
- message = ""
- sender = ""
- local tries = 0
- -- term.clear()
- mod.open(localport)
- repeat
- tries = tries + 1
- waiting = true
- event.listen("modem_message",handle)
- event.timer(10,function() waiting = false end)
- mod.broadcast(80,"searchServer#"..localport)
- while waiting do
- os.sleep(0.5)
- end
- if not running then
- goto loopend
- end
- print("Sender: "..(sender ~= nil and sender or "nil"))
- print("Message: '"..(message ~= nil and message or "nil").."'")
- until (message ~= "" and message ~= nil) or tries == 10
- mod.close(localport)
- if message == "" or message == nil then
- print("Couldn't connect to the Auth-Server...")
- print("The Program will restart in 5 seconds...")
- os.sleep(5)
- goto loopend
- end
- server.id = sender
- server.port = 443
- -- term.clear()
- print("Welcome to the Password Door Security System")
- print()
- print("Please enter Username and Password!")
- io.write("Username: ")
- local username = io.read()
- io.write("Password: ")
- local passhash = dat.sha256(text.trim(term.read(nil, false, nil, "*")))
- print("\n\nverifying credentials ... ")
- message = ""
- sender = ""
- tries = 0
- mod.open(localport)
- repeat
- tries = tries + 1
- waiting = true
- event.listen("modem_message",handle)
- event.timer(10,function() waiting = false end)
- sendData(false,"PK",pubKey:toString(),localport)
- while waiting do
- os.sleep(0.5)
- end
- print("'"..message.."'")
- if not running then
- goto loopend
- end
- until (message ~= "" and message ~= nil and sender == server.id) or tries == 2
- mod.close(localport)
- if message == "" or message == nil or sender ~= server.id then
- print("Couldn't connect to the Auth-Server...")
- print("The Program will restart in 5 seconds...")
- os.sleep(5)
- goto loopend
- end
- local data = recvData(message)
- if data[1] ~= "PK" then
- print("The Auth-Server couldn't send data...")
- print("The Program will restart in 5 seconds...")
- os.sleep(5)
- goto loopend
- end
- print(ser.serialize(data))
- server.key = classes.PublicKey.fromString(data[2])
- server.port = tonumber(data[3])
- message = ""
- sender = ""
- tries = 0
- mod.open(localport)
- repeat
- tries = tries + 1
- sendData(true,"VU",username,passhash)
- _,_,sender,_,_,message = event.pull(5,"modem_message")
- until (message ~= "" and message ~= nil and sender == server.id) or tries == 10
- mod.close(localport)
- if message == "" or message == nil or sender ~= server.id then
- print("Couldn't connect to the Auth-Server...")
- print("The Program will restart in 5 seconds...")
- os.sleep(5)
- goto loopend
- end
- local data = recvData(message)
- if data[1] ~= "UV" then
- print("The Auth-Server couldn't send data...")
- print("The Program will restart in 5 seconds...")
- os.sleep(5)
- goto loopend
- end
- if data[2] == "granted" then
- --
- -- Draw picture (need verify)
- --
- local oldBGColor = gpu.setBackground(0x33DD33)
- term.clear()
- gpu.setBackground(0x118811)
- local oldwidth,oldheight = gpu.setViewport(11,6)
- gpu.drawRectangle(2,2,9,4)
- gpu.setCursor(3,3)
- gpu.write("Access")
- gpu.setCursor(3,4)
- gpu.write("Granted")
- os.sleep(1)
- r.setOutput(side,true)
- os.sleep(5)
- r.setOutput(side,false)
- gpu.setBackground(oldBGColor)
- term.clear()
- end
- ::loopend::
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement