Normalize line endings
[msb/apigateway.git] / msb-core / openresty-ext / src / assembly / resources / openresty / nginx / luaext / make_authed.lua
1 function ipValidator(ip)\r
2         local chunks = {ip:match("(%d+)%.(%d+)%.(%d+)%.(%d+)")}\r
3         if #chunks == 4 then\r
4                 for _,v in pairs(chunks) do\r
5                         if tonumber(v) > 255 then return R.STRING end\r
6                 end\r
7                 return true\r
8         end\r
9         local chunks = {ip:match(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$"))}\r
10         if #chunks == 8 then\r
11                 for _,v in pairs(chunks) do\r
12                         if #v > 0 and tonumber(v, 16) > 65535 then return R.STRING end\r
13                 end\r
14                 return true\r
15         end\r
16         return false\r
17 end     \r
18 \r
19 if ngx.req.get_method() == "POST" then\r
20         ngx.req.read_body()\r
21         local body = ngx.req.get_body_data()    \r
22         local json = require('cjson')\r
23         local tab = json.decode(body)\r
24         local ip = tab["passIp"]\r
25         if not ip then\r
26                 ngx.log(ngx.WARN, "ip is nil.")\r
27                 ngx.exit(500)\r
28         end\r
29         if ipValidator(ip) then\r
30                 local cache = ngx.shared.ceryx\r
31                 local succ, err, forcible = cache:set(ip, "place_holder", 3600)\r
32                 if not succ then\r
33                         ngx.log(ngx.WARN, err)\r
34                         ngx.exit(500)\r
35                 end\r
36         else\r
37                 ngx.log(ngx.WARN, "not a valid ip.")\r
38                 ngx.exit(500)\r
39         end             \r
40         ngx.exit(201)\r
41 else\r
42         ngx.log(ngx.WARN, "not a POST request.")\r
43         ngx.exit(500)\r
44 end