Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Edit by gijsvdsande
- local version = "1.0"
- logs = {}
- logs[1] = "Press ` to exit the logs."
- gui = true
- compID = os.getComputerID()
- --Lightning webserver.
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- modem = peripheral.wrap(v)
- rednet.open(v)
- end
- end
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "modem" then
- if peripheral.call(v,"isWireless") then
- modem = peripheral.wrap(v)
- rednet.open(v)
- end
- end
- end
- errors = {}
- errors[400] = "%sid% Sent a bad equest"
- errors[403] = "%sid% Tried to access %msg% on %sd% which is forbidden"
- errors[404] = "Page %msg% on/or %sd% didn't exist as requested by %sid%"
- errors[413] = "Request from %sid% was too long"
- errors[418] = "I'm a teapot"
- errors[500] = "Encountered a internal server error"
- defErrors = {}
- defErrors[400] = "print('Error 400: Bad request.')"
- defErrors[403] = "print('Error 403: Forbidden.')"
- defErrors[404] = "print('Error 404: Page not found.')"
- defErrors[413] = "print('Error 413: Request Entity Too Large.')"
- defErrors[418] = "print('Error 418: I'm a teapot.')"
- defErrors[500] = "print('Error 500: Internal server error.')"
- function errorCode(wError)
- if errors[wError] and defErrors[wError] then
- if fs.exists("/errors/"..tostring(wError)) then
- local page = fs.open("/errors/"..tostring(wError),"r")
- local code = extra..page.readAll()
- rednet.send(sid, code)
- page.close()
- tPrint = errors[wError]
- tPrint = string.gsub(tPrint,"%%msg%%", msg)
- tPrint = string.gsub(tPrint,"%%sid%%", sid)
- tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
- logs[#logs+1] = tPrint
- else
- local page = defErrors[wError]
- local code = extra..page
- rednet.send(sid, code)
- tPrint = errors[wError]
- tPrint = string.gsub(tPrint,"%%msg%%", msg)
- tPrint = string.gsub(tPrint,"%%sid%%", sid)
- tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
- logs[#logs+1] = tPrint
- end
- else
- if fs.exists("/errors/500") then
- local page = fs.open("/errors/500","r")
- local code = extra..page.readAll()
- rednet.send(sid, code)
- page.close()
- tPrint = errors[500]
- tPrint = string.gsub(tPrint,"%%msg%%", msg)
- tPrint = string.gsub(tPrint,"%%sid%%", sid)
- tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
- logs[#logs+1] = tPrint
- else
- local page = defErrors[500]
- local code = extra..page
- rednet.send(sid, code)
- tPrint = errors[500]
- tPrint = string.gsub(tPrint,"%%msg%%", msg)
- tPrint = string.gsub(tPrint,"%%sid%%", sid)
- tPrint = string.gsub(tPrint,"%%sd%%",subDomain)
- logs[#logs+1] = tPrint
- end
- end
- end
- function showLog()
- scrx, scry = term.getSize()
- term.setBackgroundColor(bgColor)
- term.setTextColor(txtColor)
- term.clear()
- term.setCursorPos(1,1)
- for i = 1, scry do
- if logs[#logs-scry+i] then
- print(logs[#logs-scry+i])
- end
- end
- end
- function showGui()
- term.setBackgroundColor(bgColor)
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- print(fline)
- print(fline)
- term.setTextColor(txtColor)
- term.setCursorPos(1,1)
- write("Minedows.cc File Webserver v"..version)
- term.setCursorPos(1,2)
- write("Requests: "..requests)
- term.setCursorPos(scrx-#domainName,1)
- write(domainName)
- paintutils.drawFilledBox(2, 4, 12, 6, colors.red)
- paintutils.drawFilledBox(scrx-11, 4, scrx-1, 6, colors.blue)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.red)
- b1txt = "Shutdown"
- term.setCursorPos(6-#b1txt/2,5)
- write(b1txt)
- b2txt = "Logs"
- term.setCursorPos(scrx-7-#b2txt/2,5)
- term.setBackgroundColor(colors.blue)
- write(b2txt)
- end
- bgColor = colors.white
- txtColor = colors.lightGray
- term.setBackgroundColor(bgColor)
- term.setTextColor(txtColor)
- term.clear()
- scrx,scry = term.getSize()
- fline = string.rep(" ",scrx)
- term.setCursorPos(1,1)
- requests = 0
- if fs.exists("settings") then
- f = fs.open("settings","r")
- settings = textutils.unserialize(f.readAll())
- f.close()
- domainName = settings[1]
- extra = ""
- if settings[2] and settings[3] then
- logs[#logs+1] = "Global settings detected!"
- bgc = settings[2]
- txtc = settings[3]
- if settings[4] then
- miscData = settings[4]
- end
- extra = "term.setBackgroundColor("..tostring(bgc)..")\n"
- extra = extra.."term.setTextColor("..tostring(txtc)..")\n"
- extra = extra.."term.clear()\n"
- extra = extra.."term.setCursorPos(1,1)\n"
- extra = extra.."defaultBG = "..tostring(bgc).."\n"
- extra = extra.."defaultText = "..tostring(txtc).."\n"
- if miscData then
- extra = extra..miscData.."\n"
- end
- end
- else
- gui = false
- logs[#logs+1] = "No settings data"
- showLog()
- write("Domain: ")
- local dmn = read()
- tSettings= {dmn}
- domainName = dmn
- extra = ""
- local swrite = fs.open("settings","w")
- swrite.write(textutils.serialize(tSettings))
- swrite.close()
- term.clear()
- term.setCursorPos(1,1)
- end
- if fs.exists("advsettings") then
- f = fs.open("advsettings","r")
- fc = f.readAll()
- f.close()
- extra = extra..fc.."\n"
- logs[#logs+1] = "Advanced global settings detected"
- end
- doRedirs = false
- if fs.exists("sd_settings") then
- f = fs.open("sd_settings","r")
- sds = f.readAll()
- dRedirs = textutils.unserialize(sds)
- doRedirs = true
- f.close()
- else
- example = {}
- example["example."] = 1
- f = fs.open("sd_settings","w")
- f.write(textutils.serialize(example))
- f.close()
- end
- if not fs.exists("webapi") then
- f = fs.open("webapi","w")
- f.writeLine("wArgs = {...}")
- f.writeLine("pageRequested = wArgs[1]")
- f.writeLine("sArgs = {}")
- f.writeLine("for w in wArgs[2]:gmatch('[^&]+') do")
- f.writeLine(" table.insert(sArgs,w)")
- f.writeLine("end")
- f.writeLine("return false, false, ''")
- end
- logs[#logs+1] = "Lightning web server v"..version
- logs[#logs+1] = "Server is hosted at "..domainName
- function serv()
- rquests = 0
- while true do
- if not gui then
- showLog()
- end
- sid, msg, proto = rednet.receive()
- uWA = false
- disablePage = false
- disableExtras = false
- extCode = ""
- if type(msg) == "string" and type(proto) == "string" then
- if string.sub(string.lower(proto),#proto+1-#tostring(compID),#proto) == string.lower(tostring(compID)) then
- proto = string.sub(string.lower(proto),1,#proto-#tostring(compID))..domainName
- if string.sub(msg,1,9) == "TLD-REDIR" then
- sid = tonumber(string.sub(msg,10,14))
- msg = string.sub(msg,15,#msg)
- end
- end
- if string.sub(string.lower(proto),#proto+1-#domainName,#proto) == string.lower(domainName) then
- 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
- if string.lower(proto) == string.lower(domainName) then
- if string.sub(proto,#proto+1-#domainName,#proto+1-#domainName) == "." then
- proto = "www"..proto
- else
- proto = "www."..proto
- end
- end
- subDomain = string.sub(proto,0,#proto-#domainName)
- requests = requests +1
- rquests = rquests +1
- if rquests >= 250 then
- sleep(0)
- rquests = 0
- end
- serveRequest = true
- if doRedirs then
- if dRedirs[subDomain] then
- sidl = #tostring(sid)
- sidts = string.rep("0",5-sidl)..tostring(sid)
- protots = string.sub(proto,1,#proto-#domainName)..dRedirs[subDomain]
- rednet.send(dRedirs[subDomain],"TLD-REDIR"..sidts..msg,protots)
- serveRequest = false
- end
- end
- if serveRequest then
- loq = msg:find("?")
- if loq and #msg > loq then
- webArgs = msg:sub(loq+1,#msg)
- msg = msg:sub(1,loq-1)
- uWA = true
- end
- if fs.exists("webapi") and uWA then
- f = fs.open("webapi","r")
- fc = f.readAll()
- f.close()
- webApi = loadstring(fc)
- setfenv(webApi,getfenv())
- disablePage, disableExtras, extCode = webApi(msg,subDomain,webArgs)
- if not extCode then
- extCode = ""
- elseif #extCode < 1 then
- extCode = ""
- end
- end
- if #msg <= 256 then
- if fs.exists(subDomain.."/"..msg) then
- if msg ~= "startup" and msg ~= "settings" then
- if not disablePage then
- page = fs.open(subDomain.."/"..msg, "r")
- pageI = page.readAll()
- page.close()
- else
- pageI = ""
- end
- if not disableExtras then
- code = extra..extCode.."\n"..pageI
- else
- code = extCode.."\n"..pageI
- end
- rednet.send(sid, code)
- logs[#logs+1] = "Sent page "..subDomain..domainName.."/"..msg.." to "..sid
- else
- errorCode(403)
- end
- else
- errorCode(404)
- end
- else
- errorCode(413)
- end
- else
- logs[#logs+1] = "Redirected "..sid.." to "..dRedirs[subDomain].." for page "..subDomain
- end
- end
- end
- else
- --errorCode(400)
- end
- end
- end
- function fGui()
- showGui()
- TID = os.startTimer(0.1)
- while true do
- e,mb,clickx,clicky = os.pullEvent()
- if e == "timer" and mb == TID then
- TID = os.startTimer(0.1)
- if not gui then
- showLog()
- else
- showGui()
- end
- end
- if gui then
- if e == "mouse_click" then
- if clickx >= 2 and clickx <= 12 and clicky >= 4 and clicky <= 6 then
- sleep(1)
- os.shutdown()
- elseif clickx >= scrx-11 and clickx <= scrx-1 and clicky >= 4 and clicky <= 6 then
- gui = false
- showLog()
- end
- showGui()
- end
- end
- if e == "key" and mb == 41 then
- gui = true
- showGui()
- end
- end
- end
- parallel.waitForAny(serv,fGui)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement