Skip to content

Instantly share code, notes, and snippets.

@vinsonzou
Created June 11, 2021 09:07
Show Gist options
  • Select an option

  • Save vinsonzou/4507dca6d58d933eaf38a1079850d78b to your computer and use it in GitHub Desktop.

Select an option

Save vinsonzou/4507dca6d58d933eaf38a1079850d78b to your computer and use it in GitHub Desktop.
nginx授权下载,主要实现ipa和apk授权下载
local require = require
local ngx = ngx
local ngx_re = require "ngx.re"
local re_find = ngx.re.find
local key = "自定义key"
local uri = ngx.var.uri
local realFile, sign, ts
if re_find(uri, [[^/[A-Za-z0-9_]+.ipa/[a-z0-9]+/[0-9]+$]], "joi") then
local res, err = ngx_re.split(uri, "/")
realFile = res[2]
sign = res[3]
ts = res[4]
else
local args, err = ngx.req.get_uri_args()
if err == "truncated" then
ngx.log(ngx.ALERT, "args truncated")
ngx.exit(444)
end
sign = args.sign
ts = args.ts
end
if not sign or not ts then
ngx.log(ngx.ALERT, "args incomplete")
ngx.exit(444)
end
if not re_find(ts, [[^[0-9]+$]], "jo") then
ngx.log(ngx.ALERT, "time type incorrect")
ngx.exit(444)
end
local diffTime = tonumber(ts) - tonumber(ngx.time())
if diffTime > 300 or diffTime < -300 then
ngx.log(ngx.ALERT, "time expired")
ngx.exit(444)
end
local ngx_sign = ngx.md5(ts .. "#test#" .. key)
if ngx_sign == sign then
if realFile then
local ipa_url = "/" .. realFile .. "?sign=" .. sign .. "&ts=" .. ts
ngx.exec(ipa_url)
end
return
else
ngx.log(ngx.ALERT, "sign incorrect")
ngx.exit(444)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment