mirror of
https://github.com/dongdigua/demo.lua.git
synced 2025-02-22 03:59:46 +08:00
add resource limit (chapter 25), fix query
This commit is contained in:
parent
2751ce2d2e
commit
1f1c94c0ce
24
demo.lua
24
demo.lua
@ -69,7 +69,7 @@ local demos = {}
|
||||
demos["hello"] = "print[[hello shenjack]]"
|
||||
|
||||
local query=os.getenv("QUERY_STRING")
|
||||
if #query > 0 then
|
||||
if query ~= "" then
|
||||
T=demos[query] or ""
|
||||
end
|
||||
|
||||
@ -85,9 +85,26 @@ write[[</TEXTAREA>
|
||||
]]
|
||||
io.flush()
|
||||
|
||||
|
||||
-- the serious thing begins
|
||||
if os.getenv("REQUEST_METHOD") == "POST" then
|
||||
|
||||
-- limit resource
|
||||
local steplimit = 100
|
||||
local memlimit = 100
|
||||
|
||||
local count = 0
|
||||
local function step ()
|
||||
count = count + 1
|
||||
if collectgarbage("count") > memlimit then
|
||||
error("DDoSer uses too much memory")
|
||||
end
|
||||
if count > steplimit then
|
||||
error("DDoSer uses too much CPU")
|
||||
end
|
||||
end
|
||||
|
||||
debug.sethook(step, "", 100)
|
||||
|
||||
-- delete unsafe functions
|
||||
arg=nil
|
||||
debug.debug=nil
|
||||
@ -124,6 +141,9 @@ else
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
write("\n-- steps/100:\t", count, "\n")
|
||||
write("-- memory:\t", string.format("%.2f",collectgarbage("count")), "\n")
|
||||
|
||||
-- continue HTML
|
||||
write('</TEXTAREA><P><IMG SRC="https://lua.org/images/',I,'.png" ALIGN="absbottom">\n')
|
||||
write('Your program ',E,'.\n')
|
||||
|
Loading…
Reference in New Issue
Block a user