Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- version = "0.02 Beta"
- if not fs.isDir("/apis") then
- fs.delete("/apis/")
- fs.makeDir("/apis/")
- shell.run("pastebin get 9E5UHiqv /apis/AES")
- shell.run("pastebin get QYvNKrXE /apis/Base64")
- end
- if not fs.exists("/apis/AES") then
- shell.run("pastebin get 9E5UHiqv /apis/AES")
- end
- if not fs.exists("/apis/Base64") then
- shell.run("pastebin get QYvNKrXE /apis/Base64")
- end
- os.loadAPI("/apis/AES")
- os.loadAPI("/apis/Base64")
- modem = nil
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modem = peripheral.wrap(v)
- end
- end
- if not modem then
- error("This program requires a modem!")
- else
- modem.open(24680)
- end
- clients = {}
- uptime = 0
- run = true
- timeout = 30
- domain = "tron.com"
- edomain = domain .. "|"
- function sendTo(cid,tmsg,msg)
- modem.transmit(24680,cid,tmsg.." "..msg)
- end
- function eSendTo(cid,scode,tmsg,msg)
- ctid = nil
- for i = 1, #clients do
- if clients[i]["id"] == cid then
- ctid = i
- end
- end
- if not ctid then
- return "No such client."
- end
- key_used = tostring(cid)..tostring(clients[ctid]["dist"])..tostring(clients[ctid]["ss"])
- local tr = "E"..Base64.encode(AES.encrypt(key_used,tostring(scode).." "..tmsg.." "..msg))
- clients[ctid]["ss"] = clients[ctid]["ss"]+1
- modem.transmit(24680,cid,tr)
- return tr
- end
- function decode(cid,msg)
- ctid = nil
- for i = 1, #clients do
- if clients[i]["id"] == cid then
- ctid = i
- end
- end
- if not ctid then
- return "No such client."
- end
- ckey = tostring(tostring(cid)..clients[ctid]["dist"]) .. tostring(clients[ctid]["sent"])
- clients[ctid]["sent"] = clients[ctid]["sent"] +1
- clients[ctid]["time"] = uptime
- return AES.decrypt(ckey,Base64.decode(msg))
- end
- function network()
- tid = os.startTimer(1)
- while run == true do
- e = {os.pullEvent()}
- if e[1] == "timer" and e[2] == tid then
- clientsTimedOut = {}
- tid = os.startTimer(1)
- uptime = uptime+1
- for i = 1, #clients do
- if uptime >= (clients[i]["time"]+timeout) then
- table.insert(clientsTimedOut,i)
- end
- end
- for i = 1, #clientsTimedOut do
- table.remove(clients,clientsTimedOut[i])
- end
- elseif e[1] == "modem_message" then
- if e[3] == 24680 then
- msg = e[5]
- msgData = {}
- for word in msg:gmatch("%S+") do
- table.insert(msgData,word)
- end
- if msgData[1] == "PING" then
- if msgData[2]:lower() == domain:lower() then
- newClient = {}
- newClient["time"] = uptime
- newClient["id"] = e[4]
- newClient["dist"] = e[6]
- newClient["sent"] = 1
- newClient["ss"] = 0
- table.insert(clients,newClient)
- sendTo(e[4],"Pong","Pong!")
- end
- end
- if msg:sub(1,#edomain+1) == "E"..edomain then
- umsg = decode(e[4],msg:sub(#edomain+2,#msg))
- if umsg then
- umd = {}
- for word in umsg:gmatch("%S+") do
- table.insert(umd,word)
- end
- if umd[1] == "EPING" and umd[2] == "ENCRYPTED" and umd[3] == "PING" then
- eSendTo(e[4],200,"EPONG","ENCRYPTED PONG")
- end
- if umd[1] == "PAGE" then
- if not umd[2] or umd[2] == "" then
- umd[2] = "index"
- end
- if umd[2] and #umd[2] >= 3 then
- if umd[2]:sub(#umd[2]-2,#umd[2]) == ".cc" then
- umd[2] = umd[2]:sub(1,#umd[2]-3)
- end
- end
- if fs.exists("/www/"..umd[2]) then
- f = fs.open("/www/"..umd[2],"r")
- eSendTo(e[4],200,"PDATA",f.readAll())
- f.close()
- elseif fs.exists("/www/"..umd[2]..".cc") then
- f = fs.open("/www/"..umd[2]..".cc","r")
- eSendTo(e[4],200,"PDATA",f.readAll())
- f.close()
- elseif fs.exists("/www/errors/404") then
- f = fs.open("/www/errors/404","r")
- eSendTo(e[4],404,"PDATA",f.readAll())
- f.close()
- elseif fs.exists("/www/errors/404.cc") then
- f = fs.open("/www/errors/404.cc","r")
- eSendTo(e[4],404,"PDATA",f.readAll())
- f.close()
- else
- eSendTo(e[4],404,"PDATA","print('Error 404: Page not found.')")
- end
- end
- end
- end
- end
- end
- end
- end
- network()
Advertisement
Add Comment
Please, Sign In to add comment