Alyssa

Server_Beta

Mar 6th, 2015
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. version = "0.02 Beta"
  2. if not fs.isDir("/apis") then
  3.     fs.delete("/apis/")
  4.     fs.makeDir("/apis/")
  5.     shell.run("pastebin get 9E5UHiqv /apis/AES")
  6.     shell.run("pastebin get QYvNKrXE /apis/Base64")
  7. end
  8. if not fs.exists("/apis/AES") then
  9.     shell.run("pastebin get 9E5UHiqv /apis/AES")
  10. end
  11. if not fs.exists("/apis/Base64") then
  12.     shell.run("pastebin get QYvNKrXE /apis/Base64")
  13. end
  14. os.loadAPI("/apis/AES")
  15. os.loadAPI("/apis/Base64")
  16. modem = nil
  17. for k, v in pairs(peripheral.getNames()) do
  18.     if peripheral.getType(v) == "modem" then
  19.         modem = peripheral.wrap(v)
  20.     end
  21. end
  22. if not modem then
  23.     error("This program requires a modem!")
  24. else
  25.     modem.open(24680)
  26. end
  27. clients = {}
  28. uptime = 0
  29. run = true
  30. timeout = 30
  31. domain = "tron.com"
  32. edomain = domain .. "|"
  33. function sendTo(cid,tmsg,msg)
  34.     modem.transmit(24680,cid,tmsg.." "..msg)
  35. end
  36. function eSendTo(cid,scode,tmsg,msg)
  37.     ctid = nil
  38.     for i = 1, #clients do
  39.         if clients[i]["id"] == cid then
  40.             ctid = i
  41.         end
  42.     end
  43.     if not ctid then
  44.         return "No such client."
  45.     end
  46.     key_used = tostring(cid)..tostring(clients[ctid]["dist"])..tostring(clients[ctid]["ss"])
  47.     local tr = "E"..Base64.encode(AES.encrypt(key_used,tostring(scode).." "..tmsg.." "..msg))
  48.     clients[ctid]["ss"] = clients[ctid]["ss"]+1
  49.     modem.transmit(24680,cid,tr)
  50.     return tr
  51. end
  52. function decode(cid,msg)
  53.     ctid = nil
  54.     for i = 1, #clients do
  55.         if clients[i]["id"] == cid then
  56.             ctid = i
  57.         end
  58.     end
  59.     if not ctid then
  60.         return "No such client."
  61.     end
  62.     ckey = tostring(tostring(cid)..clients[ctid]["dist"]) .. tostring(clients[ctid]["sent"])
  63.     clients[ctid]["sent"] = clients[ctid]["sent"] +1
  64.     clients[ctid]["time"] = uptime
  65.     return AES.decrypt(ckey,Base64.decode(msg))
  66. end
  67. function network()
  68.     tid = os.startTimer(1)
  69.     while run == true do
  70.         e = {os.pullEvent()}
  71.         if e[1] == "timer" and e[2] == tid then
  72.             clientsTimedOut = {}
  73.             tid = os.startTimer(1)
  74.             uptime = uptime+1
  75.             for i = 1, #clients do
  76.                 if uptime >= (clients[i]["time"]+timeout) then
  77.                     table.insert(clientsTimedOut,i)
  78.                 end
  79.             end
  80.             for i = 1, #clientsTimedOut do
  81.                 table.remove(clients,clientsTimedOut[i])
  82.             end
  83.         elseif e[1] == "modem_message" then
  84.             if e[3] == 24680 then
  85.                 msg = e[5]
  86.                 msgData = {}
  87.                 for word in msg:gmatch("%S+") do
  88.                     table.insert(msgData,word)
  89.                 end
  90.                 if msgData[1] == "PING" then
  91.                     if msgData[2]:lower() == domain:lower() then
  92.                         newClient = {}
  93.                         newClient["time"] = uptime
  94.                         newClient["id"] = e[4]
  95.                         newClient["dist"] = e[6]
  96.                         newClient["sent"] = 1
  97.                         newClient["ss"] = 0
  98.                         table.insert(clients,newClient)
  99.                         sendTo(e[4],"Pong","Pong!")
  100.                     end
  101.                 end
  102.                 if msg:sub(1,#edomain+1) == "E"..edomain then
  103.                     umsg = decode(e[4],msg:sub(#edomain+2,#msg))
  104.                     if umsg then
  105.                         umd = {}
  106.                         for word in umsg:gmatch("%S+") do
  107.                             table.insert(umd,word)
  108.                         end
  109.                         if umd[1] == "EPING" and umd[2] == "ENCRYPTED" and umd[3] == "PING" then
  110.                             eSendTo(e[4],200,"EPONG","ENCRYPTED PONG")
  111.                         end
  112.                         if umd[1] == "PAGE" then
  113.                             if not umd[2] or umd[2] == "" then
  114.                                 umd[2] = "index"
  115.                             end
  116.                             if umd[2] and #umd[2] >= 3 then
  117.                                 if umd[2]:sub(#umd[2]-2,#umd[2]) == ".cc" then
  118.                                     umd[2] = umd[2]:sub(1,#umd[2]-3)
  119.                                 end
  120.                             end
  121.                             if fs.exists("/www/"..umd[2]) then
  122.                                 f = fs.open("/www/"..umd[2],"r")
  123.                                 eSendTo(e[4],200,"PDATA",f.readAll())
  124.                                 f.close()
  125.                             elseif fs.exists("/www/"..umd[2]..".cc") then
  126.                                 f = fs.open("/www/"..umd[2]..".cc","r")
  127.                                 eSendTo(e[4],200,"PDATA",f.readAll())
  128.                                 f.close()
  129.                             elseif fs.exists("/www/errors/404") then
  130.                                 f = fs.open("/www/errors/404","r")
  131.                                 eSendTo(e[4],404,"PDATA",f.readAll())
  132.                                 f.close()
  133.                             elseif fs.exists("/www/errors/404.cc") then
  134.                                 f = fs.open("/www/errors/404.cc","r")
  135.                                 eSendTo(e[4],404,"PDATA",f.readAll())
  136.                                 f.close()
  137.                             else
  138.                                 eSendTo(e[4],404,"PDATA","print('Error 404: Page not found.')")
  139.                             end
  140.                         end
  141.                     end
  142.                 end
  143.             end
  144.         end
  145.     end
  146. end
  147. network()
Advertisement
Add Comment
Please, Sign In to add comment