-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathrefresh_cdn_url.lua
More file actions
140 lines (111 loc) · 3.16 KB
/
refresh_cdn_url.lua
File metadata and controls
140 lines (111 loc) · 3.16 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local ngx = require 'ngx'
local cjson = require 'cjson.safe'
local hmac = require "resty.hmac"
local http = require 'resty.http'
local ok, new_tab = pcall(require, 'table.new')
if not ok then
new_tab = function(narr, nreci) return {} end
end
local get_post_json = function()
if 'POST' ~= ngx.req.get_method() then
ngx.exit(406)
end
-- get post body
ngx.req.read_body()
local body = ngx.req.get_body_data()
if nil == body then
ngx.log(ngx.ERR, "body is null")
ngx.exit(406)
end
local data = cjson.decode(body)
if nil == data then
ngx.exit(406)
end
return data
end
local protocol = 'https://'
local host = 'cdn.api.qcloud.com'
local uri = '/v2/index.php'
local action = 'RefreshCdnUrl'
local method = 'POST' -- can use GET or POST
local qcloud_secret_id = '' -- pls use your secret id
local qcloud_secret_key = '' -- pls use your secret key
local params = {
['Action'] = action,
['SecretId'] = qcloud_secret_id,
['Timestamp'] = ngx.time(),
['Nonce'] = math.random(1000),
}
local urls = get_post_json()['urls']
if 'table' ~= type(urls) then
ngx.exit(406)
end
for index, url in ipairs(urls) do
params['urls.' .. tostring(index - 1)] = url
end
-- 1. Sort parameters
local keys = new_tab(#params, 0)
local index = 1
for k, _ in pairs(params) do
keys[index] = k
index = index + 1
end
table.sort(keys)
-- 2. Concatenate request string
local tmp = new_tab(#keys, 0)
for i, k in ipairs(keys) do
tmp[i] = k .. "=" .. tostring(params[k])
end
local sig_params = table.concat(tmp, '&')
-- 3. Concatenate the original signature string
local sig_url = method .. host .. uri .. '?' .. sig_params
-- 4. Generate signature string
local hmac_sha1 = hmac:new(qcloud_secret_key, hmac.ALGOS.SHA1)
if not hmac_sha1 then
ngx.log(ngx.ERR, 'failed to create the hmac_sha1 object')
ngx.exit(500)
end
ok = hmac_sha1:update(sig_url)
if not ok then
ngx.log(ngx.ERR, 'failed to add data')
ngx.exit(500)
end
local mac = hmac_sha1:final()
local base64 = ngx.encode_base64(mac) .. '='
params['Signature'] = base64
-- 5. Send http request
local httpc = http:new()
httpc:set_timeouts(30 * 1000, 30 * 1000, 30 * 1000)
local request_url = protocol .. host .. uri
local res, err
if method == 'GET' then
res, err = httpc:request_uri(request_url, {
method = method,
query = params,
})
else
res, err = httpc:request_uri(request_url, {
method = method,
body = sig_params .. '&Signature=' .. base64,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
end
httpc:close()
if not res then
ngx.log(ngx.ERR, 'refresh_cdn_url: request failed', err)
ngx.exit(500)
end
if 200 ~= res.status then
ngx.log(ngx.ERR, 'refresh_cdn_url: response header status code is not 200')
ngx.exit(500)
end
local res_body = cjson.decode(res.body)
if not res_body['code'] or tonumber(res_body['code']) ~= 0 then
ngx.log(ngx.ERR, 'refresh_cdn_url: response body status code is not 0, body: ', res.body)
ngx.exit(500)
end
ngx.header['Content-Type'] = 'application/json'
ngx.say(res.body)
ngx.exit(200)