a = 123 -- 布尔类型只有nil和false -- 数字0,空字符串都是true -- lua中的变量如果没有特殊说明, 全是全局变量
while 循环语句
1 2 3 4 5 6 7 8
sum = 0 num = 1 while num <= 100do sum = sum + num num = num + 1 end print("sum=",sum) -- /Lua没有++或是+=这样的操作
for 循环语句
1 2 3 4 5
sum = 0 for i = 1,100do sum = sum + 1 end print("sum=", sum)
if 判断语句
1 2 3 4 5 6 7 8 9 10 11
if age == 40and sex == "Man"then print("男⼈⼤于40") elseif age > 60and sex ~= "Woman"then print("⾮⼥⼈⽽且⼤于60") else local age = io.read() print("Your age is" .. age) end -- ~=是不等于 -- 字符串的拼接操作符".." -- io库的分别从stdin和stdout读写,read和write函数
--如果IP为空-取x_forwarded_for if clientIP == nilthen clientIP = ngx.req.get_headers()["x_forwarded_for"] end
--如果IP为空-取remote_addr if clientIP == nilthen clientIP = ngx.var.remote_addr end
--定义本地的memcached对象,加载memcached local memcached = require"resty.memcached" --实例化对象 local memc, err = memcached:new()
--判断连接是否存在错误 ifnot memc then ngx.say("failed to instantiate memc: ", err) return end
--建⽴memcache连接 local ok, err = memc:connect("127.0.0.1", 11211) --⽆法连接往前端抛出错误信息 ifnot ok then ngx.say("failed to connect: ", err) return end
--获取对象中的ip-存在值赋给res local res, flags, err = memc:get(clientIP)
--ngx.say("value key: ",res,clientIP) if err then ngx.say("failed to get clientIP ", err) return end --如果值为1则调⽤local-@java_test if res == "1"then ngx.exec("@java_test") return end --否则调⽤local-@java_prod ngx.exec("@java_prod") return
-- waf.lua : local content_length=tonumber(ngx.req.get_headers()['content-length']) local method=ngx.req.get_method() local ngxmatch=ngx.re.match if whiteip() then elseif blockip() then elseif denycc() then elseif ngx.var.http_Acunetix_Aspect then ngx.exit(444) elseif ngx.var.http_X_Scan_Memo then ngx.exit(444) elseif whiteurl() then elseif ua() then elseif url() then elseif args() then elseif cookie() then elseif PostCheck then if method=="POST"then local boundary = get_boundary() if boundary then locallen = string.len local sock, err = ngx.req.socket() ifnot sock then return end ngx.req.init_body(128 * 1024) sock:settimeout(0) local content_length = nil content_length=tonumber(ngx.req.get_headers()['content-length']) local chunk_size = 4096 if content_length < chunk_size then chunk_size = content_length end local size = 0 while size < content_length do local data, err, partial = sock:receive(chunk_size) data = data or partial ifnot data then return end ngx.req.append_body(data) if body(data) then returntrue end size = size + len(data) local m = ngxmatch(data,[[Content-Disposition: form-data;(.+)filename="(.+)\\.(.*)"]],'ijo') if m then fileExtCheck(m[3]) filetranslate = true else if ngxmatch(data,"Content-Disposition:",'isjo') then filetranslate = false end if filetranslate==falsethen if body(data) then returntrue end end end local less = content_length - size if less < chunk_size then chunk_size = less end end ngx.req.finish_body() else ngx.req.read_body() local args = ngx.req.get_post_args() ifnot args then return end for key, val inpairs(args) do iftype(val) == "table"then iftype(val[1]) == "boolean"then return end data=table.concat(val, ", ") else data=val end if data andtype(data) ~= "boolean"and body(data) then body(key) end end end end else return end