當您在防盜鏈、黑白名單、定製化要求標頭和回應標頭控制、定製化改寫和重新導向等情境下,需要自訂轉寄規則時,您可參考本文AScript範例程式碼自訂流量轉寄規則。
防盜鏈需求
自訂鑒權演算法
-
需求:
-
請求URL格式:
/path/digest/?.tskey=&t=。 -
針對
.ts類請求,自訂防盜鏈需求如下:-
規則1:參數
t或參數key不存在,響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因。 -
規則2:參數
t表示絕對到期時間,若參數t小於目前時間,則響應403,增加回應標頭X-AUTH-MSG標識鑒權失敗原因。 -
規則3:
md5與digest匹配。若md5與digest不匹配,響應403。md5取值格式為:
私密金鑰 + path + 檔案名稱.尾碼。
-
-
-
對應的AScript規則:
if eq(substr($uri, -3, -1), '.ts') { if or(not($arg_t), not($arg_key)) { add_rsp_header('X-AUTH-MSG', 'auth failed - missing necessary arg') exit(403) } t = tonumber($arg_t) if not(t) { add_rsp_header('X-AUTH-MSG', 'auth failed - invalid time') exit(403) } if gt(now(), t) { add_rsp_header('X-AUTH-MSG', 'auth failed - expired url') exit(403) } pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)/([^?]+)%?(.*)') sec1 = get(pcs, 1) sec2 = get(pcs, 2) sec3 = get(pcs, 3) if or(not(sec1), not(sec2), not(sec3)) { add_rsp_header('X-AUTH-MSG', 'auth failed - malformed url') exit(403) } key = 'b98d643a-9170-4937-8524-6c33514bbc23' signstr = concat(key, sec1, sec3) digest = md5(signstr) if ne(digest, sec2) { add_rsp_header('X-AUTH-DEBUG', concat('signstr: ', signstr)) add_rsp_header('X-AUTH-MSG', 'auth failed - invalid digest') exit(403) } }
User-Agent黑名單
-
需求:當請求
User-Agent以ijkplayer或Ysten開頭時,響應403。 -
對應的AScript規則:
if and($http_user_agent, match_re($http_user_agent, '^(ijkplayer|Ysten).*$')) { add_rsp_header('X-BLOCKLIST-DEBUG', 'deny') exit(403) }
Referer白名單
-
需求:當
referer不是http[s]://***alibaba.com***時,響應403。 -
對應的AScript規則:
if and($http_referer, match_re($http_referer, '^(http|https)://(.)+\.alibaba\.com.*$')) { return true } add_rsp_header('X-WHITELIST-DEBUG', 'missing') exit(403)
黑白名單
IP黑名單
-
需求:當請求用戶端IP為
127.0.0.1或10.0.0.1時,響應403。 -
對應的AScript規則:
if match_re($remote_addr, '127.0.0.1|10.0.0.1') { add_rsp_header('X-IPBLOCK-DEBUG', 'hit') exit(403) }
定製化要求標頭和回應標頭控制
檔案自動重新命名
-
需求:當有參數
filename時,自動重新命名為filename,無參數時,使用預設命名。 -
對應的AScript規則:
if $arg_filename { hn = 'Content-Disposition' hv = concat('attachment;filename=', $arg_filename) add_rsp_header(hn, hv) } -
樣本:
add_rsp_header('Content-Disposition', concat('attachment;filename=', tochar(34), filename, tochar(34)))說明-
通過在HTTP應答中添加回應標頭
Content-Disposition:attachment來實現訊息體的強制下載,並且有參數filename時,自動重新命名為filename,無參數時,使用預設命名。 -
filename增加雙引號,雙引號的ASCII編碼為34,可經tochar轉回字串。
-
-
輸出:
Content-Disposition: attachment;filename="monitor.apk"
覆蓋來源站點回應標頭
-
需求:覆蓋來源站點
Content-Type回應標頭。 -
對應的AScript規則:
add_rsp_header('Content-Type', 'audio/mpeg')
定製化改寫和重新導向
精確URI改寫
-
需求:將使用者請求
/hello內部改寫成/index.html,回源和緩衝的URI都將變成/index.html,參數保持原樣。 -
對應的AScript規則:
if match_re($uri, '^/hello$') { rewrite('/index.html', 'break') }
檔案尾碼改寫
-
需求:將使用者請求
/1.txt內部改寫成/1.<url參數type>。例如:/1.txt?type=mp4將會被改成/1.mp4?type=mp4回源並緩衝。 -
對應的AScript規則:
if and(match_re($uri, '^/1.txt$'), $arg_type) { rewrite(concat('/1.', $arg_type), 'break') }
檔案尾碼小寫化
-
需求:將URI改成小寫。
-
對應的AScript規則:
pcs = capture_re($uri, '^(.+\.)([^.]+)') section = get(pcs, 1) postfix = get(pcs, 2) if and(section, postfix) { rewrite(concat(section, lower(postfix)), 'break') }
添加URI首碼
-
需求:將使用者請求
^/nn_live/(.*)內部改寫為/3rd/nn_live/$1。 -
對應的AScript規則:
pcs = capture_re($uri, '^/nn_live/(.*)') sec = get(pcs, 1) if sec { dst = concat('/3rd/nn_live/', sec) rewrite(dst, 'break') }
302重新導向
-
需求:將根目錄
/,302重新導向到/app/movie/pages/index/index.html頁面。 -
對應的AScript規則:
if eq($uri, '/') { rewrite('/app/movie/pages/index/index.html', 'redirect') }
302重新導向HTTPS
-
需求:
將以下URI(對根目錄匹配,
^/$)-
http://rtmp.cdnpe.com -
https://rtmp.cdnpe.com
跳轉到
https://aliyun.com,跳轉後的URI可按需填寫。 -
-
對應的AScript規則:
if eq($uri, '/') { rewrite('https://aliyun.com', 'redirect') }
URL跳轉(不帶URI參數)
-
需求:將
www.test.com的請求跳轉到www.example.com,不攜帶原先的URI參數。 -
對應的AScript規則:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite('www.example.com', 'redirect') }效果:重新導向後丟失URI參數,但結尾存在一個?號。
去除結尾的?
-
對應的AScript規則:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite('www.example.com', 'enhance_redirect') }
URL跳轉(攜帶URI參數)
-
需求:將
www.test.com的請求跳轉到www.example.com,攜帶原先的URI參數。 -
對應的AScript規則:
if and(eq(req_header('Host'), 'www.test.com')) { rewrite(concat('www.example.com', $uri), 'enhance_redirect') }
Regex匹配泛網域名稱(添加URI尾碼)
-
需求:若業務存在多個網域名稱,可使用Regex匹配來縮減配置工作量。匹配泛網域名稱並添加特定URI尾碼參數。
-
對應的AScript規則:
host_re = match_re(req_header('Host'), '.*.test.com') url_match = null(find($uri, 'index.php')) if and(host_re, url_match) { rewrite(concat($host, '/index.php?s=', $uri), 'enhance_redirect') }
HTTP重新導向到HTTPS(去除查詢參數)
-
需求:將
http://www.test.com/tmp/?a=b重新導向為https://www.test.com,去除請求攜帶的查詢參數。 -
對應的AScript規則:
host_re = match_re(req_header('Host'), 'www.test.com') url_match = not(null(find($request_uri, '?'))) # 防止一直重新導向 if req_scheme('http') { if and(host_re, url_match) { rewrite('https://www.test.com', 'enhance_redirect') # 狀態代碼302 } }
問題排查
添加x-request-id
-
需求:利用AScript產生
x-request-id,並在請求和響應中添加,便於請求追蹤及問題排查。 -
對應的AScript規則:
requestid=md5(concat(rand(1, 10000), now())) add_req_header('x-request-id', requestid) add_rsp_header('x-request-id', requestid)