Alyssa

Web_Server

Feb 17th, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.52 KB | None | 0 0
  1. local version = "2.7"
  2. logs = {}
  3. logs[1] = "Press ` to exit the logs."
  4. gui = true
  5. compID = os.getComputerID()
  6. --Lightning webserver.
  7. for k, v in pairs(peripheral.getNames()) do
  8.         if peripheral.getType(v) == "modem" then
  9.         modem = peripheral.wrap(v)
  10.         rednet.open(v)
  11.         end
  12. end
  13. for k, v in pairs(peripheral.getNames()) do
  14.         if peripheral.getType(v) == "modem" then
  15.         if peripheral.call(v,"isWireless") then
  16.             modem = peripheral.wrap(v)
  17.             rednet.open(v)
  18.         end
  19.         end
  20. end
  21.  
  22. errors = {}
  23. errors[400] = "%sid% Sent a bad equest"
  24. errors[403] = "%sid% Tried to access %msg% on %sd% which is forbidden"
  25. errors[404] = "Page %msg% on/or %sd% didn't exist as requested by %sid%"
  26. errors[413] = "Request from %sid% was too long"
  27. errors[418] = "I'm a teapot"
  28. errors[500] = "Encountered a internal server error"
  29.  
  30. defErrors = {}
  31. defErrors[400] = "print('Error 400: Bad request.')"
  32. defErrors[403] = "print('Error 403: Forbidden.')"
  33. defErrors[404] = "print('Error 404: Page not found.')"
  34. defErrors[413] = "print('Error 413: Request Entity Too Large.')"
  35. defErrors[418] = "print('Error 418: I'm a teapot.')"
  36. defErrors[500] = "print('Error 500: Internal server error.')"
  37.  
  38. function errorCode(wError)
  39.     if errors[wError] and defErrors[wError] then
  40.         if fs.exists("/errors/"..tostring(wError)) then
  41.             local page = fs.open("/errors/"..tostring(wError),"r")
  42.             local code = extra..page.readAll()
  43.             rednet.send(sid, code)
  44.             page.close()
  45.             tPrint = errors[wError]
  46.             tPrint = string.gsub(tPrint,"%%msg%%", msg)
  47.             tPrint = string.gsub(tPrint,"%%sid%%", sid)
  48.             tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  49.             logs[#logs+1] = tPrint
  50.         else
  51.             local page = defErrors[wError]
  52.             local code = extra..page
  53.             rednet.send(sid, code)
  54.             tPrint = errors[wError]
  55.             tPrint = string.gsub(tPrint,"%%msg%%", msg)
  56.             tPrint = string.gsub(tPrint,"%%sid%%", sid)
  57.             tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  58.             logs[#logs+1] = tPrint
  59.         end
  60.     else
  61.         if fs.exists("/errors/500") then
  62.             local page = fs.open("/errors/500","r")
  63.             local code = extra..page.readAll()
  64.             rednet.send(sid, code)
  65.             page.close()
  66.             tPrint = errors[500]
  67.             tPrint = string.gsub(tPrint,"%%msg%%", msg)
  68.             tPrint = string.gsub(tPrint,"%%sid%%", sid)
  69.             tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  70.             logs[#logs+1] = tPrint
  71.         else
  72.             local page = defErrors[500]
  73.             local code = extra..page
  74.             rednet.send(sid, code)
  75.             tPrint = errors[500]
  76.             tPrint = string.gsub(tPrint,"%%msg%%", msg)
  77.             tPrint = string.gsub(tPrint,"%%sid%%", sid)
  78.             tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
  79.             logs[#logs+1] = tPrint
  80.         end
  81.     end
  82. end
  83.  
  84. function showLog()
  85.     scrx, scry = term.getSize()
  86.     term.setBackgroundColor(bgColor)
  87.     term.setTextColor(txtColor)
  88.     term.clear()
  89.     term.setCursorPos(1,1)
  90.     for i = 1, scry do
  91.         if logs[#logs-scry+i] then
  92.             print(logs[#logs-scry+i])
  93.         end
  94.     end
  95. end
  96.  
  97. function showGui()
  98.     term.setBackgroundColor(bgColor)
  99.     term.clear()
  100.     term.setCursorPos(1,1)
  101.     term.setBackgroundColor(colors.gray)
  102.     print(fline)
  103.     print(fline)
  104.     term.setTextColor(txtColor)
  105.     term.setCursorPos(1,1)
  106.     write("Lightning Webserver v"..version)
  107.     term.setCursorPos(1,2)
  108.     write("Requests: "..requests)
  109.     term.setCursorPos(scrx-#domainName,1)
  110.     write(domainName)
  111.     paintutils.drawFilledBox(2, 4, 12, 6, colors.red)
  112.     paintutils.drawFilledBox(scrx-11, 4, scrx-1, 6, colors.blue)
  113.     term.setTextColor(colors.white)
  114.     term.setBackgroundColor(colors.red)
  115.     b1txt = "Shutdown"
  116.     term.setCursorPos(6-#b1txt/2,5)
  117.     write(b1txt)
  118.     b2txt = "Logs"
  119.     term.setCursorPos(scrx-7-#b2txt/2,5)
  120.     term.setBackgroundColor(colors.blue)
  121.     write(b2txt)
  122. end
  123.  
  124. bgColor = colors.white
  125. txtColor = colors.lightGray
  126. term.setBackgroundColor(bgColor)
  127. term.setTextColor(txtColor)
  128. term.clear()
  129. scrx,scry = term.getSize()
  130. fline = string.rep(" ",scrx)
  131. term.setCursorPos(1,1)
  132. requests = 0
  133.  
  134. if fs.exists("settings") then
  135.     f = fs.open("settings","r")
  136.     settings = textutils.unserialize(f.readAll())
  137.     f.close()
  138.     domainName = settings[1]
  139.     extra = ""
  140.     if settings[2] and settings[3] then
  141.         logs[#logs+1] = "Global settings detected!"
  142.         bgc = settings[2]
  143.         txtc = settings[3]
  144.         if settings[4] then
  145.             miscData = settings[4]
  146.         end
  147.         extra = "term.setBackgroundColor("..tostring(bgc)..")\n"
  148.         extra = extra.."term.setTextColor("..tostring(txtc)..")\n"
  149.         extra = extra.."term.clear()\n"
  150.         extra = extra.."term.setCursorPos(1,1)\n"
  151.         extra = extra.."defaultBG = "..tostring(bgc).."\n"
  152.         extra = extra.."defaultText = "..tostring(txtc).."\n"
  153.         if miscData then
  154.             extra = extra..miscData.."\n"
  155.         end
  156.     end
  157. else
  158.     gui = false
  159.     logs[#logs+1] = "No settings data"
  160.     showLog()
  161.     write("Domain: ")
  162.     local dmn = read()
  163.     tSettings= {dmn}
  164.     domainName = dmn
  165.     extra = ""
  166.     local swrite = fs.open("settings","w")
  167.     swrite.write(textutils.serialize(tSettings))
  168.     swrite.close()
  169.     term.clear()
  170.     term.setCursorPos(1,1)
  171. end
  172. if fs.exists("advsettings") then
  173.     f = fs.open("advsettings","r")
  174.     fc = f.readAll()
  175.     f.close()
  176.     extra = extra..fc.."\n"
  177.     logs[#logs+1] = "Advanced global settings detected"
  178. end
  179.  
  180. doRedirs = false
  181. if fs.exists("sd_settings") then
  182.     f = fs.open("sd_settings","r")
  183.     sds = f.readAll()
  184.     dRedirs = textutils.unserialize(sds)
  185.     doRedirs = true
  186.     f.close()
  187. else
  188.     example = {}
  189.     example["example."] = 1
  190.     f = fs.open("sd_settings","w")
  191.     f.write(textutils.serialize(example))
  192.     f.close()
  193. end
  194.  
  195. if not fs.exists("webapi") then
  196.     f = fs.open("webapi","w")
  197.     f.writeLine("wArgs = {...}")
  198.     f.writeLine("pageRequested = wArgs[1]")
  199.     f.writeLine("sArgs = {}")
  200.     f.writeLine("for w in wArgs[2]:gmatch('[^&]+') do")
  201.     f.writeLine("   table.insert(sArgs,w)")
  202.     f.writeLine("end")
  203.     f.writeLine("return false, false, ''")
  204. end
  205.  
  206. logs[#logs+1] = "Lightning web server v"..version
  207. logs[#logs+1] = "Server is hosted at "..domainName
  208.  
  209. function serv()
  210.     rquests = 0
  211.     while true do
  212.         if not gui then
  213.             showLog()
  214.         end
  215.         sid, msg, proto = rednet.receive()
  216.         uWA = false
  217.         disablePage = false
  218.         disableExtras = false
  219.         extCode = ""
  220.         if type(msg) == "string" and type(proto) == "string" then
  221.             if string.sub(string.lower(proto),#proto+1-#tostring(compID),#proto) == string.lower(tostring(compID)) then
  222.                 proto = string.sub(string.lower(proto),1,#proto-#tostring(compID))..domainName
  223.                 if string.sub(msg,1,9) == "TLD-REDIR" then
  224.                     sid = tonumber(string.sub(msg,10,14))
  225.                     msg = string.sub(msg,15,#msg)
  226.                 end
  227.             end
  228.             if string.sub(string.lower(proto),#proto+1-#domainName,#proto) == string.lower(domainName) then
  229.                 if string.sub(proto,#proto-#domainName,#proto-#domainName) == "." or string.sub(proto, #proto+1-#domainName,#proto+1-#domainName) == "." or string.lower(proto) == string.lower(domainName) then
  230.                     if string.lower(proto) == string.lower(domainName) then
  231.                         if string.sub(proto,#proto+1-#domainName,#proto+1-#domainName) == "." then
  232.                             proto = "www"..proto
  233.                         else
  234.                             proto = "www."..proto
  235.                         end
  236.                     end
  237.                     subDomain = string.sub(proto,0,#proto-#domainName)
  238.                     requests = requests +1
  239.                     rquests = rquests +1
  240.                     if rquests >= 250 then
  241.                         sleep(0)
  242.                         rquests = 0
  243.                     end
  244.                     serveRequest = true
  245.                     if doRedirs then
  246.                         if dRedirs[subDomain] then
  247.                             sidl = #tostring(sid)
  248.                             sidts = string.rep("0",5-sidl)..tostring(sid)
  249.                             protots = string.sub(proto,1,#proto-#domainName)..dRedirs[subDomain]
  250.                             rednet.send(dRedirs[subDomain],"TLD-REDIR"..sidts..msg,protots)
  251.                             serveRequest = false
  252.                         end
  253.                     end
  254.                     if serveRequest then
  255.                         loq = msg:find("?")
  256.                         if loq and #msg > loq then
  257.                             webArgs = msg:sub(loq+1,#msg)
  258.                             msg = msg:sub(1,loq-1)
  259.                             uWA = true
  260.                         end
  261.                         if fs.exists("webapi") and uWA then
  262.                             f = fs.open("webapi","r")
  263.                             fc = f.readAll()
  264.                             f.close()
  265.                             webApi = loadstring(fc)
  266.                             setfenv(webApi,getfenv())
  267.                             disablePage, disableExtras, extCode = webApi(msg,subDomain,webArgs)
  268.                             if not extCode then
  269.                                 extCode = ""
  270.                             elseif #extCode < 1 then
  271.                                 extCode = ""
  272.                             end
  273.                         end
  274.                         if #msg <= 256 then
  275.                             if fs.exists(subDomain.."/"..msg) then
  276.                                 if msg ~= "startup" and msg ~= "settings" then
  277.                                     if not disablePage then
  278.                                         page = fs.open(subDomain.."/"..msg, "r")
  279.                                         pageI = page.readAll()
  280.                                         page.close()
  281.                                     else
  282.                                         pageI = ""
  283.                                     end
  284.                                     if not disableExtras then
  285.                                         code = extra..extCode.."\n"..pageI
  286.                                     else
  287.                                         code = extCode.."\n"..pageI
  288.                                     end
  289.                                     rednet.send(sid, code)
  290.                                     logs[#logs+1] = "Sent page "..subDomain..domainName.."/"..msg.." to "..sid
  291.                                 else
  292.                                     errorCode(403)
  293.                                 end
  294.                             else
  295.                                 errorCode(404)
  296.                             end
  297.                         else
  298.                             errorCode(413)
  299.                         end
  300.                     else
  301.                         logs[#logs+1] = "Redirected "..sid.." to "..dRedirs[subDomain].." for page "..subDomain
  302.                     end
  303.                 end
  304.             end
  305.         else
  306.             --errorCode(400)
  307.         end
  308.     end
  309. end
  310.  
  311. function fGui()
  312.     showGui()
  313.     TID = os.startTimer(0.1)
  314.     while true do
  315.         e,mb,clickx,clicky = os.pullEvent()
  316.         if e == "timer" and mb == TID then
  317.             TID = os.startTimer(0.1)
  318.             if not gui then
  319.                 showLog()
  320.             else
  321.                 showGui()
  322.             end
  323.         end
  324.         if gui then
  325.             if e == "mouse_click" then
  326.                 if clickx >= 2 and clickx <= 12 and clicky >= 4 and clicky <= 6 then
  327.                     sleep(1)
  328.                     os.shutdown()
  329.                 elseif clickx >= scrx-11 and clickx <= scrx-1 and clicky >= 4 and clicky <= 6 then
  330.                     gui = false
  331.                     showLog()
  332.                 end
  333.                 showGui()
  334.             end
  335.         end
  336.         if e == "key" and mb == 41 then
  337.             gui = true
  338.             showGui()
  339.         end
  340.     end
  341. end
  342.  
  343. parallel.waitForAny(serv,fGui)
Advertisement
Add Comment
Please, Sign In to add comment