-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckcode.lua.bak
More file actions
80 lines (77 loc) · 2.68 KB
/
Copy pathcheckcode.lua.bak
File metadata and controls
80 lines (77 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
local checkcode = require("cmcheckcode")
local method = ngx.req.get_method()
local uri = ngx.var.uri
local cache_code = ngx.shared.checkcode
function get_from_cache(key)
local value = cache_code:get(key)
return value
end
function set_to_cache(key, value, exptime)
if not exptime then
exptime = 0
end
local succ, err, forcible = cache_code:set(key, value, exptime)
--ngx.say("err:" .. err .. '<br/>')
return succ
end
if uri == '/restapi/v1/captchas' or uri == '/' then
if method == 'GET' then
ngx.say('{"errno": -1,"errmsg": "Method Not Allowed","data":""}')
ngx.exit(ngx.HTTP_OK)
else
local hash = checkcode.getmd5key()
set_to_cache(hash, '', 300) -- remain 5min
--ngx.say('{"code":"' .. hash .. '"}')
ngx.say('{"errno":0,"errmsg": "success","data":"' .. hash .. '"}')
ngx.exit(ngx.HTTP_OK)
end
else
if method == 'GET' then
local hash = string.sub(ngx.var.hashkey, 2)
local value =get_from_cache(hash)
if value then
local code = checkcode.doIt()
set_to_cache(hash, code, 300)
-- local value =get_from_cache(hash)
-- ngx.say(value)
-- ngx.say(code)
-- ngx.exit(200)
local res = ngx.location.capture('/codeimg/' .. code)
if res then
ngx.header.content_type = "image/png";
ngx.say(res.body)
ngx.exit(ngx.HTTP_OK)
else
ngx.say("Get image fail")
ngx.exit(ngx.HTTP_NOT_FOUND)
end
end
ngx.say('{"errno": -2,"errmsg": "code hash is invalid","data": ""}')
ngx.exit(ngx.HTTP_OK)
else
local check = string.sub(ngx.var.hashkey, 2)
if check == "check" then
ngx.req.read_body()
local arg = ngx.req.get_post_args()
if arg.key and arg.code then
local value =get_from_cache(arg.key)
set_to_cache(arg.key, nil, 1) -- remain 1s
-- ngx.say(arg.key)
-- ngx.say(arg.code)
-- ngx.say(value)
if value and string.lower(value) == string.lower(arg.code) then
ngx.say('{"errno": 0,"errmsg": "success" }')
ngx.exit(ngx.HTTP_OK)
end
ngx.say('{"errno": -3,"errmsg": "fail" }')
ngx.exit(ngx.HTTP_OK)
end
ngx.say('{"errno": -1,"errmsg": "Method Not Allowed","data": ""}')
ngx.exit(ngx.HTTP_OK)
end
ngx.say('{"errno": -1,"errmsg":"Method Not Allowed","data": ""}')
ngx.exit(ngx.HTTP_OK)
end
end
ngx.say('{"errno": -1,"errmsg": "Method Not Allowed","data": ""}')
ngx.exit(ngx.HTTP_OK)