diff options
author | 2016-08-25 10:17:35 +0800 | |
---|---|---|
committer | 2016-08-25 10:24:54 +0800 | |
commit | 8597cf5e116012c038e83f763a816ca106cad4d2 (patch) | |
tree | 4f6671079047db1f565060dcce4bf2607856cf68 /msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua | |
parent | eb9df7aa9e268192dd68a46af4576671b17adcf9 (diff) |
Solve api gateway issue
Change-Id: I88fedc04faaf4d799b9dbf4c306aa073cabd6218
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua')
-rw-r--r-- | msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua b/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua new file mode 100644 index 0000000..c4c2d78 --- /dev/null +++ b/msb-core/openresty-ext/src/assembly/resources/openresty/nginx/luaext/make_authed.lua @@ -0,0 +1,44 @@ +function ipValidator(ip)
+ local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}
+ if #chunks == 4 then
+ for _,v in pairs(chunks) do
+ if tonumber(v) > 255 then return R.STRING end
+ end
+ return true
+ end
+ local chunks = {ip:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}
+ if #chunks == 8 then
+ for _,v in pairs(chunks) do
+ if #v > 0 and tonumber(v, 16) > 65535 then return R.STRING end
+ end
+ return true
+ end
+ return false
+end
+
+if ngx.req.get_method() == "POST" then
+ ngx.req.read_body()
+ local body = ngx.req.get_body_data()
+ local json = require('cjson')
+ local tab = json.decode(body)
+ local ip = tab["passIp"]
+ if not ip then
+ ngx.log(ngx.WARN, "ip is nil.")
+ ngx.exit(500)
+ end
+ if ipValidator(ip) then
+ local cache = ngx.shared.ceryx
+ local succ, err, forcible = cache:set(ip, "place_holder", 3600)
+ if not succ then
+ ngx.log(ngx.WARN, err)
+ ngx.exit(500)
+ end
+ else
+ ngx.log(ngx.WARN, "not a valid ip.")
+ ngx.exit(500)
+ end
+ ngx.exit(201)
+else
+ ngx.log(ngx.WARN, "not a POST request.")
+ ngx.exit(500)
+end
\ No newline at end of file |