Skip to content

Instantly share code, notes, and snippets.

@mozhu1024
Created July 15, 2018 16:11
Show Gist options
  • Save mozhu1024/13a31bad434c023babe3e5240410fdb7 to your computer and use it in GitHub Desktop.
Save mozhu1024/13a31bad434c023babe3e5240410fdb7 to your computer and use it in GitHub Desktop.
[A Lua script for grep hash md5 with nginx] #lua #nginx
function split( str,reps )
local res = {}
string.gsub(str,'[^'..reps..']+',function ( w )
table.insert(res,w)
end)
return res
end
local hashfile = '/data/hashgrep/hash_md5.vir'
if "GET" == ngx.var.request_method then
local args, err = ngx.req.get_uri_args()
if args ~= nil then
local _start = args['pos'] or 0
local _code = args['code'] or 0
if _start and _code then
local p = nil
local res = nil
local _c = ""
if _start then
_c = '[a-f0-9]{'.._start..'}'
end
p = io.popen("grep -E '\\d* ".._c.._code.."' "..hashfile)
res = p:read("*l")
p:close()
if string.len(res) > 32 then
local ra = nil
ra = split(res,' ')
res = '{"num":"'..ra[1]..'","md5":"'..ra[2]..'","code":"'.._code..'"}'
else
res = '{"error":"Not Found","code":"'.._code..'"}'
ngx.status = ngx.NGX_HTTP_NOT_FOUND
end
ngx.header.content_type = "application/json"
-- ngx.req.set_header("Content-Type", "application/json;charset=utf8");
ngx.say(res)
ngx.exit(0)
end
end
end
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
ngx.say("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment